refactor: 新增AliPayController,重构AliPayIntegration类,改为实现 AliPayService 接口
parent
ee27b542cd
commit
8d04847cd2
|
@ -146,7 +146,7 @@ public class OssUtil {
|
|||
}
|
||||
|
||||
public static String getOssFilePath(String filePath){
|
||||
String fileSuf = filePath.substring(filePath.indexOf(".") + 1);
|
||||
String fileSuf = filePath.substring(filePath.lastIndexOf(".") + 1);
|
||||
return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,211 @@
|
|||
package com.mcwl.web.controller.pay.AliPay;
|
||||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.extra.qrcode.QrCodeUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alipay.easysdk.factory.Factory;
|
||||
import com.alipay.easysdk.kernel.Config;
|
||||
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
|
||||
import com.mcwl.common.JSONUtils;
|
||||
import com.mcwl.common.annotation.Anonymous;
|
||||
import com.mcwl.common.core.controller.BaseController;
|
||||
import com.mcwl.common.core.domain.AjaxResult;
|
||||
import com.mcwl.common.core.page.TableDataInfo;
|
||||
import com.mcwl.common.core.redis.RedisCache;
|
||||
import com.mcwl.common.domain.IdsParam;
|
||||
import com.mcwl.common.utils.SecurityUtils;
|
||||
import com.mcwl.pay.domain.OrderTrade;
|
||||
import com.mcwl.pay.domain.OrderTradeDto;
|
||||
import com.mcwl.pay.service.AliPayService;
|
||||
import com.mcwl.pay.service.OrderTradeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author:ChenYan
|
||||
* @Project:McWl
|
||||
* @Package:com.mcwl.web.controller.pay
|
||||
* @Filename:OrderTradeController
|
||||
* @Description 支付模块
|
||||
* @Date:2025/1/3 14:46
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/ali/pay")
|
||||
@Validated
|
||||
public class AliPayController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private OrderTradeService orderTradeService;
|
||||
|
||||
@Autowired
|
||||
private AliPayService aliPayService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
|
||||
/**
|
||||
* 授权二维码
|
||||
* @param response 响应
|
||||
* @throws Exception 抛出异常
|
||||
*/
|
||||
@GetMapping("/generateQrCode")
|
||||
public void generateQrCode(HttpServletResponse response) throws Exception {
|
||||
String scope = "auth_user"; // 需要获取用户信息
|
||||
String state = RandomUtil.randomString(3); // 防止CSRF攻击
|
||||
|
||||
String encodedRedirectUri = URLEncoder.encode("https://3195d9a3.r27.cpolar.top/ali/pay/callback", "UTF-8");
|
||||
String authUrl = String.format(
|
||||
"https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=%s&scope=%s&redirect_uri=%s&state=%s",
|
||||
"2021005114616085", scope, encodedRedirectUri, state
|
||||
);
|
||||
|
||||
QrCodeUtil.generate(authUrl, 300, 300, "png", response.getOutputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权回调
|
||||
* @param authCode 授权码
|
||||
*/
|
||||
@GetMapping("/callback")
|
||||
public void callback(@RequestParam("auth_code") String authCode) {
|
||||
|
||||
System.out.println("authCode = " + authCode);
|
||||
aliPayService.bindingCallback(authCode);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支付接口
|
||||
*
|
||||
* @param orderTradeDto 订单实体
|
||||
* @param response 响应
|
||||
* @throws Exception
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/doPay")
|
||||
public void doPay(@RequestBody OrderTradeDto orderTradeDto, HttpServletResponse response) throws Exception {
|
||||
String qrUrl = null;
|
||||
|
||||
String type = orderTradeDto.getType();
|
||||
|
||||
if ("member".equalsIgnoreCase(type)) {
|
||||
qrUrl = aliPayService.memberPay(orderTradeDto);
|
||||
} else if ("points".equalsIgnoreCase(type)) {
|
||||
qrUrl = aliPayService.pointsPay(orderTradeDto.getPaymentAmount());
|
||||
}
|
||||
|
||||
QrCodeUtil.generate(qrUrl, 300, 300, "png", response.getOutputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看余额
|
||||
*/
|
||||
@GetMapping("/balance")
|
||||
public void balance() throws Exception {
|
||||
aliPayService.balance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现接口
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/withdraw")
|
||||
public void withdraw(@RequestBody OrderTradeDto orderTradeDto, HttpServletResponse response) throws Exception {
|
||||
String outBizNo = UUID.fastUUID().toString(true);
|
||||
String payerUserId = "2088102167258880";
|
||||
String payeeUserId = "2088102167258880";
|
||||
String amount = "100";
|
||||
aliPayService.transfer(outBizNo, payerUserId, payeeUserId, amount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支付回调接口
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/notify") // 注意这里必须是POST接口
|
||||
public String payNotify(HttpServletRequest request) throws Exception {
|
||||
|
||||
|
||||
if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
|
||||
System.out.println("=========支付宝异步回调========");
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (String name : requestParams.keySet()) {
|
||||
params.put(name, request.getParameter(name));
|
||||
// System.out.println(name + " = " + request.getParameter(name));
|
||||
}
|
||||
|
||||
// 支付宝验签
|
||||
if (Factory.Payment.Common().verifyNotify(params)) {
|
||||
// System.out.println("交易名称: " + params.get("subject"));
|
||||
// System.out.println("交易状态: " + params.get("trade_status"));
|
||||
// System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
|
||||
// System.out.println("商户订单号: " + params.get("out_trade_no"));
|
||||
// System.out.println("交易金额: " + params.get("total_amount"));
|
||||
// System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
|
||||
// System.out.println("买家付款时间: " + params.get("gmt_payment"));
|
||||
// System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
|
||||
// 验签通过
|
||||
|
||||
String code = params.get("out_trade_no"); // 商户订单号
|
||||
// 获取订单后缀
|
||||
String suffix = code.substring(code.lastIndexOf("_") + 1);
|
||||
String orderTradeJson = redisCache.getCacheObject(code);
|
||||
OrderTrade orderTrade = JSONUtil.toBean(orderTradeJson, OrderTrade.class);
|
||||
|
||||
orderTradeService.orderHandler(orderTrade, suffix, params);
|
||||
|
||||
// 更新订单状态
|
||||
// if (!StringUtils.isEmpty(orderTradeJson)) {
|
||||
// OrderTrade orderTrade = JSONUtil.toBean(orderTradeJson, OrderTrade.class);
|
||||
// // 支付宝交易凭证号
|
||||
// orderTrade.setPaymentMethod(params.get("trade_no"));
|
||||
// orderTrade.setTransactionId(1);
|
||||
// orderTrade.setOrderTime(DateUtils.parseDate(params.get("gmt_payment")));
|
||||
// orderTrade.setOrderStatus(3);
|
||||
// orderTrade.setPayStatus(2);
|
||||
// String totalAmountStr = params.get("total_amount");
|
||||
// if (totalAmountStr != null && !totalAmountStr.isEmpty()) {
|
||||
// BigDecimal totalAmount = new BigDecimal(totalAmountStr);
|
||||
// orderTrade.setTotalAmount(totalAmount.intValue());
|
||||
// }
|
||||
// String buyerPayAmountStr = params.get("buyer_pay_amount");
|
||||
// if (buyerPayAmountStr != null && !buyerPayAmountStr.isEmpty()) {
|
||||
// BigDecimal buyerPayAmount = new BigDecimal(buyerPayAmountStr);
|
||||
// orderTrade.setPaymentAmount(buyerPayAmount.intValue());
|
||||
// }
|
||||
// orderTradeService.save(orderTrade);
|
||||
// }
|
||||
} else {
|
||||
// 验签失败
|
||||
System.out.println("验签失败");
|
||||
}
|
||||
} else {
|
||||
// 验签失败
|
||||
System.out.println("验签失败");
|
||||
}
|
||||
|
||||
return "success";
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,82 +1,127 @@
|
|||
package com.mcwl.web.controller.pay.AliPay;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.AlipayConfig;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.alipay.api.diagnosis.DiagnosisUtils;
|
||||
import com.alipay.api.domain.AlipayFundAccountQueryModel;
|
||||
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
|
||||
import com.alipay.api.domain.Participant;
|
||||
import com.alipay.api.request.AlipayFundAccountQueryRequest;
|
||||
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
|
||||
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
|
||||
import com.alipay.api.request.AlipayUserInfoAuthRequest;
|
||||
import com.alipay.api.response.AlipayFundAccountQueryResponse;
|
||||
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
|
||||
import com.alipay.easysdk.base.oauth.models.AlipaySystemOauthTokenResponse;
|
||||
import com.alipay.easysdk.factory.Factory;
|
||||
import com.alipay.easysdk.kernel.Config;
|
||||
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
|
||||
import com.mcwl.common.core.redis.RedisCache;
|
||||
import com.mcwl.common.exception.ServiceException;
|
||||
import com.mcwl.common.utils.SecurityUtils;
|
||||
import com.mcwl.memberCenter.domain.MemberLevel;
|
||||
import com.mcwl.memberCenter.service.MemberLevelService;
|
||||
import com.mcwl.pay.config.AliConfig;
|
||||
import com.mcwl.pay.domain.OrderTrade;
|
||||
import com.mcwl.pay.domain.OrderTradeDto;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 支付宝支付
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AliPayIntegration {
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
|
||||
@Autowired
|
||||
private MemberLevelService memberLevelService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private AliConfig aliConfig;
|
||||
|
||||
|
||||
public AliPayIntegration(Config config) {
|
||||
Factory.setOptions(config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调用支付宝预下订单接口
|
||||
*
|
||||
* @param orderTradeDto 订单实体
|
||||
* @return 二维码url
|
||||
* @throws Exception
|
||||
*/
|
||||
// public String orderPay(OrderTradeDto orderTradeDto) throws Exception {
|
||||
//package com.mcwl.web.controller.pay.AliPay;
|
||||
//
|
||||
//import cn.hutool.core.bean.BeanUtil;
|
||||
//import cn.hutool.core.lang.UUID;
|
||||
//import cn.hutool.json.JSONUtil;
|
||||
//import com.alibaba.fastjson.JSONObject;
|
||||
//import com.alipay.api.AlipayApiException;
|
||||
//import com.alipay.api.AlipayClient;
|
||||
//import com.alipay.api.AlipayConfig;
|
||||
//import com.alipay.api.DefaultAlipayClient;
|
||||
//import com.alipay.api.diagnosis.DiagnosisUtils;
|
||||
//import com.alipay.api.domain.AlipayFundAccountQueryModel;
|
||||
//import com.alipay.api.domain.AlipayFundTransUniTransferModel;
|
||||
//import com.alipay.api.domain.Participant;
|
||||
//import com.alipay.api.request.AlipayFundAccountQueryRequest;
|
||||
//import com.alipay.api.request.AlipayFundTransUniTransferRequest;
|
||||
//import com.alipay.api.request.AlipaySystemOauthTokenRequest;
|
||||
//import com.alipay.api.request.AlipayUserInfoAuthRequest;
|
||||
//import com.alipay.api.response.AlipayFundAccountQueryResponse;
|
||||
//import com.alipay.api.response.AlipayFundTransUniTransferResponse;
|
||||
//import com.alipay.easysdk.base.oauth.models.AlipaySystemOauthTokenResponse;
|
||||
//import com.alipay.easysdk.factory.Factory;
|
||||
//import com.alipay.easysdk.kernel.Config;
|
||||
//import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
|
||||
//import com.mcwl.common.core.redis.RedisCache;
|
||||
//import com.mcwl.common.exception.ServiceException;
|
||||
//import com.mcwl.common.utils.SecurityUtils;
|
||||
//import com.mcwl.memberCenter.domain.MemberLevel;
|
||||
//import com.mcwl.memberCenter.service.MemberLevelService;
|
||||
//import com.mcwl.pay.config.AliConfig;
|
||||
//import com.mcwl.pay.domain.OrderTrade;
|
||||
//import com.mcwl.pay.domain.OrderTradeDto;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import java.util.Optional;
|
||||
//import java.util.concurrent.TimeUnit;
|
||||
//
|
||||
///**
|
||||
// * 支付宝支付
|
||||
// */
|
||||
//@Component
|
||||
//@Slf4j
|
||||
//public class AliPayIntegration {
|
||||
//
|
||||
// @Autowired
|
||||
// private RedisCache redisCache;
|
||||
//
|
||||
//
|
||||
// @Autowired
|
||||
// private MemberLevelService memberLevelService;
|
||||
//
|
||||
//
|
||||
// @Autowired
|
||||
// private AliConfig aliConfig;
|
||||
//
|
||||
//
|
||||
// public AliPayIntegration(Config config) {
|
||||
// Factory.setOptions(config);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 调用支付宝预下订单接口
|
||||
// *
|
||||
// * @param orderTradeDto 订单实体
|
||||
// * @return 二维码url
|
||||
// * @throws Exception
|
||||
// */
|
||||
//// public String orderPay(OrderTradeDto orderTradeDto) throws Exception {
|
||||
//// Integer productId = orderTradeDto.getProductId();
|
||||
//// if (!Optional.ofNullable(productId).isPresent()) {
|
||||
//// throw new ServiceException("mallProductId不能为空");
|
||||
//// }
|
||||
////
|
||||
//// MallProduct mallProduct = mallProductService.getById(productId);
|
||||
//// if (!Optional.ofNullable(mallProduct).isPresent()) {
|
||||
//// throw new ServiceException("mallProduct不存在");
|
||||
//// }
|
||||
////
|
||||
//// // 设置orderTrade信息
|
||||
//// OrderTrade tradeEntity = new OrderTrade();
|
||||
//// BeanUtil.copyProperties(orderTradeDto, tradeEntity);
|
||||
//// tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
//// tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
//// tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
//// tradeEntity.setProductName(mallProduct.getProductName());
|
||||
////
|
||||
//// //调用支付宝的接口
|
||||
//// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
//// .preCreate(mallProduct.getProductName(),
|
||||
//// tradeEntity.getCode() + "_product",
|
||||
//// orderTradeDto.getPaymentAmount().toString());
|
||||
//// // 缓存到redis
|
||||
//// redisCache.setCacheObject(tradeEntity.getCode() + "_product", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
////// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace().preCreate("订单主题:Mac笔记本", "LS123qwe123", "19999");
|
||||
//// //参照官方文档响应示例,解析返回结果
|
||||
//// String httpBodyStr = payResponse.getHttpBody();
|
||||
//// JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
//// return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
//// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 调用支付宝预下订单接口
|
||||
// *
|
||||
// * @param orderTradeDto 订单实体
|
||||
// * @return 二维码url
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public String memberPay(OrderTradeDto orderTradeDto) throws Exception {
|
||||
//
|
||||
// // 会员等级id
|
||||
// Integer productId = orderTradeDto.getProductId();
|
||||
//
|
||||
// if (!Optional.ofNullable(productId).isPresent()) {
|
||||
// throw new ServiceException("mallProductId不能为空");
|
||||
// throw new ServiceException("memberLevelId不能为空");
|
||||
// }
|
||||
//
|
||||
// MallProduct mallProduct = mallProductService.getById(productId);
|
||||
// if (!Optional.ofNullable(mallProduct).isPresent()) {
|
||||
// throw new ServiceException("mallProduct不存在");
|
||||
// MemberLevel memberLevel = memberLevelService.getById(productId);
|
||||
//
|
||||
// if (!Optional.ofNullable(memberLevel).isPresent()) {
|
||||
// throw new ServiceException("memberLevel不存在");
|
||||
// }
|
||||
//
|
||||
// // 设置orderTrade信息
|
||||
|
@ -85,232 +130,187 @@ public class AliPayIntegration {
|
|||
// tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
// tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
// tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
// tradeEntity.setProductName(mallProduct.getProductName());
|
||||
// tradeEntity.setProductName(memberLevel.getMemberName());
|
||||
//
|
||||
// //调用支付宝的接口
|
||||
// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
// .preCreate(mallProduct.getProductName(),
|
||||
// tradeEntity.getCode() + "_product",
|
||||
// // 设置过期时
|
||||
// .preCreate(memberLevel.getMemberName(),
|
||||
// tradeEntity.getCode() + "_member",
|
||||
// orderTradeDto.getPaymentAmount().toString());
|
||||
// // 缓存到redis
|
||||
// redisCache.setCacheObject(tradeEntity.getCode() + "_product", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
// redisCache.setCacheObject(tradeEntity.getCode() + "_member", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
// redisCache.setCacheObject(tradeEntity.getCode() + "_member" + "_promotionId", orderTradeDto.getPromotionId(), 3, TimeUnit.MINUTES);
|
||||
//// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace().preCreate("订单主题:Mac笔记本", "LS123qwe123", "19999");
|
||||
// //参照官方文档响应示例,解析返回结果
|
||||
// String httpBodyStr = payResponse.getHttpBody();
|
||||
// JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
// return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* 调用支付宝预下订单接口
|
||||
*
|
||||
* @param orderTradeDto 订单实体
|
||||
* @return 二维码url
|
||||
* @throws Exception
|
||||
*/
|
||||
public String memberPay(OrderTradeDto orderTradeDto) throws Exception {
|
||||
|
||||
// 会员等级id
|
||||
Integer productId = orderTradeDto.getProductId();
|
||||
|
||||
if (!Optional.ofNullable(productId).isPresent()) {
|
||||
throw new ServiceException("memberLevelId不能为空");
|
||||
}
|
||||
|
||||
MemberLevel memberLevel = memberLevelService.getById(productId);
|
||||
|
||||
if (!Optional.ofNullable(memberLevel).isPresent()) {
|
||||
throw new ServiceException("memberLevel不存在");
|
||||
}
|
||||
|
||||
// 设置orderTrade信息
|
||||
OrderTrade tradeEntity = new OrderTrade();
|
||||
BeanUtil.copyProperties(orderTradeDto, tradeEntity);
|
||||
tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
tradeEntity.setProductName(memberLevel.getMemberName());
|
||||
|
||||
//调用支付宝的接口
|
||||
AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
// 设置过期时
|
||||
.preCreate(memberLevel.getMemberName(),
|
||||
tradeEntity.getCode() + "_member",
|
||||
orderTradeDto.getPaymentAmount().toString());
|
||||
// 缓存到redis
|
||||
redisCache.setCacheObject(tradeEntity.getCode() + "_member", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
redisCache.setCacheObject(tradeEntity.getCode() + "_member" + "_promotionId", orderTradeDto.getPromotionId(), 3, TimeUnit.MINUTES);
|
||||
// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace().preCreate("订单主题:Mac笔记本", "LS123qwe123", "19999");
|
||||
//参照官方文档响应示例,解析返回结果
|
||||
String httpBodyStr = payResponse.getHttpBody();
|
||||
JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调用支付宝预下订单接口
|
||||
*
|
||||
* @param paymentAmount 充值金额
|
||||
* @return 二维码url
|
||||
* @throws Exception
|
||||
*/
|
||||
public String pointsPay(Double paymentAmount) throws Exception {
|
||||
// 设置orderTrade信息
|
||||
OrderTrade tradeEntity = new OrderTrade();
|
||||
tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
tradeEntity.setProductId(-1);
|
||||
tradeEntity.setProductName("积分充值");
|
||||
tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
tradeEntity.setPaymentAmount(paymentAmount.intValue());
|
||||
|
||||
//调用支付宝的接口
|
||||
AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
.preCreate(tradeEntity.getProductName(),
|
||||
tradeEntity.getCode() + "_points",
|
||||
tradeEntity.getPaymentAmount().toString());
|
||||
// 缓存到redis
|
||||
redisCache.setCacheObject(tradeEntity.getCode() + "_points", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
//参照官方文档响应示例,解析返回结果
|
||||
String httpBodyStr = payResponse.getHttpBody();
|
||||
JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝转账方法
|
||||
* @param outBizNo 外部业务单号
|
||||
* @param payerUserId 付款方用户ID
|
||||
* @param payeeUserId 收款方用户ID
|
||||
* @param amount 转账金额
|
||||
* @return 返回支付宝转账响应的内容
|
||||
*/
|
||||
public String transfer(String outBizNo, String payerUserId, String payeeUserId, String amount) throws AlipayApiException {
|
||||
|
||||
// 初始化SDK
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
|
||||
// 构造请求参数以调用接口
|
||||
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
|
||||
AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
|
||||
|
||||
// 设置商家侧唯一订单号
|
||||
model.setOutBizNo(outBizNo);
|
||||
|
||||
// 设置订单总金额
|
||||
model.setTransAmount(amount);
|
||||
|
||||
// 设置描述特定的业务场景
|
||||
model.setBizScene("DIRECT_TRANSFER");
|
||||
|
||||
// 设置业务产品码
|
||||
model.setProductCode("TRANS_ACCOUNT_NO_PWD");
|
||||
|
||||
// 设置转账业务的标题
|
||||
model.setOrderTitle("测试");
|
||||
|
||||
// 设置收款方信息
|
||||
Participant payeeInfo = new Participant();
|
||||
payeeInfo.setCertType("IDENTITY_CARD");
|
||||
payeeInfo.setCertNo("1201152******72917");
|
||||
payeeInfo.setIdentity("2088123412341234");
|
||||
payeeInfo.setName("黄龙国际有限公司");
|
||||
payeeInfo.setIdentityType("ALIPAY_USER_ID");
|
||||
model.setPayeeInfo(payeeInfo);
|
||||
|
||||
// 设置业务备注
|
||||
model.setRemark("201905代发");
|
||||
|
||||
// 设置转账业务请求的扩展参数
|
||||
model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
|
||||
|
||||
request.setBizModel(model);
|
||||
AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
|
||||
System.out.println(response.getBody());
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("调用成功");
|
||||
} else {
|
||||
System.out.println("调用失败");
|
||||
// sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
|
||||
String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
|
||||
System.out.println(diagnosisUrl);
|
||||
}
|
||||
return response.getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看余额
|
||||
*/
|
||||
public void balance() throws Exception {
|
||||
|
||||
AlipaySystemOauthTokenResponse token = Factory.Base.OAuth().getToken("code");
|
||||
|
||||
|
||||
// 初始化SDK
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
|
||||
// 构造请求参数以调用接口
|
||||
AlipayFundAccountQueryRequest request = new AlipayFundAccountQueryRequest();
|
||||
AlipayFundAccountQueryModel model = new AlipayFundAccountQueryModel();
|
||||
|
||||
// uid参数未来计划废弃,存量商户可继续使用,新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。
|
||||
// model.setAlipayUserId("2088301409188095");
|
||||
|
||||
// 设置支付宝openId
|
||||
model.setAlipayOpenId("061P6NAblcWDWJoDRxSVvOYz-ufp-3wQaA4E_szQyMFTXse");
|
||||
|
||||
// 设置查询的账号类型
|
||||
model.setAccountType("ACCTRANS_ACCOUNT");
|
||||
|
||||
request.setBizModel(model);
|
||||
AlipayFundAccountQueryResponse response = alipayClient.execute(request);
|
||||
System.out.println(response.getBody());
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("调用成功");
|
||||
} else {
|
||||
System.out.println("调用失败");
|
||||
// sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
|
||||
// String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
|
||||
// System.out.println(diagnosisUrl);
|
||||
}
|
||||
}
|
||||
|
||||
private AlipayConfig getAlipayConfig() {
|
||||
String privateKey = aliConfig.getPrivateKey();
|
||||
String alipayPublicKey = aliConfig.getPublicKey();
|
||||
AlipayConfig alipayConfig = new AlipayConfig();
|
||||
alipayConfig.setServerUrl(aliConfig.getGatewayUrl());
|
||||
alipayConfig.setAppId(aliConfig.getAppId());
|
||||
alipayConfig.setPrivateKey(privateKey);
|
||||
alipayConfig.setFormat("json");
|
||||
alipayConfig.setAlipayPublicKey(alipayPublicKey);
|
||||
alipayConfig.setCharset("UTF-8");
|
||||
alipayConfig.setSignType("RSA2");
|
||||
return alipayConfig;
|
||||
}
|
||||
|
||||
//TODO 绑定回调,获取openId,保存到数据库
|
||||
public void bindingCallback(String authCode) {
|
||||
try {
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
||||
request.setCode(authCode);
|
||||
request.setGrantType("authorization_code");
|
||||
com.alipay.api.response.AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
|
||||
if (response.isSuccess()) {
|
||||
String openId = response.getOpenId(); // 支付宝用户唯一ID
|
||||
// 将openId与当前商城用户绑定(保存到数据库)
|
||||
System.out.println("绑定成功!openId:" + openId);
|
||||
} else {
|
||||
System.out.println("绑定失败:" + response.getSubMsg());
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 调用支付宝预下订单接口
|
||||
// *
|
||||
// * @param paymentAmount 充值金额
|
||||
// * @return 二维码url
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public String pointsPay(Double paymentAmount) throws Exception {
|
||||
// // 设置orderTrade信息
|
||||
// OrderTrade tradeEntity = new OrderTrade();
|
||||
// tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
// tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
// tradeEntity.setProductId(-1);
|
||||
// tradeEntity.setProductName("积分充值");
|
||||
// tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
// tradeEntity.setPaymentAmount(paymentAmount.intValue());
|
||||
//
|
||||
// //调用支付宝的接口
|
||||
// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
// .preCreate(tradeEntity.getProductName(),
|
||||
// tradeEntity.getCode() + "_points",
|
||||
// tradeEntity.getPaymentAmount().toString());
|
||||
// // 缓存到redis
|
||||
// redisCache.setCacheObject(tradeEntity.getCode() + "_points", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
// //参照官方文档响应示例,解析返回结果
|
||||
// String httpBodyStr = payResponse.getHttpBody();
|
||||
// JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
// return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 支付宝转账方法
|
||||
// * @param outBizNo 外部业务单号
|
||||
// * @param payerUserId 付款方用户ID
|
||||
// * @param payeeUserId 收款方用户ID
|
||||
// * @param amount 转账金额
|
||||
// * @return 返回支付宝转账响应的内容
|
||||
// */
|
||||
// public String transfer(String outBizNo, String payerUserId, String payeeUserId, String amount) throws AlipayApiException {
|
||||
//
|
||||
// // 初始化SDK
|
||||
// AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
//
|
||||
// // 构造请求参数以调用接口
|
||||
// AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
|
||||
// AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
|
||||
//
|
||||
// // 设置商家侧唯一订单号
|
||||
// model.setOutBizNo(outBizNo);
|
||||
//
|
||||
// // 设置订单总金额
|
||||
// model.setTransAmount(amount);
|
||||
//
|
||||
// // 设置描述特定的业务场景
|
||||
// model.setBizScene("DIRECT_TRANSFER");
|
||||
//
|
||||
// // 设置业务产品码
|
||||
// model.setProductCode("TRANS_ACCOUNT_NO_PWD");
|
||||
//
|
||||
// // 设置转账业务的标题
|
||||
// model.setOrderTitle("测试");
|
||||
//
|
||||
// // 设置收款方信息
|
||||
// Participant payeeInfo = new Participant();
|
||||
// payeeInfo.setCertType("IDENTITY_CARD");
|
||||
// payeeInfo.setCertNo("1201152******72917");
|
||||
// payeeInfo.setIdentity("2088123412341234");
|
||||
// payeeInfo.setName("黄龙国际有限公司");
|
||||
// payeeInfo.setIdentityType("ALIPAY_USER_ID");
|
||||
// model.setPayeeInfo(payeeInfo);
|
||||
//
|
||||
// // 设置业务备注
|
||||
// model.setRemark("201905代发");
|
||||
//
|
||||
// // 设置转账业务请求的扩展参数
|
||||
// model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
|
||||
//
|
||||
// request.setBizModel(model);
|
||||
// AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
|
||||
// System.out.println(response.getBody());
|
||||
//
|
||||
// if (response.isSuccess()) {
|
||||
// System.out.println("调用成功");
|
||||
// } else {
|
||||
// System.out.println("调用失败");
|
||||
// // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
|
||||
// String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
|
||||
// System.out.println(diagnosisUrl);
|
||||
// }
|
||||
// return response.getBody();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 查看余额
|
||||
// */
|
||||
// public void balance() throws Exception {
|
||||
//
|
||||
// AlipaySystemOauthTokenResponse token = Factory.Base.OAuth().getToken("code");
|
||||
//
|
||||
//
|
||||
// // 初始化SDK
|
||||
// AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
//
|
||||
// // 构造请求参数以调用接口
|
||||
// AlipayFundAccountQueryRequest request = new AlipayFundAccountQueryRequest();
|
||||
// AlipayFundAccountQueryModel model = new AlipayFundAccountQueryModel();
|
||||
//
|
||||
// // uid参数未来计划废弃,存量商户可继续使用,新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。
|
||||
// // model.setAlipayUserId("2088301409188095");
|
||||
//
|
||||
// // 设置支付宝openId
|
||||
// model.setAlipayOpenId("061P6NAblcWDWJoDRxSVvOYz-ufp-3wQaA4E_szQyMFTXse");
|
||||
//
|
||||
// // 设置查询的账号类型
|
||||
// model.setAccountType("ACCTRANS_ACCOUNT");
|
||||
//
|
||||
// request.setBizModel(model);
|
||||
// AlipayFundAccountQueryResponse response = alipayClient.execute(request);
|
||||
// System.out.println(response.getBody());
|
||||
//
|
||||
// if (response.isSuccess()) {
|
||||
// System.out.println("调用成功");
|
||||
// } else {
|
||||
// System.out.println("调用失败");
|
||||
// // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
|
||||
// // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
|
||||
// // System.out.println(diagnosisUrl);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private AlipayConfig getAlipayConfig() {
|
||||
// String privateKey = aliConfig.getPrivateKey();
|
||||
// String alipayPublicKey = aliConfig.getPublicKey();
|
||||
// AlipayConfig alipayConfig = new AlipayConfig();
|
||||
// alipayConfig.setServerUrl(aliConfig.getGatewayUrl());
|
||||
// alipayConfig.setAppId(aliConfig.getAppId());
|
||||
// alipayConfig.setPrivateKey(privateKey);
|
||||
// alipayConfig.setFormat("json");
|
||||
// alipayConfig.setAlipayPublicKey(alipayPublicKey);
|
||||
// alipayConfig.setCharset("UTF-8");
|
||||
// alipayConfig.setSignType("RSA2");
|
||||
// return alipayConfig;
|
||||
// }
|
||||
//
|
||||
// //TODO 绑定回调,获取openId,保存到数据库
|
||||
// public void bindingCallback(String authCode) {
|
||||
// try {
|
||||
// AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
// AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
||||
// request.setCode(authCode);
|
||||
// request.setGrantType("authorization_code");
|
||||
// com.alipay.api.response.AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
|
||||
// if (response.isSuccess()) {
|
||||
// String openId = response.getOpenId(); // 支付宝用户唯一ID
|
||||
// // 将openId与当前商城用户绑定(保存到数据库)
|
||||
// System.out.println("绑定成功!openId:" + openId);
|
||||
// } else {
|
||||
// System.out.println("绑定失败:" + response.getSubMsg());
|
||||
// }
|
||||
//
|
||||
// } catch (AlipayApiException e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.mcwl.common.domain.IdsParam;
|
|||
import com.mcwl.common.utils.SecurityUtils;
|
||||
import com.mcwl.pay.domain.OrderTrade;
|
||||
import com.mcwl.pay.domain.OrderTradeDto;
|
||||
import com.mcwl.pay.service.AliPayService;
|
||||
import com.mcwl.pay.service.OrderTradeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
@ -56,36 +57,6 @@ public class OrderTradeController extends BaseController {
|
|||
@Autowired
|
||||
private OrderTradeService orderTradeService;
|
||||
|
||||
@Autowired
|
||||
private AliPayIntegration aliPayIntegration;
|
||||
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
|
||||
@GetMapping("/generateQrCode")
|
||||
public void generateQrCode(HttpServletResponse response) throws Exception {
|
||||
String scope = "auth_user"; // 需要获取用户信息
|
||||
String state = RandomUtil.randomString(3); // 防止CSRF攻击
|
||||
|
||||
String encodedRedirectUri = URLEncoder.encode("https://3195d9a3.r27.cpolar.top/web/pay/callback", "UTF-8");
|
||||
String authUrl = String.format(
|
||||
"https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=%s&scope=%s&redirect_uri=%s&state=%s",
|
||||
"2021005114616085", scope, encodedRedirectUri, state
|
||||
);
|
||||
|
||||
QrCodeUtil.generate(authUrl, 300, 300, "png", response.getOutputStream());
|
||||
}
|
||||
|
||||
@GetMapping("/callback")
|
||||
public void callback(@RequestParam("auth_code") String authCode) {
|
||||
|
||||
System.out.println("authCode = " + authCode);
|
||||
aliPayIntegration.bindingCallback(authCode);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*/
|
||||
|
@ -136,30 +107,6 @@ public class OrderTradeController extends BaseController {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支付接口
|
||||
*
|
||||
* @param orderTradeDto 订单实体
|
||||
* @param response 响应
|
||||
* @throws Exception
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/doPay")
|
||||
public void doPay(@RequestBody OrderTradeDto orderTradeDto, HttpServletResponse response) throws Exception {
|
||||
String qrUrl = null;
|
||||
|
||||
String type = orderTradeDto.getType();
|
||||
|
||||
if ("member".equalsIgnoreCase(type)) {
|
||||
qrUrl = aliPayIntegration.memberPay(orderTradeDto);
|
||||
} else if ("points".equalsIgnoreCase(type)) {
|
||||
qrUrl = aliPayIntegration.pointsPay(orderTradeDto.getPaymentAmount());
|
||||
}
|
||||
|
||||
QrCodeUtil.generate(qrUrl, 300, 300, "png", response.getOutputStream());
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/queryTradeStatus")
|
||||
public Object queryTradeStatus(@RequestParam String outTradeNo) throws Exception {
|
||||
Factory.setOptions(config);
|
||||
|
@ -176,102 +123,5 @@ public class OrderTradeController extends BaseController {
|
|||
return map.get("alipay_trade_query_response");
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看余额
|
||||
*/
|
||||
@GetMapping("/balance")
|
||||
public void balance() throws Exception {
|
||||
aliPayIntegration.balance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提现接口
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/withdraw")
|
||||
public void withdraw(@RequestBody OrderTradeDto orderTradeDto, HttpServletResponse response) throws Exception {
|
||||
String outBizNo = UUID.fastUUID().toString(true);
|
||||
String payerUserId = "2088102167258880";
|
||||
String payeeUserId = "2088102167258880";
|
||||
String amount = "100";
|
||||
aliPayIntegration.transfer(outBizNo, payerUserId, payeeUserId, amount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支付回调接口
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/notify") // 注意这里必须是POST接口
|
||||
public String payNotify(HttpServletRequest request) throws Exception {
|
||||
|
||||
|
||||
if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
|
||||
System.out.println("=========支付宝异步回调========");
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (String name : requestParams.keySet()) {
|
||||
params.put(name, request.getParameter(name));
|
||||
// System.out.println(name + " = " + request.getParameter(name));
|
||||
}
|
||||
|
||||
// 支付宝验签
|
||||
if (Factory.Payment.Common().verifyNotify(params)) {
|
||||
// System.out.println("交易名称: " + params.get("subject"));
|
||||
// System.out.println("交易状态: " + params.get("trade_status"));
|
||||
// System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
|
||||
// System.out.println("商户订单号: " + params.get("out_trade_no"));
|
||||
// System.out.println("交易金额: " + params.get("total_amount"));
|
||||
// System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
|
||||
// System.out.println("买家付款时间: " + params.get("gmt_payment"));
|
||||
// System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
|
||||
// 验签通过
|
||||
|
||||
String code = params.get("out_trade_no"); // 商户订单号
|
||||
// 获取订单后缀
|
||||
String suffix = code.substring(code.lastIndexOf("_") + 1);
|
||||
String orderTradeJson = redisCache.getCacheObject(code);
|
||||
OrderTrade orderTrade = JSONUtil.toBean(orderTradeJson, OrderTrade.class);
|
||||
|
||||
orderTradeService.orderHandler(orderTrade, suffix, params);
|
||||
|
||||
// 更新订单状态
|
||||
// if (!StringUtils.isEmpty(orderTradeJson)) {
|
||||
// OrderTrade orderTrade = JSONUtil.toBean(orderTradeJson, OrderTrade.class);
|
||||
// // 支付宝交易凭证号
|
||||
// orderTrade.setPaymentMethod(params.get("trade_no"));
|
||||
// orderTrade.setTransactionId(1);
|
||||
// orderTrade.setOrderTime(DateUtils.parseDate(params.get("gmt_payment")));
|
||||
// orderTrade.setOrderStatus(3);
|
||||
// orderTrade.setPayStatus(2);
|
||||
// String totalAmountStr = params.get("total_amount");
|
||||
// if (totalAmountStr != null && !totalAmountStr.isEmpty()) {
|
||||
// BigDecimal totalAmount = new BigDecimal(totalAmountStr);
|
||||
// orderTrade.setTotalAmount(totalAmount.intValue());
|
||||
// }
|
||||
// String buyerPayAmountStr = params.get("buyer_pay_amount");
|
||||
// if (buyerPayAmountStr != null && !buyerPayAmountStr.isEmpty()) {
|
||||
// BigDecimal buyerPayAmount = new BigDecimal(buyerPayAmountStr);
|
||||
// orderTrade.setPaymentAmount(buyerPayAmount.intValue());
|
||||
// }
|
||||
// orderTradeService.save(orderTrade);
|
||||
// }
|
||||
} else {
|
||||
// 验签失败
|
||||
System.out.println("验签失败");
|
||||
}
|
||||
} else {
|
||||
// 验签失败
|
||||
System.out.println("验签失败");
|
||||
}
|
||||
|
||||
return "success";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -72,7 +74,11 @@ public class MallProductController extends BaseController {
|
|||
public AjaxResult Malifile(@RequestParam MultipartFile file) {
|
||||
|
||||
String s = OssUtil.uploadMultipartFile(file);
|
||||
return AjaxResult.success(s);
|
||||
String fileName = file.getOriginalFilename();
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("fileName", fileName);
|
||||
map.put("url", s);
|
||||
return AjaxResult.success(map);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -129,11 +129,11 @@ public class SecurityConfig
|
|||
.authorizeHttpRequests((requests) -> {
|
||||
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
|
||||
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
|
||||
requests.antMatchers("/login", "/register", "/captchaImage","/web/pay/doPay","/web/pay/notify","/web/pay/generateQrCode","/web/pay/callback").permitAll()
|
||||
requests.antMatchers("/login", "/register", "/captchaImage","/ali/pay/doPay","/ali/pay/notify",
|
||||
"/ali/pay/generateQrCode","/ali/pay/callback").permitAll()
|
||||
// 静态资源,可匿名访问
|
||||
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
|
||||
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
|
||||
.antMatchers("/web/pay/notify").permitAll()
|
||||
// 除上面外的所有请求全部需要鉴权认证
|
||||
.anyRequest().authenticated();
|
||||
})
|
||||
|
|
|
@ -55,5 +55,18 @@
|
|||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alipay.sdk</groupId>
|
||||
<artifactId>alipay-sdk-java</artifactId>
|
||||
<version>4.40.30.ALL</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alipay.sdk</groupId>
|
||||
<artifactId>alipay-easysdk</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.mcwl.pay.service;
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.mcwl.pay.domain.OrderTradeDto;
|
||||
|
||||
public interface AliPayService {
|
||||
void bindingCallback(String authCode);
|
||||
|
||||
String memberPay(OrderTradeDto orderTradeDto) throws Exception;
|
||||
|
||||
String pointsPay(Double paymentAmount) throws Exception;
|
||||
|
||||
void balance() throws Exception;
|
||||
|
||||
String transfer(String outBizNo, String payerUserId, String payeeUserId, String amount) throws AlipayApiException;
|
||||
}
|
|
@ -0,0 +1,321 @@
|
|||
package com.mcwl.pay.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.AlipayConfig;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.alipay.api.diagnosis.DiagnosisUtils;
|
||||
import com.alipay.api.domain.AlipayFundAccountQueryModel;
|
||||
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
|
||||
import com.alipay.api.domain.Participant;
|
||||
import com.alipay.api.request.AlipayFundAccountQueryRequest;
|
||||
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
|
||||
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
|
||||
import com.alipay.api.response.AlipayFundAccountQueryResponse;
|
||||
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
|
||||
import com.alipay.easysdk.base.oauth.models.AlipaySystemOauthTokenResponse;
|
||||
import com.alipay.easysdk.factory.Factory;
|
||||
import com.alipay.easysdk.kernel.Config;
|
||||
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
|
||||
import com.mcwl.common.core.redis.RedisCache;
|
||||
import com.mcwl.common.exception.ServiceException;
|
||||
import com.mcwl.common.utils.SecurityUtils;
|
||||
import com.mcwl.memberCenter.domain.MemberLevel;
|
||||
import com.mcwl.memberCenter.service.MemberLevelService;
|
||||
import com.mcwl.pay.config.AliConfig;
|
||||
import com.mcwl.pay.domain.OrderTrade;
|
||||
import com.mcwl.pay.domain.OrderTradeDto;
|
||||
import com.mcwl.pay.service.AliPayService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 支付宝支付
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AliPayServiceImpl implements AliPayService {
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
|
||||
@Autowired
|
||||
private MemberLevelService memberLevelService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private AliConfig aliConfig;
|
||||
|
||||
|
||||
public AliPayServiceImpl(Config config) {
|
||||
Factory.setOptions(config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调用支付宝预下订单接口
|
||||
*
|
||||
* @param orderTradeDto 订单实体
|
||||
* @return 二维码url
|
||||
* @throws Exception
|
||||
*/
|
||||
// public String orderPay(OrderTradeDto orderTradeDto) throws Exception {
|
||||
// Integer productId = orderTradeDto.getProductId();
|
||||
// if (!Optional.ofNullable(productId).isPresent()) {
|
||||
// throw new ServiceException("mallProductId不能为空");
|
||||
// }
|
||||
//
|
||||
// MallProduct mallProduct = mallProductService.getById(productId);
|
||||
// if (!Optional.ofNullable(mallProduct).isPresent()) {
|
||||
// throw new ServiceException("mallProduct不存在");
|
||||
// }
|
||||
//
|
||||
// // 设置orderTrade信息
|
||||
// OrderTrade tradeEntity = new OrderTrade();
|
||||
// BeanUtil.copyProperties(orderTradeDto, tradeEntity);
|
||||
// tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
// tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
// tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
// tradeEntity.setProductName(mallProduct.getProductName());
|
||||
//
|
||||
// //调用支付宝的接口
|
||||
// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
// .preCreate(mallProduct.getProductName(),
|
||||
// tradeEntity.getCode() + "_product",
|
||||
// orderTradeDto.getPaymentAmount().toString());
|
||||
// // 缓存到redis
|
||||
// redisCache.setCacheObject(tradeEntity.getCode() + "_product", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
//// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace().preCreate("订单主题:Mac笔记本", "LS123qwe123", "19999");
|
||||
// //参照官方文档响应示例,解析返回结果
|
||||
// String httpBodyStr = payResponse.getHttpBody();
|
||||
// JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
// return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* 调用支付宝预下订单接口
|
||||
*
|
||||
* @param orderTradeDto 订单实体
|
||||
* @return 二维码url
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String memberPay(OrderTradeDto orderTradeDto) throws Exception {
|
||||
|
||||
// 会员等级id
|
||||
Integer productId = orderTradeDto.getProductId();
|
||||
|
||||
if (!Optional.ofNullable(productId).isPresent()) {
|
||||
throw new ServiceException("memberLevelId不能为空");
|
||||
}
|
||||
|
||||
MemberLevel memberLevel = memberLevelService.getById(productId);
|
||||
|
||||
if (!Optional.ofNullable(memberLevel).isPresent()) {
|
||||
throw new ServiceException("memberLevel不存在");
|
||||
}
|
||||
|
||||
// 设置orderTrade信息
|
||||
OrderTrade tradeEntity = new OrderTrade();
|
||||
BeanUtil.copyProperties(orderTradeDto, tradeEntity);
|
||||
tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
tradeEntity.setProductName(memberLevel.getMemberName());
|
||||
|
||||
//调用支付宝的接口
|
||||
AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
// 设置过期时
|
||||
.preCreate(memberLevel.getMemberName(),
|
||||
tradeEntity.getCode() + "_member",
|
||||
orderTradeDto.getPaymentAmount().toString());
|
||||
// 缓存到redis
|
||||
redisCache.setCacheObject(tradeEntity.getCode() + "_member", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
redisCache.setCacheObject(tradeEntity.getCode() + "_member" + "_promotionId", orderTradeDto.getPromotionId(), 3, TimeUnit.MINUTES);
|
||||
// AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace().preCreate("订单主题:Mac笔记本", "LS123qwe123", "19999");
|
||||
//参照官方文档响应示例,解析返回结果
|
||||
String httpBodyStr = payResponse.getHttpBody();
|
||||
JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调用支付宝预下订单接口
|
||||
*
|
||||
* @param paymentAmount 充值金额
|
||||
* @return 二维码url
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String pointsPay(Double paymentAmount) throws Exception {
|
||||
// 设置orderTrade信息
|
||||
OrderTrade tradeEntity = new OrderTrade();
|
||||
tradeEntity.setCode(UUID.randomUUID().toString(true).substring(0, 30));
|
||||
tradeEntity.setUserId(SecurityUtils.getUserId());
|
||||
tradeEntity.setProductId(-1);
|
||||
tradeEntity.setProductName("积分充值");
|
||||
tradeEntity.setUserName(SecurityUtils.getUsername());
|
||||
tradeEntity.setPaymentAmount(paymentAmount.intValue());
|
||||
|
||||
//调用支付宝的接口
|
||||
AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
.preCreate(tradeEntity.getProductName(),
|
||||
tradeEntity.getCode() + "_points",
|
||||
tradeEntity.getPaymentAmount().toString());
|
||||
// 缓存到redis
|
||||
redisCache.setCacheObject(tradeEntity.getCode() + "_points", JSONUtil.toJsonStr(tradeEntity), 3, TimeUnit.MINUTES);
|
||||
//参照官方文档响应示例,解析返回结果
|
||||
String httpBodyStr = payResponse.getHttpBody();
|
||||
JSONObject jsonObject = JSONObject.parseObject(httpBodyStr);
|
||||
return jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝转账方法
|
||||
* @param outBizNo 外部业务单号
|
||||
* @param payerUserId 付款方用户ID
|
||||
* @param payeeUserId 收款方用户ID
|
||||
* @param amount 转账金额
|
||||
* @return 返回支付宝转账响应的内容
|
||||
*/
|
||||
@Override
|
||||
public String transfer(String outBizNo, String payerUserId, String payeeUserId, String amount) throws AlipayApiException {
|
||||
|
||||
// 初始化SDK
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
|
||||
// 构造请求参数以调用接口
|
||||
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
|
||||
AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
|
||||
|
||||
// 设置商家侧唯一订单号
|
||||
model.setOutBizNo(outBizNo);
|
||||
|
||||
// 设置订单总金额
|
||||
model.setTransAmount(amount);
|
||||
|
||||
// 设置描述特定的业务场景
|
||||
model.setBizScene("DIRECT_TRANSFER");
|
||||
|
||||
// 设置业务产品码
|
||||
model.setProductCode("TRANS_ACCOUNT_NO_PWD");
|
||||
|
||||
// 设置转账业务的标题
|
||||
model.setOrderTitle("测试");
|
||||
|
||||
// 设置收款方信息
|
||||
Participant payeeInfo = new Participant();
|
||||
payeeInfo.setCertType("IDENTITY_CARD");
|
||||
payeeInfo.setCertNo("1201152******72917");
|
||||
payeeInfo.setIdentity("2088123412341234");
|
||||
payeeInfo.setName("黄龙国际有限公司");
|
||||
payeeInfo.setIdentityType("ALIPAY_USER_ID");
|
||||
model.setPayeeInfo(payeeInfo);
|
||||
|
||||
// 设置业务备注
|
||||
model.setRemark("201905代发");
|
||||
|
||||
// 设置转账业务请求的扩展参数
|
||||
model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");
|
||||
|
||||
request.setBizModel(model);
|
||||
AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
|
||||
System.out.println(response.getBody());
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("调用成功");
|
||||
} else {
|
||||
System.out.println("调用失败");
|
||||
// sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
|
||||
String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
|
||||
System.out.println(diagnosisUrl);
|
||||
}
|
||||
return response.getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看余额
|
||||
*/
|
||||
@Override
|
||||
public void balance() throws Exception {
|
||||
|
||||
AlipaySystemOauthTokenResponse token = Factory.Base.OAuth().getToken("code");
|
||||
|
||||
|
||||
// 初始化SDK
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
|
||||
// 构造请求参数以调用接口
|
||||
AlipayFundAccountQueryRequest request = new AlipayFundAccountQueryRequest();
|
||||
AlipayFundAccountQueryModel model = new AlipayFundAccountQueryModel();
|
||||
|
||||
// uid参数未来计划废弃,存量商户可继续使用,新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。
|
||||
// model.setAlipayUserId("2088301409188095");
|
||||
|
||||
// 设置支付宝openId
|
||||
model.setAlipayOpenId("061P6NAblcWDWJoDRxSVvOYz-ufp-3wQaA4E_szQyMFTXse");
|
||||
|
||||
// 设置查询的账号类型
|
||||
model.setAccountType("ACCTRANS_ACCOUNT");
|
||||
|
||||
request.setBizModel(model);
|
||||
AlipayFundAccountQueryResponse response = alipayClient.execute(request);
|
||||
System.out.println(response.getBody());
|
||||
|
||||
if (response.isSuccess()) {
|
||||
System.out.println("调用成功");
|
||||
} else {
|
||||
System.out.println("调用失败");
|
||||
// sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
|
||||
// String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
|
||||
// System.out.println(diagnosisUrl);
|
||||
}
|
||||
}
|
||||
|
||||
private AlipayConfig getAlipayConfig() {
|
||||
String privateKey = aliConfig.getPrivateKey();
|
||||
String alipayPublicKey = aliConfig.getPublicKey();
|
||||
AlipayConfig alipayConfig = new AlipayConfig();
|
||||
alipayConfig.setServerUrl(aliConfig.getGatewayUrl());
|
||||
alipayConfig.setAppId(aliConfig.getAppId());
|
||||
alipayConfig.setPrivateKey(privateKey);
|
||||
alipayConfig.setFormat("json");
|
||||
alipayConfig.setAlipayPublicKey(alipayPublicKey);
|
||||
alipayConfig.setCharset("UTF-8");
|
||||
alipayConfig.setSignType("RSA2");
|
||||
return alipayConfig;
|
||||
}
|
||||
|
||||
//TODO 绑定回调,获取openId,保存到数据库
|
||||
@Override
|
||||
public void bindingCallback(String authCode) {
|
||||
try {
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
|
||||
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
||||
request.setCode(authCode);
|
||||
request.setGrantType("authorization_code");
|
||||
com.alipay.api.response.AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
|
||||
if (response.isSuccess()) {
|
||||
String openId = response.getOpenId(); // 支付宝用户唯一ID
|
||||
// 将openId与当前商城用户绑定(保存到数据库)
|
||||
System.out.println("绑定成功!openId:" + openId);
|
||||
} else {
|
||||
System.out.println("绑定失败:" + response.getSubMsg());
|
||||
}
|
||||
|
||||
} catch (AlipayApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -54,6 +54,11 @@ public class ModelVersion extends BaseEntity {
|
|||
*/
|
||||
@ApiModelProperty(value = "文件地址")
|
||||
private String filePath;
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
@ApiModelProperty(value = "文件名")
|
||||
private String fileName;
|
||||
/**
|
||||
* 版本介绍(富文本编辑)
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue