|
@@ -11,15 +11,22 @@ import com.caimei365.order.model.ResponseJson;
|
|
|
import com.caimei365.order.model.bo.PayParamBo;
|
|
|
import com.caimei365.order.model.dto.PayDto;
|
|
|
import com.caimei365.order.model.po.BalanceRecordPo;
|
|
|
+import com.caimei365.order.model.po.DiscernReceiptPo;
|
|
|
+import com.caimei365.order.model.po.OrderReceiptRelationPo;
|
|
|
+import com.caimei365.order.model.po.UserBeansHistoryPo;
|
|
|
import com.caimei365.order.model.vo.DiscernReceiptVo;
|
|
|
+import com.caimei365.order.model.vo.OrderPayLinkVo;
|
|
|
import com.caimei365.order.model.vo.OrderProductVo;
|
|
|
import com.caimei365.order.model.vo.OrderVo;
|
|
|
import com.caimei365.order.service.PayOrderService;
|
|
|
+import com.caimei365.order.service.RemoteCallService;
|
|
|
import com.caimei365.order.utils.MathUtil;
|
|
|
import com.caimei365.order.utils.PayUtil;
|
|
|
+import com.caimei365.order.utils.pay.HttpRequest;
|
|
|
import com.caimei365.order.utils.pay.RSAUtil;
|
|
|
import com.google.common.util.concurrent.AtomicDouble;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.ArrayUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -27,6 +34,9 @@ import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.spec.InvalidKeySpecException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
@@ -57,6 +67,8 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
private WeChatService weChatService;
|
|
|
@Value("${caimei.notifyUrl}")
|
|
|
private String notifyUrl;
|
|
|
+ @Resource
|
|
|
+ private RemoteCallService remoteCallService;
|
|
|
|
|
|
/**
|
|
|
* 订单支付前效验付款规则
|
|
@@ -391,4 +403,247 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
}
|
|
|
return ResponseJson.success(result);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付异步通知回调
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String paymentCallback(String data) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
|
|
+ log.info("******************** 支付异步回调 start *******************");
|
|
|
+ // 公钥解密
|
|
|
+ JSONObject json = PayUtil.decryptDataPublic(data, PayUtil.publicKey);
|
|
|
+ log.info("公钥解密>>>>>>" + json);
|
|
|
+ // 公钥验签
|
|
|
+ String signaa = json.getString("sign");
|
|
|
+ json.remove("sign");
|
|
|
+ String signbb = PayUtil.getPaySign(json, PayUtil.publicKey);
|
|
|
+ if (!signaa.equals(signbb)) {
|
|
|
+ return "验签失败";
|
|
|
+ }
|
|
|
+ // 订单状态
|
|
|
+ String orderStatus = json.getString("orderStatus");
|
|
|
+ // 附加数据,下单时若有传输则原样返回,下单时为空,则不返回该数据
|
|
|
+ String attach = json.getString("attach");
|
|
|
+ // 平台唯一流水号
|
|
|
+ String mbOrderId = json.getString("mbOrderId");
|
|
|
+ // 商户唯一订单号
|
|
|
+ String orderRequestNo = json.getString("orderId");
|
|
|
+ // 订单金额,以元为单位
|
|
|
+ Double amount = json.getDouble("amount");
|
|
|
+ log.info("订单状态>>>>>>" + orderStatus);
|
|
|
+ if ("FAILED".equals(orderStatus)) {
|
|
|
+ return "支付失败";
|
|
|
+ }
|
|
|
+ String[] split = attach.split(",");
|
|
|
+ //订单id
|
|
|
+ Integer orderId = Integer.valueOf(split[0]);
|
|
|
+ //支付类型
|
|
|
+ String payType = split[1];
|
|
|
+ // 订单信息
|
|
|
+ OrderVo order = payOrderMapper.getOrderByOrderId(orderId);
|
|
|
+ if (null == order) {
|
|
|
+ return "订单不存在";
|
|
|
+ }
|
|
|
+ // 支付记录
|
|
|
+ List<DiscernReceiptVo> discernReceiptList = orderCommonMapper.getDiscernReceipt(order.getOrderId());
|
|
|
+ if (!discernReceiptList.isEmpty()) {
|
|
|
+ AtomicDouble receiptAmount = new AtomicDouble(0d);
|
|
|
+ discernReceiptList.forEach(discernReceipt -> {
|
|
|
+ if (3 == discernReceipt.getReceiptStatus()) {
|
|
|
+ receiptAmount.set(MathUtil.add(receiptAmount.get(), discernReceipt.getAssociateAmount()).doubleValue());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ order.setReceiptAmount(receiptAmount.get());
|
|
|
+ }
|
|
|
+ log.info("【支付异步回调】>>>>>>>>>>>>>>已付金额+本次支付金额:" + order.getReceiptAmount());
|
|
|
+ Date date = new Date();
|
|
|
+ String curDateStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
|
|
|
+ if (MathUtil.compare(order.getPayableAmount(), order.getReceiptAmount()) == 0) {
|
|
|
+ /*
|
|
|
+ * 订单全部支付
|
|
|
+ * 0待确认,11待收待发,12待收部发,13待收全发,21部收待发,22部收部发,23部收全发,
|
|
|
+ * 31已收待发,32已收部发,33已收全发,4交易完成,5订单完成,6已关闭,7交易全退
|
|
|
+ */
|
|
|
+ if (11 == order.getStatus() || 21 == order.getStatus()) {
|
|
|
+ order.setStatus(31);
|
|
|
+ } else if (12 == order.getStatus() || 22 == order.getStatus()) {
|
|
|
+ order.setStatus(32);
|
|
|
+ } else {
|
|
|
+ order.setStatus(33);
|
|
|
+ }
|
|
|
+ order.setPayFlag(1);
|
|
|
+ order.setOnlinePayFlag(0);
|
|
|
+ //(收款买家)收款状态:1待收款、2部分收款、3已收款
|
|
|
+ order.setReceiptStatus(3);
|
|
|
+ log.info("【支付异步回调】>>>>>>>>>>>>>>订单(全部支付),修改订单状态:" + order.getStatus() + ",orderId:" + orderId);
|
|
|
+ } else {
|
|
|
+ // 部分支付
|
|
|
+ if (11 == order.getStatus() || 21 == order.getStatus()) {
|
|
|
+ order.setStatus(21);
|
|
|
+ } else if (12 == order.getStatus() || 22 == order.getStatus()) {
|
|
|
+ order.setStatus(22);
|
|
|
+ } else {
|
|
|
+ order.setStatus(23);
|
|
|
+ }
|
|
|
+ order.setOnlinePayFlag(0);
|
|
|
+ //(收款买家)收款状态:1待收款、2部分收款、3已收款
|
|
|
+ order.setReceiptStatus(2);
|
|
|
+ log.info("【支付异步回调】>>>>>>>>>>>>>>订单(部分支付),修改订单状态:" + order.getStatus() + ",orderId:" + orderId);
|
|
|
+ }
|
|
|
+ // 更新付款次数
|
|
|
+ Integer paySuccessCounter = (null == order.getPaySuccessCounter()) ? 1 : order.getPaySuccessCounter() +1;
|
|
|
+ order.setPaySuccessCounter(paySuccessCounter);
|
|
|
+ order.setUpdateDate(curDateStr);
|
|
|
+ // 更新订单支付状态
|
|
|
+ payOrderMapper.updateOrderStatus(order);
|
|
|
+ // 修改支付链接状态
|
|
|
+ OrderPayLinkVo orderPayLink = payOrderMapper.getOrderPayLink(orderId, amount);
|
|
|
+ if (null != orderPayLink && ("12".equals(payType) || "17".equals(payType))) {
|
|
|
+ orderPayLink.setPayStatus(1);
|
|
|
+ payOrderMapper.updateOrderPayLinkStatus(orderPayLink);
|
|
|
+ }
|
|
|
+ // 保存收款记录
|
|
|
+ DiscernReceiptPo discernReceipt = new DiscernReceiptPo();
|
|
|
+ discernReceipt.setPayWay(1);
|
|
|
+ discernReceipt.setPayType(Integer.valueOf(payType));
|
|
|
+ discernReceipt.setReceiptType(1);
|
|
|
+ discernReceipt.setReceiptStatus(3);
|
|
|
+ discernReceipt.setReceiptAmount(amount);
|
|
|
+ discernReceipt.setConfirmType(4);
|
|
|
+ discernReceipt.setRePayFlag(1);
|
|
|
+ discernReceipt.setFormData(json.toJSONString());
|
|
|
+ discernReceipt.setReceiptDate(curDateStr);
|
|
|
+ discernReceipt.setConfirmDate(curDateStr);
|
|
|
+ discernReceipt.setReviewDate(curDateStr);
|
|
|
+ discernReceipt.setUpdateDate(curDateStr);
|
|
|
+ discernReceipt.setDelFlag(0);
|
|
|
+ // 保存 收款记录
|
|
|
+ baseMapper.insertDiscernReceipt(discernReceipt);
|
|
|
+ log.info("【支付异步回调】>>>>>>>>>>>保存识别款项(insert[cm_discern_receipt])id:" + discernReceipt.getId() + ",orderId:" + orderId);
|
|
|
+ // 收款项和订单关系表
|
|
|
+ OrderReceiptRelationPo relation = new OrderReceiptRelationPo();
|
|
|
+ relation.setReceiptId(discernReceipt.getId());
|
|
|
+ relation.setOrderId(orderId);
|
|
|
+ relation.setAssociateAmount(amount);
|
|
|
+ relation.setMbOrderId(mbOrderId);
|
|
|
+ relation.setOrderRequestNo(orderRequestNo);
|
|
|
+ relation.setSplitStatus(0);
|
|
|
+ relation.setRelationType(2);
|
|
|
+ relation.setDelFlag(0);
|
|
|
+ // 保存 收款项和订单关系
|
|
|
+ baseMapper.insertOrderReceiptRelation(relation);
|
|
|
+ log.info("【支付异步回调】>>>>>>>>>>>收款项和订单关系(insert[cm_receipt_order_relation])id:" + relation.getId() + ",orderId:" + orderId);
|
|
|
+
|
|
|
+ // 商品数据
|
|
|
+ List<OrderProductVo> orderProductList = orderCommonMapper.getOrderProductByOrderId(orderId);
|
|
|
+ // 判断是否是充值商品
|
|
|
+ Integer rechargeFlag = 0;
|
|
|
+ // 缴纳订金订单
|
|
|
+ int[] productId1 = {6060, 6061, 6062, 6063, 6064};
|
|
|
+ // 充值余额订单
|
|
|
+ int[] productId2 = {6065, 6066, 6067, 6068, 6069};
|
|
|
+ for (OrderProductVo product : orderProductList) {
|
|
|
+ if (ArrayUtils.contains(productId1, product.getProductId())) {
|
|
|
+ rechargeFlag = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (ArrayUtils.contains(productId2, product.getProductId())) {
|
|
|
+ rechargeFlag = 2;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (rechargeFlag > 0) {
|
|
|
+ // 账户余额
|
|
|
+ double oldUserMoney = baseMapper.getUserMoney(order.getUserId());
|
|
|
+ double userMoney = MathUtil.add(oldUserMoney, amount).doubleValue();
|
|
|
+ // 可用余额
|
|
|
+ Double oldAvailableMoney = baseMapper.getAbleUserMoney(order.getUserId());
|
|
|
+ double availableMoney = MathUtil.add(oldAvailableMoney, amount).doubleValue();
|
|
|
+ payOrderMapper.updateMoneyByUserId(userMoney, availableMoney, order.getUserId());
|
|
|
+ log.info("【支付异步回调】>>>>>>>>>>>更新用户余额(update[user])userId:" + order.getUserId() + ",orderId:" + orderId);
|
|
|
+ //保存余额到余额收支记录
|
|
|
+ BalanceRecordPo balanceRecord = new BalanceRecordPo();
|
|
|
+ balanceRecord.setUserId(order.getUserId());
|
|
|
+ balanceRecord.setType(1);
|
|
|
+ if (rechargeFlag == 1) {
|
|
|
+ balanceRecord.setBalanceType(6);
|
|
|
+ } else {
|
|
|
+ balanceRecord.setBalanceType(7);
|
|
|
+ }
|
|
|
+ balanceRecord.setAddDate(new Date());
|
|
|
+ balanceRecord.setAmount(amount);
|
|
|
+ balanceRecord.setOrderId(orderId);
|
|
|
+ balanceRecord.setRemark("订单商品充值余额");
|
|
|
+ balanceRecord.setDelFlag(0);
|
|
|
+ // 保存 余额收支记录
|
|
|
+ baseMapper.insertBalanceRecord(balanceRecord);
|
|
|
+ log.info("【支付异步回调】>>>>>>>>>>>>>>>>>>>>>>>>>>订单商品充值余额(insert[cm_user_balance_record])orderId:" + orderId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 线上支付与自主下单送豆
|
|
|
+ if (3 == order.getReceiptStatus() && 0 == order.getSecondHandOrderFlag()) {
|
|
|
+ UserBeansHistoryPo beansHistory = new UserBeansHistoryPo();
|
|
|
+ beansHistory.setUserId(order.getUserId());
|
|
|
+ beansHistory.setOrderId(orderId);
|
|
|
+ beansHistory.setBeansType(6);
|
|
|
+ beansHistory.setType(1);
|
|
|
+ beansHistory.setNum(200);
|
|
|
+ beansHistory.setPushStatus(0);
|
|
|
+ beansHistory.setAddTime(date);
|
|
|
+ beansHistory.setDelFlag(0);
|
|
|
+ // 保存 采美豆使用记录
|
|
|
+ baseMapper.insertBeansHistory(beansHistory);
|
|
|
+ // 用户采美豆
|
|
|
+ Integer userBeans = baseMapper.getUserBeans(order.getUserId());
|
|
|
+ // 更新用户剩余采美豆数量
|
|
|
+ userBeans = userBeans + beansHistory.getNum();
|
|
|
+
|
|
|
+ // 自主下单且没有促销活动送豆
|
|
|
+ Integer orderPromotionsId = payOrderMapper.getOrderPromotionsId(order.getOrderId());
|
|
|
+ if (1 == order.getOrderType() && null == orderPromotionsId) {
|
|
|
+ Double payTotalFee = order.getPayTotalFee();
|
|
|
+ int num;
|
|
|
+ if (MathUtil.compare(1000, payTotalFee) > -1) {
|
|
|
+ num = 0;
|
|
|
+ } else if (MathUtil.compare(5000, payTotalFee) > -1) {
|
|
|
+ num = 1000;
|
|
|
+ } else if (MathUtil.compare(10000, payTotalFee) > -1) {
|
|
|
+ num = 2500;
|
|
|
+ } else if (MathUtil.compare(25000, payTotalFee) > -1) {
|
|
|
+ num = 5000;
|
|
|
+ } else if (MathUtil.compare(100000, payTotalFee) > -1) {
|
|
|
+ num = 7500;
|
|
|
+ } else if (MathUtil.compare(250000, payTotalFee) > -1) {
|
|
|
+ num = 10000;
|
|
|
+ } else if (MathUtil.compare(500000, payTotalFee) > -1) {
|
|
|
+ num = 12500;
|
|
|
+ } else {
|
|
|
+ num = 20000;
|
|
|
+ }
|
|
|
+ beansHistory.setBeansType(5);
|
|
|
+ beansHistory.setNum(num);
|
|
|
+ if (num > 0) {
|
|
|
+ // 保存 采美豆使用记录
|
|
|
+ baseMapper.insertBeansHistory(beansHistory);
|
|
|
+ // 更新用户剩余采美豆数量
|
|
|
+ userBeans = userBeans + beansHistory.getNum();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ baseMapper.updateUserBeans(beansHistory.getUserId(), userBeans);
|
|
|
+ log.info("【支付异步回调】>>>>>更新用户采美豆(update[user(userBeans:"+ userBeans +")]),userId:" + beansHistory.getUserId());
|
|
|
+ }
|
|
|
+ // 已支付短信推送
|
|
|
+ boolean smsPushFlag = !orderRequestNo.contains("BETA") && !orderRequestNo.contains("DEV");
|
|
|
+ String bindMobile = baseMapper.getBindMobileByUserId(order.getUserId());
|
|
|
+ if (smsPushFlag && StringUtils.isNotBlank(bindMobile)) {
|
|
|
+ String shortLink = remoteCallService.getShortLink(8, 6, "https://www.caimei365.com/user/order/detail.html?orderId=" + orderId);
|
|
|
+ String content = "您已成功支付订单(订单编号:" + order.getOrderNo() + ")全部款项,支付总金额¥" + order.getPayTotalFee() + ",采美平台将立即安排发货。您可关注采美公众号或者访问采美微信小程序和网站查看订单。" +
|
|
|
+ "平台公众号:微信搜索“采美365网”; 微信小程序:微信搜索“采美采购商城”;网址:www.caimei365.com/t/" + shortLink;
|
|
|
+ boolean sendSms = remoteCallService.getSendSms(6, bindMobile, content);
|
|
|
+ if (!sendSms) {
|
|
|
+ log.info("取消订单推送失败,orderId>>>>" + orderId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "SUCCESS";
|
|
|
+ }
|
|
|
}
|