手机网站支付接口,请参考支付宝官方文档:https://b.alipay.com/signing/productSet.htm?navKey=all
1.需要提供应用ID、应用私钥、支付宝公钥
2.代码实现:
下载官方SDK,并编译引用到项目中去。
初始化对象:private readonly IAopClient _client= new DefaultAopClient("https://openapi.alipay.com/gateway.do", "应用ID", "应用私钥", "json", "1.0", "签名类型", "支付宝公钥", "字符编码格式,支持GBK和UTF-8", false);
支付接口:
1>调用支付宝支付网关

/// <summary> /// 手机网站支付 /// </summary> /// <param name="postProcessPaymentRequest"></param> private void AlipayTradeWapPayment(订单实体 order) { if (order == null) { Log.Error(this.GetType().ToString(),"订单数据不能为空"); return; } Log.Info(this.GetType().ToString(), "AlipayTradeWapPayment Start......"); string responseText = string.Empty; try { #region 调用支付宝支付接口 AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.SetReturnUrl(http://XXXXXXXXX/foxconn/AliPayment/AppPaymentReturn);//页面跳转同步通知页面路径 request.SetNotifyUrl("http://XXXXXXXXX/foxconn/AliPayment/AppPaymentNotify");//服务器异步通知页面路径 request.BizContent = "{" + " \"body\":\"\"," + " \"subject\":\"" + order.订单名称述 + "\"," + " \"out_trade_no\":\"" + order.订单编号 + "\"," + " \"timeout_express\":\"90m\"," + " \"total_amount\":\"" + order.订单金额 + "\"," + " \"seller_id\":\"" + _alipayPaymentSettings.SellerId + "\"," + " \"product_code\":\"QUICK_WAP_PAY\"" + " }"; AlipayTradeWapPayResponse response = _client.pageExecute(request); responseText = response.Body; _httpContext.Response.Clear(); _httpContext.Response.Write(responseText); _httpContext.Response.End(); #endregion } catch (Exception ex) { Log.Error(this.GetType().ToString(), "Exception: " + ex.Message); } Log.Info(this.GetType().ToString(), "AlipayTradeWapPayment End......"); }
2>接收支付宝同步通知(get)

/// <summary> /// 支付宝手机网站支付同步通知 /// </summary> /// <returns></returns> [HttpGet] public ActionResult AppPaymentReturn() { string result = string.Empty; Log.Info(this.GetType().ToString(), "AppPaymentReturn End......"); try { SortedDictionary<string, string> sPara = GetRequestGet();//将异步通知中收到的所有参数都存放到map中 if (sPara.Count > 0)//判断是否有带返回参数 { bool verifyResult = AlipaySignature.RSACheckV1(sPara, _aliPaymentSettings.App_Alipay_Public_Key, _aliPaymentSettings.App_Charset, _aliPaymentSettings.App_SignType, false); //调用SDK验证签名 if (verifyResult) { //验签成功后,按照支付结果异步通知中的描述,对支付结果中的业务内容进行二次校验,校验成功后在response中返回success并继续商户自身业务处理,校验失败返回failure //商户订单号 string out_trade_no = Request.QueryString["out_trade_no"]; //支付宝交易号 string trade_no = Request.QueryString["trade_no"]; //交易状态 string trade_status = Request.QueryString["trade_status"]; if (Request.QueryString["trade_status"] == "TRADE_FINISHED" || Request.QueryString["trade_status"] == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 } else { result = "trade_status=" + Request.QueryString["trade_status"]; } //打印页面 result = "验证成功"; } else//验证失败 { //验签失败则记录异常日志,并在response中返回failure. result = "验证失败"; } } else { result = "无返回参数"; } Log.Info(this.GetType().ToString(), "result:" + result); } catch (Exception ex) { Log.Info(this.GetType().ToString(), "Exception:" + ex.Message); } Log.Info(this.GetType().ToString(), "AppPaymentReturn End......"); return 跳转页面; }
3>接收支付宝异步通知(post)

