【微信支付】微信v3支付案例,SpringBoot集成IJPay实现微信v3支付

前言

这篇文章主要实现一下通过IJPay来实现微信v3支付案例,本篇文章使用的是JSAPI即小程序支付

IJPay码云仓库:https://gitee.com/javen205/IJPay/tree/dev
IJPay官方文档:https://javen205.gitee.io/ijpay/

准备工作

导入依赖

	   <dependency>
            <groupId>com.github.javen205</groupId>
            <artifactId>IJPay-WxPay</artifactId>
            <version>2.9.6</version>
        </dependency>
  • appId 由微信生成的应用ID,全局唯一。
  • mchId 直连商户的商户号,由微信支付生成并下发。
  • apiV3Key v3密钥
  • mchSerialNo 商户证书序列号

还需要三个证书文件
在这里插入图片描述

  1. 通过微信官方指引下载证书并解压缩后得到的文件。apiclient_cert.pem称为商户证书apiclient_key.pem成为商户证书密钥
  2. 通过IJPay的接口获取,称之为微信平台证书。(一般没有这个文件,所以暂时先不管,一会获取的时候会讲这个)

在这里我们给这三个文件起个别名,以便下方代码更容易区分。

  • apiclient_cert.pem:privateCertPath
  • apiclient_key.pem:privateKeyPath
  • wx_platform_cert.pem:platformCertPath

开始

1 首先将上面所有的参数配置到application.yml文件中,或者配置到nacos上。
在这里插入图片描述

  • wxJsapiUrl:是获取微信预支付参数的url路径
  • 最下面的三个是文件的绝对路径

2 读取配置文件的配置项,新建WxPayConfig

@Data
@Component
@ConfigurationProperties(prefix = "wx.pay")
public class WxPayConfig {
	/**
	 * appId
	 */
	private String appId;

	/**
	 * 商户号
	 */
	private String mchId;

	/**
	 * 商户证书序列号
	 */
	private String mchSerialNo;

	/**
	 * apiv3密钥
	 */
	private String apiV3Key;

	/**
	 * 支付回调地址
	 */
	private String notifyUrl;

	/**
	 * 微信支付请求url
	 */
	private String wxJsapiUrl;

	/**
	 * 私钥路径
	 */
	private String privateKeyPath;

	/**
	 * 商户证书路径
	 */
	private String privateCertPath;

	/**
	 * 微信平台证书路径
	 */
	private String platformCertPath;
}

