소스 검색

微信分账功能添加

fulonglong 3 년 전
부모
커밋
1f200a5a41

+ 2 - 0
src/main/java/com/charging/chargingparking/entity/ParkingMerchant.java

@@ -37,6 +37,8 @@ public class ParkingMerchant implements Serializable {
      */
     private String appAuthToken;
 
+    private String openId;
+
     /**
      * 是否分润(0:不分润,1:分润)
      */

+ 55 - 4
src/main/java/com/charging/chargingparking/modules/pay/alipay/AlipayController.java

@@ -8,17 +8,16 @@ import com.alipay.api.AlipayApiException;
 import com.alipay.api.AlipayClient;
 import com.alipay.api.DefaultAlipayClient;
 import com.alipay.api.internal.util.AlipaySignature;
-import com.alipay.api.request.AlipayBossProdAntlawOrderhitstatusQueryRequest;
-import com.alipay.api.request.AlipaySystemOauthTokenRequest;
-import com.alipay.api.request.AlipayTradeCreateRequest;
-import com.alipay.api.request.AlipayTradePrecreateRequest;
+import com.alipay.api.request.*;
 import com.alipay.api.response.AlipaySystemOauthTokenResponse;
 import com.alipay.api.response.AlipayTradeCreateResponse;
 import com.alipay.api.response.AlipayTradePrecreateResponse;
+import com.alipay.api.response.AlipayTradeRefundResponse;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.charging.chargingparking.dto.MessageResult;
 import com.charging.chargingparking.dto.ParkingCostDTO;
 import com.charging.chargingparking.dto.UserInfo;
+import com.charging.chargingparking.entity.ParkingInfo;
 import com.charging.chargingparking.entity.ParkingPay;
 import com.charging.chargingparking.entity.ParkingRecord;
 import com.charging.chargingparking.modules.pay.properties.AliPayProperties;
@@ -281,4 +280,56 @@ public class AlipayController {
 
     }
 
+
+    /**
+     * 支付宝退款
+     * @param id
+     * @return
+     */
+    @PostMapping(value = "refund")
+    public MessageResult refund(String id) {
+        MessageResult result = new MessageResult();
+
+        try{
+
+            ParkingPay parkingPay = parkingPayService.getById(id);
+            if (parkingPay == null) {
+                throw new Exception("订单不存在");
+            }
+            AlipayClient alipayClient = new DefaultAlipayClient(aliPayProperties.getServiceUrl(), aliPayProperties.getAppId(), aliPayProperties.getPrivateKey(), "json", aliPayProperties.getInputCharset(), aliPayProperties.getZfbPublicKey(), aliPayProperties.getSignType());
+            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
+
+
+            Map<String, Object> maps = new HashMap<>();
+            maps.put("out_trade_no", parkingPay.getOutOrderNo());
+            maps.put("refund_reason", "正常退款");
+            maps.put("refund_amount", parkingPay.getPayAmount());
+
+            JSONObject extendParams = new JSONObject();
+
+            extendParams.put("sys_service_provider_id", aliPayProperties.getMchId());
+            maps.put("extend_params", extendParams);
+            request.putOtherTextParam("app_auth_token", aliPayProperties.getAppAuthToken());
+            //把订单信息转换为json对象的字符串
+            String postdata = JSONObject.toJSONString(maps);
+            request.setBizContent(postdata);
+
+
+            AlipayTradeRefundResponse response = alipayClient.execute(request);
+           if ("10000".equals(response.getCode())){
+               return result;
+           }else {
+               throw new Exception(response.getMsg());
+           }
+
+        }catch (Exception ex){
+            log.error(ex.getMessage(),ex);
+            result.setResult(false);
+            result.setCode(400);
+            result.setMessage(ex.getMessage());
+            return result;
+        }
+
+    }
+
 }

+ 75 - 1
src/main/java/com/charging/chargingparking/modules/pay/wechat/WxPayController.java

