Commit 0a169770 authored by xuxin's avatar xuxin

代码修改以及bug修复

parent b6f8a32c
...@@ -263,6 +263,9 @@ public class AccountController { ...@@ -263,6 +263,9 @@ public class AccountController {
return paymentService.wechatPayCallback(tradno); return paymentService.wechatPayCallback(tradno);
} }
/**
* 使用微信进行账户充值成功后,微信会调用该接口
*/
@RequestMapping(path = "/0xwxcheckorderstatus/{tradno}/{chargeType}", method = RequestMethod.GET) @RequestMapping(path = "/0xwxcheckorderstatus/{tradno}/{chargeType}", method = RequestMethod.GET)
public UserPaymentDto wechatCheckOrderStatus(Principal principal, @PathVariable String tradno, @PathVariable int chargeType) { public UserPaymentDto wechatCheckOrderStatus(Principal principal, @PathVariable String tradno, @PathVariable int chargeType) {
return paymentService.wxCheckOrderStatus(tradno, chargeType); return paymentService.wxCheckOrderStatus(tradno, chargeType);
...@@ -281,6 +284,9 @@ public class AccountController { ...@@ -281,6 +284,9 @@ public class AccountController {
paymentService.alipaCallback(tradno); paymentService.alipaCallback(tradno);
} }
/**
* 使用 支付宝 充值 成功后回调
*/
@RequestMapping(path = "/0xalicheckorderstatus/{tradno}/{chargeType}", method = RequestMethod.GET) @RequestMapping(path = "/0xalicheckorderstatus/{tradno}/{chargeType}", method = RequestMethod.GET)
public UserPaymentDto alipayCheckOrderStatus(Principal principal, @PathVariable(required = false) String tradno, @PathVariable int chargeType) { public UserPaymentDto alipayCheckOrderStatus(Principal principal, @PathVariable(required = false) String tradno, @PathVariable int chargeType) {
return paymentService.aliCheckOrderStatus(tradno, chargeType); return paymentService.aliCheckOrderStatus(tradno, chargeType);
...@@ -292,7 +298,6 @@ public class AccountController { ...@@ -292,7 +298,6 @@ public class AccountController {
*/ */
@RequestMapping(path = "/0xalipay/{amount}", method = RequestMethod.GET) @RequestMapping(path = "/0xalipay/{amount}", method = RequestMethod.GET)
public void alipayPutOrder(HttpServletRequest request, HttpServletResponse response, @PathVariable int amount) throws Exception { public void alipayPutOrder(HttpServletRequest request, HttpServletResponse response, @PathVariable int amount) throws Exception {
String by = request.getParameter("by"); String by = request.getParameter("by");
String form = paymentService.alipayPutPayOrder(request.getUserPrincipal().getName(), amount, by); String form = paymentService.alipayPutPayOrder(request.getUserPrincipal().getName(), amount, by);
response.setContentType("text/html;charset=utf-8"); response.setContentType("text/html;charset=utf-8");
......
...@@ -82,7 +82,6 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -82,7 +82,6 @@ public class PaymentServiceImpl implements PaymentService {
@Override @Override
public void alipaCallback(String tradno) { public void alipaCallback(String tradno) {
UserPayment byTradeNo = userPaymentRepository.findByTradeNo(tradno); UserPayment byTradeNo = userPaymentRepository.findByTradeNo(tradno);
if (byTradeNo != null && !byTradeNo.isSucceed()) { if (byTradeNo != null && !byTradeNo.isSucceed()) {
aliCheckOrderStatus(byTradeNo.getTradeNo(), 0); aliCheckOrderStatus(byTradeNo.getTradeNo(), 0);
} }
...@@ -113,17 +112,10 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -113,17 +112,10 @@ public class PaymentServiceImpl implements PaymentService {
} }
try { try {
UserBalance balance = userBalanceRepository.findById(byTradeNo.getUsername()).orElse(null); UserBalance balance = getUserBalance(result, byTradeNo);
if (balance != null) {
result.setBalance(Math.round(balance.getBalanced()));
}
if (balance == null) {
balance = new UserBalance();
balance.setUsername(byTradeNo.getUsername());
}
// 1. 构建微信支付订单查询数据,并获取微信支付订单信息 // 1. 构建微信支付订单查询数据,并获取微信支付订单信息
// 获取微信支付订单存在失败的情况,所以针对失败情况去使用定时任务获取 // 获取微信支付订单存在失败的情况,所以针对失败情况去使用定时任务来重新调用本方法
boolean isVpsClient = true; boolean isVpsClient = true;
WXPayConfig ourWxPayConfig = isVpsClient ? new FGLWxConfig() : new CloudamWxConfig(); WXPayConfig ourWxPayConfig = isVpsClient ? new FGLWxConfig() : new CloudamWxConfig();
WXPay wxPay = new WXPay(ourWxPayConfig); WXPay wxPay = new WXPay(ourWxPayConfig);
...@@ -141,42 +133,9 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -141,42 +133,9 @@ public class PaymentServiceImpl implements PaymentService {
* PAYERROR--支付失败(其他原因,如银行返回失败) * PAYERROR--支付失败(其他原因,如银行返回失败)
*/ */
result.setStatus(respData.get("trade_state")); result.setStatus(respData.get("trade_state"));
// 2. 如果订单支付成功 // 2. 如果订单支付成功(具体业务逻辑)
if ("SUCCESS".equals(respData.get("trade_state"))) { if ("SUCCESS".equals(respData.get("trade_state"))) {
byTradeNo.setSucceed(true); dealPaySuccess(chargeType, result, byTradeNo, byName, balance);
// 2.1 更新 userPayment 的支付状态为成功
boolean b = userPaymentRepository.updatePaymentResult(byTradeNo, true);
if (b) {
// 2.2 获取充值优惠赠送
PayBack payBack = payBackRepository.findByPay(byTradeNo.getAmount());
if (payBack == null) {
payBack = new PayBack();
}
// chargeType 为 0 代表 充值
if (chargeType == 0) {
// 2.3 构建用户充值信息并保存
UserPrePaidBilling bill = getUserPrePaidBilling(byTradeNo, byName, balance, payBack);
userPrePaidBillingRepository.save(bill);
}
// 2.4 更新账户余额 userBalance
balance.setBalanced(balance.getBalanced() + byTradeNo.getAmount() + payBack.getBack());
userBalanceRepository.save(balance);
// 2.5 如果当前用户通过邀请码注册的,则更新邀请人的佣金
if (byName.getPromotionCode() != null) {
Account account = accountRepository.findByPromotion(byName.getPromotionCode());
if (account != null) {
account.getPromotion().setCommission(account.getPromotion().getCommission() + byTradeNo.getAmount());
accountRepository.save(account);
}
}
}
result.setBalance(Math.round(balance.getBalanced()));
result.setPaid(true);
return result; return result;
} }
} }
...@@ -188,7 +147,6 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -188,7 +147,6 @@ public class PaymentServiceImpl implements PaymentService {
return result; return result;
} }
private String convertAlipayStatus(String src) { private String convertAlipayStatus(String src) {
switch (src) { switch (src) {
case "WAIT_BUYER_PAY": case "WAIT_BUYER_PAY":
...@@ -207,42 +165,33 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -207,42 +165,33 @@ public class PaymentServiceImpl implements PaymentService {
public UserPaymentDto aliCheckOrderStatus(String tradno, int chargeType) { public UserPaymentDto aliCheckOrderStatus(String tradno, int chargeType) {
UserPaymentDto result = new UserPaymentDto(); UserPaymentDto result = new UserPaymentDto();
result.setPaid(false); result.setPaid(false);
if (StringUtils.isBlank(tradno)) if (StringUtils.isBlank(tradno)) {
return result; return result;
try { }
UserPayment byTradeNo = userPaymentRepository.findByTradeNo(tradno); UserPayment byTradeNo = userPaymentRepository.findByTradeNo(tradno);
UserBalance balance = userBalanceRepository.findById(byTradeNo.getUsername()).orElse(null); if (byTradeNo == null) {
if (balance != null)
result.setBalance(Math.round(balance.getBalanced()));
if (byTradeNo == null)
return result; return result;
}
if (byTradeNo.isSucceed()) { if (byTradeNo.isSucceed()) {
result.setPaid(true); result.setPaid(true);
return result; return result;
} }
Account byName = accountService.findByName(byTradeNo.getUsername()); Account byName = accountService.findByName(byTradeNo.getUsername());
if (byName == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST, "account does not exist: " + byTradeNo.getUsername());
boolean isVpsClient = true; try {
UserBalance balance = getUserBalance(result, byTradeNo);
boolean isVpsClient = true;
AlipayConfig alipayConfig = new VpsAlipayConfig(); AlipayConfig alipayConfig = new VpsAlipayConfig();
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(), alipayConfig.getAPPID(), alipayConfig.getAPP_PRIVATE_KEY(), AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(), alipayConfig.getAPPID(),
"json", alipayConfig.getCHARSET(), alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGN_TYPE()); alipayConfig.getAPP_PRIVATE_KEY(), "json", alipayConfig.getCHARSET(),
alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGN_TYPE());
AlipayTradeQueryRequest alipayRequest = buildAlipayTradeQueryRequest(byTradeNo);
AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
String out_trade_no = byTradeNo.getTradeNo();
alipayRequest.setBizContent(""
+ "{"
+ "\"out_trade_no\":\"" + out_trade_no + "\""
+ "}"
);
AlipayTradeQueryResponse response = alipayClient.execute(alipayRequest); AlipayTradeQueryResponse response = alipayClient.execute(alipayRequest);
log.info("ali order status :" + JSONObject.toJSONString(response)); log.info("ali order status :" + JSONObject.toJSONString(response));
if (response.getTradeStatus() == null) { if (response.getTradeStatus() == null) {
result.setPaid(false); result.setPaid(false);
...@@ -252,63 +201,8 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -252,63 +201,8 @@ public class PaymentServiceImpl implements PaymentService {
result.setStatus(convertAlipayStatus(response.getTradeStatus())); result.setStatus(convertAlipayStatus(response.getTradeStatus()));
if ("TRADE_SUCCESS".equals(response.getTradeStatus()) || "TRADE_FINISHED".equals(response.getTradeStatus())) { if ("TRADE_SUCCESS".equals(response.getTradeStatus()) || "TRADE_FINISHED".equals(response.getTradeStatus())) {
byTradeNo.setSucceed(true); // 2. 如果订单支付成功(具体业务逻辑)
boolean b = userPaymentRepository.updatePaymentResult(byTradeNo, true); dealPaySuccess(chargeType, result, byTradeNo, byName, balance);
if (b) {
if (balance == null) {
balance = new UserBalance();
balance.setUsername(byTradeNo.getUsername());
}
PayBack payBack = payBackRepository.findByPay(byTradeNo.getAmount());
if (payBack == null)
payBack = new PayBack();
if (chargeType == 0) {
UserPrePaidBilling bill = new UserPrePaidBilling();
Account account = accountRepository.findByName(byTradeNo.getUsername()).get();
if (account != null && account.getParent() != null)
bill.setAdministrator(account.getParent());
else
bill.setAdministrator(account.getName());
bill.setTradeNo(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + SmsUtils.createRandom(true, 4));
bill.setChargeType(0);
bill.setAmount(0);
bill.setUnit(null);
bill.setPeriod(0);
bill.setPayMethod(1);
bill.setUsername(byTradeNo.getUsername());
bill.setTotal((float) byTradeNo.getAmount());
bill.setStatus(BillStatus.PAID);
bill.setPrepaid(true);
bill.setTimestamp(Instant.now().toEpochMilli());
bill.setBalanced(balance.getBalanced() + byTradeNo.getAmount() + payBack.getBack());
final YearMonth lastmonth = YearMonth.now();
int monthValue = lastmonth.getMonthValue();
int year = lastmonth.getYear();
bill.setYear(year);
bill.setMonth(monthValue);
userPrePaidBillingRepository.save(bill);
}
balance.setBalanced(balance.getBalanced() + byTradeNo.getAmount() + payBack.getBack());
userBalanceRepository.save(balance);
if (byName.getPromotionCode() != null) {
Account account = accountRepository.findByPromotion(byName.getPromotionCode());
if (account != null) {
account.getPromotion().setCommission(account.getPromotion().getCommission() + byTradeNo.getAmount());
accountRepository.save(account);
}
}
}
result.setBalance(Math.round(balance.getBalanced()));
result.setPaid(true);
return result; return result;
} }
...@@ -320,48 +214,45 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -320,48 +214,45 @@ public class PaymentServiceImpl implements PaymentService {
return result; return result;
} }
@NotNull
private UserBalance getUserBalance(UserPaymentDto result, UserPayment byTradeNo) {
UserBalance balance = userBalanceRepository.findById(byTradeNo.getUsername()).orElse(null);
if (balance != null) {
result.setBalance(Math.round(balance.getBalanced()));
}
if (balance == null) {
balance = new UserBalance();
balance.setUsername(byTradeNo.getUsername());
}
return balance;
}
@Override @Override
public String alipayPutPayOrder(String username, int amount, String by) { public String alipayPutPayOrder(String username, int amount, String by) {
Account byName = accountService.findByName(username); Account byName = accountService.findByName(username);
if (byName == null) if (byName.getPermission() < 4) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST, "account does not exist: " + username);
if (byName.getPermission() < 4)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION, "account does not have permission: " + username); throw new ClientRequestException(AccountErrorCode.NOPERMISSION, "account does not have permission: " + username);
boolean isVpsClient = true; }
UserPayment internalOrder = new UserPayment();
internalOrder.setAmount(amount);
internalOrder.setPaymentMethod(PaymentMethod.ALIPAY);
internalOrder.setSucceed(false);
internalOrder.setUsername(username);
try { try {
// 创建并封装 UserPayment 信息
UserPayment internalOrder = getUserPayment(username, amount, PaymentMethod.ALIPAY);
// 获取支付宝支付的配置信息
boolean isVpsClient = true;
AlipayConfig alipayConfig = isVpsClient ? new VpsAlipayConfig() : new CloudamAlipayConfig(); AlipayConfig alipayConfig = isVpsClient ? new VpsAlipayConfig() : new CloudamAlipayConfig();
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(), alipayConfig.getAPPID(), alipayConfig.getAPP_PRIVATE_KEY(), // 获取支付宝的 client
"json", alipayConfig.getCHARSET(), alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGN_TYPE()); AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(), alipayConfig.getAPPID(),
alipayConfig.getAPP_PRIVATE_KEY(), "json", alipayConfig.getCHARSET(),
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGN_TYPE());
alipayRequest.setReturnUrl(alipayConfig.getReturnUrl(isVpsClient, by) + internalOrder.getTradeNo()); // 构建请求信息 并设置请求成功的回调 url
alipayRequest.setNotifyUrl(alipayConfig.getNOTIFY_URL() + internalOrder.getTradeNo()); AlipayTradePagePayRequest alipayRequest = buildAlipayTradePagePayRequest(amount, by, internalOrder, isVpsClient, alipayConfig);
String out_trade_no = internalOrder.getTradeNo(); // 调用 支付宝支付接口
String product_code = "FAST_INSTANT_TRADE_PAY";
String total_amount = "" + amount + ".00";
String subject = "订单" + out_trade_no;
String body = "";
alipayRequest.setBizContent(""
+ "{"
+ "\"out_trade_no\":\"" + out_trade_no + "\","
+ "\"product_code\":\"" + product_code + "\","
+ "\"total_amount\":\"" + total_amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\""
+ "}"
);
AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayRequest); AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayRequest);
// 将支付信息保存到 userPayment
internalOrder.setTransId(response.getTradeNo()); internalOrder.setTransId(response.getTradeNo());
userPaymentRepository.save(internalOrder); userPaymentRepository.save(internalOrder);
...@@ -383,7 +274,7 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -383,7 +274,7 @@ public class PaymentServiceImpl implements PaymentService {
try { try {
// 创建并封装 UserPayment 信息 // 创建并封装 UserPayment 信息
UserPayment internalOrder = getUserPayment(username, amount); UserPayment internalOrder = getUserPayment(username, amount, PaymentMethod.WECHAT);
// 回调的url // 回调的url
String notifyUrl = AccountController.WECHAT_PAY_CALLBACK_URL + internalOrder.getTradeNo(); String notifyUrl = AccountController.WECHAT_PAY_CALLBACK_URL + internalOrder.getTradeNo();
...@@ -636,6 +527,8 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -636,6 +527,8 @@ public class PaymentServiceImpl implements PaymentService {
} }
} }
@Deprecated
@Override @Override
public UserPaymentDto wxCheckOrderStatus(String tradeno) { public UserPaymentDto wxCheckOrderStatus(String tradeno) {
UserPaymentDto result = new UserPaymentDto(); UserPaymentDto result = new UserPaymentDto();
...@@ -691,8 +584,10 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -691,8 +584,10 @@ public class PaymentServiceImpl implements PaymentService {
} }
// 充值返现 // 充值返现
PayBack payBack = payBackRepository.findByPay(byTradeNo.getAmount()); PayBack payBack = payBackRepository.findByPay(byTradeNo.getAmount());
if (payBack == null) if (payBack == null) {
payBack = new PayBack(); payBack = new PayBack();
}
balance.setBalanced(balance.getBalanced() + byTradeNo.getAmount() + payBack.getBack()); balance.setBalanced(balance.getBalanced() + byTradeNo.getAmount() + payBack.getBack());
balance = userBalanceRepository.save(balance); balance = userBalanceRepository.save(balance);
} }
...@@ -710,6 +605,7 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -710,6 +605,7 @@ public class PaymentServiceImpl implements PaymentService {
return result; return result;
} }
@Deprecated
@Override @Override
public UserPaymentDto aliCheckOrderStatus(String tradno) { public UserPaymentDto aliCheckOrderStatus(String tradno) {
UserPaymentDto result = new UserPaymentDto(); UserPaymentDto result = new UserPaymentDto();
...@@ -739,14 +635,7 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -739,14 +635,7 @@ public class PaymentServiceImpl implements PaymentService {
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(), alipayConfig.getAPPID(), alipayConfig.getAPP_PRIVATE_KEY(), AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getURL(), alipayConfig.getAPPID(), alipayConfig.getAPP_PRIVATE_KEY(),
"json", alipayConfig.getCHARSET(), alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGN_TYPE()); "json", alipayConfig.getCHARSET(), alipayConfig.getALIPAY_PUBLIC_KEY(), alipayConfig.getSIGN_TYPE());
AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest(); AlipayTradeQueryRequest alipayRequest = buildAlipayTradeQueryRequest(byTradeNo);
String out_trade_no = byTradeNo.getTradeNo();
alipayRequest.setBizContent(""
+ "{"
+ "\"out_trade_no\":\"" + out_trade_no + "\""
+ "}"
);
AlipayTradeQueryResponse response = alipayClient.execute(alipayRequest); AlipayTradeQueryResponse response = alipayClient.execute(alipayRequest);
log.info("ali order status :" + JSONObject.toJSONString(response)); log.info("ali order status :" + JSONObject.toJSONString(response));
result.setStatus(convertAlipayStatus(response.getTradeStatus())); result.setStatus(convertAlipayStatus(response.getTradeStatus()));
...@@ -783,13 +672,13 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -783,13 +672,13 @@ public class PaymentServiceImpl implements PaymentService {
} }
@NotNull @NotNull
private UserPayment getUserPayment(String username, int amount) { private UserPayment getUserPayment(String username, int amount, PaymentMethod paymentMethod) {
UserPayment internalOrder = new UserPayment(); UserPayment internalOrder = new UserPayment();
internalOrder.setUsername(username);
internalOrder.setAmount(amount); internalOrder.setAmount(amount);
internalOrder.setPaymentMethod(PaymentMethod.WECHAT); internalOrder.setPaymentMethod(paymentMethod);
// 此时充值尚未成功,需要等微信回调才能确认是否成功 // 此时充值尚未成功,需要等 微信或者支付宝 回调才能确认是否成功
internalOrder.setSucceed(false); internalOrder.setSucceed(false);
internalOrder.setUsername(username);
return internalOrder; return internalOrder;
} }
...@@ -837,7 +726,14 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -837,7 +726,14 @@ public class PaymentServiceImpl implements PaymentService {
bill.setAmount(0); bill.setAmount(0);
bill.setUnit(null); bill.setUnit(null);
bill.setPeriod(0); bill.setPeriod(0);
if (byTradeNo.getPaymentMethod().equals(PaymentMethod.ALIPAY)) {
bill.setPayMethod(1);
}
if (byTradeNo.getPaymentMethod().equals(PaymentMethod.WECHAT)) {
bill.setPayMethod(2); bill.setPayMethod(2);
}
bill.setUsername(byTradeNo.getUsername()); bill.setUsername(byTradeNo.getUsername());
bill.setTotal((float) byTradeNo.getAmount()); bill.setTotal((float) byTradeNo.getAmount());
bill.setStatus(BillStatus.PAID); bill.setStatus(BillStatus.PAID);
...@@ -853,4 +749,76 @@ public class PaymentServiceImpl implements PaymentService { ...@@ -853,4 +749,76 @@ public class PaymentServiceImpl implements PaymentService {
return bill; return bill;
} }
@NotNull
private AlipayTradePagePayRequest buildAlipayTradePagePayRequest(int amount, String by, UserPayment internalOrder, boolean isVpsClient, AlipayConfig alipayConfig) {
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(alipayConfig.getReturnUrl(isVpsClient, by) + internalOrder.getTradeNo());
// 调用支付宝支付接口,支付成功后会回调 notifyUrl
alipayRequest.setNotifyUrl(alipayConfig.getNOTIFY_URL() + internalOrder.getTradeNo());
String out_trade_no = internalOrder.getTradeNo();
String product_code = "FAST_INSTANT_TRADE_PAY";
String total_amount = "" + amount + ".00";
String subject = "订单" + out_trade_no;
String body = "";
alipayRequest.setBizContent(""
+ "{"
+ "\"out_trade_no\":\"" + out_trade_no + "\","
+ "\"product_code\":\"" + product_code + "\","
+ "\"total_amount\":\"" + total_amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\""
+ "}"
);
return alipayRequest;
}
@NotNull
private AlipayTradeQueryRequest buildAlipayTradeQueryRequest(UserPayment byTradeNo) {
AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
String out_trade_no = byTradeNo.getTradeNo();
alipayRequest.setBizContent(""
+ "{"
+ "\"out_trade_no\":\"" + out_trade_no + "\""
+ "}"
);
return alipayRequest;
}
private void dealPaySuccess(int chargeType, UserPaymentDto result, UserPayment byTradeNo, Account byName, UserBalance balance) {
byTradeNo.setSucceed(true);
// 2.1 更新 userPayment 的支付状态为成功
boolean b = userPaymentRepository.updatePaymentResult(byTradeNo, true);
if (b) {
// 2.2 获取充值优惠赠送
PayBack payBack = payBackRepository.findByPay(byTradeNo.getAmount());
if (payBack == null) {
payBack = new PayBack();
}
// chargeType 为 0 代表 充值
if (chargeType == 0) {
// 2.3 构建用户充值信息并保存
UserPrePaidBilling bill = getUserPrePaidBilling(byTradeNo, byName, balance, payBack);
userPrePaidBillingRepository.save(bill);
}
// 2.4 更新账户余额 userBalance
balance.setBalanced(balance.getBalanced() + byTradeNo.getAmount() + payBack.getBack());
userBalanceRepository.save(balance);
// 2.5 如果当前用户通过邀请码注册的,则更新邀请人的佣金
if (byName.getPromotionCode() != null) {
Account account = accountRepository.findByPromotion(byName.getPromotionCode());
if (account != null) {
account.getPromotion().setCommission(account.getPromotion().getCommission() + byTradeNo.getAmount());
accountRepository.save(account);
}
}
}
result.setBalance(Math.round(balance.getBalanced()));
result.setPaid(true);
}
} }
...@@ -159,10 +159,8 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -159,10 +159,8 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override @Override
public List<String> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception { public List<String> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception {
if (ipResourceRequestDto.getRegion() == null) { // 1. 数据校验
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); if (ipResourceRequestDto.getRegion() == null || ipResourceRequestDto.getRegionCn() == null) {
}
if (ipResourceRequestDto.getRegionCn() == null) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
} }
Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST)); Account account = accountRepository.findByName(username).orElseThrow(() -> new ClientRequestException(AccountErrorCode.NAMENOTEXIST));
...@@ -171,6 +169,7 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -171,6 +169,7 @@ public class IpResourceServiceImpl implements IpResourceService {
} }
// 2. 计算 ip 单价信息
double newprice = 0; double newprice = 0;
if (!"own".equals(ipResourceRequestDto.getVendor())) { if (!"own".equals(ipResourceRequestDto.getVendor())) {
Map<String, List<String>> priceList = ipOptionsRepository.findAll().get(0).getIpPlatForm(); Map<String, List<String>> priceList = ipOptionsRepository.findAll().get(0).getIpPlatForm();
...@@ -182,12 +181,15 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -182,12 +181,15 @@ public class IpResourceServiceImpl implements IpResourceService {
newprice = "week".equals(ipResourceRequestDto.getUnit()) ? (Integer.valueOf(price) / 3) : Integer.valueOf(price); newprice = "week".equals(ipResourceRequestDto.getUnit()) ? (Integer.valueOf(price) / 3) : Integer.valueOf(price);
} }
// 3. 计算总共需要花费多少钱 并 校验账户余额 够不够扣费
IpChargeResultDto ipChargeResultDto = accountService.preChargeByMoney(username, newprice * ipResourceRequestDto.getAmount() * ipResourceRequestDto.getPeriod()); IpChargeResultDto ipChargeResultDto = accountService.preChargeByMoney(username, newprice * ipResourceRequestDto.getAmount() * ipResourceRequestDto.getPeriod());
if (!ipChargeResultDto.isSuccess()) { if (!ipChargeResultDto.isSuccess()) {
throw new ClientRequestException(AccountErrorCode.NOTENOUGHBALANCE); throw new ClientRequestException(AccountErrorCode.NOTENOUGHBALANCE);
} }
// 生成随机密码
String password = StringUtils.isNotBlank(ipResourceRequestDto.getPassword()) ? ipResourceRequestDto.getPassword() : genRandom(3, 12); String password = StringUtils.isNotBlank(ipResourceRequestDto.getPassword()) ? ipResourceRequestDto.getPassword() : genRandom(3, 12);
List<IpResourceDto> ipResourceDtos = new ArrayList<>(); List<IpResourceDto> ipResourceDtos = new ArrayList<>();
List<String> ipIds = new ArrayList<>(); List<String> ipIds = new ArrayList<>();
...@@ -195,19 +197,22 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -195,19 +197,22 @@ public class IpResourceServiceImpl implements IpResourceService {
IpResource ipResource = new IpResource(); IpResource ipResource = new IpResource();
ipResource.setPeriod(ipResourceRequestDto.getPeriod()); ipResource.setPeriod(ipResourceRequestDto.getPeriod());
//充6送1 //充6送1
if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 6) { if (ipResourceRequestDto.getUnit().equals("month")) {
if (ipResourceRequestDto.getPeriod() == 6) {
ipResource.setPeriod(7); ipResource.setPeriod(7);
} else if (ipResourceRequestDto.getUnit().equals("month") && ipResourceRequestDto.getPeriod() == 12) { }
if (ipResourceRequestDto.getPeriod() == 12) {
ipResource.setPeriod(14); ipResource.setPeriod(14);
} }
}
// 根据 ip 的 vendor 来封装部分的 ipResource 信息
if (ipResourceRequestDto.getVendor().equals("local")) { if (ipResourceRequestDto.getVendor().equals("local")) {
ipResource.setAddr("本地Ip未使用"); ipResource.setAddr("本地Ip未使用");
ipResource.setIpType(IpType.LOCAL); ipResource.setIpType(IpType.LOCAL);
ipResource.setVendor(Vendor.valueOf(ipResourceRequestDto.getVendor())); ipResource.setVendor(Vendor.valueOf(ipResourceRequestDto.getVendor()));
ipResource.setVendorCn("本地"); ipResource.setVendorCn("本地");
ipResource.setStatus(4); ipResource.setStatus(4);
ipResource.setUsername(USERNAME);
long validTime = 0; long validTime = 0;
if (ipResourceRequestDto.getUnit().equals("week")) { if (ipResourceRequestDto.getUnit().equals("week")) {
...@@ -216,12 +221,10 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -216,12 +221,10 @@ public class IpResourceServiceImpl implements IpResourceService {
validTime = Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli(); validTime = Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli();
} }
ipResource.setValidTime(validTime); ipResource.setValidTime(validTime);
ipResource.setUsername(USERNAME);
ipResource.setPort(port); ipResource.setPort(port);
} else if (ipResourceRequestDto.getVendor().equals("own")) { } else if (ipResourceRequestDto.getVendor().equals("own")) {
if (ipResourceRequestDto.getAddr() == null || ipResourceRequestDto.getAddr().isEmpty()) { if (ipResourceRequestDto.getAddr() == null || ipResourceRequestDto.getAddr().isEmpty() || StringUtils.isBlank(ipResourceRequestDto.getAddr().get(i))) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
}
if (StringUtils.isBlank(ipResourceRequestDto.getAddr().get(i))) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
} }
...@@ -252,18 +255,18 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -252,18 +255,18 @@ public class IpResourceServiceImpl implements IpResourceService {
break; break;
} }
ipResource.setStatus(6); ipResource.setStatus(6);
long validTime = 0;
if (ipResourceRequestDto.getUnit().equals("week")) { if (ipResourceRequestDto.getUnit().equals("week")) {
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusWeeks(ipResource.getPeriod()).toInstant().toEpochMilli()); validTime = Instant.now().atZone(ZoneOffset.UTC).plusWeeks(ipResource.getPeriod()).toInstant().toEpochMilli();
} else { } else {
ipResource.setValidTime(Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli()); validTime = Instant.now().atZone(ZoneOffset.UTC).plusMonths(ipResource.getPeriod()).toInstant().toEpochMilli();
} }
ipResource.setValidTime(validTime);
ipResource.setUsername(USERNAME); ipResource.setUsername(USERNAME);
ipResource.setPort(port); ipResource.setPort(port);
} }
ipResource.setPurchasedTime(Instant.now().toEpochMilli());
if (account.getParent() != null) { if (account.getParent() != null) {
ipResource.setUserParent(account.getParent()); ipResource.setUserParent(account.getParent());
} }
...@@ -279,6 +282,7 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -279,6 +282,7 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource.setHealthLockTimestamp(Instant.now().minusSeconds(60 * 20).toEpochMilli()); ipResource.setHealthLockTimestamp(Instant.now().minusSeconds(60 * 20).toEpochMilli());
} }
ipResource.setPurchasedTime(Instant.now().toEpochMilli());
ipResource.setRegionCn(ipResourceRequestDto.getRegionCn()); ipResource.setRegionCn(ipResourceRequestDto.getRegionCn());
ipResource.setProtocol(protocol); ipResource.setProtocol(protocol);
ipResource.setPassword(password); ipResource.setPassword(password);
...@@ -296,10 +300,13 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -296,10 +300,13 @@ public class IpResourceServiceImpl implements IpResourceService {
} }
if (!ipResourceRequestDto.getVendor().equals("own")) { if (!ipResourceRequestDto.getVendor().equals("own")) {
// 封装购买 ip 的费用 信息
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResourceRequestDto, 1, ipResourceRequestDto.getPayMethod()); IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResourceRequestDto, 1, ipResourceRequestDto.getPayMethod());
// 用来记录购买与消费的操作 userbalance 与 费用明细 userprepaidbilling
accountService.chargeByMoney(username, newprice * ipChargeRequestDto.getAmount() * ipChargeRequestDto.getPeriod(), ipChargeRequestDto); accountService.chargeByMoney(username, newprice * ipChargeRequestDto.getAmount() * ipChargeRequestDto.getPeriod(), ipChargeRequestDto);
} }
// 如果ip需要绑定店铺,则将店铺与ip绑定
if (ipResourceRequestDto.getShopId() != null) { if (ipResourceRequestDto.getShopId() != null) {
ShopRequestDto shopRequestDto = new ShopRequestDto(); ShopRequestDto shopRequestDto = new ShopRequestDto();
shopRequestDto.setIpId(ipResourceDtos.get(0).getId()); shopRequestDto.setIpId(ipResourceDtos.get(0).getId());
......
...@@ -234,14 +234,14 @@ public class ShopServiceImpl implements ShopService { ...@@ -234,14 +234,14 @@ public class ShopServiceImpl implements ShopService {
ipAndShopService.unBindShop(username, shopRequestDto); ipAndShopService.unBindShop(username, shopRequestDto);
} }
// 4. 删除当前店铺关联的所有的 usershop信息 // 4. 获取当前店铺关联的所有的 usershop信息 并删除数据库中 usershop信息
List<UserShop> userShops = userShopRepository.findByShopId(shopId);
boolean result = userShopRepository.deleteByShopId(shopId); boolean result = userShopRepository.deleteByShopId(shopId);
if (result) { if (result) {
// 5. 删除当前店铺,并更新 account信息 todo // 5. 删除当前店铺,并更新 account信息 todo
shopRepository.deleteById(shopId); shopRepository.deleteById(shopId);
// 6. 更新 和店铺关联的所有的 account 的信息 // 6. 更新 和店铺关联的所有的 account 的信息
List<UserShop> userShops = userShopRepository.findByShopId(shopId);
List<Account> accountList = accountRepository.findByNameIn(userShops.stream().map(UserShop::getUsername).collect(Collectors.toList())); List<Account> accountList = accountRepository.findByNameIn(userShops.stream().map(UserShop::getUsername).collect(Collectors.toList()));
for (Account a : accountList) { for (Account a : accountList) {
a.setShopCount(account.getShopCount() - 1); a.setShopCount(account.getShopCount() - 1);
......
...@@ -43,13 +43,17 @@ public class PaymentTask { ...@@ -43,13 +43,17 @@ public class PaymentTask {
payment -> { payment -> {
PaymentMethod paymentMethod = payment.getPaymentMethod(); PaymentMethod paymentMethod = payment.getPaymentMethod();
try { try {
// 自动调用 微信或者支付宝的支付接口来确认 支付状态 /*
* 自动调用 微信或者支付宝的支付接口来确认 支付状态
* chargeType 取值范围为 0-4
* 此处设置为 100 仅仅只是为了避免 wxCheckOrderStatus() 方法中 chargeType == 0 相关代码块
*/
if (PaymentMethod.WECHAT.equals(paymentMethod)) { if (PaymentMethod.WECHAT.equals(paymentMethod)) {
paymentService.wxCheckOrderStatus(payment.getTradeNo()); paymentService.wxCheckOrderStatus(payment.getTradeNo(), 100);
} }
if (PaymentMethod.ALIPAY.equals(paymentMethod)) { if (PaymentMethod.ALIPAY.equals(paymentMethod)) {
paymentService.aliCheckOrderStatus(payment.getTradeNo()); paymentService.aliCheckOrderStatus(payment.getTradeNo(), 100);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("checkPayments", e); log.error("checkPayments", e);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment