|
|
|
|
@@ -1,14 +1,30 @@
|
|
|
|
|
package com.gxwebsoft.shop.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
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.service.ShopOrderService;
|
|
|
|
|
import com.gxwebsoft.shop.entity.ShopOrder;
|
|
|
|
|
import com.gxwebsoft.shop.param.ShopOrderParam;
|
|
|
|
|
import com.gxwebsoft.common.core.web.PageParam;
|
|
|
|
|
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 javax.annotation.Resource;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -19,6 +35,18 @@ import java.util.List;
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
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
|
|
|
|
|
public PageResult<ShopOrder> pageRel(ShopOrderParam param) {
|
|
|
|
|
@@ -44,4 +72,95 @@ public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|