/// <summary> /// 支付宝手机网站支付异步通知 /// </summary> /// <returns></returns> [ValidateInput(false)] public ActionResult AppPaymentNotify() { string result = string.Empty; try { Log.Info(this.GetType().ToString(), "AppPaymentNotify Start......"); SortedDictionary<string, string> sPara = GetRequestPost(); if (sPara.Count > 0)//判断是否有带返回参数 { bool verifyResult = AlipaySignature.RSACheckV1(sPara, _aliPaymentSettings.App_Alipay_Public_Key, _aliPaymentSettings.App_Charset, _aliPaymentSettings.App_SignType, false); //调用SDK验证签名 Log.Info(this.GetType().ToString(), "verifyResult:" + verifyResult); if (verifyResult)//验证成功 { bool isRefund = false; string batch_no = string.Empty; if(sPara.ContainsKey("out_biz_no")) { isRefund = true; batch_no = sPara["out_biz_no"]; } //商户订单号 string out_trade_no = Request.Form["out_trade_no"]; //支付宝交易号 string trade_no = Request.Form["trade_no"]; //交易状态 string trade_status = Request.Form["trade_status"]; string buyer_id = Request.Form["buyer_id"]; string buyer_emial = Request.Form["buyer_logon_id"]; if (string.IsNullOrEmpty(out_trade_no)) { throw new Exception("商户订单号不能为空"); } Log.Debug(this.GetType().ToString(), string.Format("out_trade_no:【{0}】-trade_no:【{1}】-trade_status:【{2}】", out_trade_no, trade_no, trade_status)); if (trade_status == "TRADE_FINISHED") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的 //如果有做过处理,不执行商户的业务程序 //处理业务逻辑 //注意: //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知 } else if (trade_status == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的 //如果有做过处理,不执行商户的业务程序 if (isRefund) { //处理退款业务 Log.Info(this.GetType().ToString(), "退款成功"); } else { //处理订单业务 Log.Info(this.GetType().ToString(), "付款成功"); } //注意: //付款完成后,支付宝系统发送该交易状态通知 } else if (trade_status == "TRADE_CLOSED")//未付款交易超时关闭,或支付完成后全额退款 { if (isRefund) { //处理退款业务 Log.Info(this.GetType().ToString(), "退款成功"); } } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— Response.Write("success"); //请不要修改或删除 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } else//验证失败 { Response.Write("failure"); } } else { Response.Write("无通知参数"); Log.Info(this.GetType().ToString(), "result:无通知参数"); } } catch (Exception ex) { Response.Write("failure"); Log.Info(this.GetType().ToString(), "Exception:" + ex.Message); } Response.End(); Log.Info(this.GetType().ToString(), "AppPaymentNotify End......"); return Content("fail"); }
退款接口:

/// <summary> /// 退款 /// </summary> /// <param name="refundPaymentRequest"></param> /// <returns></returns> private bool AlipayTradeWapRefund(退款实体 refundModel) { var result = false; if (refundModel == null) { Log.Error(this.GetType().ToString(), "退款数据不能为空"); return result; } string responseText = string.Empty; try { #region 调用支付宝退款接口 AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();//创建API对应的request类 //request.SetNotifyUrl(_alipayPaymentSettings.App_RefundNotifyUrl);//服务器异步通知页面路径 string batch_no = refundModel.退款编号; request.BizContent = "{" + " \"out_trade_no\":\"" + refundModel.订单编号 + "\"," + " \"trade_no\":\"" + refundModel.交易号 + "\"," + " \"out_request_no\":\"" + batch_no + "\"," + " \"refund_amount\":\"" + refundModel.退款金额 + "\"" + " }";//设置业务参数 AlipayTradeRefundResponse response = _client.Execute(request);//通过alipayClient调用API,获得对应的response类 //根据response中的结果继续业务逻辑处理 if (response.Code == "10000")//接口调用成功 { } #endregion } catch (Exception ex) { responseText = ex.Message; Log.Error(this.GetType().ToString(), "Exception: " + ex.Message); } Log.Info(this.GetType().ToString(), "Refund End......"); return result; }
查询接口:

/// <summary> /// 查询订单在支付宝的状态 /// </summary> /// <param name="queryOrderRequest"></param> /// <returns></returns> private bool AlipayTradeWapQuery(订单查询实体 queryOrder) { var result = false; if (queryOrder == null) { Log.Error(this.GetType().ToString(), "查询数据不能为空"); return result; } Log.Info(this.GetType().ToString(), "AlipayTradeWapQuery Start......"); string responseText = string.Empty; try { #region 调用支付宝支付查询接口 AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); request.BizContent = "{" + " \"out_trade_no\":\"" + queryOrder.订单编号 + "\"," + " \"trade_no\":\"" + queryOrder.交易号 + "\"" + " }"; AlipayTradeQueryResponse response = _client.Execute(request); string tradeStatus = response.TradeStatus; responseText = tradeStatus; if (tradeStatus == "TRADE_SUCCESS" || tradeStatus == "TRADE_CLOSED" || tradeStatus == "TRADE_FINISHED") { //处理支付业务 result=true; Log.Info(this.GetType().ToString(), "支付成功"); } else { Log.Info(this.GetType().ToString(), "支付失败"); } #endregion } catch (Exception ex) { responseText = ex.Message; Log.Error(this.GetType().ToString(), "Exception: " + ex.Message); } Log.Debug(this.GetType().ToString(), string.Format("支付宝手机网站支付查询跟踪信息。GetForm:{0}", responseText); Log.Info(this.GetType().ToString(), "AlipayTradeWapQuery End......"); return result; }