3 首先获取到微信平台证书

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.mjfz.fc.weixin.WxPayConfig;
import com.ijpay.core.IJPayHttpResponse;
import com.ijpay.core.enums.RequestMethodEnum;
import com.ijpay.core.kit.AesUtil;
import com.ijpay.core.kit.PayKit;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.enums.WxDomainEnum;
import com.ijpay.wxpay.enums.v3.OtherApiEnum;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;


	@Resource
	private WxPayConfig wxPayConfig;

	public void v3Get() {
		// 获取平台证书列表
		try {
			IJPayHttpResponse response = WxPayApi.v3(
					RequestMethodEnum.GET,
					WxDomainEnum.CHINA.toString(),
					OtherApiEnum.GET_CERTIFICATES.toString(),
					wxPayConfig.getMchId(),
					wxPayConfig.getMchSerialNo(),
					null,
					wxPayConfig.getPrivateKeyPath(),
					""
			);

			String timestamp = response.getHeader("Wechatpay-Timestamp");
			String nonceStr = response.getHeader("Wechatpay-Nonce");
			String serialNumber = response.getHeader("Wechatpay-Serial");
			String signature = response.getHeader("Wechatpay-Signature");

			String body = response.getBody();
			int status = response.getStatus();

			log.info("serialNumber: {}", serialNumber);
			log.info("status: {}", status);
			log.info("body: {}", body);
			if (status == 200) {
				JSONObject jsonObject = JSONUtil.parseObj(body);
				JSONArray dataArray = jsonObject.getJSONArray("data");
				// 默认认为只有一个平台证书
				JSONObject encryptObject = dataArray.getJSONObject(0);
				JSONObject encryptCertificate = encryptObject.getJSONObject("encrypt_certificate");
				String associatedData = encryptCertificate.getStr("associated_data");
				String cipherText = encryptCertificate.getStr("ciphertext");
				String nonce = encryptCertificate.getStr("nonce");
				String serialNo = encryptObject.getStr("serial_no");
				final String platSerialNo = savePlatformCert(associatedData, nonce, cipherText, wxPayConfig.getPlatformCertPath());
				log.info("平台证书序列号: {} serialNo: {}", platSerialNo, serialNo);
			}
			// 根据证书序列号查询对应的证书来验证签名结果
			boolean verifySignature = WxPayKit.verifySignature(response, wxPayConfig.getPlatformCertPath());
			System.out.println("verifySignature:" + verifySignature);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String savePlatformCert(String associatedData, String nonce, String cipherText, String certPath) {
		try {
			AesUtil aesUtil = new AesUtil(apiV3key.getBytes(StandardCharsets.UTF_8));
			// 平台证书密文解密
			// encrypt_certificate 中的  associated_data nonce  ciphertext
			String publicKey = aesUtil.decryptToString(
					associatedData.getBytes(StandardCharsets.UTF_8),
					nonce.getBytes(StandardCharsets.UTF_8),
					cipherText
			);
			// 保存证书
			FileWriter writer = new FileWriter(certPath);
			writer.write(publicKey);
			writer.close();
			// 获取平台证书序列号
			X509Certificate certificate = PayKit.getCertificate(new ByteArrayInputStream(publicKey.getBytes()));
			return certificate.getSerialNumber().toString(16).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

4 支付和回调

//支付
	@RequestMapping("/jsApiPay")
	@ResponseBody
	public String jsApiPay(String openId) {
		try {
			String timeExpire = DateTimeZoneUtil.dateToTimeZone(System.currentTimeMillis() + 1000 * 60 * 3);
			UnifiedOrderModel unifiedOrderModel = new UnifiedOrderModel()
					.setAppid(wxPayConfig.getAppId())
					.setMchid(wxPayConfig.getMchId())
					.setDescription("IJPay 让支付触手可及")
					.setOut_trade_no(PayKit.generateStr())
					.setTime_expire(timeExpire)
					.setAttach("微信系开发脚手架 https://gitee.com/javen205/TNWX")
					.setNotify_url(wxPayConfig.getNotifyUrl())
					.setAmount(new Amount().setTotal(1))
					.setPayer(new Payer().setOpenid(openId));

			log.info("统一下单参数 {}", JSONUtil.toJsonStr(unifiedOrderModel));
			IJPayHttpResponse response = WxPayApi.v3(
					RequestMethodEnum.POST,
					WxDomainEnum.CHINA.toString(),
					BasePayApiEnum.JS_API_PAY.toString(),
					wxPayConfig.getMchId(),
					wxPayConfig.getMchSerialNo(),
					null,
					wxPayConfig.getPrivateKeyPath(),
					JSONUtil.toJsonStr(unifiedOrderModel)
			);

			log.info("统一下单响应 {}", response);
			// 根据证书序列号查询对应的证书来验证签名结果
			boolean verifySignature = WxPayKit.verifySignature(response, wxPayConfig.getPlatformCertPath());
			log.info("verifySignature: {}", verifySignature);
			if (response.getStatus() == 200 && verifySignature) {
				String body = response.getBody();
				JSONObject jsonObject = JSONUtil.parseObj(body);
				String prepayId = jsonObject.getStr("prepay_id");
				Map<String, String> map = WxPayKit.jsApiCreateSign(wxPayConfig.getAppId(), prepayId, wxPayConfig.getPrivateKeyPath());
				log.info("唤起支付参数:{}", map);
				return JSONUtil.toJsonStr(map);
			}
			return JSONUtil.toJsonStr(response);
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/payNotify", method = {org.springframework.web.bind.annotation.RequestMethod.POST, org.springframework.web.bind.annotation.RequestMethod.GET})
	@ResponseBody
	public void payNotify(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> map = new HashMap<>(12);
		try {
			String timestamp = request.getHeader("Wechatpay-Timestamp");
			String nonce = request.getHeader("Wechatpay-Nonce");
			String serialNo = request.getHeader("Wechatpay-Serial");
			String signature = request.getHeader("Wechatpay-Signature");

			log.info("timestamp:{} nonce:{} serialNo:{} signature:{}", timestamp, nonce, serialNo, signature);
			String result = HttpKit.readData(request);
			log.info("支付通知密文 {}", result);

			// 需要通过证书序列号查找对应的证书,verifyNotify 中有验证证书的序列号
			String plainText = WxPayKit.verifyNotify(serialNo, result, signature, nonce, timestamp,
					wxPayConfig.getApiV3Key(), wxPayConfig.getPlatformCertPath());

			log.info("支付通知明文 {}", plainText);

			if (StrUtil.isNotEmpty(plainText)) {
				response.setStatus(200);
				map.put("code", "SUCCESS");
				map.put("message", "SUCCESS");
			} else {
				response.setStatus(500);
				map.put("code", "ERROR");
				map.put("message", "签名错误");
			}
			response.setHeader("Content-type", ContentType.JSON.toString());
			response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
			response.flushBuffer();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

总结

简单实现微信v3支付,如有问题可以指在评论区,制作不易,点个赞吧!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值