支付宝付款码支付接入流程。

官方文档地址: 小程序文档 - 支付宝文档中心

接入前提:

去控制台申请appId、应用私钥、支付宝公钥(注意不是应该公钥)、环境区分(有沙箱和正式环境区分)

代码贴图:

service代码

实现类方法

代码如下:

@Service
@Slf4j
public class AliPayServiceImpl implements AliPayService {// 支付宝网关名、partnerId和appId@Value("${ali.pay.openApiDomain}")public String openApiDomain;//public String mcloud_api_domain = "http://mcloudmonitor.com/gateway.do";// public String pid = "";@Value("${ali.pay.appId}")public String appId;// #RSA私钥、公钥和支付宝公钥@Value("${ali.pay.privateKey}")public String privateKey;//对应支付宝公钥@Value("${ali.pay.alipayPublicKey}")public String alipayPublicKey;/*** 支付宝付款码支付** @param payInfoVo* @return*/@Override@Transactional(rollbackFor = Exception.class)public ResponseWrapper aliPay(PayInfoVo payInfoVo, String ordNo) throws AlipayApiException, InterruptedException {AlipayClient alipayClient = new DefaultAlipayClient(openApiDomain, appId, privateKey, "json", "GBK", alipayPublicKey, "RSA2");AlipayTradePayRequest request = new AlipayTradePayRequest();Map<Object,Object> map = new HashMap<>();map.put("out_trade_no", ordNo);map.put("total_amount", payInfoVo.getMoney());//标题   必填map.put("subject", "订单付款");map.put("scene", "bar_code");map.put("auth_code", payInfoVo.getAuthCode());Gson gson = new Gson();String json = gson.toJson(map);request.setBizContent(json);AlipayTradePayResponse response = alipayClient.execute(request);log.info("支付宝支付返回支付信息 :" + GsonUtils.toJsonString(response));if (response.isSuccess()) {log.info("支付宝返回成功走code逻辑开始");if (response.getCode().equals(AliPayTradeCodeConstant.RETURN_CODE.getCode())) {return new ResponseWrapper(true, ReturnCode.PAY_SUCCESS.getCode(), "支付宝支付成功", null);}if (response.getCode().equals(AliPayTradeCodeConstant.WAIT_PAY.getCode())) {log.info("支付中----" + "进入支付中判断");//调用失败的话进行轮询查询返回结果Boolean payResult =ClientUtils.synGetResult(30000, 5000, () -> aliPayQuery(ordNo));log.info("支付中轮询查看支付结果" + payResult);if (payResult != null && payResult) {return new ResponseWrapper(true, ReturnCode.PAY_SUCCESS.getCode(), "支付宝支付成功", null);}log.info("超时走撤单接口");//撤销订单Boolean aliPayCancel = aliPayCancel(ordNo);if (aliPayCancel) {log.info("撤单成功");return new ResponseWrapper(true, ReturnCode.CANCEL_SUCCESS.getCode(), "超时撤单成功", null);} else {throw new BaseException("支付宝超时撤单失败");}}throw new BaseException("支付宝支付失败,失败原因为" + response.getMsg(), response.getMsg());} else {log.info("支付宝调用失败");throw new BaseException("支付宝支付失败,失败原因为" + response.getMsg(), response.getMsg());}}/*** 查询交易状态** @throws AlipayApiException*/public Boolean aliPayQuery(String ordNo) {try {AlipayClient alipayClient = new DefaultAlipayClient(openApiDomain, appId, privateKey,  "json", "GBK", alipayPublicKey, "RSA2");AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();//订单支付时传入的商户订单号,和支付宝交易号不能同时为空、trade_no,out_trade_no如果同时存在优先取trade_no//支付宝交易号,和商户订单号不能同时为空Map<Object,Object> map = new HashMap<>();map.put("out_trade_no", ordNo);Gson gson = new Gson();String json = gson.toJson(map);request.setBizContent(json);AlipayTradeQueryResponse response = alipayClient.execute(request);if (response.isSuccess()) {log.info("支付宝查询结果返回结果报文--" + JSON.toJSONString(response));if (response.getTradeStatus().equals(AliPayTradeStatusConstant.TRADE_SUCCESS.getCode()) || response.getTradeStatus().equals(AliPayTradeStatusConstant.TRADE_FINISHED.getCode())) {return Boolean.TRUE;} else if(response.getTradeStatus().equals(AliPayTradeStatusConstant.WAIT_BUYER_PAY.getCode())){log.info("查询返回不是支付成功," + "失败原因为:用户支付中" );return null;}else {log.error("查询返回不是支付成功," + "失败原因为:" + response.getSubMsg());return null;}} else {log.error("调用查询接口失败");return null;}} catch (Exception e) {log.error("AliPayServiceImpl aliPayQuery error:",e);return null;}}/*** 交易关闭接口** @throws AlipayApiException*/public void aliPayClose() throws AlipayApiException {AlipayClient alipayClient = new DefaultAlipayClient(openApiDomain, appId, privateKey,  "json", "GBK", alipayPublicKey, "RSA2");AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();Map<Object,Object> map = new HashMap<>();map.put("trade_no", "2013112611001004680073956707");Gson gson = new Gson();String json = gson.toJson(map);request.setBizContent(json);AlipayTradeCloseResponse response = alipayClient.execute(request);if (response.isSuccess()) {System.out.println("调用成功");} else {System.out.println("调用失败");}}/*** 交易撤销接口** @throws AlipayApiException*/public Boolean aliPayCancel(String ordNo) throws AlipayApiException {AlipayClient alipayClient = new DefaultAlipayClient(openApiDomain, appId, privateKey,  "json", "GBK", alipayPublicKey, "RSA2");AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();Map<Object,Object> map = new HashMap<>();map.put("out_trade_no", ordNo);Gson gson = new Gson();String json = gson.toJson(map);request.setBizContent(json);AlipayTradeCancelResponse response = alipayClient.execute(request);log.info("撤销订单接口返回--" +GsonUtils.toJsonString(response));if (response.isSuccess()) {if (response.getCode().equals(AliPayTradeCodeConstant.RETURN_CODE.getCode())) {log.info("撤销订单接口返回10000");return true;}return false;} else {System.out.println("调用失败");return false;}}/*** 退款** @throws Exception*/@Transactional(rollbackFor = Exception.class)@Overridepublic ResponseWrapper refund(String orderNo, String refundAmount) throws AlipayApiException {AlipayClient alipayClient = new DefaultAlipayClient(openApiDomain, appId, privateKey,  "json", "GBK", alipayPublicKey, "RSA2");AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();Map<Object,Object> map = new HashMap<>();map.put("out_trade_no", orderNo);map.put("refund_amount", refundAmount);//部分退款唯一值map.put("out_request_no", UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32));Gson gson = new Gson();String json = gson.toJson(map);request.setBizContent(json);AlipayTradeRefundResponse response = alipayClient.execute(request);if (response.isSuccess()) {System.out.println("调用成功");if (response.getCode().equals(AliPayTradeCodeConstant.RETURN_CODE.getCode())) {return new ResponseWrapper(true, ReturnCode.ALI_REFUNDZ_ERROR.getCode(), "支付宝退款成功", null);}//商户账户余额不足if (response.getSubCode().equals(AliPayTradeCodeConstant.SELLER_BALANCE_NOT_ENOUGH.getCode())) {return new ResponseWrapper(false, response.getCode(), response.getSubMsg(), response.getMsg());}}return new ResponseWrapper(false, response.getSubCode(), response.getSubMsg(), response.getSubMsg());}
}

