支付宝付款码支付以及退款流程代码
生活随笔
收集整理的這篇文章主要介紹了
支付宝付款码支付以及退款流程代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
支付寶付款碼支付接入流程。
官方文檔地址:?小程序文檔 - 支付寶文檔中心
接入前提:
去控制臺申請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,這個參數是掃碼槍或者小白盒讀取到的付款碼數字。傳到后端,后端請求支付寶接口時攜帶。為必填選項
總結
以上是生活随笔為你收集整理的支付宝付款码支付以及退款流程代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AH8601:12v转5v 2a大电流转
- 下一篇: 【论文笔记】An Unsupervise