一.准备工作
配置申请地址 https://open.alipay.com/platform/home.html(开发对接中需要以下参数,参数请务必妥善保管,不可泄露)
详细接口或参数说明,以支付宝官方文档为准, 支付宝API网址(https://opendocs.alipay.com/apis)
//支付宝网关地址:https://openapi.alipay.com/gateway.do
//支付宝申请的APPID
//商户应用私钥(与支付宝数据交互时,保证数据安全,防止数据被篡改)
//支付宝公钥(与支付宝数据交互时,保证数据安全,防止数据被篡改)
//签名算法类型(RSA2或者RSA,支付宝推荐使用RSA2)
//编码格式(utf-8,gb2312,gbk)
二.导入支付宝依赖(https://mvnrepository.com)
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.9.124.ALL</version>
</dependency>
三.代码实现
1.1 H5支付
//网关地址,APPID,商户应用私钥,数据格式,编码格式,支付宝公钥,签名算法类型
AlipayClient alipayClient = new DefaultAlipayClient(aliPayGateway,appID,rsa_private_key,format,charset,alipayPublicKey,signType);//不同支付类型需使用不同的请求对象
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
//请求参数集合对象,除了公共参数之外,所有参数都可通过此对象传递(不同支付类型需使用不同的请求参数对象)
AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
//订单描述
model.setBody("订单描述");
//订单标题
model.setSubject("显示效果见于下图的标题");
//商户订单号
model.setOutTradeNo(orderNo);
//订单的过期时长(取值为5m - 15d,即五分钟到十五天)
model.setTimeoutExpress("30m");
//订单总金额
model.setTotalAmount(String.valueOf(cashNum));
//产品码 QUICK_WAP_WAY:无线快捷支付产品
model.setProductCode("QUICK_MSECURITY_PAY");
//用户付款中途退出返回商户网站的地址
model.setQuitUrl("https://wwww.baidu.com");
request.setBizModel(model);
//支付成功后的跳转地址
request.setReturnUrl("支付成功之后的跳转地址");
//支付成功后的回调地址(此地址必须为公网地址,用于支付宝收到用户付款之后,通知我们的服务端,我们可以在此接口中更改订单状态为已付款或后续操作)
request.setNotifyUrl(aliPayNotifyUrl);
String orderStr = "";
AlipayTradeWapPayResponse responseH5 = null;
try {
//获取支付宝返回格式,当前是以表单的方式进行返回,可以设置为 url 的方式返回 支付连接
responseH5 = alipayClient.pageExecute(request);
//以url的方式进行返回
// aliPayUrl = alipayClient.pageExecute(request,"GET").getBody();
} catch (AlipayApiException e) {
return ReturnUtils.returnVal(CommonConstants.appCode.UNKNOWNERROR.get(),null);
}
orderStr = responseH5.getBody();
Map<String, String> result = new HashMap<String, String>();
result.put("orderStr", orderStr);
result.put("outTradeNo", orderNo);
return ReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(), result);
1.2.响应参数(前端解析表单或者跳转URl,取决于Java的返回)
{
"code": 49000,
"msg": "操作成功",
"result": {
"orderStr": "<form name="punchout_form" method="post" action="https://openapi.alipay.com/gateway.do?charset=utf-8&method=alipay.trade.wap.pay&sign=
jZpBgo2oaAvd%2FAyXEnWlvd8PRRdJAaaI%2B35bTg4QDvhCaPY3X2lSi0Gb7AXDqKfPe%2BwDHMjwWkFCsl9Pu%2F%2BYZBDqof7w0NxJL5V2eqfuKzRIzRpEbmTgDbWovGPvbYD8lYK%2BJ41QpcJUFZzi%2BPQ4GCmWIbTj
%2FMypJVvqWptpzo57kdXqcaydaP2CK0WhkDhMtqQOFFc%2BiEw9%2Bj6TyZ1BtZM9zcGsBQlBaw%2FhMxIlogOHP5C3vadZPWluvI%2FR18ywoduxdbueJ2Dkblpph3mRe8m6buXelMOqjeT5r9jwnrYu4pxa%2Bj0CMyqSFGJ
tB0z%2BwaOC1T5MGDAUDQdq6U957A%3D%3D&return_url=http%3A%2F%2F192.168.16.108%3A8041%2Fmall%2Fcommodity%2FpaymentByPay%3Fid%3D1100%26uid%3D2%26hash%3D39364710049877767526540774
15810075258431176020006439440944207350&notify_url=http%3A%2F%2Ftest.hqjplatform.com%2Fmall%2Fapp%2FmodifyOrderCurstate&version=1.0&app_id=2021001181677544&sign_type=RSA2&time
stamp=2021-09-03+09%3A07%3A39&alipay_sdk=alipay-sdk-java-dynamicVersionNo&format=json">n<input type="hidden" name="biz_content" value="{"body":"优选商城订单
","out_trade_no":"243545656435254654654","product_code":"QUICK_MSECURITY_PAY","quit_url":"http://192.168.16.108:8041
/mall/commodity/getPayMent/1100?uid=2&hash=3936471004987776752654077415810075258431176020006439440944207350","subject":"优选商城","timeout_express":
"30m","total_amount":"0.1"}">n<input type="submit" value="立即支付" style="display:none" >n</form>n<script>document.forms[0].submit();</script>",
"outTradeNo": "243545656435254654654"
}
}
1.3.进行支付
1.4. H5支付的补充
//服务端返回支付的支付表单操作可能很很麻烦,可以转换为Https的支付链接地址
String payLink = alipayClient.pageExecute(request,"GET").getBody();
{
"code": 49000,
"msg": "操作成功",
"result": {
"orderStr": "https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2021001181677544&biz_content=%7B%22body%22%3A%22%E4%BC%98%E9%80%89%E5%95%86%E5%9F%8E%E8%AE%A2%E5%8D%95%22%2C%22out_trade_no%22%3A%22lmc_1bff0c78856a4c2aa5c84caaf491a101%22%2C%22product_code%22%3A%22QUICK_WAP_PAY%22%2C%22quit_url%22%3A%22http%3A%5C%2F%5C%2Fwww.baidu.com%22%2C%22subject%22%3A%22%E4%BC%98%E9%80%89%E5%95%86%E5%9F%8E%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%227779.97%22%7D&charset=utf-8&format=json&method=alipay.trade.wap.pay¬ify_url=http%3A%2F%2Ftest.hqjplatform.com%2FoptimizingMall%2FpayService%2FaliPayNotify&return_url=http%3A%2F%2Fwww.baidu.com&sign=EZ4MSYUyaC2tLtO%2FZfO1%2B7jPxzsj%2FGHil9OwZ4%2B5tKNannkGqSa7rXbLaN3ELh0yibuwRqb6llYfrnASzvmFbJRFE7S0C%2BO9cGzrEQjCmumkyMLuXkPdvHRTNbxSZYwzx3VI8bqinAUMFl3LJ6z%2FWJDokYfTHnS2MWJwi3CL2ya9ZwrwyuFOhp3xZV0SGaZxreoyUCrTCLbjEa%2FKdV5mrEppBg7kliOR4rHLS48l0Ih80ODDgyFKFO1yvP%2FMIiFSiaZ6Z22%2FA5J7ARWb5vns%2FXN7tliZbp1nxC03qXhZ%2FUUh%2FmvzbQUzkUeclAIvjLLOjaaKnLtw7F58yuKgz8saMw%3D%3D&sign_type=RSA2×tamp=2022-09-02+08%3A52%3A38&version=1.0",
"outTradeNo": "243545656435254654654"
}
}
2.APP支付
2.1关键代码
//网关地址,APPID,商户应用私钥,数据格式,编码格式,支付宝公钥,签名算法类型
AlipayClient alipayClient = new DefaultAlipayClient(aliPayGateway,appID,rsa_private_key,format,charset,alipayPublicKey,signType);
//不同支付方式需使用不同的请求对象
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//请求参数集合对象,除了公共参数之外,所有参数都可通过此对象传递(不同支付类型需使用不同的请求参数对象)
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody("订单描述");
model.setSubject("订单标题");
//订单编号,此订单号不可重复
model.setOutTradeNo(getTradeNo());
//设置过期时间
model.setTimeoutExpress("30m");
model.setProductCode("QUICK_WAP_WAY");
model.setTotalAmount(String.valueOf(cashNum));
request.setBizModel(model);
//设置回调地址
request.setNotifyUrl("支付成功后,支付宝来回调的地址,此地址必须为公网地址,否则出现调不通");
String orderStr = "";
AlipayTradeAppPayResponse response1 = null;
try {
response1 = alipayClient.sdkExecute(request);
} catch (AlipayApiException e) {
return ReturnUtils.returnVal(CommonConstants.appCode.UNKNOWNERROR.get(), null);
}
orderStr = response1.getBody();
Map<String, String> result = new HashMap<String, String>();
result.put("orderStr", orderStr);
result.put("outTradeNo", getTradeNo());
return ReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(), result);
2.2 返回
//此响应信息为自己包装过的数据,result数据返回至APP即可{
"code": 49000,
"msg": "操作成功",
"result": {
"orderStr": "alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2021001181677544&biz_content=%7B%22body%22%3A%2223212%22%2C%22out_trade_no%22%3A%22d89006ef-630b-41e5-a78e-3fee47e72506%22%2C%22product<br> _code%22%3A%22QUICK_WAP_WAY%22%2C%22subject%22%3A%22%E6%94%AF%E4%BB%98%E5%AE%9D%E6%94%AF%E4%BB%98%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%22null%22%7D&charset=utf-8<br> &format=json&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2Ftest.hqjplatform.com%2Fmall%2Fapp%2FmodifyOrderCurstate&sign=ffaE9yTKYUXpKm%2FeVr2Uc1kt4DVPCutvfOg5h%2BvnaMuZgSqWuSwYCFkIheF6dw<br> 4PwjSX2gLDMROVdVzDzdpwRuSJpVZ2iNsSHR3Fs%2FLAKaHBJSzXO5ZM2emId1nVRIvdcZekpW3Y7FaiqGwBxm9xRir6%2FqV9ahmMZOyefmCzIhfl6WEIWyJgEDzVAKKlqwDZ%2FKmpJNscODi4fxUgVvaZpOre27QrSd%2Fi3ykoMbp6YtTs5aQg3HfcB9H8va<br> drOSejMxEU8pd0bWMuZDl5BiDBx49DSbA%2FRbLfQbpXYh7oSyOrdDaIletI%2FwSy0UVa6%2BD93ywFg%2BpZ7UcEu%2Ff7FVoeJA%3D%3D&sign_type=RSA2×tamp=2021-09-03+14%3A07%3A16&version=1.0",
"outTradeNo": "980363f7-3ece-4d71-8cf1-85a9b08ffde7"
}
}
原文地址:https://blog.csdn.net/qq_42444963/article/details/128212285
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_24798.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。