|
@@ -2,6 +2,7 @@ package com.miaxis.wx.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.github.wxpay.sdk.WXPay;
|
|
|
import com.github.wxpay.sdk.WXPayConfig;
|
|
|
import com.github.wxpay.sdk.WXPayConstants;
|
|
@@ -14,8 +15,14 @@ import com.miaxis.common.enums.OrderStatusEnum;
|
|
|
import com.miaxis.common.exception.CustomException;
|
|
|
import com.miaxis.common.utils.RSAUtils;
|
|
|
import com.miaxis.common.utils.SecurityUtils;
|
|
|
+import com.miaxis.common.utils.StringUtils;
|
|
|
import com.miaxis.common.utils.XmlUtil;
|
|
|
+import com.miaxis.order.domain.OrderInfo;
|
|
|
+import com.miaxis.order.domain.OrderSplit;
|
|
|
+import com.miaxis.order.service.IOrderInfoService;
|
|
|
+import com.miaxis.order.service.IOrderSplitService;
|
|
|
import com.miaxis.system.service.ISysConfigService;
|
|
|
+import com.miaxis.system.service.ISysUserService;
|
|
|
import com.miaxis.user.service.IUserInfoService;
|
|
|
import com.miaxis.wx.domain.WxExtract;
|
|
|
import com.miaxis.wx.domain.WxExtractBank;
|
|
@@ -36,6 +43,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.TreeMap;
|
|
@@ -57,6 +65,15 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
|
|
|
@Autowired
|
|
|
private IWxExtractService wxExtractService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IOrderInfoService orderInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IOrderSplitService orderSplitService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RSAUtils rsaUtils;
|
|
|
|
|
@@ -71,9 +88,142 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
|
|
|
@Autowired
|
|
|
private IWxExtractBankService wxExtractBankService;
|
|
|
|
|
|
- @Value("${gzh.appid}")
|
|
|
- private String appid;
|
|
|
+ @Value("${xcx.appId}")
|
|
|
+ private String xcxAppid;
|
|
|
+
|
|
|
+ @Value("${xcx.mchId}")
|
|
|
+ private String mchId;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public boolean wxAddFenZhang(OrderInfo orderInfo) throws Exception {
|
|
|
+ //查询上级
|
|
|
+ Map<String, Object> accountMap = getAccount(orderInfo);
|
|
|
+ String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
|
|
|
+
|
|
|
+ String nonceStr = RandomStringUtils.randomAlphanumeric(32);
|
|
|
+ //用于签名
|
|
|
+ Map<String, String> sortMap = new TreeMap<String, String>();
|
|
|
+ sortMap.put("mch_id", mchId);
|
|
|
+ sortMap.put("appid", xcxAppid);
|
|
|
+ sortMap.put("nonce_str", nonceStr);
|
|
|
+ sortMap.put("sign_type", "HMAC-SHA256");
|
|
|
+ Map<String, String> receiverMap = new TreeMap<String, String>();
|
|
|
+ receiverMap.put("type", "PERSONAL_OPENID");
|
|
|
+
|
|
|
+ if (accountMap != null) {
|
|
|
+ String account = (String) accountMap.get("account");
|
|
|
+ if (StringUtils.isNotEmpty(account)) { //如果存在上级,分给上给
|
|
|
+ receiverMap.put("account", account);
|
|
|
+ } else { //如果不存在上级,分给固定的
|
|
|
+ receiverMap.put("account", "o7N3j5XxUrgOKE4ulP-HseFpBCiM");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ receiverMap.put("relation_type", "SERVICE_PROVIDER"); //合作合伙
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ String receiverMapJson = objectMapper.writeValueAsString(receiverMap);
|
|
|
+ sortMap.put("receiver", receiverMapJson);
|
|
|
|
|
|
+ WXPayConfig config = new WxPayConfigImpl();
|
|
|
+ String sign = WXPayUtil.generateSignature(sortMap, config.getKey(), WXPayConstants.SignType.HMACSHA256);
|
|
|
+ sortMap.put("sign", sign);
|
|
|
+
|
|
|
+ WXPay pay = new WXPay(config);
|
|
|
+ String xmlStr = pay.requestWithoutCert(url, sortMap, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs());
|
|
|
+
|
|
|
+ Map<String, String> resMap = XmlUtil.xmlToMap(xmlStr);
|
|
|
+ if (!"SUCCESS".equals(resMap.get("return_code")) || !"SUCCESS".equals(resMap.get("result_code"))) {
|
|
|
+ // throw new CustomException("微信添加分账出错");
|
|
|
+ log.info("微信添加分账出错");
|
|
|
+ log.info(resMap.toString());
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public boolean wxProfitsharing(OrderInfo orderInfo) throws Exception {
|
|
|
+ String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
|
|
|
+ String nonceStr = RandomStringUtils.randomAlphanumeric(32);
|
|
|
+ //用于签名
|
|
|
+ Map<String, String> sortMap = new TreeMap<String, String>();
|
|
|
+ sortMap.put("mch_id", mchId);
|
|
|
+ sortMap.put("appid", xcxAppid);
|
|
|
+ sortMap.put("nonce_str", nonceStr);
|
|
|
+ sortMap.put("sign_type", "HMAC-SHA256");
|
|
|
+ sortMap.put("transaction_id", orderInfo.getTransactionId());
|
|
|
+ String orderCode = getOrderCode(orderInfo.getUserId()); //商户分账单号
|
|
|
+ sortMap.put("out_order_no", orderCode);
|
|
|
+
|
|
|
+ //扣除手续费后
|
|
|
+ double commission = orderInfo.getTotal() * 0.01;
|
|
|
+ double total = orderInfo.getTotal() - commission;
|
|
|
+ int amount = (int) Math.floor(total * 0.3);
|
|
|
+ Map<String, Object> receiverMap = new TreeMap<String, Object>();
|
|
|
+ receiverMap.put("type", "PERSONAL_OPENID");
|
|
|
+
|
|
|
+ //获取上级
|
|
|
+ Map<String, Object> accountMap = getAccount(orderInfo);
|
|
|
+ String account = null;
|
|
|
+ Integer openIdType = null;
|
|
|
+ if (accountMap != null) {
|
|
|
+ account = (String) accountMap.get("account");
|
|
|
+ openIdType = (Integer) accountMap.get("openIdType");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(account)) { //如果存在上级,分给上给
|
|
|
+ receiverMap.put("account", account);
|
|
|
+ } else { //如果不存在上级,分给固定的
|
|
|
+ receiverMap.put("account", "o7N3j5XxUrgOKE4ulP-HseFpBCiM");
|
|
|
+ }
|
|
|
+
|
|
|
+ receiverMap.put("amount", amount);
|
|
|
+ receiverMap.put("description", "分到个人"); //合作合伙
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ String receiverMapJson = objectMapper.writeValueAsString(receiverMap);
|
|
|
+ sortMap.put("receivers", receiverMapJson);
|
|
|
+
|
|
|
+ WXPayConfig config = new WxPayConfigImpl();
|
|
|
+ String sign = WXPayUtil.generateSignature(sortMap, config.getKey(), WXPayConstants.SignType.HMACSHA256);
|
|
|
+ sortMap.put("sign", sign);
|
|
|
+
|
|
|
+ WXPay pay = new WXPay(config);
|
|
|
+ String xmlStr = pay.requestWithCert(url, sortMap, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs());
|
|
|
+ Map<String, String> resMap = XmlUtil.xmlToMap(xmlStr);
|
|
|
+ if (!"SUCCESS".equals(resMap.get("return_code")) || !"SUCCESS".equals(resMap.get("result_code"))) {
|
|
|
+ //throw new CustomException("微信分账出错");
|
|
|
+ log.info("微信分账出错");
|
|
|
+ log.info(resMap.toString());
|
|
|
+ orderInfo.setProfitSharing(3);
|
|
|
+ orderInfoService.updateById(orderInfo);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //更新成交订单表
|
|
|
+ orderInfo.setProfitSharing(2);
|
|
|
+ orderInfoService.updateById(orderInfo);
|
|
|
+
|
|
|
+ OrderSplit orderSplit = new OrderSplit();
|
|
|
+ orderSplit.setOutSplitNo(orderCode);
|
|
|
+ orderSplit.setOutTradeNo(orderInfo.getOutTradeNo());
|
|
|
+ orderSplit.setAmount(amount);
|
|
|
+ orderSplit.setOpenId(account);
|
|
|
+ if (openIdType!=null) {
|
|
|
+ orderSplit.setOpenIdType(openIdType);
|
|
|
+ } else {
|
|
|
+ orderSplit.setOpenIdType(2);
|
|
|
+ }
|
|
|
+ orderSplit.setDescription("分到个人");
|
|
|
+ return orderSplitService.save(orderSplit);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
@Override
|
|
@@ -92,7 +242,7 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
|
|
|
// }
|
|
|
|
|
|
Map<String, String> sortMap = new TreeMap<String, String>();
|
|
|
- sortMap.put("mch_appid",appid);
|
|
|
+ sortMap.put("mch_appid",xcxAppid);
|
|
|
sortMap.put("mchid",wxpayConfig.getMerchantId());
|
|
|
sortMap.put("nonce_str", RandomStringUtils.randomAlphanumeric(32));
|
|
|
sortMap.put("partner_trade_no",getOrderCode(null));
|
|
@@ -193,5 +343,40 @@ public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> impl
|
|
|
return resMap.get("return_msg");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询出上级unionId
|
|
|
+ *
|
|
|
+ * @param orderInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, Object> getAccount(OrderInfo orderInfo) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<String, Object>();
|
|
|
+
|
|
|
+ String account = null;
|
|
|
+ UserInfo userInfo = sysUserService.getStudentByUnionId(orderInfo.getUnionId());
|
|
|
+ UserInfo pUserInfo = null;
|
|
|
+ if (userInfo.getPUnionId() != null) {
|
|
|
+ pUserInfo = sysUserService.getStudentByUnionId(userInfo.getPUnionId());
|
|
|
+ }
|
|
|
+ if (pUserInfo != null) {
|
|
|
+ if (StringUtils.isNotEmpty(pUserInfo.getXcxOpenid())) {
|
|
|
+ account = pUserInfo.getXcxOpenid();
|
|
|
+ resultMap.put("account", account);
|
|
|
+ resultMap.put("openIdType", 2);
|
|
|
+ } else if (StringUtils.isNotEmpty(pUserInfo.getGzhOpenid())) {
|
|
|
+ account = pUserInfo.getGzhOpenid();
|
|
|
+ resultMap.put("account", account);
|
|
|
+ resultMap.put("openIdType", 3);
|
|
|
+ } else if (StringUtils.isNotEmpty(pUserInfo.getAppOpenid())) {
|
|
|
+ account = pUserInfo.getAppOpenid();
|
|
|
+ resultMap.put("account", account);
|
|
|
+ resultMap.put("openIdType", 1);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|