当前位置:   article > 正文

Java实现微信支付_微信支付java

微信支付java

1.引入依赖

<dependency>
  <groupId>com.github.wechatpay-apiv3</groupId>
  <artifactId>wechatpay-java</artifactId>
  <version>0.2.10</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2.创建预支付订单

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class WeiXinPay {
    //@Value("${weixin.appId}")
    private String weixinAppId;

    /** 商户号 */
    public static String merchantId = "";
    /** 商户API私钥路径 */
    public static String privateKeyPath = "";
    /** 商户证书序列号 例如-证书序列号:54776TTTF8F77EXXX3641FAB5F940FII11C65347*/
    public static String merchantSerialNumber = "";
    /** 微信支付平台证书路径 例如:wechatpay_3A4AF69999DF01F39BB08C21C1C29B6AA17C074N.pem*/
    public static String wechatPayCertificatePath = "";
    /** 微信支付 APIv3 密钥 */
    public static String apiV3Key = "";

    public String createPreparePay() {
        Config config =
                new RSAConfig.Builder()
                        //商户ID
                        .merchantId(merchantId)
                        //商户私钥文件(apiclient_key.pem)路径
                        .privateKeyFromPath(privateKeyPath)
                        //商户证书序列号 例如-证书序列号:54776TTTF8F77EXXX3641FAB5F940FII11C65347
                        .merchantSerialNumber(merchantSerialNumber)
                        //微信支付平台证书文件路径 例如:wechatpay_3A4AF69999DF01F39BB08C21C1C29B6AA17C074N.pem
                        .wechatPayCertificatesFromPath(wechatPayCertificatePath)
                        .build();

        JsapiService service = new JsapiService.Builder().config(config).build();

        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(100);
        request.setAmount(amount);
        request.setAppid("wxa9d9651ae******");
        request.setMchid("190000****");
        request.setDescription("测试商品标题");
        request.setNotifyUrl("https://notify_url");
        request.setOutTradeNo("out_trade_no_001");
        Payer payer = new Payer();
        payer.setOpenid("oLTPCuN5a-nBD4rAL_fa********");
        request.setPayer(payer);

        PrepayResponse response = service.prepay(request);
        System.out.println(response.getPrepayId());
        return "";
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/467348
推荐阅读
相关标签
  

闽ICP备14008679号