H5手机网站接入支付宝的支付接口,推荐使用支付宝提供的SDK来快速开发
我使用的是SDK开发
引用命名空间
using Aop.Api;using Aop.Api.Request;using Aop.Api.Response;using Aop.Api.Util;
首页需要定义一些常量
static string serverUrl = "https://openapi.alipaydev.com/gateway.do"; static string app_id = "**"; //开发者的应用ID static string format = "JSON"; static string charset = "utf-8"; static string sign_type = "RSA2"; //签名格式 static string version = "1.0"; string UID = "2088102169707816";//卖家支付宝账户号 //商户私钥 static string merchant_private_key = "***"; //支付宝公钥 static string alipay_public_key = "***";
这里的app_id,merchant_private_key,alipay_public_key 我就没有列出来了,获取的方法需要自己去支付宝平台完成一些操作进行获取
在用户点击网站付款时,我们需要唤醒支付宝,来进行支付
public string H5RequestPayWay(OrderPO order) { IAopClient client = new DefaultAopClient(serverUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, charset, false); AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); string address= "http://m." + PathLogic1.RootDomain; request.SetReturnUrl(address+ "/WebPay/AlipayPayResult");//同步请求 request.SetNotifyUrl(address + "/WebPay/AsyncPay");//异步请求 var lstDetail = Context.Data.OrderDetail.Where(x => x.OrderNo == order.OrderNo).ToSelectList(x=>new { x.SkuName}); StringBuilder sb = new StringBuilder(); for (int i = 0; i < lstDetail.Count(); i++) { sb.Append(lstDetail[i].SkuName + ","); } request.BizContent = "{" + "/"body/":/""+sb.ToString().Substring(0,sb.Length-1)+"/"," + "/"subject/":/"袋鼠巴巴商品支付/"," + "/"out_trade_no/":/""+order.OrderNo+"/"," + "/"timeout_express/":/"90m/"," + "/"total_amount/":"+(order.TotalAmount.Value+order.TotalFreight.Value)+"," + "/"product_code/":/"QUICK_WAP_PAY/"" + " }"; AlipayTradeWapPayResponse response = client.pageExecute(request); return response.Body; }
具体发送给支付宝的参数,自行去查看
执行方法后,买家输入自己的支付宝账号密码进行支付,支付成功的结果,支付宝会以post的方式异步请求你的SetNotifyUrl的地址
这个SetNotifyUrl的地址必须要外网可以访问,支付宝的请求才能进来
买家支付成功,商家修改订单状态和数据库的操作,都在异步请求中执行
同步请求
public ActionResult AlipayPayResult() { ViewBag.result = "success"; return View("PayResult"); }
/// <summary> /// 验证通知数据的正确性 /// </summary> /// <param name="out_trade_no"></param> /// <param name="total_amount"></param> /// <param name="seller_id"></param> /// <returns></returns> private SortedDictionary<string, string> GetRequestPost() { int i = 0; SortedDictionary<string, string> sArray = new SortedDictionary<string, string>(); NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll = Request.Form; // Get names of all forms into a string array. String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { sArray.Add(requestItem[i], Request.Form[requestItem[i]]); } return sArray; }
新闻热点
疑难解答
图片精选