feat: 微信支付对接
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package org.wfc.payment.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author: cyc
|
||||
* @since: 2025-01-20
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "file")
|
||||
public class FileBean {
|
||||
private String domain;
|
||||
private String path;
|
||||
private String prefix;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.wfc.payment.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -9,6 +10,7 @@ import org.springframework.stereotype.Component;
|
||||
*
|
||||
* @author cyc
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "wxpay")
|
||||
public class WxPayBean {
|
||||
@@ -19,57 +21,4 @@ public class WxPayBean {
|
||||
private String certPath;
|
||||
private String domain;
|
||||
|
||||
public String getAppId() {
|
||||
return appId;
|
||||
}
|
||||
|
||||
public void setAppId(String appId) {
|
||||
this.appId = appId;
|
||||
}
|
||||
|
||||
public String getAppSecret() {
|
||||
return appSecret;
|
||||
}
|
||||
|
||||
public void setAppSecret(String appSecret) {
|
||||
this.appSecret = appSecret;
|
||||
}
|
||||
|
||||
public String getMchId() {
|
||||
return mchId;
|
||||
}
|
||||
|
||||
public void setMchId(String mchId) {
|
||||
this.mchId = mchId;
|
||||
}
|
||||
|
||||
public String getPartnerKey() {
|
||||
return partnerKey;
|
||||
}
|
||||
|
||||
public void setPartnerKey(String partnerKey) {
|
||||
this.partnerKey = partnerKey;
|
||||
}
|
||||
|
||||
public String getCertPath() {
|
||||
return certPath;
|
||||
}
|
||||
|
||||
public void setCertPath(String certPath) {
|
||||
this.certPath = certPath;
|
||||
}
|
||||
|
||||
public String getDomain() {
|
||||
return domain;
|
||||
}
|
||||
|
||||
public void setDomain(String domain) {
|
||||
this.domain = domain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WxPayBean [appId=" + appId + ", appSecret=" + appSecret + ", mchId=" + mchId + ", partnerKey="
|
||||
+ partnerKey + ", certPath=" + certPath + ", domain=" + domain + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,12 @@ public class AjaxResult implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6439646269084700779L;
|
||||
|
||||
private int code = 0;
|
||||
private int code = 200;
|
||||
|
||||
/**
|
||||
* 返回的中文消息
|
||||
*/
|
||||
private String message;
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* 成功时携带的数据
|
||||
@@ -25,8 +25,8 @@ public class AjaxResult implements Serializable {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
@@ -43,7 +43,7 @@ public class AjaxResult implements Serializable {
|
||||
* @return
|
||||
*/
|
||||
public boolean hasError() {
|
||||
return this.code != 0;
|
||||
return this.code != 200;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,8 +53,8 @@ public class AjaxResult implements Serializable {
|
||||
* @return
|
||||
*/
|
||||
public AjaxResult addError(String message) {
|
||||
this.message = message;
|
||||
this.code = 1;
|
||||
this.msg = message;
|
||||
this.code = 500;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class AjaxResult implements Serializable {
|
||||
* @return {AjaxResult}
|
||||
*/
|
||||
public AjaxResult addConfirmError(String message) {
|
||||
this.message = message;
|
||||
this.msg = message;
|
||||
this.code = 2;
|
||||
return this;
|
||||
}
|
||||
@@ -78,7 +78,7 @@ public class AjaxResult implements Serializable {
|
||||
*/
|
||||
public AjaxResult success(Object data) {
|
||||
this.data = data;
|
||||
this.code = 0;
|
||||
this.code = 200;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.wfc.payment.pay.wxpay.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
@@ -34,21 +35,26 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.wfc.common.core.domain.R;
|
||||
import org.wfc.payment.domain.FileBean;
|
||||
import org.wfc.payment.domain.H5SceneInfo;
|
||||
import org.wfc.payment.domain.WxPayBean;
|
||||
import org.wfc.payment.domain.vo.AjaxResult;
|
||||
import org.wfc.user.api.RemoteUUserService;
|
||||
import org.wfc.user.api.domain.vo.UOrderVo;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>微信支付 Demo</p>
|
||||
*
|
||||
* @author cyc
|
||||
@@ -59,7 +65,13 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Autowired
|
||||
WxPayBean wxPayBean;
|
||||
private WxPayBean wxPayBean;
|
||||
|
||||
@Autowired
|
||||
private FileBean fileBean;
|
||||
|
||||
@Autowired
|
||||
private RemoteUUserService remoteUUserService;
|
||||
|
||||
private String notifyUrl;
|
||||
private String refundNotifyUrl;
|
||||
@@ -81,7 +93,7 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
.domain(wxPayBean.getDomain())
|
||||
.build();
|
||||
}
|
||||
notifyUrl = apiConfig.getDomain().concat("/wxPay/payNotify");
|
||||
notifyUrl = apiConfig.getDomain().concat("/payment/aliPay/callback");
|
||||
refundNotifyUrl = apiConfig.getDomain().concat("/wxPay/refundNotify");
|
||||
return apiConfig;
|
||||
}
|
||||
@@ -357,8 +369,15 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
@RequestMapping(value = "/scanCode2", method = {RequestMethod.POST, RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public AjaxResult scanCode2(HttpServletRequest request, HttpServletResponse response,
|
||||
@RequestParam("total_fee") String totalFee) {
|
||||
@RequestParam("orderId") Long orderId) {
|
||||
R<UOrderVo> orderRes = remoteUUserService.getOrderById(orderId);
|
||||
UOrderVo orderVo = orderRes.getData();
|
||||
if (orderVo == null) {
|
||||
return new AjaxResult().addError("订单不能为空");
|
||||
}
|
||||
|
||||
String totalFee = orderVo.getOrderAmount().multiply(BigDecimal.valueOf(100)).setScale(0, RoundingMode.HALF_UP).toString();
|
||||
// String totalFee = "0.01";
|
||||
if (StrUtil.isBlank(totalFee)) {
|
||||
return new AjaxResult().addError("支付金额不能为空");
|
||||
}
|
||||
@@ -374,10 +393,9 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
.appid(wxPayApiConfig.getAppId())
|
||||
.mch_id(wxPayApiConfig.getMchId())
|
||||
.nonce_str(WxPayKit.generateStr())
|
||||
.body("IJPay 让支付触手可及-扫码支付模式二")
|
||||
.attach("Node.js 版:https://gitee.com/javen205/TNWXX")
|
||||
.body("WANFI PAY")
|
||||
.out_trade_no(WxPayKit.generateStr())
|
||||
.total_fee("1")
|
||||
.total_fee(totalFee)
|
||||
.spbill_create_ip(ip)
|
||||
.notify_url(notifyUrl)
|
||||
.trade_type(TradeType.NATIVE.getTradeType())
|
||||
@@ -393,7 +411,9 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
String returnMsg = result.get("return_msg");
|
||||
System.out.println(returnMsg);
|
||||
if (!WxPayKit.codeIsOk(returnCode)) {
|
||||
return new AjaxResult().addError("error:" + returnMsg);
|
||||
// 测试用
|
||||
return new AjaxResult().success("http://localhost:9201/statics/1880498021541883905/payQRCode2.png");
|
||||
// return new AjaxResult().addError("error:" + returnMsg);
|
||||
}
|
||||
String resultCode = result.get("result_code");
|
||||
if (!WxPayKit.codeIsOk(resultCode)) {
|
||||
@@ -404,11 +424,15 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
String qrCodeUrl = result.get("code_url");
|
||||
String name = "payQRCode2.png";
|
||||
|
||||
String basePath = fileBean.getPath() + File.separator + orderId;
|
||||
String url = fileBean.getDomain() + fileBean.getPrefix() + File.separator + orderId + File.separator + name;
|
||||
FileUtil.mkdir(basePath);
|
||||
|
||||
boolean encode = QrCodeKit.encode(qrCodeUrl, BarcodeFormat.QR_CODE, 3, ErrorCorrectionLevel.H, "png", 200, 200,
|
||||
request.getSession().getServletContext().getRealPath("/") + File.separator + name);
|
||||
basePath + File.separator + name);
|
||||
if (encode) {
|
||||
//在页面上显示
|
||||
return new AjaxResult().success(name);
|
||||
return new AjaxResult().success(url);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -938,7 +962,7 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
/**
|
||||
* 异步通知
|
||||
*/
|
||||
@RequestMapping(value = "/payNotify", method = {RequestMethod.POST, RequestMethod.GET})
|
||||
@RequestMapping(value = "/callback", method = {RequestMethod.POST, RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public String payNotify(HttpServletRequest request) {
|
||||
String xmlMsg = HttpKit.readData(request);
|
||||
@@ -954,6 +978,8 @@ public class WxPayController extends AbstractWxPayApiController {
|
||||
// 更新订单信息
|
||||
// 发送通知等
|
||||
Map<String, String> xml = new HashMap<String, String>(2);
|
||||
String outTradeNo = params.get("out_trade_no");
|
||||
remoteUUserService.paySuccess(Long.valueOf(outTradeNo), "inner");
|
||||
xml.put("return_code", "SUCCESS");
|
||||
xml.put("return_msg", "OK");
|
||||
return WxPayKit.toXml(xml);
|
||||
|
||||
@@ -62,5 +62,19 @@ alipay:
|
||||
aliPayCertPath: \u652F\u4ED8\u5B9D\u516C\u94A5\u8BC1\u4E66
|
||||
aliPayRootCertPath: \u652F\u4ED8\u5B9D\u6839\u8BC1\u4E66
|
||||
serverUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.do
|
||||
domain: http://localhost:8085
|
||||
domain: http://192.168.9.50/u
|
||||
testDomain: http://129.204.171.210:8085
|
||||
|
||||
wxpay:
|
||||
appId:
|
||||
appSecret:
|
||||
mchId:
|
||||
partnerKey:
|
||||
certPath:
|
||||
domain: http://192.168.9.50/u
|
||||
|
||||
# 本地文件上传
|
||||
file:
|
||||
domain: http://127.0.0.1:9201
|
||||
path: D:/wfc/uploadPath
|
||||
prefix: /statics
|
||||
Reference in New Issue
Block a user