|
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zanxiang.common.constant.Constants;
|
|
|
import com.zanxiang.common.enums.HttpStatusEnum;
|
|
|
import com.zanxiang.common.exception.BaseException;
|
|
|
+import com.zanxiang.module.util.DateUtil;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
import com.zanxiang.module.util.bean.BeanUtil;
|
|
|
import com.zanxiang.module.util.pojo.ResultVO;
|
|
@@ -12,6 +13,7 @@ import com.zanxiang.module.web.util.IpUtil;
|
|
|
import com.zanxiang.mybatis.entity.PayApplication;
|
|
|
import com.zanxiang.mybatis.entity.PayMerchant;
|
|
|
import com.zanxiang.mybatis.mapper.PayApplicationMapper;
|
|
|
+import com.zanxiang.sdk.constant.RedisKeyConstant;
|
|
|
import com.zanxiang.sdk.constant.WxPayConstants;
|
|
|
import com.zanxiang.sdk.domain.dto.PayApplicationDTO;
|
|
|
import com.zanxiang.sdk.domain.dto.PayBoxDTO;
|
|
@@ -22,6 +24,7 @@ import com.zanxiang.sdk.service.IPayBoxService;
|
|
|
import com.zanxiang.sdk.service.IPayMerchantService;
|
|
|
import com.zanxiang.sdk.service.api.WxApiService;
|
|
|
import com.zanxiang.sdk.util.HttpUtil;
|
|
|
+import com.zanxiang.sdk.util.RedisUtil;
|
|
|
import com.zanxiang.sdk.util.WxPayUtil;
|
|
|
import com.zanxiang.sdk.util.XmlUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -62,6 +65,9 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
|
@Autowired
|
|
|
private IPayBoxService payBoxService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil<String> redisUtil;
|
|
|
+
|
|
|
private static final String WX_PAY_JSAPI = "JSAPI";
|
|
|
|
|
|
private static final String SIGN_TYPE = "MD5";
|
|
@@ -129,6 +135,8 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
|
Map<String, String> successMap = this.unifiedOrder(paramMap);
|
|
|
//构造支付参数
|
|
|
Map<Object, Object> payParamMap = this.payParamMap(orderId, payApplication.getAppId(), payConfigMap.get("apiKey"), successMap);
|
|
|
+ //添加过期缓存
|
|
|
+ this.orderExpire(orderId, payApplication.getAppId());
|
|
|
//返回
|
|
|
return ResultVO.ok(payParamMap);
|
|
|
} catch (Exception e) {
|
|
@@ -137,6 +145,16 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void orderExpire(String orderId, String appId) {
|
|
|
+ //过期时间
|
|
|
+ double expire = (double) DateUtil.localDateTimeToMilli(LocalDateTime.now().plusMinutes(5));
|
|
|
+ //参数
|
|
|
+ String param = orderId + "_" + appId;
|
|
|
+ //设置缓存
|
|
|
+ redisUtil.addZSet(RedisKeyConstant.ORDER_EXPIRE, param, expire);
|
|
|
+ log.error("商城小程序订单添加过期缓存, orderId : {}, expire : {}", orderId, expire);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String appletStoreNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
// 读取参数
|
|
@@ -248,4 +266,60 @@ public class PayApplicationServiceImpl extends ServiceImpl<PayApplicationMapper,
|
|
|
throw new BaseException("微信小程序支付下单异常");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void appletStoreCancel(String param) {
|
|
|
+ //参数切割
|
|
|
+ String[] split = param.split("_");
|
|
|
+ //小程序appId
|
|
|
+ String appId = split[0];
|
|
|
+ String orderId = split[1];
|
|
|
+ //查询商城小程序信息
|
|
|
+ PayApplication payApplication = super.getOne(new LambdaQueryWrapper<PayApplication>()
|
|
|
+ .eq(PayApplication::getAppId, appId));
|
|
|
+ if (payApplication == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //查询商户信息
|
|
|
+ PayMerchantDTO payMerchantDTO = payMerchantService.getByMerchantNo(payApplication.getMerchantNo());
|
|
|
+ //商户支付配置
|
|
|
+ Map<String, String> payConfigMap = JsonUtil.toMap(payMerchantDTO.getPayConfig(), Map.class, String.class);
|
|
|
+ //关闭订单参数
|
|
|
+ Map<String, String> payParamMap = new HashMap<>(3);
|
|
|
+ payParamMap.put("appid", appId);
|
|
|
+ payParamMap.put("mch_id", payMerchantDTO.getMerchantNo());
|
|
|
+ payParamMap.put("out_trade_no", orderId);
|
|
|
+ payParamMap.put("apiKey", payConfigMap.get("apiKey"));
|
|
|
+ //关闭订单
|
|
|
+ this.closeOrder(payParamMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void closeOrder(Map<String, String> paramMap) {
|
|
|
+ try {
|
|
|
+ Map<String, String> paramData = new HashMap<>(6);
|
|
|
+ paramData.put("appid", paramMap.get("appId"));
|
|
|
+ paramData.put("mch_id", paramMap.get("mchId"));
|
|
|
+ paramData.put("out_trade_no", paramMap.get("orderId"));
|
|
|
+ paramData.put("nonce_str", WxPayUtil.generateNonceStr());
|
|
|
+ paramData.put("sign_type", SIGN_TYPE);
|
|
|
+ //接口签名
|
|
|
+ String sign = WxPayUtil.generateSignature(paramData, paramMap.get("apiKey"));
|
|
|
+ paramData.put("sign", sign);
|
|
|
+ //取消订单, 获取结果
|
|
|
+ String result = HttpUtil.postData(WxPayConstants.CLOSE_ORDER_URL, XmlUtil.mapToXml(paramData));
|
|
|
+ Map<String, String> successMap = XmlUtil.xmlToMap(result);
|
|
|
+ // 结果状态码
|
|
|
+ String resultCode = successMap.get("result_code");
|
|
|
+ // 返回状态码
|
|
|
+ String returnCode = successMap.get("return_code");
|
|
|
+ //成功, 返回结果
|
|
|
+ if (Constants.SUCCESS.equalsIgnoreCase(returnCode) && returnCode.equals(resultCode)) {
|
|
|
+ log.error("微信支付关闭订单成功, out_trade_no:{}", paramMap.get("orderId"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.error("微信支付关闭订单失败, paramData:{}, successMap : {}", JsonUtil.toString(paramData), JsonUtil.toString(successMap));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("微信支付关闭订单异常, 订单号:{}, e : {}", paramMap.get("orderId"), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|