2
0

feat: wx pay update

This commit is contained in:
wfc
2024-12-19 21:38:07 +08:00
parent 17a07ea4bc
commit cd3a860a34
13 changed files with 233 additions and 231 deletions

View File

@@ -82,6 +82,13 @@
<version>20.0.0</version>
</dependency>
<!-- for WeChat Pay -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.6.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,78 @@
package org.wfc.payment.config;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.config.WxPayConfig;
// import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.wfc.payment.service.impl.WxPayServiceImpl;
import org.wfc.payment.service.IWxPayService;
@Configuration
public class WWxPayConfig extends WxPayConfig {
private boolean useSandboxEnv;
public void setUseSandboxEnv(boolean useSandboxEnv) {
this.useSandboxEnv = useSandboxEnv;
}
public boolean isUseSandboxEnv() {
return useSandboxEnv;
}
@Value("${wx.pay.appId}")
private String appId;
@Value("${wx.pay.mchId}")
private String mchId;
@Value("${wx.pay.mchKey}")
private String mchKey;
@Value("${wx.pay.keyPath}")
private String keyPath;
public void setAppId(String appId) {
this.appId = appId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
public void setMchKey(String mchKey) {
this.mchKey = mchKey;
}
public void setKeyPath(String keyPath) {
this.keyPath = keyPath;
}
// private String getSandboxSignKey() {
// try {
// WXPayConfigImpl config = new WXPayConfigImpl();
// Map<String, String> params = new HashMap<>();
// params.put("mch_id", config.getMchID());
// params.put("nonce_str", WXPayUtil.generateNonceStr());
// params.put("sign", WXPayUtil.generateSignature(params, config.getKey()));
// String strXML = wxPay.requestWithoutCert("/sandboxnew/pay/getsignkey", params, this.getHttpConnectTimeoutMs(), this.getHttpReadTimeoutMs());
// Map<String, String> result = WXPayUtil.xmlToMap(strXML);
// if ("SUCCESS".equals(result.get("return_code"))) {
// return result.get("sandbox_signkey");
// } else {
// // 处理错误情况
// }
// } catch (Exception e) {
// // 处理异常
// }
// return null;
// }
@Bean
public IWxPayService wxPayService() throws Exception {
WWxPayConfig config = new WWxPayConfig();
config.setAppId(appId);
config.setMchId(mchId);
config.setMchKey(mchKey);
config.setKeyPath(keyPath);
config.setUseSandboxEnv(true); // 启用沙箱环境
return new WxPayServiceImpl(config);
}
}

View File

@@ -1,73 +0,0 @@
package org.wfc.payment.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType;
import org.wfc.common.security.annotation.RequiresPermissions;
import org.wfc.payment.domain.WechatPay;
import org.wfc.payment.service.IWechatPayService;
/**
* Paypal controller
*
* @author wfc
*/
@RestController
@RequestMapping("/wechatpay")
public class WechatPayController extends BaseController
{
@Autowired
private IWechatPayService wechatpayService;
/**
* WechatPay
*/
@RequiresPermissions("payment:wechatpayInfo:query")
@Log(title = "wechatpay info management", businessType = BusinessType.OTHER)
@GetMapping("/{userId}")
public AjaxResult query(@PathVariable Long userId) {
return success(wechatpayService.selectWechatPayInfoByUserId(userId));
}
/**
* WechatPay
*/
@RequiresPermissions("payment:wechatpayInfo:add")
@Log(title = "wechatpay info management", businessType = BusinessType.INSERT)
@PostMapping("/{wechatpay}")
public AjaxResult add(@PathVariable WechatPay wechatpay)
{
return toAjax(wechatpayService.insertWechatPayInfo(wechatpay));
}
/**
* WechatPay
*/
@RequiresPermissions("payment:wechatpayInfo:edit")
@Log(title = "wechatpay info management", businessType = BusinessType.UPDATE)
@PutMapping("/{id}")
public AjaxResult edit(@PathVariable Long id) {
return toAjax(wechatpayService.updateWechatPayInfoById(id));
}
/**
* WechatPay
*/
@RequiresPermissions("payment:wechatpayInfo:remove")
@Log(title = "wechatpay info management", businessType = BusinessType.DELETE)
@DeleteMapping("/{id}")
public AjaxResult remove(@PathVariable Long id) {
wechatpayService.deleteWechatPayInfoById(id);
return success();
}
}

View File

@@ -0,0 +1,36 @@
package org.wfc.payment.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType;
import org.wfc.common.security.annotation.RequiresPermissions;
import org.wfc.payment.domain.WxPay;
import org.wfc.payment.service.IWxPayService;
import org.springframework.web.bind.annotation.RequestBody;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
@RestController
@RequestMapping("/wxpay")
public class WxPayController extends BaseController {
@Autowired
private WxPayService wxPayService;
@PostMapping("/notify")
public String payNotify(@RequestBody String xmlData) throws WxPayException {
WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlData);
// 处理支付结果
return WxPayNotifyResponse.success("WeChat pay successfully");
}
}

