feat:
parent
d8d3bd97dd
commit
81dd9362cd
|
@ -0,0 +1,42 @@
|
|||
package com.mcwl.web.controller.pay;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.easysdk.factory.Factory;
|
||||
import com.alipay.easysdk.kernel.Config;
|
||||
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
|
||||
import com.mcwl.pay.domain.OrderTrade;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 支付宝支付
|
||||
|
||||
*/
|
||||
@Component
|
||||
public class AliPayIntegration {
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
|
||||
/**
|
||||
* 调用支付宝预下订单接口
|
||||
*
|
||||
* @param tradeEntity 订单实体
|
||||
* @return 二维码url
|
||||
* @throws Exception
|
||||
*/
|
||||
public String pay(OrderTrade tradeEntity) throws Exception {
|
||||
Factory.setOptions(config);
|
||||
//调用支付宝的接口
|
||||
AlipayTradePrecreateResponse payResponse = Factory.Payment.FaceToFace()
|
||||
.preCreate(tradeEntity.getUserName(),
|
||||
tradeEntity.getCode(),
|
||||
tradeEntity.getPaymentAmount().toString());
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,179 @@
|
|||
package com.mcwl.web.controller.pay;
|
||||
|
||||
import cn.hutool.extra.qrcode.QrCodeUtil;
|
||||
import com.alipay.easysdk.factory.Factory;
|
||||
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.domain.IdsParam;
|
||||
import com.mcwl.common.utils.SecurityUtils;
|
||||
import com.mcwl.pay.domain.OrderTrade;
|
||||
import com.mcwl.pay.service.OrderTradeService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.alipay.easysdk.kernel.Config;
|
||||
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.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
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/web/pay")
|
||||
@Validated
|
||||
public class OrderTradeController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@Autowired
|
||||
private OrderTradeService orderTradeService;
|
||||
|
||||
@Autowired
|
||||
private AliPayIntegration aliPayIntegration;
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo list(OrderTrade orderTrade)
|
||||
{
|
||||
startPage();
|
||||
List<OrderTrade> list = orderTradeService.selectMallProductList(orderTrade);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*/
|
||||
@PostMapping("/add")
|
||||
public AjaxResult add(@RequestBody OrderTrade orderTrade)
|
||||
{
|
||||
// 获取当前用户
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
if (userId == null) {
|
||||
return AjaxResult.warn("用户未登录");
|
||||
}
|
||||
orderTrade.setUserId(userId);
|
||||
orderTrade.setCreateBy(getUsername());
|
||||
return toAjax(orderTradeService.insertMallProduct(orderTrade));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*/
|
||||
@PostMapping("/upda")
|
||||
public AjaxResult upda(@RequestBody OrderTrade orderTrade)
|
||||
{
|
||||
// 获取当前用户
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
if (userId == null) {
|
||||
return AjaxResult.warn("用户未登录");
|
||||
}
|
||||
orderTrade.setUserId(userId);
|
||||
orderTrade.setUpdateBy(getUsername());
|
||||
return toAjax(orderTradeService.updateMallProduct(orderTrade));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
@PostMapping
|
||||
public AjaxResult remove(@RequestBody IdsParam ids)
|
||||
{
|
||||
orderTradeService.deleteMallProductByIds(ids);
|
||||
return success();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支付接口
|
||||
*
|
||||
* @param tradeEntity 订单实体
|
||||
* @param response 响应
|
||||
* @throws Exception
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/doPay")
|
||||
public void doPay(@RequestBody OrderTrade tradeEntity, HttpServletResponse response) throws Exception {
|
||||
String qrUrl = aliPayIntegration.pay(tradeEntity);
|
||||
QrCodeUtil.generate(qrUrl, 300, 300, "png", response.getOutputStream());
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/queryTradeStatus")
|
||||
public Object queryTradeStatus(@RequestParam String outTradeNo) throws Exception {
|
||||
Factory.setOptions(config);
|
||||
AlipayTradeQueryResponse query = Factory.Payment.Common().query(outTradeNo);
|
||||
Map<String, Object> map = JSONUtils.jsonToMap(query.getHttpBody());
|
||||
|
||||
// 返回交易结果, 是否交易成功需要根据该对象中的 trade_status 来确定
|
||||
// trade_status 的枚举值如下, 请见 https://opendocs.alipay.com/apis/api_1/alipay.trade.query
|
||||
// WAIT_BUYER_PAY(交易创建,等待买家付款)
|
||||
// TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)
|
||||
// TRADE_SUCCESS(交易支付成功)
|
||||
// TRADE_FINISHED(交易结束,不可退款)
|
||||
// 当 trade_status 等于 TRADE_SUCCESS 或 TRADE_FINISHED 时, 表示支付成功
|
||||
return map.get("alipay_trade_query_response");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支付回调接口
|
||||
* @param request
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Anonymous
|
||||
@PostMapping("/notify") // 注意这里必须是POST接口
|
||||
public String payNotify(HttpServletRequest request) throws Exception {
|
||||
|
||||
log.info("已经进入回调接口");
|
||||
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));
|
||||
}
|
||||
|
||||
String tradeNo = params.get("out_trade_no");
|
||||
String gmtPayment = params.get("gmt_payment");
|
||||
String alipayTradeNo = params.get("trade_no");
|
||||
// 支付宝验签
|
||||
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"));
|
||||
// 更新订单状态
|
||||
}
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue