一.准备工作

配置申请地址 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>
    &lt;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&amp;method=alipay.trade.wap.pay&amp;sign=
     jZpBgo2oaAvd%2FAyXEnWlvd8PRRdJAaaI%2B35bTg4QDvhCaPY3X2lSi0Gb7AXDqKfPe%2BwDHMjwWkFCsl9Pu%2F%2BYZBDqof7w0NxJL5V2eqfuKzRIzRpEbmTgDbWovGPvbYD8lYK%2BJ41QpcJUFZzi%2BPQ4GCmWIbTj
     %2FMypJVvqWptpzo57kdXqcaydaP2CK0WhkDhMtqQOFFc%2BiEw9%2Bj6TyZ1BtZM9zcGsBQlBaw%2FhMxIlogOHP5C3vadZPWluvI%2FR18ywoduxdbueJ2Dkblpph3mRe8m6buXelMOqjeT5r9jwnrYu4pxa%2Bj0CMyqSFGJ
     tB0z%2BwaOC1T5MGDAUDQdq6U957A%3D%3D&amp;return_url=http%3A%2F%2F192.168.16.108%3A8041%2Fmall%2Fcommodity%2FpaymentByPay%3Fid%3D1100%26uid%3D2%26hash%3D39364710049877767526540774
      15810075258431176020006439440944207350&amp;notify_url=http%3A%2F%2Ftest.hqjplatform.com%2Fmall%2Fapp%2FmodifyOrderCurstate&amp;version=1.0&amp;app_id=2021001181677544&amp;sign_type=RSA2&amp;time
     stamp=2021-09-03+09%3A07%3A39&amp;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"
    }
}

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&notify_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&timestamp=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&notify_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&timestamp=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进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注