first commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.peanut.modules.pay.alipay.service;
|
||||
|
||||
|
||||
|
||||
import com.peanut.modules.pay.alipay.dto.AlipayDTO;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
|
||||
public interface AliPayService {
|
||||
|
||||
/**
|
||||
* 支付
|
||||
* @param alipayDTO
|
||||
* @return
|
||||
*/
|
||||
String pay(AlipayDTO alipayDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 支付宝异步回调
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
String aliNotify(HttpServletRequest request);
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package com.peanut.modules.pay.alipay.service.impl;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.internal.util.AlipaySignature;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.peanut.common.utils.CopyUtils;
|
||||
import com.peanut.common.utils.OrderUtils;
|
||||
import com.peanut.modules.book.entity.*;
|
||||
import com.peanut.modules.book.service.*;
|
||||
import com.peanut.modules.pay.alipay.config.AliPayConfig;
|
||||
import com.peanut.modules.pay.alipay.config.AliPayUtil;
|
||||
import com.peanut.modules.pay.alipay.dto.AlipayDTO;
|
||||
import com.peanut.modules.pay.alipay.service.AliPayService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 支付宝支付实现
|
||||
*/
|
||||
@Service("AliPayService")
|
||||
@Slf4j
|
||||
public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
@Autowired
|
||||
private AliPayUtil aliPayUtil;
|
||||
@Autowired
|
||||
private MyUserService userService;
|
||||
@Autowired
|
||||
private PayZfbOrderService payZfbOrderService;
|
||||
@Autowired
|
||||
private BookBuyConfigService bookBuyConfigService;
|
||||
@Autowired
|
||||
private TransactionDetailsService transactionDetailsService;
|
||||
@Autowired
|
||||
private PayPaymentOrderService payPaymentOrderService;
|
||||
@Autowired
|
||||
private BuyOrderService buyOrderService;
|
||||
|
||||
@Override
|
||||
public String pay(AlipayDTO payDto) {
|
||||
Integer type = payDto.getType();
|
||||
String orderInfo = "";
|
||||
if (type == 1) {
|
||||
// 走H5请求支付宝支付接口
|
||||
log.info(">>>>>>>>>>H5请求支付宝支付接口");
|
||||
String result = aliPayUtil.aliPayH5(payDto).getBody();
|
||||
log.info(">>>>>>>>>>>支付宝返回的信息是 result = {}", result);
|
||||
orderInfo = result;
|
||||
}
|
||||
if (type == 2) {
|
||||
// 走APP请求支付宝支付接口
|
||||
log.info(">>>>>>>>>>App请求支付宝支付接口");
|
||||
payDto.setOutTradeNo(OrderUtils.getOrderCode(Integer.valueOf(payDto.getCustomerId())));
|
||||
Map<String, Object> map = aliPayUtil.aliPayOrder(payDto);
|
||||
Object obj = map.get("msg");
|
||||
String resJson = obj.toString();
|
||||
|
||||
log.info(">>>>>>>>>>>支付宝返回的信息是 resJson = {}", resJson);
|
||||
PayZfbOrderEntity aliNotifyDto = new PayZfbOrderEntity();
|
||||
aliNotifyDto.setOutTradeNo(payDto.getOutTradeNo());
|
||||
aliNotifyDto.setCustomerid(payDto.getCustomerId());
|
||||
aliNotifyDto.setRelevanceoid(payDto.getRelevanceoid());
|
||||
payZfbOrderService.save(aliNotifyDto);
|
||||
BuyOrderEntity order = this.buyOrderService.getOne(new QueryWrapper<BuyOrderEntity>().eq("order_sn",payDto.getRelevanceoid()).eq("del_flag","0"));
|
||||
order.setPaymentDate(new Date());
|
||||
this.buyOrderService.updateById(order);
|
||||
|
||||
orderInfo = resJson;
|
||||
}
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public String aliNotify(HttpServletRequest request) {
|
||||
log.info(">>>>>>>>>>支付宝异步回调开始<<<<<<<<<<");
|
||||
Map<String, String> params = new HashMap<>();
|
||||
try {
|
||||
Map requestParams = request.getParameterMap();
|
||||
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
|
||||
String name = (String) iter.next();
|
||||
String[] values = (String[]) requestParams.get(name);
|
||||
String valueStr = "";
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
valueStr = (i == values.length - 1) ? valueStr + values[i]
|
||||
: valueStr + values[i] + ",";
|
||||
}
|
||||
valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
|
||||
params.put(name, valueStr);
|
||||
}
|
||||
log.info(">>>>>>>>>>支付宝回调 sign = {}, trade_status = {}, out_trade_no = {}, 参数 = {}", params.get("sign"),
|
||||
params.get("trade_status"), params.get("out_trade_no"), params.toString());
|
||||
//调用SDK验证签名,验证是阿里回调,而不是其他恶意回调
|
||||
boolean flag = AlipaySignature.rsaCheckV1(params, AliPayConfig.HS_ALI_PUBLIC_KEY, AliPayConfig.CHARSET, AliPayConfig.SIGN_TYPE);
|
||||
log.info(">>>>>>>>>>验签结果 flag = {}", flag);
|
||||
if (flag) {
|
||||
log.info(">>>>>>>>>>验签通过");
|
||||
//验签通过 获取交易状态
|
||||
String tradeStatus = params.get("trade_status");
|
||||
//只处理支付成功的订单: 修改交易表状态,支付成功
|
||||
//只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,支付宝才会认定为买家付款成功。
|
||||
if (tradeStatus.equals("TRADE_SUCCESS") || tradeStatus.equals("TRADE_FINISHED")) {
|
||||
//TODO 获取支付宝通知完成充值后续业务
|
||||
//交易成功 获取商户订单号
|
||||
/**修改订单信息*/
|
||||
String toJSON = JSONObject.toJSONString(params);
|
||||
PayZfbOrderEntity aliNotifyDto = JSONObject.toJavaObject(JSON.parseObject(toJSON), PayZfbOrderEntity.class);
|
||||
//判断数据库是否存在当前订单
|
||||
PayZfbOrderEntity payZfbOrderEntity = payZfbOrderService.getOne(new QueryWrapper<PayZfbOrderEntity>().eq("trade_no", aliNotifyDto.getTradeNo()));
|
||||
if (payZfbOrderEntity != null) {
|
||||
return "success";
|
||||
}
|
||||
PayZfbOrderEntity oldPayZfbOrderEntity = payZfbOrderService.getOne(new QueryWrapper<PayZfbOrderEntity>().eq("out_trade_no", aliNotifyDto.getOutTradeNo()));
|
||||
CopyUtils.copyProperties(aliNotifyDto,oldPayZfbOrderEntity);
|
||||
|
||||
payZfbOrderService.updateById(oldPayZfbOrderEntity);
|
||||
|
||||
String subject = oldPayZfbOrderEntity.getSubject();
|
||||
|
||||
// 会员开通
|
||||
String body = oldPayZfbOrderEntity.getBody();
|
||||
String customerid = oldPayZfbOrderEntity.getCustomerid();
|
||||
|
||||
if ("vip".equals(subject)) {
|
||||
//获取会员开通 日期
|
||||
|
||||
BookBuyConfigEntity bookBuyConfigEntity = bookBuyConfigService.getById(Integer.valueOf(body));
|
||||
String month = bookBuyConfigEntity.getMonth();
|
||||
userService.openMember(Integer.valueOf(customerid),Integer.valueOf(month));
|
||||
|
||||
// 插入 开通记录
|
||||
|
||||
buyOrderService.updateOrderStatus(Integer.valueOf(customerid),oldPayZfbOrderEntity.getRelevanceoid(),"2");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if("point".equals(subject)){
|
||||
// 插入花生币 变动记录
|
||||
BookBuyConfigEntity bookBuyConfigEntity = bookBuyConfigService.getById(Integer.valueOf(body));
|
||||
String realMoney = bookBuyConfigEntity.getRealMoney();
|
||||
Integer money = Integer.valueOf(realMoney);
|
||||
userService.rechargeHSPoint(Integer.valueOf(customerid),money);
|
||||
TransactionDetailsEntity transactionDetailsEntity = new TransactionDetailsEntity();
|
||||
transactionDetailsEntity.setUserId(Integer.valueOf(customerid));
|
||||
transactionDetailsEntity.setChangeAmount(new BigDecimal(money));
|
||||
transactionDetailsEntity.setOrderType("充值");
|
||||
transactionDetailsEntity.setRelationId(oldPayZfbOrderEntity.getId().intValue());
|
||||
transactionDetailsEntity.setRemark("充值");
|
||||
MyUserEntity user = userService.getById(Integer.valueOf(customerid));
|
||||
Integer peanutCoin = user.getPeanutCoin();
|
||||
BigDecimal balance = new BigDecimal(peanutCoin);
|
||||
transactionDetailsEntity.setUserBalance(balance);
|
||||
transactionDetailsService.save(transactionDetailsEntity);
|
||||
// 插入 花生币 充值记录
|
||||
PayPaymentOrderEntity payPaymentOrderEntity = new PayPaymentOrderEntity();
|
||||
payPaymentOrderEntity.setUserId(Integer.valueOf(customerid));
|
||||
payPaymentOrderEntity.setOrderId(String.valueOf(oldPayZfbOrderEntity.getId()));
|
||||
payPaymentOrderEntity.setRealAmount(new BigDecimal(bookBuyConfigEntity.getRealMoney()));
|
||||
payPaymentOrderEntity.setRechargeAmount(new BigDecimal(bookBuyConfigEntity.getMoney()));
|
||||
payPaymentOrderEntity.setRechargeChannel(bookBuyConfigEntity.getQudao());
|
||||
payPaymentOrderEntity.setRechargeStatus("success");
|
||||
payPaymentOrderEntity.setSuccessTime(new Date());
|
||||
payPaymentOrderService.save(payPaymentOrderEntity);
|
||||
buyOrderService.updateOrderStatus(Integer.valueOf(customerid),oldPayZfbOrderEntity.getRelevanceoid(),"2");
|
||||
}
|
||||
if ("order".equals(subject)) {
|
||||
BuyOrderEntity orderEntity = buyOrderService.getBaseMapper().selectOne(new QueryWrapper<BuyOrderEntity>().eq("order_sn", oldPayZfbOrderEntity.getRelevanceoid()));
|
||||
BigDecimal realMoney = orderEntity.getRealMoney();
|
||||
|
||||
BigDecimal bigDecimal = new BigDecimal(oldPayZfbOrderEntity.getBuyerPayAmount());
|
||||
|
||||
//更新 订单 记录
|
||||
if (bigDecimal.compareTo(realMoney) == 0) {
|
||||
buyOrderService.updateOrderStatus(Integer.valueOf(customerid),oldPayZfbOrderEntity.getRelevanceoid(),"0");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**余额到账*/
|
||||
//这里就主要是做你们自己的业务需求了,修改一些表什么的..
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
log.info(">>>>>>>>>>验签失败");
|
||||
return "fail";
|
||||
|
||||
} catch (Exception e) {
|
||||
log.info(">>>>>>>>>>订单回调异常: 订单号 = {}, msg = {}", params.get("out_trade_no"), e.getMessage());
|
||||
return "fail";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//TODO 更新订单状态
|
||||
|
||||
|
||||
|
||||
//TODO 更新用户优惠券状态
|
||||
|
||||
|
||||
|
||||
//TODO 推送一路健康优惠券
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user