@@ -25,6 +25,7 @@ import com.ijpay.core.kit.WxPayKit;
 import com.ijpay.wxpay.WxPayApi;
 import com.ijpay.wxpay.model.ProfitSharingModel;
 import com.ijpay.wxpay.model.ReceiverModel;
+import com.ijpay.wxpay.model.RefundModel;
 import com.ijpay.wxpay.model.UnifiedOrderModel;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -34,6 +35,7 @@ import javax.servlet.http.HttpServletRequest;
 import java.math.BigDecimal;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -312,6 +314,71 @@ public class WxPayController {
     }
 
 
+
+    /**
+     * 微信退款
+     * @param id
+     * @return
+     * @throws Exception
+     */
+    @PostMapping(value = "/refund")
+    @ResponseBody
+    public MessageResult refund(String id){
+        MessageResult msgResult = new MessageResult<>();
+
+        try{
+
+            ParkingPay parkingPay = parkingPayService.getById(id);
+
+
+            String totalFee = parkingPay.getPayAmount().multiply(new BigDecimal(100)).setScale(0).toString();
+            Map<String, String> params = RefundModel.builder()
+                    .appid(wxPayProperties.getAppId())
+                    .mch_id(wxPayProperties.getMchId())
+                    .sub_mch_id(wxPayProperties.getSubMchId())
+                    .nonce_str(WxPayKit.generateStr())
+                    .transaction_id(parkingPay.getTransactionNo())
+                    .out_trade_no(parkingPay.getOutOrderNo())
+                    .out_refund_no(WxPayKit.generateStr())
+                    .total_fee(totalFee)
+                    .refund_fee(totalFee)
+                    .notify_url(wxPayProperties.getRefundUrl())
+                    .build()
+                    .createSign(wxPayProperties.getMchKey(), SignType.HMACSHA256);
+            String result = WxPayApi.orderRefund(false, params, wxPayProperties.getCertPath(), wxPayProperties.getMchId());
+            Map<String, String> map = WxPayKit.xmlToMap(result);
+            String returnCode = map.get("return_code");
+            String resultCode = map.get("result_code");
+            if (WxPayKit.codeIsOk(returnCode) && WxPayKit.codeIsOk(resultCode)) {
+                msgResult.setResult(true);
+                return msgResult;
+            } else {
+                // 退款失败
+                throw new Exception("退款失败," + map.get("return_msg") + "," + map.get("err_code_des"));
+            }
+        }catch (Exception ex){
+            msgResult.setCode(400);
+            msgResult.setMessage(ex.getMessage());
+            msgResult.setResult(false);
+            log.error(ex.getMessage(),ex);
+            return msgResult;
+        }
+
+
+    }
+
+    @RequestMapping(value = "/wxJsapiRefundNotify", method = {RequestMethod.POST, RequestMethod.GET})
+    @ResponseBody
+    public MessageResult wxJsapiRefundNotify(HttpServletRequest request){
+        MessageResult msgResult = new MessageResult<>();
+
+        String xmlMsg = HttpKit.readData(request);
+        log.warn("微信退款回调:"+xmlMsg);
+
+        return msgResult;
+    }
+
+
     /**
      * 添加分账接收方
      */
