服务商 微信小程序支付 这个是服务商版本的
上代码
/***服务商商户号*/
private static String merchantId = new String();
/***子商户号*/
private static String subMerchantId = new String();
/***证书序列号 **/
private static String merchantSerialNumber = new String();
/*** 服务商商户私钥*/
private static String privateKey = new String();
private static String apiV3Key = new String();
private static String subAppid = new String();
private static String spAppid = new String();
@Autowired
private RedisUtil redisUtil;
/*** 该注解在构造方法之后会被执行*/
@PostConstruct
private void beforeInit() {
merchantId = oConvertUtils.isEmpty(redisUtil.get(CxkjConstant.WX_AP_MERCHANT_ID)) ? "" : redisUtil.get(CxkjConstant.WX_AP_MERCHANT_ID).toString();
subMerchantId = oConvertUtils.isEmpty(redisUtil.get(CxkjConstant.WX_AP_SUB_MERCHANT_ID)) ? "" : redisUtil.get(CxkjConstant.WX_AP_SUB_MERCHANT_ID).toString();
merchantSerialNumber = oConvertUtils.isEmpty(redisUtil.get(CxkjConstant.WX_AP_MERCHANT_SERIAL_NUMBER)) ? "" : redisUtil.get(CxkjConstant.WX_AP_MERCHANT_SERIAL_NUMBER).toString();
privateKey = oConvertUtils.isEmpty(redisUtil.get(CxkjConstant.WX_AP_PRIVATE_KEY)) ? "" : redisUtil.get(CxkjConstant.WX_AP_PRIVATE_KEY).toString();
apiV3Key = oConvertUtils.isEmpty(redisUtil.get(CxkjConstant.WX_AP_APIV3_KEY)) ? "" : redisUtil.get(CxkjConstant.WX_AP_APIV3_KEY).toString();
subAppid = oConvertUtils.isEmpty(redisUtil.get(CxkjConstant.WX_AP_SUB_APPID)) ? "" : redisUtil.get(CxkjConstant.WX_AP_SUB_APPID).toString();
spAppid = oConvertUtils.isEmpty(redisUtil.get(CxkjConstant.WX_AP_SP_APPID)) ? "" : redisUtil.get(CxkjConstant.WX_AP_SP_APPID).toString();
}
/**
* 使用AK&SK初始化账号Client
*
* @return Client
* @throws Exception
*/
public static CloseableHttpClient createClient() throws Exception {
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
// 获取证书管理器实例
CertificatesManager certificatesManager = CertificatesManager.getInstance();
// 向证书管理器增加需要自动更新平台证书的商户信息
certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId,
new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)), apiV3Key.getBytes(StandardCharsets.UTF_8));
// ... 若有多个商户号,可继续调用putMerchant添加商户信息
// 从证书管理器中获取verifier
Verifier verifier = certificatesManager.getVerifier(merchantId);
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier));
// 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签
// CloseableHttpClient httpClient = builder.build();
return builder.build();
}
public static String sign(String timeStamp, String nonceStr, String package1) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
String message = subAppid + "\n"
+ timeStamp + "\n"
+ nonceStr + "\n"
+ package1 + "\n";
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(merchantPrivateKey);
sign.update(message.getBytes("utf-8"));
return Base64.getEncoder().encodeToString(sign.sign());
}
小程序 下单代码
public static JSONObject createJsApiOrder(String payNum, BigDecimal payPrice, String notifyUrl, String description, String openId) throws Exception {
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type", "application/json; charset=utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("sp_appid", spAppid)
.put("sp_mchid", merchantId)
.put("sub_mchid", subMerchantId)
.put("sub_appid", subAppid)
.put("description", description)
.put("notify_url", notifyUrl)
.put("out_trade_no", payNum);
rootNode.putObject("amount")
.put("total", payPrice.multiply(new BigDecimal(100)).intValue()); // 1 payPrice.multiply(new BigDecimal(100)).intValue()
rootNode.putObject("payer")
.put("sub_openid", openId);
System.err.println("==支付参数==" + rootNode.toString());
objectMapper.writeValue(bos, rootNode);
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
CloseableHttpResponse response = createClient().execute(httpPost);
JSONObject bodyAsString = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
System.err.println(bodyAsString);
JSONObject jo = new JSONObject();
Long timeStamp = System.currentTimeMillis() / 1000;
String nonceStr = WXPayUtil.generateNonceStr();
jo.put("appId", spAppid);
jo.put("timeStamp", timeStamp + "");
jo.put("nonceStr", nonceStr);
jo.put("package", "prepay_id=" + bodyAsString.get("prepay_id"));
jo.put("signType", "RSA");
jo.put("paySign", sign(timeStamp + "", nonceStr, "prepay_id=" + bodyAsString.get("prepay_id")));
return jo;
}
业务代码
try {
// 获取订单信息
JSONObject jo = WxPayUtil.createJsApiOrder(cxIntegralOrder.getOrderNum(), cxIntegralOrder.getPayPrice(), redisUtil.get(CxkjConstant.PROJECT_DOMAIN_NAME) + "/api/mod/WxPay/async/integralPayAsync", "商品购买", openId);
return Result.OK("支付下单成功", jo);
} catch (CxkjException e) {
return Result.error(100, e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return Result.error(100, e.getMessage());
}
正文完