30秒查询工具类代码:

package com.zt.helios.utils.wx;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;/*** 第三方接口工具类** @version V1.0* @className ClientUtils* @date 2022/4/22**/
@Slf4j
public class ClientUtils {/*** 同步返回结果** @param timeout     超时时间,单位:毫秒* @param cyclingTime 循环时间,单位:毫秒* @param supplier    业务逻辑,非null:完成;null:失败;* @methodName: synGetResult* @return: boolean* @author: ybw* @date: 2022/4/22**/public static <T> T synGetResult(long timeout, long cyclingTime, Supplier<T> supplier) {if (cyclingTime > timeout) {//如果循环时间>超时时间log.error("ClientUtils synGetResult cyclingTime > timeout");return null;}long start = System.currentTimeMillis();while (true) {T t = supplier.get();if (t != null) {//返回结果log.info("ClientUtils synGetResult 获取结果成功");return t;}try {TimeUnit.MILLISECONDS.sleep(cyclingTime);} catch (InterruptedException e) {log.error("ClientUtils synGetResult error:", e);}if (System.currentTimeMillis() - start > timeout) {//超时log.info("ClientUtils synGetResult 超时");return null;}}}
}

退款代码讲解:

代码块上面有。

整体接入流程就是这样,相对简单。前端扫码会携带一个参数auth_code,这个参数是扫码枪或者小白盒读取到的付款码数字。传到后端,后端请求支付宝接口时携带。为必填选项

支付宝付款码支付以及退款流程代码相关推荐

