续集开始
经过一阵折腾,一阵折腾,目前支付宝RSA2支付接口、查询支付结果接口、转帐接口、查询转账接口都调通了。
在线体验
支付
https://i.zuime.com/order/alipay
支付结果查询
https://i.zuime.com/order/alipay/pay/query?tradeNo=200001568282831558
转账
https://i.zuime.com/order/alipay/transfer
转账结果查询
https://i.zuime.com/order/alipay/transfer/query?bizNo=T00011568284758838
支付单查询结果
{"alipay_trade_query_response":{"code":"10000","msg":"Success","buyer_logon_id":"z***","buyer_pay_amount":"0.00","buyer_user_id":"208","invoice_amount":"0.00","out_trade_no":"200001568282831558","point_amount":"0.00","receipt_amount":"0.00","send_pay_date":"2019-09-12 18:08:04","total_amount":"1.00","trade_no":"2019091222001404870598378129","trade_status":"TRADE_SUCCESS"},"alipay_cert_sn":"d129f75444e247d1fd7641eca25e8e8e","sign":"dksLpk0DN6FOMGpys2vsR6TfjFay3abv4aGh5lbikEuG7TBo2e2OSP4ilxjg9CR061wks7fLsQkriqomu0Yt36nq5HHex5BO+WInV4YiC+4HA1fxfNCIzXNFXGIIrNxO+H/AnpGbzU7uIjJfK3k56qKLnW9sWy0J83kdIUjohyrUkdIWqYFHiQrsb6P2akxV6743haC718QjzCuU6U7lpLRD9bncQ8OH7NXOSLUN396t8O7tBkLuIsHL6Q9TlbxW41sjzUpWUs2JBA3KPy32zxpsubl918HVM2s5eIdHkP6xYBUtSPU9ZMVl6tTbeya+n302tuVeW0c52Uf5lJYEOw=="}
转帐结果
{"alipay_fund_trans_order_query_response":{"code":"10000","msg":"Success","order_fee":"0.00","order_id":"20190912110070001506080013570558","out_biz_no":"T00011568280515442","pay_date":"2019-09-12 17:28:37","status":"SUCCESS"},"alipay_cert_sn":"d129f75444e247d1fd7641eca25e8e8e","sign":"JanJ5dR9SRIP/3MCFuqGc7iPCOs2DsaQQ6AI3fYPZDXHHpUVm865iEQAuPFavXIDB0huhWnz+sE38krPgD/srSJGOCxbEF0C7e0mTqNPDUSFGxaQ7nQX3t1iIorvKylNvqT7/+3A5qm837gtwKU98Z1Xzi7b6OsqAT+1UwEOdAs1QIysUK8kt515au9J/0pNC95ObkQEq4i+hCHzt+CFXFag7EThairmT4a9ZDegFpwBrFgHODYT6N3dqS9gE5hg9ULJh0tkAjKROQFsYiaBW+YIqzgAS/d+xjt37DLNwYhGMYxPYdlN/FwDg37Ao5wNkSqfYPJEamfbMK1VBDwVXA=="}
转帐查询结果
{"alipay_fund_trans_order_query_response":{"code":"10000","msg":"Success","order_fee":"0.00","order_id":"20190912110070001506080013570558","out_biz_no":"T00011568280515442","pay_date":"2019-09-12 17:28:37","status":"SUCCESS"},"alipay_cert_sn":"d129f75444e247d1fd7641eca25e8e8e","sign":"JanJ5dR9SRIP/3MCFuqGc7iPCOs2DsaQQ6AI3fYPZDXHHpUVm865iEQAuPFavXIDB0huhWnz+sE38krPgD/srSJGOCxbEF0C7e0mTqNPDUSFGxaQ7nQX3t1iIorvKylNvqT7/+3A5qm837gtwKU98Z1Xzi7b6OsqAT+1UwEOdAs1QIysUK8kt515au9J/0pNC95ObkQEq4i+hCHzt+CFXFag7EThairmT4a9ZDegFpwBrFgHODYT6N3dqS9gE5hg9ULJh0tkAjKROQFsYiaBW+YIqzgAS/d+xjt37DLNwYhGMYxPYdlN/FwDg37Ao5wNkSqfYPJEamfbMK1VBDwVXA=="}
获取实验Demo源代码,关注公众号,回复alipay
经验分享
-
以下接口蚂蚁金服支付宝答复已经下线,不再接受新签约
单笔转账可以转账到用户的银行账户
网银直连 直连网银
-
沙盒问题:RSA2所需要的支付宝公钥证书找不到
所使用的Alipay SDK POM
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.6.0.ALL</version>
</dependency>
官方电脑网站支付 Demo
https://docs.open.alipay.com/270/106291/
官方展示的代码是java jsp版本,非RSA2证书版本
RAS证书版本对接所用到参数
APPID
应用私钥 (private key)
应用公钥证书(public key cert)
支付宝公钥(alipay public key)
支付宝公钥证书(alipay public key cert)
支付宝根证书(alipay root cert)
AES Key
示范代码片段
构建一个配置好证书的AlipayClient,之后与alipay gateway交互通过TA来进行。展示的版本是配置Aes key的,对发出去的报文进行了加密。加密解密都是一个Aes key。看sdk源代码,这两个key是分开的,但是使用的时候又当一成是一样的使用了。
构建AlipayClient
private AlipayClient buildAlipayClient() throws Exception {
// 构造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
// 设置网关地址
certAlipayRequest.setServerUrl(gateway);
// 设置应用Id
certAlipayRequest.setAppId(appId);
// 设置应用私钥
certAlipayRequest.setPrivateKey(privateKey);
// 设置请求格式,固定值json
certAlipayRequest.setFormat(AlipayConstants.FORMAT_JSON);
// 设置字符集
certAlipayRequest.setCharset(AlipayConstants.CHARSET_UTF8);
// 设置签名类型
certAlipayRequest.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
// 设置应用公钥证书路径
certAlipayRequest.setCertPath(appCertPath);
// 设置支付宝公钥证书路径
certAlipayRequest.setAlipayPublicCertPath(alipayCertPath);
// 设置支付宝根证书路径
certAlipayRequest.setRootCertPath(alipayRootCertPath);
// 构造client
certAlipayRequest.setEncryptor(aesKey);
certAlipayRequest.setEncryptType(AlipayConstants.ENCRYPT_TYPE_AES);
return new DefaultAlipayClient(certAlipayRequest);
}
调用接口
所有的接口调用,命名都跟官方文档的一样,有规律可循。来来来,先看一小段代码片段。以查询转账结果为例。
转账查询代码
public String transferQuery(String bizNo) throws Exception{
AlipayClient alipayClient = buildAlipayClient();
AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest();
AlipayFundTransOrderQueryModel model = new AlipayFundTransOrderQueryModel();
model.setOutBizNo(bizNo);
request.setBizModel(model); //划重点
request.setNeedEncrypt(true);//划重点
AlipayFundTransOrderQueryResponse response= alipayClient.certificateExecute(request);
// 在这里检查订单的状态值
return response.getBody();
}
官方文档的https://docs.open.alipay.com/api_28/alipay.fund.trans.order.query的接口命名正好是sdk中的xxxModel,xxxRequeset,xxxResponse,按着接口命名来,调用起来就顺了。
之于,网上DIY一个AlipayOrderBean,自己设置JsonFeild指定实体的json别名,也是做重复工作了,sdk已经配置好了,只需要直接调用即可。从内部系统到外部系统,这个地方还值得考虑一下。
Aes加密解密,sdk 内置了,只要配置一下aes密钥就好,不需要自己重复造个轮子啦。可参考上面的示例代码片段。
最大的坑
支付宝公钥证书(alipay public key cert),这个是要求提供的,在沙盒环境配置界面,找不到下载入口。怎么破?直接使用的正式环境,还好有个正式上线的商户账号。正式的帐号可以通过安装一个开源商城,快速通过审核获取到。^_^
最不习惯的变化
好久没有接支付的,变得越来越快了,网银直连下线了不说,现在竟然中tradePagePay,返回的不是跳转地址了,直接一个完整的html,看到这个返回结果,顿时崩溃,以为见到鬼了~
开源与分享
本次支付宝对接采用了RSA2+AES,踩的坑也不少,做为实验练习代码demo已经整合在jblog中,后续还会完善。从产品的角度来讲,代码谈不上。做为免费开源的代码来讲,可以防坑~~
获取源代码,关注公众号回复 code