赞
踩
首先,没有做过微信支付,又想快速开发,来快速熟悉微信支付流程。可以考虑用开源项目。
本文主要介绍微信公众号,服务商模式下微信支付。
WxJava是一个对微信支付封装的开源项目。
项目地址:https://gitee.com/binary/weixin-java-tools?_from=gitee_search
官方demo:https://gitee.com/binary/weixin-java-tools/blob/develop/demo.md
直接开始正片
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>(不同模块参考下文)</artifactId>
<version>4.1.0</version>
</dependency>
比如,本人要做的是公众号微信支付,可以引入weixin-java-pay,(貌似都包括了)
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.1.0</version>
</dependency>
在application.yml中,加入如下配置,(下面的值我都轻微的改过了,不可以拿过去就用哦!)
其中p12证书,这里没写,但是注释写的很清楚,不影响下单。本人目前接触到它的作用,退款时候需要用到,其他还没用到过。能力有限罪过,罪过。
wx:
pay:
appId: wx9c4130f5db12345 #微信公众号或者小程序等的appid
mchId: 1611921123 #微信支付商户号
mchKey: JIWUwB4juAUMb9XI3SjmEJESJzo12321 #微信支付商户密钥
subAppId: wx9c413021323d #服务商模式下的子商户公众账号ID
subMchId: 162312372 #服务商模式下的子商户号
keyPath: # p12证书的位置,可以指定绝对路径,也可以指定类路径(以classpath:开头)
将上述,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; }
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; } }
继续抄官方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); } }
测试下单接口:
由于本人要做微信公众号的微信支付,所以是使用jsapi的调用方式,于是,postman参数设置如下:
{
"body":"hky测试支付",
"totalFee":"1",
"notifyUrl":"https://www.hanky.work:8890",
"outTradeNo":"hanky34kdsjfkajsdf",
"spbillCreateIp":"127.0.0.1",
"tradeType":"JSAPI",
"openid":"oOiaA6pjAWn09asbMQ5SPcI4M9r8"
}
简单解释一下:
这些返回的参数,就是前端要用来调起微信支付的参数了,后端工作基本结束。
以上,只是简单微信下单,其他操作自行官网,由于本人公司有自己的各类支付封装的项目,打扰了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。