日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

支付宝付款码支付以及退款流程代码

發(fā)布時間:2024/1/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 支付宝付款码支付以及退款流程代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

支付寶付款碼支付接入流程。

官方文檔地址:?小程序文檔 - 支付寶文檔中心

接入前提:

去控制臺申請appId、應(yīng)用私鑰、支付寶公鑰(注意不是應(yīng)該公鑰)、環(huán)境區(qū)分(有沙箱和正式環(huán)境區(qū)分)

代碼貼圖:

service代碼

?實現(xiàn)類方法

?代碼如下:

@Service @Slf4j public class AliPayServiceImpl implements AliPayService {// 支付寶網(wǎng)關(guān)名、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;//對應(yīng)支付寶公鑰@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("支付中----" + "進入支付中判斷");//調(diào)用失敗的話進行輪詢查詢返回結(jié)果Boolean payResult =ClientUtils.synGetResult(30000, 5000, () -> aliPayQuery(ordNo));log.info("支付中輪詢查看支付結(jié)果" + 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("支付寶調(diào)用失敗");throw new BaseException("支付寶支付失敗,失敗原因為" + response.getMsg(), response.getMsg());}}/*** 查詢交易狀態(tài)** @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如果同時存在優(yōu)先取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("支付寶查詢結(jié)果返回結(jié)果報文--" + 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("調(diào)用查詢接口失敗");return null;}} catch (Exception e) {log.error("AliPayServiceImpl aliPayQuery error:",e);return null;}}/*** 交易關(guān)閉接口** @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("調(diào)用成功");} else {System.out.println("調(diào)用失敗");}}/*** 交易撤銷接口** @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("調(diào)用失敗");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("調(diào)用成功");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 {/*** 同步返回結(jié)果** @param timeout 超時時間,單位:毫秒* @param cyclingTime 循環(huán)時間,單位:毫秒* @param supplier 業(yè)務(wù)邏輯,非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) {//如果循環(huán)時間>超時時間log.error("ClientUtils synGetResult cyclingTime > timeout");return null;}long start = System.currentTimeMillis();while (true) {T t = supplier.get();if (t != null) {//返回結(jié)果log.info("ClientUtils synGetResult 獲取結(jié)果成功");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;}}} }

退款代碼講解:

?代碼塊上面有。

整體接入流程就是這樣,相對簡單。前端掃碼會攜帶一個參數(shù)auth_code,這個參數(shù)是掃碼槍或者小白盒讀取到的付款碼數(shù)字。傳到后端,后端請求支付寶接口時攜帶。為必填選項

總結(jié)

以上是生活随笔為你收集整理的支付宝付款码支付以及退款流程代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。