AlipayHK 回调及相关数据处理

This commit is contained in:
2024-05-31 16:28:23 +08:00
parent 1aea8e93f2
commit 86e7119cfb
15 changed files with 84 additions and 60 deletions

View File

@@ -2,13 +2,11 @@ package com.ai.da.service;
import com.ai.da.model.dto.AlipayHKCallbackDTO;
import java.util.Map;
public interface AlipayHKService {
String createOrder(Integer amount, String wallet);
String callback(Map<String, String> params);
String callback(String paramString);
void processOrder(AlipayHKCallbackDTO alipayHKCallbackDTO);

View File

@@ -11,7 +11,7 @@ public interface CreditsService extends IService<CreditsDetail> {
void initCredits();
Boolean buyCredits(Long accountId, Integer quantity);
Boolean buyCredits(Long accountId, Float quantity);
void creditsIncrease(Long accountId, String event);

View File

@@ -216,7 +216,7 @@ public class AliPayServiceImpl implements AliPayService {
CreditsEventsEnum.BUY_CREDITS.getValue(),
"positive");
// 更新积分
creditsService.buyCredits(orderByOrderNo.getAccountId(),Integer.parseInt(totalAmount) / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
creditsService.buyCredits(orderByOrderNo.getAccountId(),Float.parseFloat(totalAmount) / Float.parseFloat(CreditsEventsEnum.PRICE.getValue()));
} finally {
//要主动释放锁
lock.unlock();
@@ -318,7 +318,7 @@ public class AliPayServiceImpl implements AliPayService {
CreditsEventsEnum.BUY_CREDITS.getValue(),
"positive");
// 更新积分
creditsService.buyCredits(orderByOrderNo.getAccountId(),(int)(orderByOrderNo.getTotalFee() / Float.parseFloat(CreditsEventsEnum.PRICE.getValue())));
creditsService.buyCredits(orderByOrderNo.getAccountId(),orderByOrderNo.getTotalFee() / Float.parseFloat(CreditsEventsEnum.PRICE.getValue()));
}
}

View File

@@ -71,7 +71,8 @@ public class AlipayHKServiceImpl implements AlipayHKService {
// ALIPAYHK 或者 ALIPAYCN
param.put("wallet", wallet);
param.put("segment_id", segmentId);
param.put("payment_solution", "WAP");
// param.put("payment_solution", "WAP");
param.put("payment_solution", "PC2MOBILE");
log.info("alipay-hk 创建订单,参数信息: {}", param);
// 生成订单
log.info("创建订单");
@@ -125,44 +126,46 @@ public class AlipayHKServiceImpl implements AlipayHKService {
}
/**
* 异步回调
* @return
*/
public String callback(Map<String, String> params){
log.info("支付通知正在执行");
log.info("通知参数 ===> {}", params);
public String callback(String paramString){
log.info("支付HK通知正在执行");
Map<String, String> params = JSONObject.parseObject(paramString, Map.class);
String result = "failure";
String data = params.get("data");
String signature = params.get("signature");
try {
// 异步回调验签
Boolean verification = alipayHKEncryptionUtil.signatureVerification(data, signature);
if (!verification){
log.error("alipay-hk 验签失败");
log.error("Alipay-hk 验签失败");
return result;
}
log.info("Alipay-HK 验签成功");
AlipayHKCallbackDTO alipayHKCallbackDTO = JSONObject.parseObject(data, AlipayHKCallbackDTO.class);
String dataString = data.replace("\\\"", "\"")
.replace("\"{", "{")
.replace("}\"", "}");
AlipayHKCallbackDTO alipayHKCallbackDTO = JSONObject.parseObject(dataString, AlipayHKCallbackDTO.class);
//按照支付结果异步通知中的描述,对支付结果中的业务内容进行二次校验,
//1 商户需要验证该通知数据中的 out_trade_no 是否为商户系统中创建的订单号
String outTradeNo = alipayHKCallbackDTO.getOut_trade_no();
OrderInfo order = orderInfoService.getOrderByOrderNo(outTradeNo);
if(order == null){
log.error("订单不存在");
log.error("订单{} 不存在",outTradeNo);
return result;
}
//2 判断 total_amount 是否确实为该订单的实际金额(即商户订单创建时的金额)
Long totalAmount = alipayHKCallbackDTO.getAmount();
Long totalFee = order.getTotalFee().longValue();
Float totalAmount = alipayHKCallbackDTO.getAmount();
Float totalFee = order.getTotalFee();
if(!Objects.equals(totalAmount, totalFee)){
log.error("金额校验失败");
log.error("金额校验失败,回调中金额为:{},实际金额为:{}", totalAmount, totalFee);
return result;
}
@@ -170,7 +173,7 @@ public class AlipayHKServiceImpl implements AlipayHKService {
String sellerId = alipayHKCallbackDTO.getMerchant_id();
String sellerIdProperty = merchantId;
if(!sellerId.equals(sellerIdProperty)){
log.error("商家pid校验失败");
log.error("商家merchantId校验失败回调中merchantId为{}实际merchantId为{}", sellerId, sellerIdProperty);
return result;
}
@@ -178,7 +181,7 @@ public class AlipayHKServiceImpl implements AlipayHKService {
String segId = alipayHKCallbackDTO.getSegment_id();
String segmentIdProperty = segmentId;
if(!segId.equals(segmentIdProperty)){
log.error("segmentId校验失败");
log.error("segmentId校验失败回调中segmentId为{}实际segmentId为{}", segId, segmentIdProperty);
return result;
}
@@ -190,13 +193,14 @@ public class AlipayHKServiceImpl implements AlipayHKService {
return result;
}
log.info("Alipay-HK参数二次校验成功进入数据处理与存储");
processOrder(alipayHKCallbackDTO);
result = "success";
}catch (Exception e){
log.error(e.getMessage());
}
log.info("Alipay-HK 回调处理结果:{}", result);
return result;
}
@@ -226,21 +230,25 @@ public class AlipayHKServiceImpl implements AlipayHKService {
// String orderStatus = orderInfoService.getOrderStatus(orderNo);
OrderInfo orderByOrderNo = orderInfoService.getOrderByOrderNo(orderNo);
String orderStatus = orderByOrderNo.getOrderStatus();
// 当订单状态处于未支付或超时已关闭时,更新订单状态
if (!OrderStatusEnum.NOT_PAY.getType().equals(orderStatus) || !OrderStatusEnum.TIMEOUT_CLOSED.getType().equals(orderStatus)) {
// 当订单状态处于未支付或超时已关闭时,更新订单状态,其他状态均不更新订单状态
if (!OrderStatusEnum.NOT_PAY.getType().equals(orderStatus) && !OrderStatusEnum.TIMEOUT_CLOSED.getType().equals(orderStatus)) {
log.info("订单状态 : {}", orderStatus);
return;
}
//更新订单状态
orderInfoService.updateStatusByOrderNo(orderNo, OrderStatusEnum.SUCCESS);
log.info("Alipay-HK 订单:{} 状态更新成功",orderNo);
//记录支付日志
paymentInfoService.createPaymentInfoForAliPayHK(alipayHKCallbackDTO);
log.info("Alipay-HK 订单:{} 支付信息状态更新成功",orderNo);
// 添加积分变更记录
creditsService.insertToCreditsDetail(orderByOrderNo.getAccountId(),
CreditsEventsEnum.BUY_CREDITS.getName() + "--AlipayHK",
CreditsEventsEnum.BUY_CREDITS.getValue(),
"positive");
// 更新积分
creditsService.buyCredits(orderByOrderNo.getAccountId(),Integer.parseInt(totalAmount) / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
creditsService.buyCredits(orderByOrderNo.getAccountId(),Float.parseFloat(totalAmount) / Float.parseFloat(CreditsEventsEnum.PRICE.getValue()));
log.info("用户:{} 积分信息更新成功",orderByOrderNo.getAccountId());
} finally {
//要主动释放锁
lock.unlock();

View File

@@ -48,7 +48,7 @@ public class CreditsServiceImpl extends ServiceImpl<CreditsDetailMapper, Credits
}
@Override
public Boolean buyCredits(Long accountId, Integer quantity) {
public Boolean buyCredits(Long accountId, Float quantity) {
BigDecimal existingCredits = accountMapper.selectById(accountId).getCredits();
BigDecimal newCredits = new BigDecimal(CreditsEventsEnum.BUY_CREDITS.getValue()).multiply(new BigDecimal(quantity));
BigDecimal added = existingCredits.add(newCredits);

View File

@@ -592,7 +592,7 @@ public class PayPalCheckoutServiceImpl implements PayPalCheckoutService {
CreditsEventsEnum.BUY_CREDITS.getValue(),
"positive");
// 更新积分
creditsService.buyCredits(orderInfo.getAccountId(), (int)(orderInfo.getTotalFee() / Float.parseFloat(CreditsEventsEnum.PRICE.getValue())));
creditsService.buyCredits(orderInfo.getAccountId(), orderInfo.getTotalFee() / Float.parseFloat(CreditsEventsEnum.PRICE.getValue()));
}
}
@@ -635,7 +635,7 @@ public class PayPalCheckoutServiceImpl implements PayPalCheckoutService {
"positive");
// 更新积分
// creditsService.buyCredits(orderByOrderNo.getAccountId(),orderByOrderNo.getTotalFee() / Integer.parseInt(CreditsEventsEnum.PRICE.getValue()));
creditsService.buyCredits(orderByOrderNo.getAccountId(),(int)(orderByOrderNo.getTotalFee() / Float.parseFloat(CreditsEventsEnum.PRICE.getValue())));
creditsService.buyCredits(orderByOrderNo.getAccountId(),orderByOrderNo.getTotalFee() / Float.parseFloat(CreditsEventsEnum.PRICE.getValue()));
}
}

View File

@@ -49,8 +49,8 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
paymentInfo.setTransactionId(transactionId);
paymentInfo.setTradeType(tradeType);
paymentInfo.setTradeState(tradeState);
// 原来的单位是:分 Int 现改为:元 Long
paymentInfo.setPayerTotal(payerTotal / 100L);
// 原来的单位是:分 Int 现改为:元 Float
paymentInfo.setPayerTotal(payerTotal / 100.0F);
paymentInfo.setContent(plainText);
baseMapper.insert(paymentInfo);
@@ -83,7 +83,7 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
paymentInfo.setTradeType("电脑网站支付");
paymentInfo.setTradeState(tradeStatus);
// 原来的单位是分 Int 现改为元 Long
paymentInfo.setPayerTotal(totalAmountInt / 100L);
paymentInfo.setPayerTotal(totalAmountInt / 100.0F);
Gson gson = new Gson();
String json = gson.toJson(params, HashMap.class);
@@ -98,7 +98,7 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
log.info("记录支付日志");
// int totalAmountInt = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
Long totalAmountInt = new BigDecimal(order.purchaseUnits().get(0).payments().captures().get(0).amount().value()).longValue();
Float totalAmountFloat = new BigDecimal(order.purchaseUnits().get(0).payments().captures().get(0).amount().value()).floatValue();
PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setOrderNo(order.id());
@@ -107,7 +107,7 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
paymentInfo.setTradeType("电脑网站支付");
paymentInfo.setTradeState(order.status());
// todo 确认这里的数据单位是不是元
paymentInfo.setPayerTotal(totalAmountInt);
paymentInfo.setPayerTotal(totalAmountFloat);
Gson gson = new Gson();
String json = gson.toJson(order, Order.class);
@@ -130,7 +130,7 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
//交易金额
String totalAmount = alipayHKCallbackDTO.getAmount().toString();
// int totalAmountInt = new BigDecimal(totalAmount).multiply(new BigDecimal("100")).intValue();
Long totalAmountInt = new BigDecimal(totalAmount).longValue();
Float totalAmountFloat = new BigDecimal(totalAmount).floatValue();
PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setOrderNo(orderNo);
@@ -138,7 +138,7 @@ public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, Payme
paymentInfo.setTransactionId(transactionId);
paymentInfo.setTradeType("电脑网站支付");
paymentInfo.setTradeState(tradeStatus);
paymentInfo.setPayerTotal(totalAmountInt);
paymentInfo.setPayerTotal(totalAmountFloat);
Gson gson = new Gson();
String json = gson.toJson(alipayHKCallbackDTO);