|
|
@@ -1,6 +1,9 @@
|
|
|
package com.izouma.dingdong.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.izouma.dingdong.config.KsherPaySdk;
|
|
|
+import com.izouma.dingdong.config.WxPayProperties;
|
|
|
+import com.izouma.dingdong.domain.FeeRecord;
|
|
|
import com.izouma.dingdong.domain.OrderInfo;
|
|
|
import com.izouma.dingdong.enums.PayMethod;
|
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
|
@@ -21,8 +24,7 @@ public class ConsumptionService {
|
|
|
@Value("${ksher.private_key}")
|
|
|
private String privateKey;
|
|
|
|
|
|
-/* @Value("${wx.mp.app_id}")
|
|
|
- private String channelSubAppid;*/
|
|
|
+ private WxPayProperties properties;
|
|
|
|
|
|
/*
|
|
|
支付类型
|
|
|
@@ -31,6 +33,10 @@ public class ConsumptionService {
|
|
|
*/
|
|
|
private String feeType = "CNY";
|
|
|
|
|
|
+ public ConsumptionService(WxPayProperties properties) {
|
|
|
+ this.properties = properties;
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
app支付 只能微信和支付宝
|
|
|
*/
|
|
|
@@ -47,9 +53,14 @@ public class ConsumptionService {
|
|
|
channel = "alipay";
|
|
|
}
|
|
|
|
|
|
+ String subOpenid = null;
|
|
|
+ if (orderInfo.getUser().getOpenId() != null) {
|
|
|
+ subOpenid = orderInfo.getUser().getOpenId();
|
|
|
+ }
|
|
|
try {
|
|
|
//调app支付
|
|
|
- ksherPaySdk.AppPay(mchOrderNo, feeType, channel, null, null, total);
|
|
|
+ String request = ksherPaySdk.AppPay(mchOrderNo, feeType, channel, subOpenid, properties.getAppId(), total);
|
|
|
+ success(mchOrderNo, channel, total, request);
|
|
|
} catch (Exception e) {
|
|
|
log.info("支付异常", e);
|
|
|
throw new BusinessException(e.getMessage());
|
|
|
@@ -65,7 +76,90 @@ public class ConsumptionService {
|
|
|
//商家产生的唯一no
|
|
|
String mchOrderNo = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
|
|
|
//支付方式
|
|
|
- String channel = "ktbcard";
|
|
|
- //ksherPaySdk.GatewayPay(mchOrderNo,feeType)
|
|
|
+ String channelList = "ktbcard,alipay,wechat";
|
|
|
+ //商家方的订单号
|
|
|
+ String mchCode = orderInfo.getOrderNo();
|
|
|
+ //总数 分为单位
|
|
|
+ Integer total = orderInfo.getRealAmount().multiply(BigDecimal.valueOf(100)).intValue();
|
|
|
+ //设备
|
|
|
+ String device = "H5";
|
|
|
+ //内容
|
|
|
+ String productName = "购买商品";
|
|
|
+ try {
|
|
|
+ String request = ksherPaySdk.GatewayPay(mchOrderNo, feeType, channelList, mchCode, null, null, productName, null, device, total);
|
|
|
+ success(mchOrderNo, channelList, total, request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("支付异常", e);
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /*
|
|
|
+ 小程序支付 只能微信和支付宝
|
|
|
+ */
|
|
|
+ public void miniproPay(OrderInfo orderInfo) {
|
|
|
+ //ksher支付SDK
|
|
|
+ KsherPaySdk ksherPaySdk = new KsherPaySdk(appid, privateKey);
|
|
|
+ //商家产生的唯一no
|
|
|
+ String mchOrderNo = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
|
|
|
+ //总数 分为单位
|
|
|
+ Integer total = orderInfo.getRealAmount().multiply(BigDecimal.valueOf(100)).intValue();
|
|
|
+ //支付方式
|
|
|
+ String channel = "wechat";
|
|
|
+ if (PayMethod.ALI_PAY.equals(orderInfo.getPayMethod())) {
|
|
|
+ channel = "alipay";
|
|
|
+ }
|
|
|
+
|
|
|
+ String subOpenid = null;
|
|
|
+ if (orderInfo.getUser().getOpenId() != null) {
|
|
|
+ subOpenid = orderInfo.getUser().getOpenId();
|
|
|
+ } else {
|
|
|
+ throw new BusinessException("openId为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //调小程序支付
|
|
|
+ String request = ksherPaySdk.MiniproPay(mchOrderNo, feeType, channel, subOpenid, properties.getAppId(), total);
|
|
|
+ success(mchOrderNo, channel, total, request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("支付异常", e);
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ 订单退款
|
|
|
+ */
|
|
|
+ public void refund(FeeRecord feeRecord) {
|
|
|
+ //ksher支付SDK
|
|
|
+ KsherPaySdk ksherPaySdk = new KsherPaySdk(appid, privateKey);
|
|
|
+ String mchRefundNo = String.valueOf(new SnowflakeIdWorker(1, 1).nextId());
|
|
|
+ try {
|
|
|
+ //全部退款
|
|
|
+ String request = ksherPaySdk.OrderRefund(mchRefundNo, feeType, feeRecord.getMchNo(), feeRecord.getTotalFee(), feeRecord
|
|
|
+ .getTotalFee());
|
|
|
+ success(mchRefundNo, feeRecord.getChannel(), feeRecord.getTotalFee(), request);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("退款异常", e);
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void success(String mchOrderNo, String channel, Integer total, String request) {
|
|
|
+ JSONObject json = JSONObject.parseObject(request);
|
|
|
+ String result = json.getJSONObject("data").getString("result");
|
|
|
+ if ("SUCCESS".equals(result)) {
|
|
|
+ FeeRecord.builder()
|
|
|
+ .channel(channel)
|
|
|
+ .feeType(feeType)
|
|
|
+ .isRefund(false)
|
|
|
+ .totalFee(total)
|
|
|
+ .isPay(true)
|
|
|
+ .mchNo(mchOrderNo)
|
|
|
+ .enabled(true)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|