first commit
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.peanut.modules.pay.alipay.config;
|
||||
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
|
||||
/**
|
||||
* 支付宝开放平台常量
|
||||
*/
|
||||
public class AliPayConfig {
|
||||
|
||||
/**
|
||||
* 花生appId 沙箱id 2021000117696096
|
||||
*/
|
||||
public static final String HS_APP_ID = "2021000118600784";
|
||||
|
||||
/**
|
||||
* 花生应用私钥
|
||||
*/
|
||||
public static final String HS_MERCHANT_PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCIrSciDRMFvEopt/F8O/SpQBZcyUXUcqe3rz2Sl0StpP5eu1IoB4GsX2wbYmOe6a0jfVrRLhvJINjYLrZiWUVGdSD3pkVtfQyp0pjmvi7sv/xtiUMhR7TZ2xWHieVN4h/4JsmdGMbirCnxJcqIKsdOkzHpm2CkmvwaBcLW1t5CGF5p2QKYAWVrtRfZemeXbseIHSQI0yhNVNA2moVVNny7+DMEY7jBwG/rsT5vymRa0R+Em4aBsi5LdR3h71hCxNwuizM8Z9WWamO6L0xj8s/FsaIxizprgcUH2WagqY0il5KBaOr3Kp/Tb8NvcTT4P9etwYV9YZAXqAkCHOo6zqbLAgMBAAECggEACPg8IjrzbaPuxhAhVALnSpg/H3XCFHA125Mvmkg9JJw4aWUnviLH+tUEVkO9v/cb+sFGTYYWIrv6Hy3poEi1iCs1r6sE0WusghSlHjeLAtSSr2mbszpufwGmxw9gshzN9k4tWxb+LnAYDJBrW0ZbcCeu1eLxk7vsIZURNWJCkdnIV2ColgqRPydvLAxPQTzz6P93BaG9jQrBBDhjpfqd1MI3QoRDSkFmSDAfv78DKV1G893lGWcbdDWdXNyR4EVmOLcT/Mmod1wY9dZSwT/faCYMekoVvXYl/r1KMq8HjgcHXArBELGayiA1dis2qO+Biz6t+ImJyHAK1LTsIRg4AQKBgQDsMWAzS8hbmj23EfPotmOLUZdIjfRlOLZ+zQ9PXn+Uxr0qWRmf3FFvcHm76onHFfkkYjIjqfibBVPeVP4cyYexEiHJSWbMNa8sRJAs8+2nyxufQqiN5l65KGGEVVREN6xWpacTdYGcF3RLeHlGrVYgJrAvkpCia2nLIUoIC0hyywKBgQCUI1eKJvQYiViG2BpjK1BUj12Pd0DmvyDI318N0xrpqWXjTklVwWHgESBM5lhtoTG4SKIclxKglnqsn9CC6IbiugFyxC6dWJpXi5Fl0R8CPcIUSwzQJAFUjgbMNqpx+9j/UkG/BETfFdvyb8GJ7mhKJIwOHzlPK3MzD3GEUegcAQKBgF8hU/nVIBGlAg5mMLMeIuHOLvyOEE1kljxkIzJ4gTJPCYmw8V0tw676wk0pneyNsRcyZbdvki3961W1LdQyZ3CuOOV1pu3BAZj21IsGhEX8zFdM5c6hZq100jIWnCYNtl//wFdaEKXp3HE5SL9quTGh43yFr7N7R9UEx2iOAtQZAoGANMJlkhmPhBGeit/6felWQdeRnd/+KHBXz9LmOrk984xWcl9WdQlbv365/0lmoxzu5VBRFFel1bXiWQrOK2F/cM6IZ4SBiZ6d0aIulZGmNpkiPzH/jT3Vj3TJ7P9nMxNcVIrKSarOkJlrbWvqBSL417cufnUWZyD1dXEBn/u/7AECgYB2722mJNfoRX6NAdP1VgJQ2i7Mt4Gv4RVLgYC9f3p/QETaexDvwuwbxzhTHPPBtcfCtlMlLhA5b6seFiQkBZJS+2NuJp/r7VRAw99ZBTb3Yn6NXXu713VUnZsisokKlqzNvLoH722NWPOVAmA8eAQnZqmWtPOkIcq6rjaGOKpnTA==";
|
||||
|
||||
/**
|
||||
* 花生支付宝应用公钥
|
||||
*/
|
||||
public static final String HS_ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiK0nIg0TBbxKKbfxfDv0qUAWXMlF1HKnt689kpdEraT+XrtSKAeBrF9sG2JjnumtI31a0S4bySDY2C62YllFRnUg96ZFbX0MqdKY5r4u7L/8bYlDIUe02dsVh4nlTeIf+CbJnRjG4qwp8SXKiCrHTpMx6ZtgpJr8GgXC1tbeQhheadkCmAFla7UX2Xpnl27HiB0kCNMoTVTQNpqFVTZ8u/gzBGO4wcBv67E+b8pkWtEfhJuGgbIuS3Ud4e9YQsTcLoszPGfVlmpjui9MY/LPxbGiMYs6a4HFB9lmoKmNIpeSgWjq9yqf02/Db3E0+D/XrcGFfWGQF6gJAhzqOs6mywIDAQAB";
|
||||
|
||||
//沙箱支付宝公钥
|
||||
|
||||
public static final String HS_ALI_PUBLIC_KEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmjFqCkYZ7/2TmINtDlFap8MUu/Z+SN6anxJH73LojB5qmQAHlUN5n55NvhcY+eaE6khpn9BCQDJ8URWs2DDhVKSw6EUwEFafkXmWNEAAjhpT9O5YrMACg8d0zhQirS4zrG/6tjPvx9X2X/CIohlu8jif79m46o11NI+iUmdiaT/IU4gMSUFqZ9KR9Qz6xzjJQ0tULHl+7S9qTQLbqqfJgzDEh06SU9F7KnQ0T9wIIPI7KCfn72kNMRUC2DGOZxFA8eztg1bprRhwufrT52ijEH+z/8hkyYVz1+MrrCaDGvSrC9xVj1FFzAofsMQxgKtSgjbgs0+reLoJ/uL/SKXWpQIDAQAB";
|
||||
|
||||
|
||||
/**
|
||||
* 回调地址
|
||||
*/
|
||||
public static final String NOTIFY_URL = "http://59.110.212.44:9100/pb/pay/aliPay/notify";
|
||||
// public static final String NOTIFY_URL = "http://kvv6y4.natappfree.cc/pb/pay/aliPay/notify";
|
||||
|
||||
/**
|
||||
* 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数
|
||||
*/
|
||||
public static final String RETURN_URL = "http://外网ip:端口/sccess.html";
|
||||
|
||||
/**
|
||||
* 签名方式
|
||||
*/
|
||||
public static final String SIGN_TYPE = "RSA2";
|
||||
|
||||
/**
|
||||
* 字符编码格式
|
||||
*/
|
||||
public static final String CHARSET = "utf-8";
|
||||
|
||||
/**
|
||||
* 返回格式
|
||||
*/
|
||||
public static String FORMAT = "json";
|
||||
|
||||
/**
|
||||
* 支付宝网关
|
||||
*/
|
||||
public static final String GATEWAYURL = "https://openapi.alipay.com/gateway.do";
|
||||
|
||||
private static AlipayClient alipayClient = null;
|
||||
|
||||
public static AlipayClient getAliPayClient() throws AlipayApiException {
|
||||
//实例化客户端
|
||||
if (alipayClient == null) {
|
||||
synchronized (AliPayUtil.class) {
|
||||
if (null == alipayClient) {
|
||||
alipayClient = new DefaultAlipayClient(GATEWAYURL, HS_APP_ID, HS_MERCHANT_PRIVATE_KEY,
|
||||
FORMAT, CHARSET, HS_ALIPAY_PUBLIC_KEY, SIGN_TYPE);
|
||||
}
|
||||
}
|
||||
}
|
||||
return alipayClient;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package com.peanut.modules.pay.alipay.config;
|
||||
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.domain.AlipayTradeAppPayModel;
|
||||
import com.alipay.api.domain.AlipayTradeQueryModel;
|
||||
import com.alipay.api.domain.AlipayTradeRefundModel;
|
||||
import com.alipay.api.request.AlipayTradeAppPayRequest;
|
||||
import com.alipay.api.request.AlipayTradeQueryRequest;
|
||||
import com.alipay.api.request.AlipayTradeRefundRequest;
|
||||
import com.alipay.api.request.AlipayTradeWapPayRequest;
|
||||
import com.alipay.api.response.AlipayTradeAppPayResponse;
|
||||
import com.alipay.api.response.AlipayTradeQueryResponse;
|
||||
import com.alipay.api.response.AlipayTradeRefundResponse;
|
||||
import com.alipay.api.response.AlipayTradeWapPayResponse;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.peanut.modules.pay.alipay.config.AliPayConfig;
|
||||
import com.peanut.modules.pay.alipay.dto.AlipayDTO;
|
||||
import com.peanut.modules.pay.alipay.dto.ReFundDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AliPayUtil {
|
||||
|
||||
private static ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 功能描述 下单支付
|
||||
*
|
||||
* @param aliPayDto
|
||||
* @return Map<String, Object>
|
||||
*/
|
||||
public Map<String, Object> aliPayOrder(AlipayDTO aliPayDto) {
|
||||
log.info(">>>>>>>>>>支付宝下单 订单号 = {}", aliPayDto.getOutTradeNo());
|
||||
Map map = new HashMap();
|
||||
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
|
||||
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
|
||||
//(否)对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。
|
||||
model.setBody(aliPayDto.getBody());
|
||||
//(是)商品的标题/交易标题/订单标题/订单关键字等
|
||||
model.setSubject(aliPayDto.getSubject());
|
||||
//(是)商户网站唯一订单号
|
||||
model.setOutTradeNo(aliPayDto.getOutTradeNo());
|
||||
//(是)订单总金额,单位为元,精确到小数点后两位
|
||||
model.setTotalAmount(aliPayDto.getTotalAmount());
|
||||
//(否)设置未付款支付宝交易的超时时间,一旦超时,该笔交易就会自动被关闭。
|
||||
model.setTimeoutExpress("30m");
|
||||
//(是)销售产品码,商家和支付宝签约的产品码,为固定值QUICK_MSECURITY_PAY
|
||||
model.setProductCode("QUICK_MSECURITY_PAY");
|
||||
request.setBizModel(model);
|
||||
request.setNotifyUrl(AliPayConfig.NOTIFY_URL);
|
||||
try {
|
||||
//这里和普通的接口调用不同,使用的是sdkExecute
|
||||
AlipayTradeAppPayResponse responseStr = AliPayConfig.getAliPayClient().sdkExecute(request);
|
||||
//可以直接给客户端请求,无需再做处理。
|
||||
map.put("msg", responseStr.getBody());
|
||||
} catch (AlipayApiException e) {
|
||||
log.info(">>>>>>>>>>支付出现异常 exception = {}", e);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* H5请求支付宝支付
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
public AlipayTradeWapPayResponse aliPayH5(AlipayDTO h5PayDto) {
|
||||
try {
|
||||
AlipayClient alipayClient = AliPayConfig.getAliPayClient();
|
||||
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
|
||||
request.setNotifyUrl(AliPayConfig.NOTIFY_URL);
|
||||
request.setBizContent(getAliPayRequest(h5PayDto).toString());
|
||||
request.setReturnUrl(h5PayDto.getReturnUrl());
|
||||
AlipayTradeWapPayResponse responseStr = alipayClient.pageExecute(request);
|
||||
log.info(responseStr.getBody());
|
||||
return responseStr;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能描述 退款申请
|
||||
*
|
||||
* @param reFundDTO
|
||||
* @return Map<String,Object>
|
||||
*/
|
||||
public AlipayTradeRefundResponse aliPayRefund(ReFundDTO reFundDTO) {
|
||||
log.info(">>>>>>>>>支付宝退款 订单号 = {}", reFundDTO.getOutTrandeNo());
|
||||
Map map = new HashMap();
|
||||
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
|
||||
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
|
||||
// 退款金额
|
||||
model.setRefundAmount(String.valueOf(reFundDTO.getRefundAmount()));
|
||||
// 支付宝交易号
|
||||
model.setTradeNo(reFundDTO.getTradeNo());
|
||||
// 订单支付时传入的商户订单号
|
||||
model.setOutTradeNo(reFundDTO.getOutTrandeNo());
|
||||
if (StringUtils.isNotBlank(reFundDTO.getRefundReason())) {
|
||||
// 退款原因
|
||||
model.setRefundReason(reFundDTO.getRefundReason());
|
||||
}
|
||||
request.setBizModel(model);
|
||||
AlipayTradeRefundResponse response = null;
|
||||
try {
|
||||
response = AliPayConfig.getAliPayClient().execute(request);
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
log.info(">>>>>>>>>>退款出现异常 exception = {}", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 功能描述 查询订单
|
||||
* @param outTradeNo
|
||||
*/
|
||||
public AlipayTradeQueryResponse getAliPayOrder(String outTradeNo) {
|
||||
log.info(">>>>>>>>>查询支付宝订单 订单号 = {}", outTradeNo);
|
||||
Map map = new HashMap();
|
||||
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
|
||||
AlipayTradeQueryModel model = new AlipayTradeQueryModel();
|
||||
model.setOutTradeNo(outTradeNo);
|
||||
request.setBizModel(model);
|
||||
AlipayTradeQueryResponse response = null;
|
||||
try {
|
||||
response = AliPayConfig.getAliPayClient().execute(request);
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
log.info(">>>>>>>>>>查询订单出现异常 exception = {}", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
private ObjectNode getAliPayRequest(AlipayDTO h5PayDto) {
|
||||
ObjectNode obj = mapper.createObjectNode();
|
||||
//obj.put("subject", URLEncoder.encode(payRequest.getBody(), "GBK"));
|
||||
obj.put("subject", h5PayDto.getSubject());
|
||||
obj.put("out_trade_no", h5PayDto.getOutTradeNo());
|
||||
obj.put("timeout_express", 30d);
|
||||
//obj.put("time_expire", DateTimeUtils.getYmdhmFormatDate(System.currentTimeMillis() + DateUtils.DAY_IN_MILLS));
|
||||
obj.put("total_amount", h5PayDto.getTotalAmount());
|
||||
obj.put("quit_url", h5PayDto.getReturnUrl());
|
||||
obj.put("product_code", "QUICK_WAP_PAY");
|
||||
obj.put("body", h5PayDto.getBody());
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.peanut.modules.pay.alipay.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.modules.pay.alipay.dto.AlipayDTO;
|
||||
import com.peanut.modules.pay.alipay.service.AliPayService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 支付宝支付控制层
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/pay/aliPay")
|
||||
@Slf4j
|
||||
public class AliPayController {
|
||||
|
||||
@Autowired
|
||||
private AliPayService aliPayService;
|
||||
|
||||
/**
|
||||
* 请求支付宝接口支付
|
||||
*
|
||||
* @param payDto
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/pay")
|
||||
public R pay(AlipayDTO payDto) {
|
||||
String pay = aliPayService.pay(payDto);
|
||||
return R.ok().put("orderInfo",pay);
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping( "/notify")
|
||||
public R notify_url(HttpServletRequest request) {
|
||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||
String jsonStr = JSONObject.toJSONString(parameterMap);
|
||||
System.out.println("支付宝回调信息=============》"+jsonStr);
|
||||
aliPayService.aliNotify(request);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.peanut.modules.pay.alipay.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 支付实体对象
|
||||
*/
|
||||
@Data
|
||||
public class AlipayDTO {
|
||||
/**
|
||||
* 商户订单号,必填
|
||||
*/
|
||||
private String outTradeNo;
|
||||
|
||||
|
||||
/**
|
||||
* 订单名称,必填
|
||||
*/
|
||||
private String subject;
|
||||
|
||||
/**
|
||||
* 付款金额,必填
|
||||
*/
|
||||
private String totalAmount;
|
||||
|
||||
/**
|
||||
* 商品描述,可空
|
||||
*/
|
||||
private String body;
|
||||
|
||||
/**
|
||||
* 超时时间参数
|
||||
*/
|
||||
private String timeoutExpress = "10m";
|
||||
|
||||
/**
|
||||
* 销售产品码,与支付宝签约的产品码名称
|
||||
*/
|
||||
private String productCode = "QUICK_MSECURITY_PAY";
|
||||
|
||||
/**
|
||||
* 支付成功后回跳地址,H5支付特有
|
||||
*/
|
||||
private String returnUrl;
|
||||
|
||||
/**
|
||||
* 类型 如果type ==1。是H5支付,如果type == 2是app支付
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 客户编号
|
||||
*/
|
||||
private String customerId;
|
||||
|
||||
private String relevanceoid;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.peanut.modules.pay.alipay.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 退款实体对象
|
||||
*/
|
||||
@Data
|
||||
public class ReFundDTO {
|
||||
|
||||
/**
|
||||
* 退款金额
|
||||
*/
|
||||
public BigDecimal refundAmount;
|
||||
|
||||
/**
|
||||
* 支付时商户订单号
|
||||
*/
|
||||
public String outTrandeNo;
|
||||
|
||||
/**
|
||||
* 支付宝交易编号
|
||||
*/
|
||||
public String tradeNo;
|
||||
|
||||
/**
|
||||
* 退款原因
|
||||
*/
|
||||
private String refundReason;
|
||||
|
||||
/**
|
||||
* 客户编号
|
||||
*/
|
||||
private String customerId;
|
||||
|
||||
}
|
||||
@@ -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