当前位置:   article > 正文

微信支付-开源项目WxJava简单使用(公众号示例)_weixin-java-pay

weixin-java-pay

首先,没有做过微信支付,又想快速开发,来快速熟悉微信支付流程。可以考虑用开源项目。

本文主要介绍微信公众号,服务商模式下微信支付。

WxJava是一个对微信支付封装的开源项目。
项目地址:https://gitee.com/binary/weixin-java-tools?_from=gitee_search
官方demo:https://gitee.com/binary/weixin-java-tools/blob/develop/demo.md

直接开始正片

一、maven引入jar

<dependency>
  <groupId>com.github.binarywang</groupId>
  <artifactId>(不同模块参考下文)</artifactId>
  <version>4.1.0</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 微信小程序:weixin-java-miniapp
  • 微信支付:weixin-java-pay
  • 微信开放平台:weixin-java-open
  • 公众号(包括订阅号和服务号):weixin-java-mp
  • 企业号/企业微信:weixin-java-cp

比如,本人要做的是公众号微信支付,可以引入weixin-java-pay,(貌似都包括了)

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-pay</artifactId>
    <version>4.1.0</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

二、配置文件

在application.yml中,加入如下配置,(下面的值我都轻微的改过了,不可以拿过去就用哦!)
其中p12证书,这里没写,但是注释写的很清楚,不影响下单。本人目前接触到它的作用,退款时候需要用到,其他还没用到过。能力有限罪过,罪过。

wx:
  pay:
    appId: wx9c4130f5db12345 #微信公众号或者小程序等的appid
    mchId: 1611921123 #微信支付商户号
    mchKey: JIWUwB4juAUMb9XI3SjmEJESJzo12321 #微信支付商户密钥
    subAppId: wx9c413021323d #服务商模式下的子商户公众账号ID
    subMchId: 162312372 #服务商模式下的子商户号
    keyPath: # p12证书的位置,可以指定绝对路径,也可以指定类路径(以classpath:开头)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

三、装载配置

将上述,yml中配置的参数,装载进去。以下直接官方例子,开抄如下:

WxPayProperties.java

package com.hky.config.wxpay;

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

/**
 * wxpay pay properties.
 *
 * @author Binary Wang
 */
@Data
@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;

}

  • 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

WxPayConfiguration

package com.hky.config.wxpay;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
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.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Binary Wang
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfiguration {
  private WxPayProperties properties;

  @Bean
  @ConditionalOnMissingBean
  public WxPayService wxService() {
    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(false);

    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(payConfig);
    return wxPayService;
  }

}

  • 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

四、编写controller

继续抄官方demo:(由于原demo中内容较多,只保留一个统一下单,其他的自行去官方demo查看)

package com.hky.controller;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.wxpay.bean.coupon.*;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.hky.common.shiro.manager.TokenManager;
import com.hky.common.utils.web.SpringContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;


/**
 * @author Binary Wang
 */
@Api("微信支付")
@RestController
@RequestMapping("/pay")
@AllArgsConstructor
public class WxPayController {
    private WxPayService wxService;

    /**
     * 调用统一下单接口,并组装生成支付所需参数对象.
     *
     * @param request 统一下单请求参数
     * @param <T>     请使用{@link com.github.binarywang.wxpay.bean.order}包下的类
     * @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象
     */
    @ApiOperation(value = "统一下单,并组装所需支付参数")
    @PostMapping("/createOrder")
    public <T> T createOrder(@RequestBody WxPayUnifiedOrderRequest request) throws WxPayException {
        String s = JSON.toJSONString(request);
        System.out.println(s);
        request.setSpbillCreateIp("127.0.0.1");
        return this.wxService.createOrder(request);
    }

    /**
     * <pre>
     * 微信支付-申请退款
     * 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
     * 接口链接:https://api.mch.weixin.qq.com/secapi/pay/refund
     * </pre>
     *
     * @param request 请求对象
     * @return 退款操作结果
     */
    @ApiOperation(value = "退款")
    @PostMapping("/refund")
    public WxPayRefundResult refund(@RequestBody WxPayRefundRequest request) throws WxPayException {
        return this.wxService.refund(request);
    }
}
  • 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
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

五、postman测试接口

测试下单接口:
由于本人要做微信公众号的微信支付,所以是使用jsapi的调用方式,于是,postman参数设置如下:

{
    "body":"hky测试支付",
    "totalFee":"1",
    "notifyUrl":"https://www.hanky.work:8890",
    "outTradeNo":"hanky34kdsjfkajsdf",
    "spbillCreateIp":"127.0.0.1",
    "tradeType":"JSAPI",
    "openid":"oOiaA6pjAWn09asbMQ5SPcI4M9r8"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

简单解释一下:

  • body:描述
  • totalFee:要支付的钱
  • notifyUrl:支付成功后,微信会往这个地址发通知
  • outTradeNo:订单编号
  • spbillCreateIp:终端ip(可以随便写)
  • tradeType:订单类型(WEB,jspapi,app…参考微信支付的官方文档或开源项目源码哈)
  • openid:该用户在微信公众号的唯一标识(就是代表一个人)

在这里插入图片描述
这些返回的参数,就是前端要用来调起微信支付的参数了,后端工作基本结束。

以上,只是简单微信下单,其他操作自行官网,由于本人公司有自己的各类支付封装的项目,打扰了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/874193
推荐阅读
相关标签
  

闽ICP备14008679号