当前位置:   article > 正文

Java实现微信支付_wechatpay-java

wechatpay-java

Java实现微信支付

文章有不当之处,欢迎指正。

突然想到自己还有个博客,碰巧再写微信小程序支付,所以浅写一下。

正文开始。。。。。。。。。。。。

第一步:官方文档

官方文档:微信支付文档
看过之后你会发现开发有点麻烦,所以接下来选择使用第三方sdk开发,减少工作量,大佬GitHub地址:GitHub

第二步:创建maven工程,引入pom

大家可以去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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第三步:application.yml配置文件

商户号开通请参考微信官方文档,p12证书也请参考微信官方文档

wx:
   app-id: xxxxxxxxxxxx
   app-secret: xxxxxxxxxxxx
   // 商户号
   mch-id: xxxxxxxxxxxx
   // 商户密钥
   mch-key: xxxxxxxxxxxx
   // 回调地址 保证外网能访问
   notify-url: xxxxxxxxxxxx
   // p12证书的位置,可以绝对路径,可以指定类路径 以classpath:开头
   key-path: xxxxxxxxxxxx

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第四步:上代码

已忽略小程序部分,只提取出了主要部分

@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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

记得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;
	}
}
  • 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

支付相关的方法都在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) {
	// 异常信息
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/467390
推荐阅读
相关标签
  

闽ICP备14008679号