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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java微信小程序支付-回调(Jsapi-APIv3)

發布時間:2024/1/8 编程问答 103 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java微信小程序支付-回调(Jsapi-APIv3) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、接入前準備,按照這個文檔準備

? ? ? ?準備:?接入前準備-小程序支付 | 微信支付商戶平臺文檔中心

準備好了就可以獲得(第二點里需要的參數):
????????參數1?商戶號 merchantId:xxxxxx(全是數字)

????????參數2?商戶APIV3密鑰 apiV3key:xxxxxxx(32位字母數字大小寫串,開發自己準備的)

????????參數3?商戶證書序列號 merchantSerialNumber:xxxxx

? ? ????????查看方式:微信支付證書序列號在哪里看(v3商戶證書序列號在哪里查找)-李飛SEO

????????參數4?商戶API私鑰路徑 privateKeyPath:apiclient_key.pem的文件路徑

---------------------Native (二維碼)支付下單,前四個就夠了,5是用于Jsapi支付----------------
????????參數5?國密的微信支付平臺證書路徑(X509文件) wechatPayCertificatePath:wechatpay_xxxx.pem()

? ? ? ? ? ? ? ? 這個文件找了好久,最后試用下邊這個工具下下來可以用GitHub - wechatpay-apiv3/CertificateDownloader: Java 微信支付 APIv3 平臺證書的命令行下載工具
?

二、支付代碼接入

支付-官網文檔:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7

微信給apiv3做了個maven包,終于像個樣子了,結果我發現里邊native支付例子雖然完整,但是第一大點里的1-5個參數完全不知道怎么給,jsapi參數同理,我是的東拼西湊湊齊了。maven包文檔

-

貼一個jsapi的調用例子,小程序端喚起支付用它就夠了,這是在官方的例子基礎上補全了:

