licailing пре 5 година
родитељ
комит
0f1e5e137b

+ 45 - 0
src/main/java/com/izouma/dingdong/domain/FeeRecord.java

@@ -0,0 +1,45 @@
+package com.izouma.dingdong.domain;
+
+import io.swagger.annotations.ApiModel;
+import lombok.*;
+import org.hibernate.annotations.Where;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import java.time.LocalDateTime;
+
+@EqualsAndHashCode(callSuper = false)
+@Data
+@Entity
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+@Where(clause = "enabled = 1")
+@ApiModel(value = "支付表", description = "支付表")
+public class FeeRecord extends BaseEntity {
+    /*
+    mch_order_no
+    mch_refund_no
+     */
+    private String mchNo;
+
+    private Integer totalFee;
+
+    private String channel;
+
+    private String feeType;
+
+    /*
+    支付订单是否退款
+     */
+    private Boolean isRefund;
+
+    /*
+    支付true 退款false
+     */
+    private Boolean isPay;
+
+    @Column(nullable = false)
+    private Boolean enabled = true;
+
+}

+ 99 - 5
src/main/java/com/izouma/dingdong/service/ConsumptionService.java

@@ -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();
+        }
     }
+
+
 }

+ 7 - 7
src/main/resources/application.yaml

@@ -44,16 +44,16 @@ jwt:
     secret: XvAD0kboD76Dpebm
     header: Authorization
     expiration: 2592000 #30days
-#wx:
+wx:
 #    mp:
 #        app_id: wx2375cba2eec2c479
 #        app_secret: 28e4829124860d9ef9e2f32aeefd1111
-#    miniapp:
-#        app_id: wx6517cbf58115c508
-#        app_secret: 8af0e8ba28ffddfb7e574e6c7cb6aaf1
-#        msg_token: msgToken
-#        msg_aes_key: aesKey
-#        msg_format: JSON
+    miniapp:
+        app_id: wx6517cbf58115c508
+        app_secret: 8af0e8ba28ffddfb7e574e6c7cb6aaf1
+        msg_token: msgToken
+        msg_aes_key: aesKey
+        msg_format: JSON
 #    pay:
 #        appId: wx2375cba2eec2c479
 #        mchId: 1529090291