| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package common.utils.wechat;
- import com.wechat.pay.java.core.notification.NotificationConfig;
- import com.wechat.pay.java.core.notification.NotificationParser;
- import com.wechat.pay.java.core.notification.RequestParam;
- import com.wechat.pay.java.service.payments.h5.model.*;
- import com.wechat.pay.java.service.payments.h5.H5Service;
- import com.wechat.pay.java.service.payments.model.Transaction;
- public class WeChatService {
- static H5Service h5Service = WeChatConfig.getService();
- /**
- * H5支付下单
- *
- * @param amount 订单数量
- * @param outTradeNo 内部订单号
- * @param notifyUrl 支付通知url
- * @return 结果,主要是H5Url
- */
- public static PrepayResponse prepay(Integer amount, String outTradeNo, String notifyUrl, SceneInfo sceneInfo) {
- PrepayRequest request = new PrepayRequest();
-
- // 交易数量
- Amount amountM = new Amount();
- amountM.setTotal(amount);
-
- // 参数配置
- request.setAmount(amountM);
- request.setAppid(WeChatConfig.appId);
- request.setMchid(WeChatConfig.merchantId);
- request.setDescription("购买ID:" + outTradeNo);
- request.setNotifyUrl(notifyUrl);
- request.setOutTradeNo(outTradeNo);
- request.setSceneInfo(sceneInfo);
-
- // 调用接口
- return h5Service.prepay(request);
- }
- // 将请求体解析为tx
- public static Transaction parseTransaction(String requestBody,
- String wechatPaySerial,
- String wechatpayNonce,
- String wechatSignature,
- String wechatTimestamp) {
- // 构造 RequestParam
- RequestParam requestParam = new RequestParam.Builder()
- .serialNumber(wechatPaySerial)
- .nonce(wechatpayNonce)
- .signature(wechatSignature)
- .timestamp(wechatTimestamp)
- .body(requestBody)
- .build();
- // 如果已经初始化了 NotificationConfig,可直接使用
- NotificationConfig config = WeChatConfig.getNConfig();
- // 初始化 NotificationParser
- NotificationParser parser = new NotificationParser(config);
-
- // 直接返回tx
- return parser.parse(requestParam, Transaction.class);
- }
- // 测试支付
- public static void main(String[] args) {
- // String notifyUrl = System.getenv("URL_BASE") + "/deposit/deposited/";
- String notifyUrl = "https://api.dlsh-nft.com.cn" + "/deposit/deposited/";
- String outTradeNo = "DLTBH_WX_" + System.currentTimeMillis();
- Integer amount = 100;
- // 场景信息
- H5Info h5Info = new H5Info();
- h5Info.setType("Wrp"); // 使用H5支付的场景:Wap、iOS、Android
-
- SceneInfo sceneInfo = new SceneInfo();
- sceneInfo.setPayerClientIp("163.53.18.23");
- sceneInfo.setH5Info(h5Info);
-
- prepay(amount, outTradeNo, notifyUrl, sceneInfo);
- }
- }
|