@@ -321,13 +388,20 @@ public class WxPayController {
 
             ParkingMerchant parkingMerchant = parkingMerchantService.getById(merchantId);
 
-            ReceiverModel receiver = ReceiverModel.builder()
+         /*   ReceiverModel receiver = ReceiverModel.builder()
                     .type("MERCHANT_ID")
                     .account(wxPayProperties.getSubMchId())
                     .relation_type("SERVICE_PROVIDER")
+                    .build();*/
+            ReceiverModel receiver = ReceiverModel.builder()
+                    .type("PERSONAL_SUB_OPENID")
+                    .account("oK9Wr59jOWutvzTctzx97oqpxnNI")
+                    .relation_type("STAFF")
                     .build();
 
+
             Map<String, String> params = ProfitSharingModel.builder()
+                    .mch_id(wxPayProperties.getMchId())
                     .sub_mch_id(parkingMerchant.getSubMchId())
                     .appid(wxPayProperties.getAppId())
                     .sub_appid(wxPayProperties.getSubAppId())

+ 17 - 3
src/main/java/com/charging/chargingparking/service/impl/ParkingPayProfitServiceImpl.java

@@ -96,16 +96,27 @@ public class ParkingPayProfitServiceImpl extends ServiceImpl<ParkingPayProfitMap
             if (parkingPay.getProfitNeedFlag()) {
                 ProfitVo profitVo = getProfitVo(parkingPay);
                 List<ReceiverModel> list = new ArrayList<>();
+
+                //测试使用个人用户分账
                 list.add(ReceiverModel.builder()
+                        .type("PERSONAL_SUB_OPENID")
+                        .account("oK9Wr59jOWutvzTctzx97oqpxnNI")
+                        .amount(Convert.toInt(profitVo.getProfitAmount().multiply(new BigDecimal(100))))
+                        .description(profitVo.getDescription())
+                        .build());
+               /* list.add(ReceiverModel.builder()
                         .type("MERCHANT_ID")
                         .account(wxPayProperties.getSubMchId())
                         .amount(Convert.toInt(profitVo.getProfitAmount().multiply(new BigDecimal(100))))
                         .description(profitVo.getDescription())
-                        .build());
+                        .build());*/
+
 
                 Map<String, String> params = ProfitSharingModel.builder()
+                        .mch_id(wxPayProperties.getMchId())
                         .sub_mch_id(profitVo.getSubMchId())
                         .appid(wxPayProperties.getAppId())
+                        .sub_appid(wxPayProperties.getSubAppId())
                         .nonce_str(WxPayKit.generateStr())
                         .transaction_id(profitVo.getTransactionId())
                         .out_order_no(profitVo.getOutOrderNo())
@@ -127,6 +138,10 @@ public class ParkingPayProfitServiceImpl extends ServiceImpl<ParkingPayProfitMap
                 String re  = JSONObject.toJSONString(resultMap.get("receivers"));
                 List<ReceiverVo> receiverVoList = JSONArray.parseArray(re,ReceiverVo.class);
 
+                //默认一个分账用户
+                ReceiverVo receiverVo = receiverVoList.get(0);
+                log.warn("分账结果:"+receiverVo.toString());
+
                 ParkingPayProfit parkingPayProfit = new ParkingPayProfit();
                 parkingPayProfit.setParkingPayId(parkingPay.getId());
                 parkingPayProfit.setFundingSourceMchId(profitVo.getSubMchId());
@@ -136,8 +151,7 @@ public class ParkingPayProfitServiceImpl extends ServiceImpl<ParkingPayProfitMap
                 parkingPayProfit.setProfitAmount(profitVo.getProfitAmount());
                 parkingPayProfit.setProfitTime(new Date());
                 parkingPayProfit.setProfitOrderId(resultMap.get("order_id"));
-
-                //   return JSONObject.toJSONString(WxPayKit.xmlToMap(result));
+                save(parkingPayProfit);
 
             }
 

+ 2 - 1
src/main/resources/mapper/ParkingMerchantMapper.xml

@@ -9,6 +9,7 @@
             <result property="merchantName" column="merchant_name" jdbcType="VARCHAR"/>
             <result property="subMchId" column="sub_mch_id" jdbcType="VARCHAR"/>
             <result property="appAuthToken" column="app_auth_token" jdbcType="VARCHAR"/>
+            <result property="openId" column="open_id" jdbcType="VARCHAR"/>
             <result property="profitFlag" column="profit_flag" jdbcType="BIT"/>
             <result property="profitScale" column="profit_scale" jdbcType="DECIMAL"/>
             <result property="delFlag" column="del_flag" jdbcType="BIT"/>
@@ -20,7 +21,7 @@
 
     <sql id="Base_Column_List">
         id,merchant_name,sub_mch_id,
-        app_auth_token,profit_flag,profit_scale,del_flag,
+        app_auth_token,open_id,profit_flag,profit_scale,del_flag,
         create_by,create_time,update_by,
         update_time
     </sql>