springboot 使用 weixin-java-pay 支付demo

springboot引入依赖

bash 复制代码
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-pay</artifactId>
            <version>4.6.0</version>
        </dependency>

配置

bash 复制代码
wx:
  pay:
    appId: *********
    mchId: ********
    apiV3Key: ********
    certSerialNo: ********
    privateKeyPath: classpath:apiclient_key.pem #apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径
    privateCertPath: classpath:apiclient_cert.pem #apiclient_cert.pem证书文件的绝对路径或者以classpath:开头的类路径

引入证书

微信支付自动配置

bash 复制代码
package com.ruoyi.system.pay;


import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <pre>
 *  微信支付自动配置
 *  Created by BinaryWang on 2019/4/17.
 * </pre>
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Configuration
@EnableConfigurationProperties(WxPayProperties.class)
@ConditionalOnClass(WxPayService.class)
@ConditionalOnProperty(prefix = "wx.pay", value = "enabled", matchIfMissing = true)
public class WxPayAutoConfiguration {
    private WxPayProperties properties;

    @Autowired
    public WxPayAutoConfiguration(WxPayProperties properties) {
        this.properties = properties;
    }

    /**
     * 构造微信支付服务对象.
     *
     * @return 微信支付service
     */
    @Bean
    @ConditionalOnMissingBean(WxPayService.class)
    public WxPayService wxPayService() {
        final WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
        payConfig.setUseSandboxEnv(this.properties.isUseSandboxEnv());
        //以下是apiv3以及支付分相关
        payConfig.setServiceId(StringUtils.trimToNull(this.properties.getServiceId()));
        payConfig.setPayScoreNotifyUrl(StringUtils.trimToNull(this.properties.getPayScoreNotifyUrl()));
        payConfig.setPrivateKeyPath(StringUtils.trimToNull(this.properties.getPrivateKeyPath()));
        payConfig.setPrivateCertPath(StringUtils.trimToNull(this.properties.getPrivateCertPath()));
        payConfig.setCertSerialNo(StringUtils.trimToNull(this.properties.getCertSerialNo()));
        payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getApiv3Key()));

        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
bash 复制代码
package com.ruoyi.system.pay;


import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * <pre>
 *  微信支付属性配置类
 * Created by Binary Wang on 2019/4/17.
 * </pre>
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@ConfigurationProperties(prefix = "wx.pay")
public class WxPayProperties {
    /**
     * 设置微信公众号或者小程序等的appid.
     */
    private String appId;

    /**
     * 微信支付商户号.
     */
    private String mchId;

    /**
     * 微信支付商户密钥.
     */
    private String mchKey;

    /**
     * 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除.
     */
    private String subAppId;

    /**
     * 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除.
     */
    private String subMchId;

    /**
     * apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定.
     */
    private String keyPath;

    /**
     * 微信支付分serviceId
     */
    private String serviceId;

    /**
     * 证书序列号
     */
    private String certSerialNo;

    /**
     * apiV3秘钥
     */
    private String apiv3Key;

    /**
     * 微信支付分回调地址
     */
    private String payScoreNotifyUrl;

    /**
     * apiv3 商户apiclient_key.pem
     */
    private String privateKeyPath;

    /**
     * apiv3 商户apiclient_cert.pem
     */
    private String privateCertPath;

    /**
     * 微信支付是否使用仿真测试环境.
     * 默认不使用
     */
    private boolean useSandboxEnv;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getMchKey() {
        return mchKey;
    }

    public void setMchKey(String mchKey) {
        this.mchKey = mchKey;
    }

    public String getSubAppId() {
        return subAppId;
    }

    public void setSubAppId(String subAppId) {
        this.subAppId = subAppId;
    }

    public String getSubMchId() {
        return subMchId;
    }

    public void setSubMchId(String subMchId) {
        this.subMchId = subMchId;
    }

    public String getKeyPath() {
        return keyPath;
    }