  1. TP5.1实现支付宝线下收银(扫码枪扫描用户支付宝付款码支付)

    1.在支付宝开放平台创建账号,选取支付方式---当面付,若需要其他的也可以一并加入,申请审核通过后,该支付需要签约,点击签约,按流程签约即可. 2.在申请过程中,接口加签方式需要在支付宝生成钥匙工具里 ...

  2. VB.net开发微信、支付宝扫码支付源码

    扫码消费机介绍:https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-21914722028.2.2b826baawDkx32&id=170 ...

  3. VFP开发微信、支付宝扫码支付

    &&扫码消费机介绍:https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-21914722028.4.2b826baa9cTxs0& ...

  4. 支付宝扫码支付,回调和退款(沙箱环境配置和完整代码)

    支付宝扫码支付 最近重构项目时,负责了支付模块,微信扫码支付(NATIVE)和 支付宝扫码支付,也是第一次接触,虽然根据官方文档和一些博客写出来了,但是遇到的问题却很多,走了很多弯路,浪费了很多精力和 ...

  5. 搞定支付接口(一) 支付宝即时到账支付接口详细流程和代码

    搞定支付接口(一) 支付宝即时到账支付接口详细流程和java代码 为避免你们和我一样被支付接口搞得焦头烂额,写一个从申请开始到能收到钱为止的详细教程,实际上各个语言都可以用来集成支付接口,我用java ...

  6. JAVA支付宝扫码支付开发流程

    近期工作遇到支付宝扫码支付,经开发研究与3年前相比  API调用与配置更加简单方便,整体开发流程如下(后期会有对应的支付宝APP支付) 登录支付宝开发平台 -->开发接入    链接:https ...

  7. 支付宝--扫码支付流程

    1.首次知道,原来(支付宝支付接口)需要去蚂蚁金服开发者帮助文档(尴尬). 附带链接地址:https://docs.open.alipay.com 2.创建自己的应用,我选的是[自研接入](这是一个坑 ...

  8. 微信支付宝扫码支付简介

    微信平台 支付模式 付款码支付 Native支付 JSAPI支付 APP支付 H5支付 小程序支付 各种模式的定义可以查看微信官方文档 这里应用Native支付,它是商户系统按微信支付协议生成支付二维 ...

  9. java实现支付宝扫码支付详细步骤

    支付宝扫码支付API文档地址:https://opendocs.alipay.com/open/194/106078         , 先配置好public_lkey和private_key 在支付 ...

最新文章

  1. matlab的帮助命令是英文的,4 Matlab 帮助系统
  2. (Alan Murta)编制的多边形集合运算软件包(general polygon clipping library,简称GPC)
  3. python【力扣LeetCode算法题库】225-用队列实现栈
  4. POJ3450 Corporate Identity —— 后缀数组 最长公共子序列
  5. 响应式设计PageAdmin个人博客系统源码v4.0.10
  6. 创建维护计划失败_如何善于创建和维护大型系统
  7. do还是doing imagine加to_朗华环保环保管家科普之系列133:关于溶解氧DO,你真正了解吗?...
  8. JVM第三节:垃圾回收算法与垃圾回收器
  9. 注册(二)之增加绑定
  10. 如何在WORD中插入图片自动编号,并引用图号【每天一个小技巧】
  11. 你竟然是这么神奇的 CSS
  12. jsp 图片下载功能
  13. 计算机休眠后无法联网,电脑休眠后回来就不能上网了
  14. nyoj 779 兰州烧饼
  15. word2016(office 365)中安装mathtype相关问题及解决办法
  16. 计算机教师职业幸福感,教师职业幸福感
  17. JavaEE的RESTful标准技术JAX-RS,jersey-client客户端使用介绍【享学Java】
  18. 深信服安全运营工程师一面经验+二面经验
  19. TCP 与UDP的区别
  20. 【文献翻译】Concealed Object Detection(伪装目标检测)

热门文章

  1. java 泛型 PECS准则
  2. 什么是Vue.js?适合入门小白(笔记1)
  3. 华为Vs爱立信:非卫翰思无能 而是任正非太厉害
  4. 关于backiee幻灯片图片保存的问题
  5. Delphi - 创建text文件并添加数据到文件中
  6. C了个++:02 - C++的名称空间(层次)
  7. 后门之shift后门制作及修复方法
  8. 【FXCG】关于三重滤网
  9. Python图算法之深度优先搜索
  10. 利用ASCII码随机导出数字和字母