多应用多平台支付模块-微信V2JsApi支付

此前,已设计过多应用多平模块的基础开篇 。
本篇,准备对接微信V2JsApi支付基础模块,并基于此模块实现具体的业务功能逻辑 。
开发思路如下

多应用多平台支付模块-微信V2JsApi支付

文章插图
 
微信下单网络请求配置定义微信统一下单网络请求配置,提供实现类进行调用获取下单数据 。
package com.threeox.biz.order.config;import com.threeox.biz.order.entity.wx.WxOrderInfo;import com.threeox.drivenlibrary.engine.annotation.base.ParamConfig;import com.threeox.drivenlibrary.engine.annotation.request.NETWorkRequestConfig;import com.threeox.drivenlibrary.engine.annotation.request.RequestConfig;import com.threeox.drivenlibrary.engine.annotation.scan.RequestScanConfig;import com.threeox.drivenlibrary.enums.dictionary.DataType;import com.threeox.drivenlibrary.enums.dictionary.RequestMethod;/** * 支付请求配置 * * @author 赵屈犇 * @version 1.0 * @date 创建时间: 2022/4/23 16:54 */@RequestScanConfigpublic class PayRequestConfig {/*** 微信统一下单*/@RequestConfig(requestName = "统一下单", netWorkConfig = @NetWorkRequestConfig(rootUrl = "https://api.mch.weixin.qq.com/", serverUrl = "pay/unifiedorder",parsingRespDataType = DataType.XML, responseClass = WxOrderInfo.class, methodType = RequestMethod.POST_XML, body = {@ParamConfig(paramCode = "Appid", valueCode = "appid"),@ParamConfig(paramCode = "mch_id", valueCode = "mch_id"),@ParamConfig(paramCode = "nonce_str", valueCode = "nonce_str"),@ParamConfig(paramCode = "sign", valueCode = "sign"),@ParamConfig(paramCode = "sign_type", valueCode = "sign_type"),@ParamConfig(paramCode = "body", valueCode = "body"),@ParamConfig(paramCode = "detail", valueCode = "detail"),@ParamConfig(paramCode = "out_trade_no", valueCode = "out_trade_no"),@ParamConfig(paramCode = "fee_type", valueCode = "fee_type"),@ParamConfig(paramCode = "openid", valueCode = "openid"),@ParamConfig(paramCode = "total_fee", valueCode = "total_fee"),@ParamConfig(paramCode = "notify_url", valueCode = "notify_url"),@ParamConfig(paramCode = "trade_type", valueCode = "trade_type"),@ParamConfig(paramCode = "product_id", valueCode = "product_id"),@ParamConfig(paramCode = "spbill_create_ip", valueCode = "spbill_create_ip"),}))public static final String WX_API_PLACE_ORDER = "COM.THREEOX.BIZ.ORDER.CONFIG.PAYREQUESTCONFIG.WX_API_PLACE_ORDER";}支付方式工厂实现此工厂继承基础实现类,并实现下单子类函数 。
  • 调用微信统一下单接口
  • 处理下单返回数据,并返回至前端调用
package com.threeox.biz.order.factory.impl.wx;import com.alibaba.fastjson.JSONObject;import com.threeox.biz.order.config.OrderDictConstants;import com.threeox.biz.order.config.PayRequestConfig;import com.threeox.biz.order.config.extend.PaymentExtend;import com.threeox.biz.order.entity.OrderInfo;import com.threeox.biz.order.entity.wx.WxOrderInfo;import com.threeox.biz.order.enums.PayWay;import com.threeox.biz.order.factory.impl.base.BasePaymentFactory;import com.threeox.biz.order.utils.PayUtils;import com.threeox.biz.order.utils.wx.WXPayConstants;import com.threeox.biz.order.utils.wx.WXPayUtil;import com.threeox.drivenlibrary.engine.constants.config.DrivenModelDBConstants;import com.threeox.drivenlibrary.engine.entity.driven.config.OpenPlatformConfigMessage;import com.threeox.drivenlibrary.engine.request.execute.ExecuteRequestFactory;import com.threeox.drivenlibrary.enums.ResponseResult;import com.threeox.drivenlibrary.exception.ResponseException;import com.threeox.drivenlibrary.manage.entity.UserInfoMessage;import com.threeox.drivenlibrary.manage.factory.user.UserFactory;import com.threeox.httplibrary.entity.HttpResponseInfo;import com.threeox.utillibrary.date.TimeUtils;import com.threeox.utillibrary.JAVA.IDGenerate;import java.math.BigDecimal;/** * 微信JSApi支付 * * @author 赵屈犇 * @version 1.0 * @date 创建时间: 2022/4/23 17:38 */@PaymentExtend(OrderDictConstants.PayWay.WX_JS_API_V2)public class WxJsApiV2PaymentFactory extends BasePaymentFactory {@Overrideprotected JSONObject placeOrder(OrderInfo orderInfo, OpenPlatformConfigMessage openPlatform) throws Exception {UserInfoMessage userInfo = UserFactory.builder().getUser();JSONObject params = new JSONObject();params.put("fee_type", "CNY");params.put("openid", userInfo.getWxOpenId());params.put("body", orderInfo.getOrderName());params.put("nonce_str", IDGenerate.getUUId());params.put("appid", openPlatform.getWxAppId());params.put("mch_id", openPlatform.getWxMchId());params.put("detail", orderInfo.getBizSnapshot());params.put("product_id", orderInfo.getOrderId());params.put("out_trade_no", orderInfo.getOrderNum());params.put("trade_type", PayWay.WX_JS_API_V2.getCode());params.put("spbill_create_ip", orderInfo.getIpAddress());params.put("sign_type", WXPayConstants.SignType.MD5.name());params.put("total_fee", orderInfo.getPayMoney().multiply(new BigDecimal(100)).toBigInteger());params.put("notify_url", PayUtils.getNotifyUrl(PayWay.WX_JS_API_V2));params.put("sign", WXPayUtil.generateSignature(params, openPlatform.getWxMchSecret(), WXPayConstants.SignType.MD5));// 执行请求HttpResponseInfo responseInfo = ExecuteRequestFactory.builder().initConfig(DrivenModelDBConstants.DRIVEN_MANAGE_MODEL_DB_CODE, PayRequestConfig.WX_API_PLACE_ORDER).requestParam(params).execute();if (responseInfo != null && responseInfo.isSuccess()) {WxOrderInfo wxOrderInfo = responseInfo.getResult();if ("SUCCESS".equals(wxOrderInfo.getReturn_code()) && wxOrderInfo.getReturn_code().equals(wxOrderInfo.getResult_code())) {JSONObject payResult = new JSONObject();payResult.put("appId", openPlatform.getWxAppId());payResult.put("nonceStr", IDGenerate.getUUId());payResult.put("package", "prepay_id=" + wxOrderInfo.getPrepay_id());payResult.put("timeStamp", String.valueOf(TimeUtils.getNowTimeMills()));payResult.put("signType", WXPayConstants.SignType.MD5.name());payResult.put("paySign", WXPayUtil.generateSignature(payResult, openPlatform.getWxMchSecret(), WXPayConstants.SignType.MD5));return payResult;}}throw new ResponseException(ResponseResult.PLACE_ORDER_FAILURE);}}


推荐阅读