hejisan的专栏
原
新版支付宝开放平台 手机网页支付 整个流程
2017年06月29日 18:32:03
阅读数:6926
新版支付宝开放平台 手机网页支付 整个流程
在支付宝签约如下产品
然后,使用支付宝最新的签名工具生成商户的私钥和公钥
值得注意的是,如果是.net c# 请选择PKCS1,我只采用1024**长度。
然后来到支付宝的账号管理中心:
在如图的开放平台**页所示处,将上述软件生成的商户公钥设置到应用公钥处,然后设置授权回调地址,设置完后,点开查看并保存支付宝公钥,用于加密及验签。
基它地方不要设置,不要动。
下载最新的.net 支付宝SDK,引入工程
然后,在自己工程中配置支付宝参数 :
发起支付:
[csharp] view plain copy
- <code class="language-csharp">IAopClient client = new DefaultAopClient(AlipayConfig.Alipay_Server, AlipayConfig.APPID, AlipayConfig.APP_PRIVATE_KEY, "json", "1.0", "RSA", AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, false);
- AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
- request.SetNotifyUrl(AlipayConfig.Asynchronous_Notice_Location_Alipay);
- request.SetReturnUrl(AlipayConfig.Synchronous_Notice_Location_Alipay);
- string desc = "商城充值";
- string subject = "hahahaha";
- string out_trade_no = payDetail.Id.ToString();
- string total_amount = payDetail.Price.ToString("F2");
- request.BizContent = "{" +
- " \"body\":\" " + desc + "\"," +
- " \"subject\":\"" + subject + "\"," +
- " \"out_trade_no\":\"" + out_trade_no + "\"," +
- " \"timeout_express\":\"90m\"," +
- " \"total_amount\":+"+ total_amount + "," +
- " \"product_code\":\"QUICK_WAP_WAY\"" +
- " }";
- AlipayTradeWapPayResponse response = client.pageExecute(request);
- string form = response.Body;
- form = form.Replace("<script>document.forms['alipaysubmit'].submit();</script>", "");
- form = form.Replace("<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST'>",
- "<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST' style='display:none;'>");
- ViewBag.Content = form;</code>
-
IAopClient client = new DefaultAopClient(AlipayConfig.Alipay_Server, AlipayConfig.APPID, AlipayConfig.APP_PRIVATE_KEY, "json", "1.0", "RSA", AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, false);
-
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
-
request.SetNotifyUrl(AlipayConfig.Asynchronous_Notice_Location_Alipay);
-
request.SetReturnUrl(AlipayConfig.Synchronous_Notice_Location_Alipay);
-
string desc = "商城充值";
-
string subject = "hahahaha";
-
string out_trade_no = payDetail.Id.ToString();
-
string total_amount = payDetail.Price.ToString("F2");
-
request.BizContent = "{" +
-
" \"body\":\" " + desc + "\"," +
-
" \"subject\":\"" + subject + "\"," +
-
" \"out_trade_no\":\"" + out_trade_no + "\"," +
-
" \"timeout_express\":\"90m\"," +
-
" \"total_amount\":+"+ total_amount + "," +
-
" \"product_code\":\"QUICK_WAP_WAY\"" +
-
" }";
-
AlipayTradeWapPayResponse response = client.pageExecute(request);
-
string form = response.Body;
-
form = form.Replace("<script>document.forms['alipaysubmit'].submit();</script>", "");
-
form = form.Replace("<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST'>",
-
"<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST' style='display:none;'>");
-
ViewBag.Content = form;
然后在页面上调用表单提交:
-
function check() {
-
var ua = window.navigator.userAgent.toLowerCase();
-
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
-
alert('请点击右上角菜单,在浏览器中打开本页面完成支付。');
-
} else {
-
document.forms['alipaysubmit'].submit();
-
}
-
}
然后,异步通知页验签:
-
public Dictionary<string, string> GetRequestPost(HttpRequestBase request)
-
{
-
int i = 0;
-
Dictionary<string, string> sArray = new Dictionary<string, string>();
-
System.Collections.Specialized.NameValueCollection coll;
-
coll = request.Form;
-
String[] requestItem = coll.AllKeys;
-
for (i = 0; i < requestItem.Length; i++)
-
{
-
sArray.Add(requestItem[i], request.Form[requestItem[i]]);
-
}
-
return sArray;
-
}
-
try
-
{
-
bool flag = Aop.Api.Util.AlipaySignature.RSACheckV1(GetRequestPost(Request), AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA", false);
-
if(flag)
-
{
-
log4net.LogManager.GetLogger("root").Info("支付宝签名验证成功");
-
//1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
-
//2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
-
//3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email),
-
//4、验证app_id是否为该商户本身。上述1、2、3、4有任何一个验证不通过,则表明本次通知是异常通知,务必忽略。
-
//在上述验证通过后商户必须根据支付宝不同类型的业务通知,正确的进行不同的业务处理,并且过滤重复的通知结果数据。
-
//在支付宝的业务通知中,只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,支付宝才会认定为买家付款成功。
-
//商户订单号
-
string out_trade_no = Request.Form["out_trade_no"] ;
-
log4net.LogManager.GetLogger("root").Info("商户订单号" + out_trade_no);
-
string app_id = Request.Form["app_id"];
-
log4net.LogManager.GetLogger("root").Info("app_id:" + app_id);
-
if (app_id != AlipayConfig.APPID)
-
{
-
log4net.LogManager.GetLogger("root").Info("app_id != AlipayConfig.APPID" );
-
return Content("fail");
-
}
-
string trade_no = Request.Form["trade_no"] ;
-
log4net.LogManager.GetLogger("root").Info("支付宝交易号" + trade_no);
-
//交易状态
-
string trade_status = Request.Form["trade_status"] ;
-
log4net.LogManager.GetLogger("root").Info("交易状态" + trade_status);
-
string gmt_payment= Request.Form["gmt_payment"] ;
-
log4net.LogManager.GetLogger("root").Info("交易支付时间:" + gmt_payment);
-
string total_amount= Request.Form["total_amount"] ;
-
log4net.LogManager.GetLogger("root").Info("交易金额:" + total_amount);
-
decimal money=total_amount.ParseTo<decimal>(0);
-
if (Request.Form["trade_status"] == "TRADE_FINISHED" || Request.Form["trade_status"] == "TRADE_SUCCESS")
-
{
-
DealPay_Alipay(out_trade_no, trade_no, gmt_payment, money);
-
return Content("success");
-
}
-
else
-
{
-
return Content("fail");
-
}
-
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
-
}
-
}
-
catch (CustomerException ex)
-
{
-
log4net.LogManager.GetLogger("root").Info("ex:" + ex.Message);
-
return Content("fail");
-
}
-
catch (Exception ex)
-
{
-
log4net.LogManager.GetLogger("root").Info("ex:" + ex.Message + ex.InnerException.Message + ex.StackTrace);
-
return Content("fail");
-
}
-
return Content("fail");
同步通知页:
-
if (!String.IsNullOrEmpty(Request.Form["trade_no"]) || !String.IsNullOrEmpty(Request.QueryString["trade_no"]))
-
{
-
return View();
-
}
-
else
-
{
-
return RedirectToAction("AlipayFail");
-
}