修复:兼容微信支付V3Key
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -256,7 +256,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.wechatpay-apiv3</groupId>
|
<groupId>com.github.wechatpay-apiv3</groupId>
|
||||||
<artifactId>wechatpay-java</artifactId>
|
<artifactId>wechatpay-java</artifactId>
|
||||||
<version>0.2.9</version>
|
<version>0.2.15</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.gxwebsoft.shop.controller;
|
package com.gxwebsoft.shop.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
import com.gxwebsoft.common.core.web.BaseController;
|
import com.gxwebsoft.common.core.web.BaseController;
|
||||||
import com.gxwebsoft.common.system.entity.User;
|
import com.gxwebsoft.common.system.entity.User;
|
||||||
import com.gxwebsoft.shop.service.ShopOrderService;
|
import com.gxwebsoft.shop.service.ShopOrderService;
|
||||||
@@ -61,15 +62,19 @@ public class ShopOrderController extends BaseController {
|
|||||||
@ApiOperation("添加订单")
|
@ApiOperation("添加订单")
|
||||||
@PostMapping()
|
@PostMapping()
|
||||||
public ApiResult<?> save(@RequestBody ShopOrder shopOrder) {
|
public ApiResult<?> save(@RequestBody ShopOrder shopOrder) {
|
||||||
// 记录当前登录用户id
|
// 记录当前登录用户id
|
||||||
User loginUser = getLoginUser();
|
User loginUser = getLoginUser();
|
||||||
if (loginUser != null) {
|
if (loginUser != null) {
|
||||||
shopOrder.setUserId(loginUser.getUserId());
|
shopOrder.setUserId(loginUser.getUserId());
|
||||||
}
|
shopOrder.setOpenid(loginUser.getOpenid());
|
||||||
if (shopOrderService.save(shopOrder)) {
|
}
|
||||||
return success("添加成功");
|
if (shopOrder.getOrderNo() == null) {
|
||||||
}
|
shopOrder.setOrderNo(Long.toString(IdUtil.getSnowflakeNextId()));
|
||||||
return fail("添加失败");
|
}
|
||||||
|
if (shopOrderService.save(shopOrder)) {
|
||||||
|
return success("下单成功", shopOrderService.createWxOrder(shopOrder));
|
||||||
|
}
|
||||||
|
return fail("添加失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("修改订单")
|
@ApiOperation("修改订单")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.gxwebsoft.shop.entity;
|
|||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||||
@@ -175,4 +176,18 @@ public class ShopOrder implements Serializable {
|
|||||||
@ApiModelProperty(value = "创建时间")
|
@ApiModelProperty(value = "创建时间")
|
||||||
private Date createTime;
|
private Date createTime;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "自提码")
|
||||||
|
private String selfTakeCode;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "是否已收到赠品")
|
||||||
|
private Boolean hasTakeGift;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "accessToken")
|
||||||
|
@TableField(exist = false)
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "openid")
|
||||||
|
@TableField(exist = false)
|
||||||
|
private String openid;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.gxwebsoft.common.core.web.PageResult;
|
|||||||
import com.gxwebsoft.shop.entity.ShopOrder;
|
import com.gxwebsoft.shop.entity.ShopOrder;
|
||||||
import com.gxwebsoft.shop.param.ShopOrderParam;
|
import com.gxwebsoft.shop.param.ShopOrderParam;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,4 +40,5 @@ public interface ShopOrderService extends IService<ShopOrder> {
|
|||||||
*/
|
*/
|
||||||
ShopOrder getByIdRel(Integer orderId);
|
ShopOrder getByIdRel(Integer orderId);
|
||||||
|
|
||||||
|
HashMap<String, String> createWxOrder(ShopOrder shopOrder);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,30 @@
|
|||||||
package com.gxwebsoft.shop.service.impl;
|
package com.gxwebsoft.shop.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||||
|
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||||
|
import com.gxwebsoft.common.system.entity.Payment;
|
||||||
import com.gxwebsoft.shop.mapper.ShopOrderMapper;
|
import com.gxwebsoft.shop.mapper.ShopOrderMapper;
|
||||||
import com.gxwebsoft.shop.service.ShopOrderService;
|
import com.gxwebsoft.shop.service.ShopOrderService;
|
||||||
import com.gxwebsoft.shop.entity.ShopOrder;
|
import com.gxwebsoft.shop.entity.ShopOrder;
|
||||||
import com.gxwebsoft.shop.param.ShopOrderParam;
|
import com.gxwebsoft.shop.param.ShopOrderParam;
|
||||||
import com.gxwebsoft.common.core.web.PageParam;
|
import com.gxwebsoft.common.core.web.PageParam;
|
||||||
import com.gxwebsoft.common.core.web.PageResult;
|
import com.gxwebsoft.common.core.web.PageResult;
|
||||||
|
import com.wechat.pay.java.core.Config;
|
||||||
|
import com.wechat.pay.java.core.RSAConfig;
|
||||||
|
import com.wechat.pay.java.core.RSAPublicKeyConfig;
|
||||||
|
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
||||||
|
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
|
||||||
|
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
|
||||||
|
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
|
||||||
|
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,6 +35,18 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {
|
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder> implements ShopOrderService {
|
||||||
|
@Value("${spring.profiles.active}")
|
||||||
|
String active;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ConfigProperties config;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisUtil redisUtil;
|
||||||
|
|
||||||
|
public static String privateKeyPath = "/Users/gxwebsoft/Downloads/ef7f7e0430cb47019d06b93f885bf95f/apiclient_key.pem";
|
||||||
|
public static String privateCertPath = "/Users/gxwebsoft/JAVA/com.gxwebsoft.core/src/main/resources/cert/apiclient_cert.pem";
|
||||||
|
public static String wechatpayCertPath = "/Users/gxwebsoft/Downloads/ef7f7e0430cb47019d06b93f885bf95f/wechatpay_55729BDEC2502C301BA02CDC28E4CEE4DE4D1DB9.pem"; // 平台证书
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<ShopOrder> pageRel(ShopOrderParam param) {
|
public PageResult<ShopOrder> pageRel(ShopOrderParam param) {
|
||||||
@@ -44,4 +72,95 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
|||||||
return param.getOne(baseMapper.selectListRel(param));
|
return param.getOne(baseMapper.selectListRel(param));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HashMap<String, String> createWxOrder(ShopOrder order) {
|
||||||
|
Integer payType = order.getPayType();
|
||||||
|
final String uploadPath = config.getUploadPath(); // 服务器本地路径
|
||||||
|
final HashMap<String, String> orderInfo = new HashMap<>();
|
||||||
|
// 微信小程序(微信支付)
|
||||||
|
String key = "mp-weixin:".concat(order.getTenantId().toString());
|
||||||
|
final String string = redisUtil.get(key);
|
||||||
|
// System.out.println("string = " + string);
|
||||||
|
final JSONObject mpWx = JSONObject.parseObject(string);
|
||||||
|
// System.out.println("mpWx = " + mpWx);
|
||||||
|
String key2 = "Payment:".concat(payType.toString()).concat(":").concat(order.getTenantId().toString());
|
||||||
|
final Payment payment = redisUtil.get(key2, Payment.class);
|
||||||
|
// System.out.println("payment = " + payment);
|
||||||
|
|
||||||
|
// 计算金额
|
||||||
|
BigDecimal decimal = order.getTotalPrice();
|
||||||
|
final BigDecimal multiply = decimal.multiply(new BigDecimal(100));
|
||||||
|
// 将 BigDecimal 转换为 Integer
|
||||||
|
Integer money = multiply.intValue();
|
||||||
|
String privateKey = uploadPath.concat("/file").concat(payment.getApiclientKey()); // 秘钥证书
|
||||||
|
String apiclientCert = uploadPath.concat("/file").concat(payment.getApiclientCert());
|
||||||
|
String pubKey = uploadPath.concat("/file").concat(payment.getPubKey()); // 公钥证书
|
||||||
|
// 开发环境配置
|
||||||
|
if (active.equals("dev")) {
|
||||||
|
privateKey = privateKeyPath;
|
||||||
|
apiclientCert = wechatpayCertPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 兼容公钥
|
||||||
|
Config config;
|
||||||
|
if (payment.getPubKey() != null && !payment.getPubKey().isEmpty()) {
|
||||||
|
config = new RSAPublicKeyConfig.Builder()
|
||||||
|
.merchantId(payment.getMchId())
|
||||||
|
.privateKeyFromPath(privateKey)
|
||||||
|
.publicKeyFromPath(pubKey)
|
||||||
|
.publicKeyId(payment.getPubKeyId())
|
||||||
|
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||||
|
.apiV3Key(payment.getApiKey())
|
||||||
|
.build();
|
||||||
|
} else {
|
||||||
|
config = new RSAConfig.Builder()
|
||||||
|
.merchantId(payment.getMchId())
|
||||||
|
.privateKeyFromPath(privateKey)
|
||||||
|
.merchantSerialNumber(payment.getMerchantSerialNumber())
|
||||||
|
.wechatPayCertificatesFromPath(apiclientCert)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建service
|
||||||
|
JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
|
||||||
|
// 跟之前下单示例一样,填充预下单参数
|
||||||
|
PrepayRequest request = new PrepayRequest();
|
||||||
|
Amount amount = new Amount();
|
||||||
|
amount.setTotal(money);
|
||||||
|
amount.setCurrency("CNY");
|
||||||
|
request.setAmount(amount);
|
||||||
|
request.setAppid(mpWx.getString("appId"));
|
||||||
|
request.setMchid(payment.getMchId());
|
||||||
|
request.setDescription(order.getComments());
|
||||||
|
request.setOutTradeNo(order.getOrderNo());
|
||||||
|
request.setAttach(order.getTenantId().toString());
|
||||||
|
final Payer payer = new Payer();
|
||||||
|
payer.setOpenid(order.getOpenid());
|
||||||
|
request.setPayer(payer);
|
||||||
|
// 测试环境
|
||||||
|
if (active.equals("dev")) {
|
||||||
|
amount.setTotal(1);
|
||||||
|
request.setAmount(amount);
|
||||||
|
request.setNotifyUrl("http://jimei-api.natapp1.cc/api/shop/wx-pay/notify/" + order.getTenantId()); // 默认回调地址
|
||||||
|
}
|
||||||
|
// 生成环境
|
||||||
|
if (active.equals("prod")) {
|
||||||
|
request.setAmount(amount);
|
||||||
|
request.setNotifyUrl("https://server.gxwebsoft.com/api/system/wx-pay/notify/" + order.getTenantId()); // 默认回调地址
|
||||||
|
}
|
||||||
|
// if (StrUtil.isNotBlank(payment.getNotifyUrl())) {
|
||||||
|
// 后台配置的回调地址
|
||||||
|
// request.setNotifyUrl(payment.getNotifyUrl().concat("/").concat(order.getTenantId().toString()));
|
||||||
|
// }
|
||||||
|
System.out.println("request = " + request);
|
||||||
|
PrepayWithRequestPaymentResponse response = service.prepayWithRequestPayment(request);
|
||||||
|
orderInfo.put("provider", "wxpay");
|
||||||
|
orderInfo.put("timeStamp", response.getTimeStamp());
|
||||||
|
orderInfo.put("nonceStr", response.getNonceStr());
|
||||||
|
orderInfo.put("package", response.getPackageVal());
|
||||||
|
orderInfo.put("signType", "RSA");
|
||||||
|
orderInfo.put("paySign", response.getPaySign());
|
||||||
|
return orderInfo;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user