支付宝支付对接(JAVA版)

时间:2024-02-17 18:02:08
配置申请地址 https://open.alipay.com/platform/home.html(开发对接中需要以下参数)
//支付宝网关地址:https://openapi.alipay.com/gateway.do 
//支付宝申请的APPID
//商户应用私钥(与支付宝数据交互时,保证数据安全,防止数据被篡改)
//支付宝公钥(与支付宝数据交互时,保证数据安全,防止数据被篡改)
//签名算法类型(RSA2或者RSA,支付宝推荐使用RSA2)
//编码格式(utf-8,gb2312,gbk) 
详细接口或参数说明,以支付宝官方文档为准, 支付宝API网址(https://opendocs.alipay.com/apis)
 
一.手机网站支付(H5版)
  1.导入支付宝提供的jar,此为Maven版
<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>4.9.124.ALL</version>
</dependency>

  2.关键支付代码

//网关地址,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 { responseH5 = alipayClient.pageExecute(request); } 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);

  3.响应返回参数(为支付的form表单,前端解析此表单即可)

{
    "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=\"{&quot;body&quot;:&quot;优选商城订单
     &quot;,&quot;out_trade_no&quot;:&quot;243545656435254654654&quot;,&quot;product_code&quot;:&quot;QUICK_MSECURITY_PAY&quot;,&quot;quit_url&quot;:&quot;http:\/\/192.168.16.108:8041\
     /mall\/commodity\/getPayMent\/1100?uid=2&hash=3936471004987776752654077415810075258431176020006439440944207350&quot;,&quot;subject&quot;:&quot;优选商城&quot;,&quot;timeout_express&quot;:
     &quot;30m&quot;,&quot;total_amount&quot;:&quot;0.1&quot;}\">\n<input type=\"submit\" value=\"立即支付\" style=\"display:none\" >\n</form>\n<script>document.forms[0].submit();</script>", "outTradeNo": "243545656435254654654" } }
 
 
 
 
 
二.APP支付
  1.跟手机网站支付一样,需要如下参数
//支付宝网关地址:https://openapi.alipay.com/gateway.do 
//支付宝申请的APPID
//商户应用私钥(与支付宝数据交互时,保证数据安全,防止数据被篡改)
//支付宝公钥(与支付宝数据交互时,保证数据安全,防止数据被篡改)
//签名算法类型(RSA2或者RSA,支付宝推荐使用RSA2)
//编码格式(utf-8,gb2312,gbk)
  2.支付宝APP支付关键代码
     //网关地址,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);

  3.APP支付响应参数

//此响应信息为自己包装过的数据,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
     _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
     &format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Ftest.hqjplatform.com%2Fmall%2Fapp%2FmodifyOrderCurstate&sign=ffaE9yTKYUXpKm%2FeVr2Uc1kt4DVPCutvfOg5h%2BvnaMuZgSqWuSwYCFkIheF6dw
     4PwjSX2gLDMROVdVzDzdpwRuSJpVZ2iNsSHR3Fs%2FLAKaHBJSzXO5ZM2emId1nVRIvdcZekpW3Y7FaiqGwBxm9xRir6%2FqV9ahmMZOyefmCzIhfl6WEIWyJgEDzVAKKlqwDZ%2FKmpJNscODi4fxUgVvaZpOre27QrSd%2Fi3ykoMbp6YtTs5aQg3HfcB9H8va
     drOSejMxEU8pd0bWMuZDl5BiDBx49DSbA%2FRbLfQbpXYh7oSyOrdDaIletI%2FwSy0UVa6%2BD93ywFg%2BpZ7UcEu%2Ff7FVoeJA%3D%3D&sign_type=RSA2&timestamp=2021-09-03+14%3A07%3A16&version=1.0", "outTradeNo": "980363f7-3ece-4d71-8cf1-85a9b08ffde7" } }

如文中出现错误,请在评论区指出,或与WX:15514769010 联系,定及时订正