一、沙箱环境
沙箱环境是一个能让开发者快速开发以及联调的辅助环境,开发者只需要登录支付宝开放平台=>进入开发服务=>设置秘钥,获取APPID、支付宝网关、支付宝公钥、应用私钥就可以进行业务平台支付功能的开发,无需等待正式环境支付应用上线、签约的审核。
注意:
1.支付时,需要使用沙箱买家账号支付。否则扫描二维码付款时,会提示二维码失效。
2.如果下单过程出现“支付存在钓鱼风险!”,可能是登录了该沙箱环境的支付宝账号导致的,退出账号重启浏览器。
3.开发者业务平台支付功能正式上线时,务必确保相关支付配置为正式环境。
二、支付流程
-
商户系统调用 alipay.trade.page.pay(统一收单下单并支付页面接口)向支付宝发起支付请求,支付宝对商户请求参数进行校验,而后重新定向至用户登录页面。
-
用户确认支付后,支付宝通过 get 请求 returnUrl(商户入参传入),返回同步返回参数。
-
交易成功后,支付宝通过 post 请求 notifyUrl(商户入参传入),返回异步通知参数。
-
若由于网络等原因,导致商户系统没有收到异步通知,商户可自行调用 alipay.trade.query(统一收单线下交易查询)接口查询交易以及支付信息(商户也可以直接调用该查询接口,不需要依赖异步通知)。
注意:
-
由于同步返回的不可靠性,支付结果必须以异步通知或查询接口返回为准,不能依赖同步跳转。
-
商户系统接收到异步通知以后,必须通过验签(验证通知中的 sign 参数)来确保支付通知是由支付宝发送的。
-
接收到异步通知并验签通过后,请务必核对通知中的 app_id、out_trade_no、total_amount 等参数值是否与请求中的一致,并根据 trade_status 进行后续业务处理。
-
在支付宝端,partnerId 与 out_trade_no 唯一对应一笔单据,商户端保证不同次支付 out_trade_no 不可重复;若重复,支付宝会关联到原单据,基本信息一致的情况下会以原单据为准进行支付。
三、签名
支付宝开放平台支持使用 普通公钥、公钥证书 两种签名方式。
-
普通公钥:
即生成一对 RSA 密钥(应用公钥、应用私钥),使用公钥(public key)为系统进行加密,并将密文发送给解密者,解密者使用私钥(private key)解密将密文解码为明文。 -
公钥证书:
即在普通公钥的基础上增加证书签名,证书签名的优势在于引入了 CA 机构对公钥持有者进行身份识别,保证该证书所属实体的真实性,以实现报文的抗抵赖,具备更高的安全性。
四、异步通知说明
对于 PC 网站支付的交易,在用户支付完成之后,支付宝会根据 API 中商户传入的 notify_url,通过 POST 请求的形式将支付结果作为参数通知到商户系统。
- 支付宝是用 POST 方式发送通知信息,因此该页面中获取参数的方式,如:request.Form(“out_trade_no”)、$_POST[‘out_trade_no’]。
- 程序执行完后必须打印输出“success”(不包含引号)。如果商户反馈给支付宝的字符不是 success 这7个字符,支付宝服务器会不断重发通知,直到超过 24 小时 22 分钟。一般情况下,25 小时以内完成 8 次通知(通知的间隔频率一般是:4m,10m,10m,1h,2h,6h,15h)。
- 当商户收到服务器异步通知并打印出 success 时,服务器异步通知参数 notify_id 才会失效。也就是说在支付宝发送同一条异步通知时(包含商户并未成功打印出 success 导致支付宝重发数次通知),服务器异步通知参数 notify_id 是不变的。
五、集成使用SDK
支付宝开放平台提供了 服务端SDK ,包含 JAVA、PHP、NodeJS、Python 和 .NET 五种语言,封装了签名 & 验签、HTTP 接口请求等基础功能。
1.安装Nuget包:
Install-Package AlipaySDKNet -Version 4.6.0
2.业务平台支付示例:
using Alipay.Demo.App_Start;
using Aop.Api;
using Aop.Api.Request;
using Aop.Api.Response;
using Aop.Api.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Mvc;namespace Alipay.Demo.Controllers
{public class AliPayController : Controller{public ActionResult Order(){IAopClient client = new DefaultAopClient("https://openapi.alipaydev.com/gateway.do", config.app_id, config.private_key, "json", "1.0", config.sign_type, config.alipay_public_key, config.charset, false);AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();request.SetNotifyUrl("http://www.xxx.com/AliPay/NotifyUrl");request.SetReturnUrl("http://www.xxx.com/AliPay/ReturnUrl");Dictionary<string, object> bizContent = new Dictionary<string, object>();bizContent.Add("out_trade_no", "202108170101010047");bizContent.Add("total_amount", 0.01);bizContent.Add("subject", "测试商品");bizContent.Add("product_code", "FAST_INSTANT_TRADE_PAY");//bizContent.Add("time_expire", "2022-08-01 22:00:00");商品明细信息,按需传入//List<object> goodsDetails = new List<object>();//Dictionary<string, object> goods1 = new Dictionary<string, object>();//goods1.Add("goods_id", "goodsNo1");//goods1.Add("goods_name", "子商品1");//goods1.Add("quantity", 1);//goods1.Add("price", 0.01);//goodsDetails.Add(goods1);//bizContent.Add("goods_detail", goodsDetails);扩展信息,按需传入//Dictionary<string, object> extendParams = new Dictionary<string, object>();//extendParams.Add("sys_service_provider_id", "2088501624560335");//bizContent.Add("extend_params", extendParams);string Contentjson = JsonConvert.SerializeObject(bizContent);request.BizContent = Contentjson;AlipayTradePagePayResponse response = client.pageExecute(request);return Content(response.Body, "text/html");}public ActionResult NotifyUrl(){/* 实际验证过程建议商户在验签通过后,添加以下校验。1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)4、验证app_id是否为该商户本身。*/Dictionary<string, string> sArray = GetRequestPost();if (!sArray.Any()){return Json("fail");}bool flag = AlipaySignature.RSACheckV1(sArray, config.alipay_public_key, config.charset, config.sign_type, false);if (flag){//交易状态//判断该笔订单是否在商户网站中已经做过处理//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序//请务必判断请求时的total_amount与通知时获取的total_fee为一致的//如果有做过处理,不执行商户的业务程序//注意://退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知string trade_status = Request.Form["trade_status"];if (trade_status == "TRADE_FINISHED" || trade_status == "TRADE_SUCCESS"){//.....}return Json("success");}else{return Json("fail");}}public ActionResult ReturnUrl(){/* 实际验证过程建议商户在验签通过后,添加以下校验。1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)4、验证app_id是否为该商户本身。*/Dictionary<string, string> sArray = GetRequestGet();if (!sArray.Any()){return Json("参数无效",JsonRequestBehavior.AllowGet);}bool flag = AlipaySignature.RSACheckV1(sArray, config.alipay_public_key, config.charset, config.sign_type, false);if (flag){return Json("同步验证通过", JsonRequestBehavior.AllowGet);}else{return Json("同步验证失败", JsonRequestBehavior.AllowGet);}}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;}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;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;namespace Alipay.Demo.App_Start
{public class config{//APPID 即创建应用后生成。public static string app_id => "";// 支付宝网关//public static string gatewayUrl => "https://openapi.alipay.com/gateway.do";public static string gatewayUrl => "https://openapi.alipaydev.com/gateway.do";//沙箱// 商户私钥(开发者私钥,由开发者自己生成),您的原始格式RSA私钥public static string private_key => "";// 支付宝公钥,由支付宝生成。查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。public static string alipay_public_key => "";// 签名方式:商户生成签名字符串所使用的签名算法类型,目前支持 RSA2 和 RSA,推荐使用 RSA2。public static string sign_type => "RSA2";// 编码格式public static string charset => "UTF-8";// 参数返回格式,只支持 JSON 格式。public static string formate => "json";}
}
参考:
https://opendocs.alipay.com/open/270/01didh