c#支付宝支付
1、进入支付宝去申请appid和密钥 https://open.alipay.com/platform/home.htm 支付宝开放平台
2、下载sdk根据自己是啥开发语言下载啥
3、开始写代码
后台请求参数和方式
/// <summary>
/// 支付宝支付
/// </summary>
/// <param name="model"></param>
/// <param name="configPath"></param>
/// <returns></returns>
public string Alipay(HttpContext context)
{
string OrderNumber = "" ;//订单号,此单号必须唯一
string app_id = ""; //"你的app_id";
string merchant_private_key = "";//"支付宝私钥";
string alipay_public_key = "";// "支付宝公钥";
string timeout_express = "30m";//订单有效时间(分钟)
string postUrl = "https://openapi.alipay.com/gateway.do";//这个地址都是固定的
string sign_type = "RSA2";//加签方式 有两种RSA和RSA2 我这里使用的RSA2(支付宝推荐的)
string version = "1.0";//固定值 不用改
string format = "json";//固定值
string Amount = "0.01";//订单金额
string method = "alipay.trade.wap.pay";//调用接口 固定值 不用改
IAopClient client = new DefaultAopClient(postUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, "UTF-8", false);
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
request.SetNotifyUrl("Notify_url.aspx");//外网能访问的后台回调地址
request.SetReturnUrl("Return_url.aspx");//外网能访问的前台回调地址
request.BizContent = "{" +
" \"body\":\"对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。\"," +
" \"subject\":\"商品描述\"," +
" \"out_trade_no\":\"" + OrderNumber + "\"," + //商家唯一订单,填写你项目里生成的唯一订单号
" \"timeout_express\":\"" + timeout_express + "\"," +
" \"total_amount\":" + Amount + "," +
" \"product_code\":\"" + method + "\"" +
" }";
AlipayTradeWapPayResponse response = client.pageExecute(request);
string form = response.Body.Substring(0, response.Body.IndexOf("<script>"));
return form;
}
//
using Aop.Api.Util;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Return_url : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* 实际验证过程建议商户添加以下校验。
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
4、验证app_id是否为该商户本身。
*/
// 应用ID,您的APPID
string app_id = "";
// 支付宝网关
string gatewayUrl = "";
// 商户私钥,您的原始格式RSA私钥
string private_key = "";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
string alipay_public_key = "";
// 签名方式
string sign_type = "RSA2";
// 编码格式
string charset = "UTF-8";
Dictionary<string, string> sArray = GetRequestGet();
if (sArray.Count != 0)
{
bool flag = AlipaySignature.RSACheckV1(sArray, alipay_public_key, charset, sign_type, false);
if (flag)
{
Response.Write("同步验证通过");
}
else
{
Response.Write("同步验证失败");
}
}
}
public Dictionary<string, string> GetRequestGet()
{
int i = 0;
Dictionary<string, string> sArray = new Dictionary<string, string>();
NameValueCollection coll;
//coll = Request.Form;
coll = Request.QueryString;
String[] requestItem = coll.AllKeys;
for (i = 0; i < requestItem.Length; i++)
{
sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
}
return sArray;
}
}
支付完成异步通知接收地址
using Aop.Api.Util;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Notify_url : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* 实际验证过程建议商户添加以下校验。
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
4、验证app_id是否为该商户本身。
*/
// 应用ID,您的APPID
string app_id = "";
// 支付宝网关
string gatewayUrl = "";
// 商户私钥,您的原始格式RSA私钥
string private_key = "";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
string alipay_public_key = "";
// 签名方式
string sign_type = "RSA2";
// 编码格式
string charset = "UTF-8";
Dictionary<string, string> sArray = GetRequestPost();
if (sArray.Count != 0)
{
bool flag = AlipaySignature.RSACheckV1(sArray, alipay_public_key, charset, sign_type, false);
if (flag)
{
//交易状态
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
string trade_status = Request.Form["trade_status"];
Response.Write("success");
}
else
{
Response.Write("fail");
}
}
}
public Dictionary<string, string> GetRequestPost()
{
int i = 0;
Dictionary<string, string> sArray = new Dictionary<string, string>();
NameValueCollection coll;
//coll = Request.Form;
coll = Request.Form;
String[] requestItem = coll.AllKeys;
for (i = 0; i < requestItem.Length; i++)
{
sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
}
return sArray;
}
}
页面上的请求方式(这里为了方便写的非常简单)
<head runat="server">
<meta name="viewport" content="width=device-width" />
<script src="Scripts/jquery-1.7.1.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="支付啊" OnClick="Button1_Click" />
</div>
<div id="formDiv" style="display:none;">
</div>
</div>
<script type="text/javascript">
$(function(){
$("#Button1").click(function () {
$.ajax({
url: "Handler1.ashx", //页面上是到一般处理程序中处理的
data: {"action": "Alipay" },
type: "POST",
cache: false,
async: true,
success: function (data) {
alert(data);
if (data != "") {
$("#formDiv").append(data);
$("#alipaysubmit").submit(); //返回的是form表单 直接提交就可以了.
}
}
});
});
});
});
</script>
</form>
</body>
原文链接 https://www.cnblogs.com/superMay/p/9474214.html
__EOF__
本文作者:杨扬小朋友
本文链接:https://www.cnblogs.com/yyxpy/p/16359065.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/yyxpy/p/16359065.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现