1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| @Override public void batchAlipayPeriodPay(List<CustomerPeriodSubscribe> list) throws ParseException {
List<CustomerPeriodSubscribe> updatePeriodSubscribeList = new ArrayList<>();
for (CustomerPeriodSubscribe item : list) {
CustomerPeriodPayLog customerPeriodPayLog = customerPeriodPayLogMapper.findExecuteData( item.getCustomerId(), item.getId(), DateUtils.getCurrentDate()); if (!CommonUtils.isNullOrEmpty(customerPeriodPayLog)) { log.info("用户ID:{}, periodSubscribeId: {}, 今日已执行过扣款,跳过", item.getCustomerId(), item.getId()); continue; }
Application application = commonService.getRedisApplicationByAppId(item.getAppId());
VipConfig vipConfig = vipConfigMapper.findById(item.getVipConfigId()); if (CommonUtils.isNullOrEmpty(vipConfig)) { continue; }
Customer customer = customerService.findById(item.getCustomerId()); if (CommonUtils.isNullOrEmpty(customer)) { log.info("用户ID:{} 不存在,不执行扣款", item.getCustomerId()); continue; }
if (ParentVipTypeEnum.vip.getType().equals(vipConfig.getParentType().intValue())) {
if (VipTypeEnum.lifelong.getType().equals(customer.getVipType())) { log.info("用户ID:{} 已经是终身会员,不执行周期扣款", item.getCustomerId()); continue; } } else {
if (VipTypeEnum.adVipLifelong.getType().equals(customer.getAdVipType())) { log.info("用户ID:{} 已经是终身免广告会员,不执行周期扣款", item.getCustomerId()); continue; } }
BeforeBuyVipBo beforeBuyVipBo = vipService.beforeBuyVip(item.getCustomerId(), vipConfig.getParentType().intValue(), vipConfig.getType(), item.getChannel(), item.getVersion(), PayTypeEnum.alipay, item.getOaid(), true);
String parentVipTypeDesc = ParentVipTypeEnum.getDesc(vipConfig.getParentType().intValue()); String subject = item.getChannel() + "-" + item.getVersion() + "-" + item.getCustomerId() + "-" + parentVipTypeDesc + "-" + vipConfig.getTitle() + "-" + application.getAppName() + "自动续费会员支付";
String tradePayResp = this.alipayTradePay(application.getAlipayAppId(), application.getAlipayMchPrivateKey(), application.getAlipayPublicKey(), item.getAgreementNo(), item.getPayAmount().toString(), subject, beforeBuyVipBo.getPay().getOutTradeNo());
JSONObject payRespMap = null; try { payRespMap = JSON.parseObject(tradePayResp); } catch (Throwable ignored) {}
Integer payStatus = -1;
if (payRespMap != null) { if (payRespMap.getString("code").equals("10000")) { payStatus = 1; } }
customerPeriodPayLog = new CustomerPeriodPayLog(); customerPeriodPayLog.setAppId(application.getId()); customerPeriodPayLog.setCustomerId(item.getCustomerId()); customerPeriodPayLog.setPeriodSubscribeId(item.getId()); customerPeriodPayLog.setPayId(beforeBuyVipBo.getPay().getId()); customerPeriodPayLog.setSubject(subject); customerPeriodPayLog.setRespJson(tradePayResp); customerPeriodPayLog.setPayStatus(payStatus); customerPeriodPayLog.setDate(DateUtil.date().toDateStr()); customerPeriodPayLogMapper.insert(customerPeriodPayLog);
if (payStatus.equals(1)) { CustomerPeriodSubscribe updatePeriodSubscribeData = new CustomerPeriodSubscribe(); updatePeriodSubscribeData.setId(item.getId()); updatePeriodSubscribeData.setNextPayDate( DateUtil.format( DateUtil.offsetMonth( DateUtil.parseDate(item.getNextPayDate()), 1), "yyyy-MM-dd")); updatePeriodSubscribeData.setContractNextPayDate(DateUtil.format( DateUtil.offsetMonth( DateUtil.parseDate(item.getContractNextPayDate()), 1), "yyyy-MM-dd")); updatePeriodSubscribeList.add(updatePeriodSubscribeData); } }
if (updatePeriodSubscribeList.size() > 0) { customerPeriodSubscribeService.updateBatchById(updatePeriodSubscribeList); }
}
private String alipayTradePay(String alipayAppId, String alipayMchPrivateKey, String alipayPublicKey, String agreementNo, String totalAmount, String subject, String outTradeNo) {
Config config = new Config(); config.protocol = "https"; config.gatewayHost = "openapi.alipay.com"; config.signType = "RSA2"; config.appId = alipayAppId; config.merchantPrivateKey = alipayMchPrivateKey; config.alipayPublicKey = alipayPublicKey; config.notifyUrl = alipayCallbackUrl; Factory.setOptions(config);
AlipayOpenApiGenericResponse response;
try {
Map<String, String> agreementSignParams = new HashMap<>(); agreementSignParams.put("agreement_no", agreementNo);
Map<String, Object> bizContents = new HashMap<>(); bizContents.put("product_code", "CYCLE_PAY_AUTH"); bizContents.put("agreement_params", agreementSignParams); bizContents.put("out_trade_no", outTradeNo); bizContents.put("total_amount", totalAmount); bizContents.put("subject", subject);
response = Factory.Util.Generic().execute("alipay.trade.pay", new HashMap<>(), bizContents);
if (!ResponseChecker.success(response)) { System.out.println(response.toMap()); log.error("支付宝调用失败"); return JSON.toJSONString(response.toMap()); }
} catch (Throwable e) { log.error("支付宝调用失败,原因:" + e.getMessage()); return "支付宝调用失败,原因:" + e.getMessage(); }
return JSON.toJSONString(response.toMap()); }
|