    public void setKeyPath(String keyPath) {
        this.keyPath = keyPath;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public String getCertSerialNo() {
        return certSerialNo;
    }

    public void setCertSerialNo(String certSerialNo) {
        this.certSerialNo = certSerialNo;
    }

    public String getApiv3Key() {
        return apiv3Key;
    }

    public void setApiv3Key(String apiv3Key) {
        this.apiv3Key = apiv3Key;
    }

    public String getPayScoreNotifyUrl() {
        return payScoreNotifyUrl;
    }

    public void setPayScoreNotifyUrl(String payScoreNotifyUrl) {
        this.payScoreNotifyUrl = payScoreNotifyUrl;
    }

    public String getPrivateKeyPath() {
        return privateKeyPath;
    }

    public void setPrivateKeyPath(String privateKeyPath) {
        this.privateKeyPath = privateKeyPath;
    }

    public String getPrivateCertPath() {
        return privateCertPath;
    }

    public void setPrivateCertPath(String privateCertPath) {
        this.privateCertPath = privateCertPath;
    }

    public boolean isUseSandboxEnv() {
        return useSandboxEnv;
    }

    public void setUseSandboxEnv(boolean useSandboxEnv) {
        this.useSandboxEnv = useSandboxEnv;
    }
}

支付demo

bash 复制代码
package com.ruoyi.system.pay;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.util.ResourcesUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import me.chanjar.weixin.common.util.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

/**
 * 参数配置 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/pay")
public class SysPayController extends BaseController
{


    @Autowired
    private WxPayService wxPayService;
    /**
     * 微信小程序支付
     */
    @GetMapping("/demo")
    public AjaxResult demo() throws WxPayException {
        String outTradeNo = RandomUtils.getRandomStr();
        String notifyUrl = "https://api.qq.com/";
        System.out.println("outTradeNo = " + outTradeNo);
        WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();
        request.setOutTradeNo(outTradeNo);
        request.setNotifyUrl(notifyUrl);
        request.setDescription("test");

        WxPayUnifiedOrderV3Request.Payer payer = new WxPayUnifiedOrderV3Request.Payer();
        payer.setOpenid("oQfWg**********p60Kcw5s");
        request.setPayer(payer);
        //构建金额信息
        WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
        //设置币种信息
        amount.setCurrency(WxPayConstants.CurrencyType.CNY);
        //设置金额
        amount.setTotal(BaseWxPayRequest.yuan2Fen(BigDecimal.ONE));
        request.setAmount(amount);
        WxPayUnifiedOrderV3Result.JsapiResult result = this.wxPayService.createOrderV3(TradeTypeEnum.JSAPI, request);
        System.out.println(JSON.toJSONString(result));
        return success();
    }


    /**
     * 关闭订单
     * @return
     * @throws WxPayException
     */
    @GetMapping("/close")
    public AjaxResult close() throws WxPayException {
        this.wxPayService.closeOrderV3("tradeNo000001");
        return success();
    }
}
相关推荐
砍材农夫1 小时前
物联网实战|Spring Boot MQTT平台 |emqx broker实战
spring boot·spring·spring cloud·mybatis
似璟如你2 小时前
Java 开发者的 Go 语法基础:从 0 开始快速上手 Go
java·开发语言·后端·golang·go·编程语言
zzz_23682 小时前
TencentDB-Agent-Memory 深度解析:让多个 Agent 共享项目经验的记忆中枢
java·开发语言·jvm·人工智能·agent·memory·tencent db
vx-程序开发2 小时前
django医院预约挂号系统---附源码23353
java·javascript·spring boot·python·eclipse·django·php
金斗潼关2 小时前
ysoserial的使用
java
ruleslol2 小时前
volatile 关键字
java
Codelinghu3 小时前
AI 写代码越强,程序员越不能只懂代码
后端
卡卡敲码3 小时前
Skills 撞车了,Agent 怎么选
后端
羑悻3 小时前
周一早上三件事砸过来,我以为要延期,结果 AI 替我扛了一半!
后端
文艺理科生3 小时前
3 年,8 种方案,1 次重构:LangChain 记忆方案如何从混乱走向清晰
前端·后端·架构