赞
踩
文章有不当之处,欢迎指正。
突然想到自己还有个博客,碰巧再写微信小程序支付,所以浅写一下。
正文开始。。。。。。。。。。。。
官方文档:微信支付文档
看过之后你会发现开发有点麻烦,所以接下来选择使用第三方sdk开发,减少工作量,大佬GitHub地址:GitHub
大家可以去GitHub查看最新版本
<!-- 微信小程序 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>4.0.0</version>
</dependency>
<!-- 微信支付 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.1.9.B</version>
</dependency>
商户号开通请参考微信官方文档,p12证书也请参考微信官方文档
wx:
app-id: xxxxxxxxxxxx
app-secret: xxxxxxxxxxxx
// 商户号
mch-id: xxxxxxxxxxxx
// 商户密钥
mch-key: xxxxxxxxxxxx
// 回调地址 保证外网能访问
notify-url: xxxxxxxxxxxx
// p12证书的位置,可以绝对路径,可以指定类路径 以classpath:开头
key-path: xxxxxxxxxxxx
已忽略小程序部分,只提取出了主要部分
@Configuration
@ConfigurationProperties(prefix = "wx")
public class WxProperties {
private String appId;
private String appSecret;
private String mchId;
private String mchKey;
private String notifyUrl;
private String keyPath;
}
记得get set一下,lombok也可
@Configuration public class WxPayConfiguration { @Autowired private WxProperties properties; @Bean public WxPayConfig wxPayConfig() { WxPayConfig payConfig = new WxPayConfig(); payConfig.setAppId(properties.getAppId()); payConfig.setMchId(properties.getMchId()); payConfig.setMchKey(properties.getMchKey()); payConfig.setNotifyUrl(properties.getNotifyUrl()); payConfig.setKeyPath(properties.getKeyPath()); payConfig.setTradeType("JSAPI"); payConfig.setSignType("MD5"); return payConfig; } @Bean public WxPayService wxPayService(WxPayConfig payConfig) { WxPayService wxPayService = new WxPayServiceImpl(); wxPayService.setConfig(payConfig); return wxPayService; } }
支付相关的方法都在WxPayService中,附加api文档:Api
代码中的order为商品的实体类,具体含义咱就看文档吧
// 调用支付接口
WxPayMpOrderResult result = null;
try {
WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
orderRequest.setOutTradeNo(order.getOrderSn());
orderRequest.setTotalFee(order.getPrice().multiply(new BigDecimal(100)).intValue());
orderRequest.setSpbillCreateIp(IpUtil.getIpAddr(request));
orderRequest.setOpenid(wxuser.getOpenId());
result = wxPayService.createOrder(orderRequest);
} catch (WxPayException e) {
// 异常信息
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。