PS:本文非提供给新手学习.见谅.
使用的是sdk方式发起支付,相关sdk更新可以参考官方github:
https://github.com/wechatpay-apiv3/wechatpay-java
maven配置微信支付V3的sdk:
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.15</version>
</dependency>
sdk目前使用的是如上版本,有更新可以以github官方为准.
Springboot部分核心代码,粘贴到工程请自行修改自己的支付配置,支付配置这里不涉及,请自行查找,以下是实现了支付,退款和支付通知,其他请参详github的sdk.
核心代码,注意调用sdk的import,是调用jsapi相关的依赖.
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RSAPublicKeyNotificationConfig;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
// 公众号应用ID
private static String appId = "xxxxxxxxxxxxx";
/** 支付商户ID **/
public static String merchantId = "xxxxxxxxxx";
/** 商户API私钥路径 */
public static String privateKeyPath = "C:\\your_path\cert\\apiclient_key.pem";
/** 商户公钥API路径 **/
public static String publicKeyPath = "";
/** 商户公钥 ID **/
public static String publicKeyId = "";
/** 商户证书序列号 */
public static String merchantSerialNumber = "xxxxxxxxxxxxxxxx";
/** 商户APIV3密钥 */
public static String apiV3Key = "xxxxxxxxxxxxxx";
/**
* 拉起支付 - JSAPI支付下单验签
* @return
*/
@Override
public ResBody raisePay(PayQst payQst) {
Config config = new RSAAutoCertificateConfig.Builder().merchantId(merchantId)
.privateKeyFromPath(privateKeyPath).merchantSerialNumber(merchantSerialNumber)
.apiV3Key(apiV3Key).build();
serviceOfJsApiPay = new JsapiServiceExtension.Builder()
.config(config).signType("RSA").build();
PrepayRequest request = new PrepayRequest();
request.setMchid(merchantId);
request