1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 【SpringBoot学习】39 SpringBoot 集成 wxJava 微信小程序:订单支付

【SpringBoot学习】39 SpringBoot 集成 wxJava 微信小程序:订单支付

时间:2022-04-07 13:01:17

相关推荐

【SpringBoot学习】39 SpringBoot 集成 wxJava 微信小程序:订单支付

文章目录

SpringBoot 集成 wxJava 微信小程序:订单支付1、整合 wxJava 小程序2、支付配置类3、application.yml 配置4、授权登录流程5、uniapp 前端微信公众号

SpringBoot 集成 wxJava 微信小程序:订单支付

1、整合 wxJava 小程序

导入相关依赖,最新版本的可以查看官方文档 wxJava

<!-- wxjava支付 --><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-pay</artifactId><version>4.2.0</version></dependency>

2、支付配置类

直接复制粘贴到项目

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 mons.lang3.StringUtils;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)@AllArgsConstructorpublic class WxPayConfiguration {private WxPayProperties properties;@Bean@ConditionalOnMissingBeanpublic 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;}}

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;/*** 支付回调*/private String notifyUrl;}

3、application.yml 配置

# 微信配置wx:pay:appId: appidmchId: 商户idmchKey: 秘钥keykeyPath: 证书地址/apiclient_cert.p12notifyUrl: 支付回调地址

4、授权登录流程

控制层

@Autowiredprivate WeiXinPayService weiXinPayService;@ApiOperation("统一下单")@PostMapping("createOrder")public AjaxResult createOrder(@RequestBody WxOrderVo entity) {return weiXinPayService.createOrder(entity);}@ApiOperation("支付回调")@RequestMapping("notifyUrl")public String notifyUrl() {return weiXinPayService.notifyUrl();}

service 接口

/*** 统一下单** @param entity* @return*/AjaxResult createOrder(WxOrderVo entity);/*** 支付回调** @return*/String notifyUrl();

service 接口实现类

@Autowiredprivate WxPayService wxPayService;@Autowiredprivate WxPayProperties wxPayProperties;@Overridepublic AjaxResult createOrder(WxOrderVo entity) {try {String openId = SecurityUtils.getLoginUser().getUser().getOpenId();WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();orderRequest.setSignType(WxPayConstants.SignType.MD5);orderRequest.setBody("请求支付订单");orderRequest.setOutTradeNo(UUID.randomUUID().toString().substring(0, 32));orderRequest.setTradeType(WxPayConstants.TradeType.JSAPI);orderRequest.setTotalFee(BaseWxPayRequest.yuanToFen(String.valueOf(entity.getPayMoney())));orderRequest.setOpenid(openId);orderRequest.setSpbillCreateIp(ServletUtils.getClientIP());orderRequest.setNotifyUrl(wxPayProperties.getNotifyUrl());Object order = wxPayService.createOrder(orderRequest);log.error("下单成功:{}", order.toString());return AjaxResult.success("下单成功", JSON.toJSONString(order));} catch (WxPayException e) {log.error("下单失败:{}", e.toString());return AjaxResult.error(e.getMessage());}}@Overridepublic String notifyUrl() {try {HttpServletRequest request = ServletUtils.getRequest();HttpServletResponse response = ServletUtils.getResponse();String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);log.info("订单支付回调:{}", result);// 加入自己处理订单的业务逻辑,需要判断订单是否已经支付过,否则可能会重复调用String orderId = result.getOutTradeNo();String tradeNo = result.getTransactionId();String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());return WxPayNotifyResponse.success("处理成功!");} catch (Exception e) {log.error("微信回调结果异常,异常原因{}", e.getMessage());return WxPayNotifyResponse.fail(e.getMessage());}}

WxOrderVo

import lombok.Data;import lombok.experimental.Accessors;import java.math.BigDecimal;/*** 微信订餐参数** @author Tellsea* @date /3/25*/@Data@Accessors(chain = true)public class WxOrderVo {private BigDecimal payMoney;}

WxRefundVo

import lombok.Data;import lombok.experimental.Accessors;/*** 微信退款参数** @author Tellsea* @date /3/25*/@Data@Accessors(chain = true)public class WxRefundVo {}

OK ,到此后端调用逻辑都完成了

5、uniapp 前端

<template><view style="padding: 15px;"><u-form :model="form" ref="uForm" label-width="140"><u-form-item label="支付金额"><u-input v-model="form.payMoney" /></u-form-item><u-button @click="submit" type="primary">订单支付</u-button></u-form></view></template><script>let that;export default {name: "createOrder",data() {return {form: {payMoney: 0.01}}},onLoad() {that = this;},methods: {submit() {that.$u.post('/au/weixinPay/createOrder', that.form).then(res => {let data = JSON.parse(res.data);wx.requestPayment({timeStamp: data.timeStamp,nonceStr: data.nonceStr,package: data.packageValue,signType: data.signType,paySign: data.paySign});});}}}</script><style scoped></style>

微信公众号

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。