|
@@ -1,24 +1,38 @@
|
|
|
package com.charging.chargingparking.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.charging.chargingparking.entity.*;
|
|
|
+import com.charging.chargingparking.modules.pay.properties.WxPayProperties;
|
|
|
import com.charging.chargingparking.modules.vo.ProfitVo;
|
|
|
-import com.charging.chargingparking.service.ParkingInfoService;
|
|
|
-import com.charging.chargingparking.service.ParkingMerchantService;
|
|
|
-import com.charging.chargingparking.service.ParkingPayProfitService;
|
|
|
+import com.charging.chargingparking.modules.vo.ReceiverVo;
|
|
|
+import com.charging.chargingparking.service.*;
|
|
|
import com.charging.chargingparking.mapper.ParkingPayProfitMapper;
|
|
|
-import com.charging.chargingparking.service.ParkingRecordService;
|
|
|
+import com.ijpay.core.enums.SignType;
|
|
|
+import com.ijpay.core.kit.WxPayKit;
|
|
|
+import com.ijpay.wxpay.WxPayApi;
|
|
|
+import com.ijpay.wxpay.model.ProfitSharingModel;
|
|
|
+import com.ijpay.wxpay.model.ReceiverModel;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class ParkingPayProfitServiceImpl extends ServiceImpl<ParkingPayProfitMapper, ParkingPayProfit>
|
|
|
- implements ParkingPayProfitService{
|
|
|
+ implements ParkingPayProfitService {
|
|
|
|
|
|
@Autowired
|
|
|
private ParkingRecordService parkingRecordService;
|
|
@@ -26,6 +40,12 @@ public class ParkingPayProfitServiceImpl extends ServiceImpl<ParkingPayProfitMap
|
|
|
@Autowired
|
|
|
private ParkingInfoService parkingInfoService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ParkingPayService parkingPayService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WxPayProperties wxPayProperties;
|
|
|
+
|
|
|
@Autowired
|
|
|
private ParkingMerchantService parkingMerchantService;
|
|
|
|
|
@@ -35,28 +55,97 @@ public class ParkingPayProfitServiceImpl extends ServiceImpl<ParkingPayProfitMap
|
|
|
ProfitVo profitVo = new ProfitVo();
|
|
|
profitVo.setProfitFlag(false);
|
|
|
|
|
|
- try{
|
|
|
+ try {
|
|
|
ParkingRecord parkingRecord = parkingRecordService.getById(parkingPay.getParkingRecordId());
|
|
|
//停车场
|
|
|
ParkingInfo parkingInfo = parkingInfoService.getById(parkingRecord.getParkId());
|
|
|
//商户
|
|
|
ParkingMerchant parkingMerchant = parkingMerchantService.getById(parkingInfo.getMerchantId());
|
|
|
- if (parkingMerchant.getProfitFlag()){
|
|
|
- profitVo.setProfitFlag(true);
|
|
|
- profitVo.setProfitScale(parkingMerchant.getProfitScale());
|
|
|
-
|
|
|
+ if (parkingMerchant.getProfitFlag()) {
|
|
|
+
|
|
|
+ //待分账金额
|
|
|
+ BigDecimal waitProfitAmount = parkingPay.getPayAmount().multiply(parkingMerchant.getProfitScale()).setScale(2, RoundingMode.DOWN);
|
|
|
+
|
|
|
+ //分账金额大于0才有分账意义
|
|
|
+ if (waitProfitAmount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ profitVo.setProfitFlag(true);
|
|
|
+ profitVo.setProfitScale(parkingMerchant.getProfitScale());
|
|
|
+ if (parkingPay.getPayStatus() == 20) {
|
|
|
+ profitVo.setSubMchId(parkingMerchant.getSubMchId());
|
|
|
+ profitVo.setProfitAmount(waitProfitAmount);
|
|
|
+ profitVo.setTransactionId(parkingPay.getTransactionNo());
|
|
|
+ profitVo.setOutOrderNo(parkingPay.getOutOrderNo());
|
|
|
+ profitVo.setDescription("分账金来自:" + parkingMerchant.getMerchantName());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- }catch (Exception ex){
|
|
|
- log.error(ex.getMessage(),ex);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(), ex);
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
return profitVo;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void profitPay(ParkingPay parkingPay) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ if (parkingPay.getProfitNeedFlag()) {
|
|
|
+ ProfitVo profitVo = getProfitVo(parkingPay);
|
|
|
+ List<ReceiverModel> list = new ArrayList<>();
|
|
|
+ list.add(ReceiverModel.builder()
|
|
|
+ .type("MERCHANT_ID")
|
|
|
+ .account(wxPayProperties.getSubMchId())
|
|
|
+ .amount(Convert.toInt(profitVo.getProfitAmount().multiply(new BigDecimal(100))))
|
|
|
+ .description(profitVo.getDescription())
|
|
|
+ .build());
|
|
|
+
|
|
|
+ Map<String, String> params = ProfitSharingModel.builder()
|
|
|
+ .sub_mch_id(profitVo.getSubMchId())
|
|
|
+ .appid(wxPayProperties.getAppId())
|
|
|
+ .nonce_str(WxPayKit.generateStr())
|
|
|
+ .transaction_id(profitVo.getTransactionId())
|
|
|
+ .out_order_no(profitVo.getOutOrderNo())
|
|
|
+ .receivers(JSONObject.toJSONString(list))
|
|
|
+ .build()
|
|
|
+ .createSign(wxPayProperties.getMchKey(), SignType.HMACSHA256);
|
|
|
+
|
|
|
+ log.warn("请求分账参数:{}", WxPayKit.toXml(params));
|
|
|
+ String result = WxPayApi.profitSharing(params, wxPayProperties.getCertPath(), wxPayProperties.getMchId());
|
|
|
+ log.warn("请求分账结果:{}", result);
|
|
|
+ Map<String, String> resultMap = WxPayKit.xmlToMap(result);
|
|
|
+ if ("FINISHED".equals(resultMap.get("state"))){
|
|
|
+ parkingPay.setProfitResult("success");
|
|
|
+ }else {
|
|
|
+ parkingPay.setProfitResult("fail");
|
|
|
+ }
|
|
|
+ parkingPayService.updateById(parkingPay);
|
|
|
+
|
|
|
+ String re = JSONObject.toJSONString(resultMap.get("receivers"));
|
|
|
+ List<ReceiverVo> receiverVoList = JSONArray.parseArray(re,ReceiverVo.class);
|
|
|
+
|
|
|
+ ParkingPayProfit parkingPayProfit = new ParkingPayProfit();
|
|
|
+ parkingPayProfit.setParkingPayId(parkingPay.getId());
|
|
|
+ parkingPayProfit.setFundingSourceMchId(profitVo.getSubMchId());
|
|
|
+ parkingPayProfit.setProfitTotalAmount(parkingPay.getPayAmount());
|
|
|
+ parkingPayProfit.setProfitScale(profitVo.getProfitScale());
|
|
|
+ parkingPayProfit.setProfitMchId(wxPayProperties.getSubMchId());
|
|
|
+ parkingPayProfit.setProfitAmount(profitVo.getProfitAmount());
|
|
|
+ parkingPayProfit.setProfitTime(new Date());
|
|
|
+ parkingPayProfit.setProfitOrderId(resultMap.get("order_id"));
|
|
|
+
|
|
|
+ // return JSONObject.toJSONString(WxPayKit.xmlToMap(result));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(), ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|