import com.alibaba.fastjson.JSONObject; import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.RSAConfig; import com.wechat.pay.java.core.exception.HttpException; import com.wechat.pay.java.core.exception.MalformedMessageException; import com.wechat.pay.java.core.exception.ServiceException; import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; import com.wechat.pay.java.service.payments.jsapi.model.CloseOrderRequest; import com.wechat.pay.java.service.payments.jsapi.model.Payer; import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest; import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse; import com.wechat.pay.java.service.payments.jsapi.model.QueryOrderByIdRequest; import com.wechat.pay.java.service.payments.jsapi.model.QueryOrderByOutTradeNoRequest; import com.wechat.pay.java.service.payments.model.Transaction; import com.wechat.pay.java.service.payments.jsapi.model.Amount; import lombok.extern.slf4j.Slf4j;@Slf4j public class JsapiDemoService {public static String merchantId = "1xxxx"; // public static String privateKeyPath = "";public static String merchantSerialNumber = "xxxx";public static String wechatPayCertificatePath = "";public static com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension service;public static PrepayWithRequestPaymentResponse createWxJsapOrder(String certificatePath, String keyPath, JsapiReq cliReq) {String method = Thread.currentThread().getStackTrace()[1].getMethodName();// 使用自動更新平臺證書的RSA配置// 一個商戶號只能初始化一個配置,否則會因為重復的下載任務報錯Config config =new RSAConfig.Builder().merchantId(merchantId)// 使用 com.wechat.pay.java.core.util 中的函數從本地文件中加載商戶私鑰,商戶私鑰會用來生成請求的簽名.privateKeyFromPath(keyPath).merchantSerialNumber(merchantSerialNumber).wechatPayCertificatesFromPath(certificatePath).build();// 構建serviceJsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();// request.setXxx(val)設置所需參數,具體參數可見Request定義PrepayRequest request = new PrepayRequest();Payer payer = new Payer();payer.setOpenid(cliReq.getOpenid());request.setPayer(payer);Amount amount = new Amount();amount.setTotal(10);//訂單總金額,單位為分request.setAmount(amount);request.setAppid("wxddddxxxxxx");request.setMchid("1xxxx");request.setDescription("ms");request.setNotifyUrl("https://xxx.net/callBackr");//request.setOutTradeNo("out_trade_no_1");request.setOutTradeNo(cliReq.getOutTradeNo());// 調用下單方法,得到應答try {// ... 調用接口PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);// 使用微信掃描 code_url 對應的二維碼,即可體驗Native支付System.out.println(JSONObject.toJSON(response));return response;} catch (HttpException e) { // 發送HTTP請求失敗// 調用e.getHttpRequest()獲取請求打印日志或上報監控,更多方法見HttpException定義log.error(method, e);} catch (ServiceException e) { // 服務返回狀態小于200或大于等于300,例如500// 調用e.getResponseBody()獲取返回體打印日志或上報監控,更多方法見ServiceException定義log.error(method, e);} catch (MalformedMessageException e) { // 服務返回成功,返回體類型不合法,或者解析返回體失敗// 調用e.getMessage()獲取信息打印日志或上報監控,更多方法見MalformedMessageException定義log.error(method, e);} catch (Exception e) { // 服務返回成功,返回體類型不合法,或者解析返回體失敗// 調用e.getMessage()獲取信息打印日志或上報監控,更多方法見MalformedMessageException定義log.error(method, e);}return new PrepayWithRequestPaymentResponse();}/** 關閉訂單 */public static void closeOrder() {CloseOrderRequest request = new CloseOrderRequest();// 調用request.setXxx(val)設置所需參數,具體參數可見Request定義// 調用接口service.closeOrder(request);}/** JSAPI支付下單,并返回JSAPI調起支付數據 */public static PrepayWithRequestPaymentResponse prepayWithRequestPayment() {PrepayRequest request = new PrepayRequest();// 調用request.setXxx(val)設置所需參數,具體參數可見Request定義// 調用接口return service.prepayWithRequestPayment(request);}/** 微信支付訂單號查詢訂單 */public static Transaction queryOrderById() {QueryOrderByIdRequest request = new QueryOrderByIdRequest();// 調用request.setXxx(val)設置所需參數,具體參數可見Request定義// 調用接口return service.queryOrderById(request);}/** 商戶訂單號查詢訂單 */public static Transaction queryOrderByOutTradeNo() {QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();// 調用request.setXxx(val)設置所需參數,具體參數可見Request定義// 調用接口return service.queryOrderByOutTradeNo(request);} }

補下JsapiReq?的定義

public class JsapiReq implements Serializable {private static final long serialVersionUID = 1L;private String openid;private String outTradeNo; }

?另外記一下java/spring/srpingboot里resource文件讀取路徑的方法

String userDir = System.getProperty("user.dir");String certificatePath = userDir + "/src/main/resources/cert/wechatpay_xxx.pem";String keyPath = userDir + "/src/main/resources/cert/apiclient_xxxxkey.pem";

三、支付回調

支付回調-官網文檔:微信支付-開發者文檔

代碼

@PostMapping(value = "/callBack")public Map<String, String> callBack(@RequestBody JSONObject jsonObject) {String method = Thread.currentThread().getStackTrace()[1].getMethodName();try {String key = WxNativePayProxy.getWxV3Key();String json = jsonObject.toString();String associated_data = (String) JSONUtil.getByPath(JSONUtil.parse(json), "resource.associated_data");String ciphertext = (String) JSONUtil.getByPath(JSONUtil.parse(json), "resource.ciphertext");String nonce = (String) JSONUtil.getByPath(JSONUtil.parse(json), "resource.nonce");String decryptData = new AesUtil(key.getBytes(StandardCharsets.UTF_8)).decryptToString(associated_data.getBytes(StandardCharsets.UTF_8), nonce.getBytes(StandardCharsets.UTF_8), ciphertext);//驗簽成功JSONObject decryptDataObj = JSONObject.parseObject(decryptData, JSONObject.class); //decryptDataObj 為解碼后的obj,其內容如下。之后便是驗簽成功后的業務處理//{// "sp_appid": "wx8888888888888888",// "sp_mchid": "1230000109",// "sub_appid": "wxd678efh567hg6999",// "sub_mchid": "1900000109",// "out_trade_no": "1217752501201407033233368018",// "trade_state_desc": "支付成功",// "trade_type": "MICROPAY",// "attach": "自定義數據",// "transaction_id": "1217752501201407033233368018",// "trade_state": "SUCCESS",// "bank_type": "CMC",// "success_time": "2018-06-08T10:34:56+08:00",// ...// "payer": {// "openid": "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"// },// "scene_info": {// "device_id": "013467007045764"// }//}}catch (Exception e){log.info("{} ,parms{}, 異常:", method, jsonObject.toJSONString(), e);}Map<String, String> res = new HashMap<>();res.put("code", "SUCCESS");res.put("message", "成功");return res;}

其中有用到微信解碼工具類:AesUtil

import java.io.IOException; import java.security.GeneralSecurityException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.SecretKeySpec;public class AesUtil{static final int KEY_LENGTH_BYTE = 32;static final int TAG_LENGTH_BIT = 128;private final byte[] aesKey;public AesUtil(byte[] key) {if (key.length != KEY_LENGTH_BYTE) {throw new IllegalArgumentException("無效的ApiV3Key,長度必須為32個字節");}this.aesKey = key;}public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)throws GeneralSecurityException, IOException {try {Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");SecretKeySpec key = new SecretKeySpec(aesKey, "AES");GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);cipher.init(Cipher.DECRYPT_MODE, key, spec);cipher.updateAAD(associatedData);return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {throw new IllegalStateException(e);} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {throw new IllegalArgumentException(e);}} }

總結

以上是生活随笔為你收集整理的java微信小程序支付-回调(Jsapi-APIv3)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。