View File

@@ -12,7 +12,7 @@ import javax.validation.constraints.Size;
*
* @author wfc
*/
public class WechatPay extends BaseEntity
public class WxPay extends BaseEntity
{
private static final long serialVersionUID = 1L;

View File

@@ -1,43 +0,0 @@
package org.wfc.payment.mapper;
import org.wfc.payment.domain.WechatPay;
/**
* Paypal mapper
*
* @author wfc
*/
public interface WechatPayMapper
{
/**
* select wechatpay info by user id
*
* @param userId user id
* @return wechatpay information
*/
public WechatPay selectWechatPayInfoByUserId(Long userId);
/**
* select WechatPay pay information by user ID
*
* @param userId user ID
* @return WechatPay pay information
*/
public int insertWechatPayInfo(WechatPay wechatpay);
/**
* select WechatPay pay information by user ID
*
* @param userId user ID
* @return WechatPay pay information
*/
public int updateWechatPayInfoById(Long id);
/**
* select WechatPay pay information by user ID
*
* @param userId user ID
* @return WechatPay pay information
*/
public void deleteWechatPayInfoById(Long id);
}

View File

@@ -0,0 +1,43 @@
package org.wfc.payment.mapper;
import org.wfc.payment.domain.WxPay;
/**
* Paypal mapper
*
* @author wfc
*/
public interface WxPayMapper
{
/**
* select wxpay info by user id
*
* @param userId user id
* @return wxpay information
*/
public WxPay selectWxPayInfoByUserId(Long userId);
/**
* select WxPay pay information by user ID
*
* @param userId user ID
* @return WxPay pay information
*/
public int insertWxPayInfo(WxPay wxpay);
/**
* select WxPay pay information by user ID
*
* @param userId user ID
* @return WxPay pay information
*/
public int updateWxPayInfoById(Long id);
/**
* select WxPay pay information by user ID
*
* @param userId user ID
* @return WxPay pay information
*/
public void deleteWxPayInfoById(Long id);
}

View File

@@ -1,43 +0,0 @@
package org.wfc.payment.service;
import org.wfc.payment.domain.WechatPay;
/**
* Paypal pay service layer
*
* @author wfc
*/
public interface IWechatPayService
{
/**
* select WechatPay pay information by user ID
*
* @param userId user ID
* @return WechatPay pay information
*/
public WechatPay selectWechatPayInfoByUserId(Long userId);
/**
* select WechatPay pay information by user ID
*
* @param userId user ID
* @return WechatPay pay information
*/
public int insertWechatPayInfo(WechatPay wechatpay);
/**
* select WechatPay pay information by user ID
*
* @param userId user ID
* @return WechatPay pay information
*/
public int updateWechatPayInfoById(Long id);
/**
* select WechatPay pay information by user ID
*
* @param userId user ID
* @return WechatPay pay information
*/
public void deleteWechatPayInfoById(Long id);
}

View File

@@ -0,0 +1,10 @@
package org.wfc.payment.service;
import java.math.BigDecimal;
import org.wfc.payment.domain.WxPay;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
public interface IWxPayService {
public WxPayUnifiedOrderResult createOrder(String orderId, BigDecimal amount) throws WxPayException;
}

View File

@@ -1,63 +0,0 @@
package org.wfc.payment.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wfc.payment.mapper.WechatPayMapper;
import org.wfc.payment.service.IWechatPayService;
import org.wfc.payment.domain.WechatPay;
/**
* WechatPay service implementation
*
* @author wfc
*/
@Service
public class WechatPayServiceImpl implements IWechatPayService
{
@Autowired
private WechatPayMapper wechatpayMapper;
/**
* Select Paypal information by user ID
*
* @param userId user ID
* @return 结果
*/
@Override
public WechatPay selectWechatPayInfoByUserId(Long userId) {
return wechatpayMapper.selectWechatPayInfoByUserId(userId);
}
/**
* Select Paypal information by user ID
*
* @param userId user ID
* @return 结果
*/
@Override
public int insertWechatPayInfo(WechatPay wechatpay) {
return wechatpayMapper.insertWechatPayInfo(wechatpay);
}
/**
* Select Paypal information by user ID
*
* @param userId user ID
* @return 结果
*/
@Override
public int updateWechatPayInfoById(Long id) {
return wechatpayMapper.updateWechatPayInfoById(id);
}
/**
* update credit card
*
* @param creditCard User ID
* @return void
*/
@Override
public void deleteWechatPayInfoById(Long id) {
wechatpayMapper.deleteWechatPayInfoById(id);
}
}

View File

@@ -0,0 +1,43 @@
package org.wfc.payment.service.impl;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wfc.payment.mapper.WxPayMapper;
import org.wfc.payment.service.IWxPayService;
import org.wfc.payment.domain.WxPay;
import org.wfc.payment.config.WWxPayConfig;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
/**
* WxPay service implementation
*
* @author wfc
*/
@Service
public class WxPayServiceImpl implements IWxPayService
{
@Autowired
private WxPayService wxPayService;
@Autowired
private WWxPayConfig config;
public WxPayServiceImpl(WWxPayConfig config) {
this.config = config;
}
public WxPayUnifiedOrderResult createOrder(String orderId, BigDecimal amount) throws WxPayException {
WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
request.setOutTradeNo(orderId);
request.setTotalFee(BaseWxPayRequest.yuanToFen(amount.toString()));
request.setBody("Order Payment");
request.setTradeType("JSAPI");
request.setNotifyUrl("https://wfc-modules-payment:/wxpay/notify");
return wxPayService.unifiedOrder(request);
}
}

View File

@@ -42,6 +42,13 @@ spring:
# url:
# driver-class-name:
wx:
pay:
appId: app_id
mchId: merchant_id
mchKey: merchant_key
keyPath: /path/to/your/apiclient_key.pem
# mybatis-plus配置
mybatis-plus:
# 搜索指定包别名

View File

@@ -2,9 +2,9 @@
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.wfc.payment.mapper.WechatPayMapper">
<mapper namespace="org.wfc.payment.mapper.WxPayMapper">
<resultMap type="WechatPay" id="WechatPayResult">
<resultMap type="WxPay" id="WxPayResult">
<id property="deptId" column="dept_id" />
<result property="parentId" column="parent_id" />
<result property="ancestors" column="ancestors" />
@@ -23,13 +23,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="remark" column="remark" />
</resultMap>
<sql id="selectWechatPayInfoVo">
<sql id="selectWxPayInfoVo">
select d.dept_id, d.parent_id, d.ancestors, d.remark, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time
from sys_dept d
</sql>
<select id="selectWechatPayInfoByUserId" parameterType="WechatPay" resultMap="WechatPayResult">
<include refid="selectWechatPayInfoVo"/>
<select id="selectWxPayInfoByUserId" parameterType="WxPay" resultMap="WxPayResult">
<include refid="selectWxPayInfoVo"/>
where d.del_flag = '0'
<if test="deptId != null and deptId != 0">
AND dept_id = #{deptId}
@@ -48,7 +48,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
order by d.parent_id, d.order_num
</select>
<insert id="insertWechatPayInfo" parameterType="WechatPay">
<insert id="insertWxPayInfo" parameterType="WxPay">
insert into sys_dept(
<if test="deptId != null and deptId != 0">dept_id,</if>
<if test="parentId != null and parentId != 0">parent_id,</if>
@@ -78,7 +78,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
)
</insert>
<update id="updateWechatPayInfoById" parameterType="WechatPay">
<update id="updateWxPayInfoById" parameterType="WxPay">
update sys_dept
<set>
<if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
@@ -96,7 +96,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where dept_id = #{id}
</update>
<delete id="deleteWechatPayInfoById" parameterType="Long">
<delete id="deleteWxPayInfoById" parameterType="Long">
update sys_dept set del_flag = '2' where dept_id = #{id}
</delete>