提交修改
This commit is contained in:
parent
b80df3079d
commit
0783b8814f
23
api-third/src/main/java/com/heyu/api/jsapi/JSAPIConfig.java
Normal file
23
api-third/src/main/java/com/heyu/api/jsapi/JSAPIConfig.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package com.heyu.api.jsapi;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Slf4j
|
||||||
|
public class JSAPIConfig {
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JsapiPrepay jsapiPrepay(@Value("${eb.config.weixin.pay.mchid}") String mchid,
|
||||||
|
@Value("${eb.config.weixin.pay.certificateSerialNo}") String certificateSerialNo,
|
||||||
|
@Value("${eb.config.weixin.pay.privateKeyFilePath}") String privateKeyFilePath,
|
||||||
|
@Value("${eb.config.weixin.pay.wechatPayPublicKeyId}") String wechatPayPublicKeyId,
|
||||||
|
@Value("${eb.config.weixin.pay.wechatPayPublicKeyFilePath}") String wechatPayPublicKeyFilePath) {
|
||||||
|
return new JsapiPrepay(mchid, certificateSerialNo, privateKeyFilePath, wechatPayPublicKeyId, wechatPayPublicKeyFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -2,22 +2,22 @@ package com.heyu.api.jsapi;
|
|||||||
|
|
||||||
// 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/v3/merchant/4014931831
|
// 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/v3/merchant/4014931831
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.heyu.api.jsapi.dto.DirectAPIv3JsapiPrepayRequest;
|
||||||
import com.heyu.api.jsapi.dto.*;
|
import com.heyu.api.jsapi.dto.DirectAPIv3JsapiPrepayResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
import java.security.PublicKey;
|
import java.security.PublicKey;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSAPI下单
|
* JSAPI下单
|
||||||
*/
|
*/
|
||||||
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class JsapiPrepay {
|
public class JsapiPrepay {
|
||||||
|
|
||||||
@ -27,11 +27,15 @@ public class JsapiPrepay {
|
|||||||
|
|
||||||
private static String PATH = "/v3/pay/transactions/jsapi";
|
private static String PATH = "/v3/pay/transactions/jsapi";
|
||||||
|
|
||||||
private final String mchid;
|
private String mchid;
|
||||||
private final String certificateSerialNo;
|
private String certificateSerialNo;
|
||||||
private final PrivateKey privateKey;
|
private PrivateKey privateKey;
|
||||||
private final String wechatPayPublicKeyId;
|
private String wechatPayPublicKeyId;
|
||||||
private final PublicKey wechatPayPublicKey;
|
private PublicKey wechatPayPublicKey;
|
||||||
|
|
||||||
|
public JsapiPrepay() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public JsapiPrepay(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) {
|
public JsapiPrepay(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) {
|
||||||
this.mchid = mchid;
|
this.mchid = mchid;
|
||||||
@ -41,84 +45,6 @@ public class JsapiPrepay {
|
|||||||
this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath);
|
this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
// TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756
|
|
||||||
JsapiPrepay client = new JsapiPrepay(
|
|
||||||
"1731491745", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
|
|
||||||
"793D48CCEB62C6B227E0A4F46AD90279B149A7BE", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
|
|
||||||
"/Users/quyixiao/Desktop/weixincert/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径
|
|
||||||
"PUB_KEY_ID_0117314917452025110400382304001401", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
|
|
||||||
"/Users/quyixiao/Desktop/weixincert/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径
|
|
||||||
);
|
|
||||||
|
|
||||||
DirectAPIv3JsapiPrepayRequest request = new DirectAPIv3JsapiPrepayRequest();
|
|
||||||
request.setAppid("wx75fa59c097bd3dfd");
|
|
||||||
request.setMchid("1731491745");
|
|
||||||
request.setDescription("Image形象店-深圳腾大-QQ公仔");
|
|
||||||
request.setOutTradeNo("121775250");
|
|
||||||
request.setTimeExpire(WXPayUtility.generateExpireTime()); // 2025-11-05T21:02:16+08:00
|
|
||||||
request.setAttach("自定义数据说明");
|
|
||||||
request.setNotifyUrl("https://api.1024api.com/api-interface/app/weixin/payNotify");
|
|
||||||
request.setGoodsTag("WXG");
|
|
||||||
request.setSupportFapiao(false);
|
|
||||||
|
|
||||||
|
|
||||||
CommonAmountInfo commonAmountInfo = new CommonAmountInfo();
|
|
||||||
commonAmountInfo.setTotal(5L);
|
|
||||||
commonAmountInfo.setCurrency("CNY");
|
|
||||||
request.setAmount(commonAmountInfo);
|
|
||||||
|
|
||||||
JsapiReqPayerInfo payer = new JsapiReqPayerInfo();
|
|
||||||
payer.setOpenid("o6t1512tT-JuBeT6rIu6RhF Gf3BQ");
|
|
||||||
request.setPayer(payer);
|
|
||||||
|
|
||||||
CouponInfo couponInfo = new CouponInfo();
|
|
||||||
|
|
||||||
couponInfo.setCostPrice(608800L);
|
|
||||||
couponInfo.setInvoiceId("微信123");
|
|
||||||
request.setDetail(couponInfo);
|
|
||||||
List<GoodsDetail> goodsDetails = new ArrayList<>();
|
|
||||||
|
|
||||||
GoodsDetail goodsDetailItem = new GoodsDetail();
|
|
||||||
goodsDetailItem.setMerchantGoodsId("1246464644");
|
|
||||||
goodsDetailItem.setWechatpayGoodsId("1001");
|
|
||||||
goodsDetailItem.setGoodsName("iPhoneX 256G");
|
|
||||||
goodsDetailItem.setQuantity(1L);
|
|
||||||
goodsDetailItem.setUnitPrice(528800L);
|
|
||||||
goodsDetails.add(goodsDetailItem);
|
|
||||||
couponInfo.setGoodsDetail(goodsDetails);
|
|
||||||
|
|
||||||
CommonSceneInfo sceneInfo = new CommonSceneInfo();
|
|
||||||
sceneInfo.setPayerClientIp("14.23.150.211");
|
|
||||||
sceneInfo.setDeviceId("013467007045764");
|
|
||||||
|
|
||||||
StoreInfo setstoreInfo = new StoreInfo();
|
|
||||||
setstoreInfo.setId("0001");
|
|
||||||
setstoreInfo.setName("腾讯大厦分店");
|
|
||||||
setstoreInfo.setAreaCode("440305");
|
|
||||||
setstoreInfo.setAddress("广东省深圳市南山区科技中一道10000号");
|
|
||||||
|
|
||||||
SettleInfo settleInfo = new SettleInfo();
|
|
||||||
settleInfo.setProfitSharing(false);
|
|
||||||
|
|
||||||
request.setSceneInfo(sceneInfo);
|
|
||||||
sceneInfo.setStoreInfo(setstoreInfo);
|
|
||||||
request.setSettleInfo(settleInfo);
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
DirectAPIv3JsapiPrepayResponse response = client.rePay(request);
|
|
||||||
System.out.println(JSON.toJSONString(response));
|
|
||||||
// TODO: 请求成功,继续业务逻辑
|
|
||||||
System.out.println(response);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: 请求失败,根据状态码执行不同的逻辑
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public DirectAPIv3JsapiPrepayResponse rePay(DirectAPIv3JsapiPrepayRequest request) {
|
public DirectAPIv3JsapiPrepayResponse rePay(DirectAPIv3JsapiPrepayRequest request) {
|
||||||
String uri = PATH;
|
String uri = PATH;
|
||||||
@ -129,9 +55,7 @@ public class JsapiPrepay {
|
|||||||
reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId);
|
reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId);
|
||||||
reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, reqBody));
|
reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, reqBody));
|
||||||
reqBuilder.addHeader("Content-Type", "application/json");
|
reqBuilder.addHeader("Content-Type", "application/json");
|
||||||
|
|
||||||
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody);
|
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody);
|
||||||
|
|
||||||
reqBuilder.method(METHOD, requestBody);
|
reqBuilder.method(METHOD, requestBody);
|
||||||
|
|
||||||
Request httpRequest = reqBuilder.build();
|
Request httpRequest = reqBuilder.build();
|
||||||
@ -147,8 +71,7 @@ public class JsapiPrepay {
|
|||||||
}
|
}
|
||||||
if (httpResponse.code() >= 200 && httpResponse.code() < 300) {
|
if (httpResponse.code() >= 200 && httpResponse.code() < 300) {
|
||||||
// 2XX 成功,验证应答签名
|
// 2XX 成功,验证应答签名
|
||||||
WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey,
|
WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, httpResponse.headers(), respBody);
|
||||||
httpResponse.headers(), respBody);
|
|
||||||
|
|
||||||
// 从HTTP应答报文构建返回数据
|
// 从HTTP应答报文构建返回数据
|
||||||
return WXPayUtility.fromJson(respBody, DirectAPIv3JsapiPrepayResponse.class);
|
return WXPayUtility.fromJson(respBody, DirectAPIv3JsapiPrepayResponse.class);
|
||||||
|
|||||||
@ -59,8 +59,20 @@ spring:
|
|||||||
port: 5672
|
port: 5672
|
||||||
username: admin
|
username: admin
|
||||||
password: XiIg37HKfQg2
|
password: XiIg37HKfQg2
|
||||||
# rabbitmq:
|
|
||||||
# host: rabit.iwulin.tech
|
eb:
|
||||||
# port: 32407
|
config:
|
||||||
# username: admin
|
weixin:
|
||||||
# password: Ij_klfJ^$^_3IBs
|
pay:
|
||||||
|
mchid: 1731491745
|
||||||
|
certificateSerialNo: 793D48CCEB62C6B227E0A4F46AD90279B149A7BE
|
||||||
|
privateKey: /Users/quyixiao/Desktop/weixincert/apiclient_key.pem
|
||||||
|
wechatPayPublicKeyId: PUB_KEY_ID_0117314917452025110400382304001401
|
||||||
|
wechatPayPublicKey: /Users/quyixiao/Desktop/weixincert/wxp_pub.pem
|
||||||
|
appid: wx75fa59c097bd3dfd
|
||||||
|
notifyUrl: https://api.1024api.com/api-interface/app/weixin/payNotify
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -52,4 +52,19 @@ spring:
|
|||||||
host: 172.16.5.230
|
host: 172.16.5.230
|
||||||
port: 5672
|
port: 5672
|
||||||
username: admin
|
username: admin
|
||||||
password: XiIg37HKfQg2
|
password: XiIg37HKfQg2
|
||||||
|
|
||||||
|
|
||||||
|
eb:
|
||||||
|
config:
|
||||||
|
weixin:
|
||||||
|
pay:
|
||||||
|
mchid: 1731491745
|
||||||
|
certificateSerialNo: 793D48CCEB62C6B227E0A4F46AD90279B149A7BE
|
||||||
|
privateKey: /Users/quyixiao/Desktop/weixincert/apiclient_key.pem
|
||||||
|
wechatPayPublicKeyId: PUB_KEY_ID_0117314917452025110400382304001401
|
||||||
|
wechatPayPublicKey: /Users/quyixiao/Desktop/weixincert/wxp_pub.pem
|
||||||
|
appid: wx75fa59c097bd3dfd
|
||||||
|
notifyUrl: https://api.1024api.com/api-interface/app/weixin/payNotify
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,96 @@
|
|||||||
|
package com.api.test;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.heyu.api.jsapi.JsapiPrepay;
|
||||||
|
import com.heyu.api.jsapi.WXPayUtility;
|
||||||
|
import com.heyu.api.jsapi.dto.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TestJSAPI {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756
|
||||||
|
JsapiPrepay client = new JsapiPrepay(
|
||||||
|
"1731491745", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
|
||||||
|
"793D48CCEB62C6B227E0A4F46AD90279B149A7BE", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
|
||||||
|
"/Users/quyixiao/Desktop/weixincert/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径
|
||||||
|
"PUB_KEY_ID_0117314917452025110400382304001401", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
|
||||||
|
"/Users/quyixiao/Desktop/weixincert/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径
|
||||||
|
);
|
||||||
|
|
||||||
|
DirectAPIv3JsapiPrepayRequest request = new DirectAPIv3JsapiPrepayRequest();
|
||||||
|
request.setAppid("wx75fa59c097bd3dfd");
|
||||||
|
request.setMchid("1731491745");
|
||||||
|
request.setNotifyUrl("https://api.1024api.com/api-interface/app/weixin/payNotify");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
request.setDescription("Image形象店-深圳腾大-QQ公仔");
|
||||||
|
request.setOutTradeNo("121775250");
|
||||||
|
request.setTimeExpire(WXPayUtility.generateExpireTime()); // 2025-11-05T21:02:16+08:00
|
||||||
|
request.setAttach("自定义数据说明");
|
||||||
|
|
||||||
|
request.setGoodsTag("WXG");
|
||||||
|
request.setSupportFapiao(false);
|
||||||
|
|
||||||
|
|
||||||
|
CommonAmountInfo commonAmountInfo = new CommonAmountInfo();
|
||||||
|
commonAmountInfo.setTotal(5L);
|
||||||
|
commonAmountInfo.setCurrency("CNY");
|
||||||
|
request.setAmount(commonAmountInfo);
|
||||||
|
|
||||||
|
JsapiReqPayerInfo payer = new JsapiReqPayerInfo();
|
||||||
|
payer.setOpenid("o6t1512tT-JuBeT6rIu6RhF Gf3BQ");
|
||||||
|
request.setPayer(payer);
|
||||||
|
|
||||||
|
CouponInfo couponInfo = new CouponInfo();
|
||||||
|
|
||||||
|
couponInfo.setCostPrice(608800L);
|
||||||
|
couponInfo.setInvoiceId("微信123");
|
||||||
|
request.setDetail(couponInfo);
|
||||||
|
List<GoodsDetail> goodsDetails = new ArrayList<>();
|
||||||
|
|
||||||
|
GoodsDetail goodsDetailItem = new GoodsDetail();
|
||||||
|
goodsDetailItem.setMerchantGoodsId("1246464644");
|
||||||
|
goodsDetailItem.setWechatpayGoodsId("1001");
|
||||||
|
goodsDetailItem.setGoodsName("iPhoneX 256G");
|
||||||
|
goodsDetailItem.setQuantity(1L);
|
||||||
|
goodsDetailItem.setUnitPrice(528800L);
|
||||||
|
goodsDetails.add(goodsDetailItem);
|
||||||
|
couponInfo.setGoodsDetail(goodsDetails);
|
||||||
|
|
||||||
|
CommonSceneInfo sceneInfo = new CommonSceneInfo();
|
||||||
|
sceneInfo.setPayerClientIp("14.23.150.211");
|
||||||
|
sceneInfo.setDeviceId("013467007045764");
|
||||||
|
|
||||||
|
StoreInfo setstoreInfo = new StoreInfo();
|
||||||
|
setstoreInfo.setId("0001");
|
||||||
|
setstoreInfo.setName("腾讯大厦分店");
|
||||||
|
setstoreInfo.setAreaCode("440305");
|
||||||
|
setstoreInfo.setAddress("广东省深圳市南山区科技中一道10000号");
|
||||||
|
|
||||||
|
SettleInfo settleInfo = new SettleInfo();
|
||||||
|
settleInfo.setProfitSharing(false);
|
||||||
|
|
||||||
|
request.setSceneInfo(sceneInfo);
|
||||||
|
sceneInfo.setStoreInfo(setstoreInfo);
|
||||||
|
request.setSettleInfo(settleInfo);
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
DirectAPIv3JsapiPrepayResponse response = client.rePay(request);
|
||||||
|
System.out.println(JSON.toJSONString(response));
|
||||||
|
// TODO: 请求成功,继续业务逻辑
|
||||||
|
System.out.println(response);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO: 请求失败,根据状态码执行不同的逻辑
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user