第14章-支付宝支付.md 32 KB

第14章-支付宝支付

学习目标:

  • 能够说出支付宝支付业务流程
  • 能够集成支付宝支付SDK
  • 完成支付宝统一下单接口,生成支付页面
  • 完成支付宝支付结果
    • 同步回调(给用户)
    • 异步回调(给系统)
  • 完成支付宝退款

1、支付宝介绍

1.1 支付宝简介

支付宝(中国)网络技术有限公司 [1] 是国内的第三方支付平台,致力于提供“简单、安全、快速”的支付解决方案 [2] 。支付宝公司从2004年建立开始,始终以“信任”作为产品和服务的核心。旗下有“支付宝”与“支付宝钱包”两个独立品牌。自2014年第二季度开始成为当前全球最大的移动支付厂商。

当用户提交订单会跳转到选择支付渠道页面!

http://payment.gmall.com/pay.html?orderId=43

img

​ 当用户点击立即支付时生成支付的二维码

img

使用支付宝app 进行扫码支付

img

img

1.2 过程分析

img

1.3 对接支付宝的准备工作

1、申请条件:企业或个体工商户可申请;2. 提供真实有效的营业执照,且支付宝账户名称需与营业执照主体一致;3. 网站能正常访问且页面信息有完整商品内容;4. 网站必须通过ICP备案,个体户备案需与账户主体一致。(团购类网站不支持个体工商户签约)

支付手续费

img

1.4 申请步骤:

1、 支付宝商家中心中申请 https://www.alipay.com/

2、 https://b.alipay.com/signing/productSetV2.htm?mrchportalwebServer=https%3A%2F%2Fmrchportalweb.alipay.com

img

一个工作日后登录到蚂蚁金服开发者中心中:

img

可以查看到一个已经签约上线的应用。 其中非常重要的是这个APPID,需要记录下来之后的程序中要用到这个参数。

点击查看

img

到此为止,电商网站可以访问支付宝的最基本的准备已经完成。

接下来搭建支付模块

2、支付模块搭建

支付宝开发手册:https://open.alipay.com/

2.1 搭建service-payment

gmall-service父工程下新建子模块:service-payment 。搭建方式如service-order

image-20221209001110555

2.2 修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>gmall-service</artifactId>
        <groupId>com.atguigu.gmall</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-payment</artifactId>
    
    <dependencies>
        <dependency>
            <groupId>com.atguigu.gmall</groupId>
            <artifactId>service-order-client</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>service-payment</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.3 启动类

package com.atguigu.gmall;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class PaymentApp {

   public static void main(String[] args) {
      SpringApplication.run(PaymentApp.class, args);
   }

}

2.4 添加配置

bootstrap.properties

spring.application.name=service-payment
spring.profiles.active=dev
spring.cloud.nacos.discovery.server-addr=192.168.200.128:8848
spring.cloud.nacos.config.server-addr=192.168.200.128:8848
spring.cloud.nacos.config.prefix=${spring.application.name}
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.shared-configs[0].data-id=common.yaml

注意修改service-payment-dev.yaml配置中心配置文件

image-20230313151235284

2.5 导入sdk包

https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java

service-payment导入依赖

<!--导入支付宝支付sdk-->
<dependency>
   <groupId>com.alipay.sdk</groupId>
   <artifactId>alipay-sdk-java</artifactId>
   <version>4.31.7.ALL</version>
</dependency>

3、显示付款页面信息

支付页面信息展示流程

img

3.1 在根据订单id获取订单信息接口

YAPI接口地址:http://192.168.200.128:3000/project/11/interface/api/787

3.1.1 在service-order添加接口

OrderController

/**
 * 查询订单信息(包含订单明细)
 * @param orderId
 * @return
 */
@GetMapping("/inner/getOrderInfo/{orderId}")
public OrderInfo getOrderInfo(@PathVariable("orderId") Long orderId){
    return orderInfoService.getOrderInfo(orderId);
}

OrderInfoService接口

/**
 * 查询订单信息(包含订单明细)
 * @param orderId
 * @return
 */
OrderInfo getOrderInfo(Long orderId);

OrderServiceImpl实现类

/**
 * 查询订单信息(包含订单明细)
 *
 * @param orderId
 * @return
 */
@Override
public OrderInfo getOrderInfo(Long orderId) {
    OrderInfo orderInfo = this.getById(orderId);
    if (orderInfo != null) {
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId, orderId);
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper);
        orderInfo.setOrderDetailList(orderDetailList);
    }
    return orderInfo;
}

3.1.2 在service-order-client添加接口

远程调用Feign接口:OrderFeignClient

package com.atguigu.gmall.order.client;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.order.client.impl.OrderDegradeFeignClient;
import com.atguigu.gmall.order.model.OrderInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@FeignClient(value = "service-order", path = "/api/order", fallback = OrderDegradeFeignClient.class)
public interface OrderFeignClient {

    /**
     * 查询订单信息(包含订单明细)
     * @param orderId
     * @return
     */
    @GetMapping("/inner/getOrderInfo/{orderId}")
    public OrderInfo getOrderInfo(@PathVariable("orderId") Long orderId);
}

服务降级类:OrderDegradeFeignClient增加方法

package com.atguigu.gmall.order.client.impl;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.order.model.OrderInfo;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-06-25 10:57
 */
@Component
public class OrderDegradeFeignClient implements OrderFeignClient {

    @Override
    public OrderInfo getOrderInfo(Long orderId) {
        return null;
    }
}

3.2 server-gateway模块网关配置

- id: web-payment
  uri: lb://web-all
  predicates:
  - Host=payment.gmall.com
- id: service-payment
  uri: lb://service-payment
  predicates:
  - Path=/*/payment/** # 路径匹配

3.3 创建支付控制器PaymentController

Web-all模块

package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.order.model.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: atguigu
 * @create: 2023-06-27 15:37
 */
@Controller
public class PaymentHtmlController {

    @Autowired
    private OrderFeignClient orderFeignClient;


    /**
     * 渲染选择支付页面
     * @param orderId
     * @return
     */
    @GetMapping("/pay.html")
    public String choosePayHtml(Model model, @RequestParam("orderId") Long orderId){
        //1.远程调用订单服务获取订单信息
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        //2.设置数据
        model.addAttribute("orderInfo", orderInfo);
        return "payment/pay";
    }
}

3.4 页面渲染

页面资源: \templates\payment\pay.html

<div class="checkout-tit">
   <h4 class="tit-txt"><span class="success-icon"></span><span  class="success-info">订单提交成功,请您及时付款,以便尽快为您发货~~</span></h4>
   <div class="paymark">
      <span class="fl">请您在提交订单<em class="orange time">4小时</em>之内完成支付,超时订单会自动取消。订单号:<em th:text="${orderInfo.id}">00</em></span>
      <span class="fr"><em class="sui-lead">应付金额:</em><em  class="orange money" th:text="'¥'+${orderInfo.totalAmount}">¥1</em></span>
   </div>
</div>

4、支付功能实现

img

支付宝有了同步通知为什么还需要异步通知?

同步回调两个作用

  • 第一是从支付宝的页面上返回自己的网站继续后续操作;

  • 第二是携带支付状态的get参数;让自己的网站用于验证;

同步通知后;还需要异步通知主要是为了防止出现意外情况:

因为涉及到金钱;这是一个对安全和稳定要求比较严格的场景; 如果同步通知的过程中;用户不小心关闭了浏览器;或者浏览器卡死了; 异步也能收到通知;记录支付状态;

即便是用户端没问题;万一自己的服务器网络异常了一下呢?

如果自己的服务器没有正确返回接受到通知的状态; 支付宝的服务器会在一段时间内持续的往自己的服务器发送异步通知; 一直到成功;

顺便去确认了下;这个一段时间是: 25 小时以内完成 8 次通知(通知的间隔频率一般是:4m,10m,10m,1h,2h,6h,15h)

4.1 思路分析

  1. 将支付数据保存到数据库,以便跟支付宝进行对账

  2. 生成要支付的二维码

生成二维码需要的参数列表请参考官方文档

https://opendocs.alipay.com/open/270/105899

4.2 保存支付信息的表结构

表结构 payment_info

img

id 主键自动生成
out_trade_no 订单中已生成的对外交易编号。订单中获取
order_id 订单编号
payment_type 支付类型(微信与支付宝)
trade_no 交易号,回调时生成
total_amount 订单金额。订单中获取
subject 交易内容。利用商品名称拼接。
payment_status 支付状态,默认值未支付。
create_time 创建时间,当前时间。
callback_time 回调时间,初始为空,支付宝异步回调时记录
callback_content 回调信息,初始为空,支付宝异步回调时记录

4.3 编写接口,实现类PaymentService

4.3.1 接口 PaymentService

package com.atguigu.gmall.payment.service;

import com.atguigu.gmall.payment.model.PaymentInfo;
import com.baomidou.mybatisplus.extension.service.IService;

public interface PaymentInfoService extends IService<PaymentInfo> {

    /**
     * 保存本地交易记录信息
     *
     * @param paymentInfo
     * @param paymentType
     */
    public void savePaymentInfo(PaymentInfo paymentInfo, String paymentType);

}

4.3.2 实现类PaymentServiceImpl

package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.model.PaymentInfo;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @author: atguigu
 * @create: 2023-08-12 15:17
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    /**
     * 保存本地交易记录信息
     *
     *
     * @param paymentInfo 保证入参对象相关属性都赋值完成
     * @param paymentType
     */
    @Override
    public void savePaymentInfo(PaymentInfo paymentInfo, String paymentType) {
        //1.根据订单编号+支付方式查询本地交易记录
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo, paymentInfo.getOutTradeNo());
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        PaymentInfo paymentInfoDB = this.getOne(queryWrapper);

        //2.如果本地交易记录存在,直接返回接口
        if (paymentInfoDB != null) {
            return;
        }
        //3.如果本地交易记录不存在,新增本地交易记录即可
        this.save(paymentInfo);
    }
}

4.3.3 定义PaymentMapper接口

PaymentInfoMapper

package com.atguigu.gmall.payment.mapper;

import com.atguigu.gmall.payment.model.PaymentInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface PaymentInfoMapper extends BaseMapper<PaymentInfo> {
}

4.4 编写支付宝支付接口

YAPI接口地址:http://192.168.200.128:3000/project/11/interface/api/836

4.4.1 制作AlipayClient工具类

service-payment模块中创建配置类

package com.atguigu.gmall.payment.config;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AlipayConfig {

    @Value("${alipay_url}")
    private String alipay_url;

    @Value("${app_private_key}")
    private String app_private_key;

    @Value("${app_id}")
    private String app_id;


    public final static String format = "json";
    public final static String charset = "utf-8";
    public final static String sign_type = "RSA2";

    public static String return_payment_url;

    public static String notify_payment_url;

    public static String return_order_url;

    public static String alipay_public_key;

    @Value("${alipay_public_key}")
    public void setAlipay_public_key(String alipay_public_key) {
        AlipayConfig.alipay_public_key = alipay_public_key;
    }

    @Value("${return_payment_url}")
    public void setReturn_url(String return_payment_url) {
        AlipayConfig.return_payment_url = return_payment_url;
    }

    @Value("${notify_payment_url}")
    public void setNotify_url(String notify_payment_url) {
        AlipayConfig.notify_payment_url = notify_payment_url;
    }

    @Value("${return_order_url}")
    public void setReturn_order_url(String return_order_url) {
        AlipayConfig.return_order_url = return_order_url;
    }

    // <bean id="alipayClient" class="com.alipay.api.AlipayClient"> </bean>
    @Bean
    public AlipayClient alipayClient() {
        // AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE); //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(alipay_url, app_id, app_private_key, format, charset, alipay_public_key, sign_type);
        return alipayClient;
    }

}

4.4.2.2 控制器

AlipayApiController

package com.atguigu.gmall.payment.controller;

import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.payment.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: atguigu
 * @create: 2023-08-12 15:25
 */
@RestController
@RequestMapping("/api/payment")
public class AlipayController {


    @Autowired
    private AlipayService alipayService;

    /**
     * 调用支付方接口生成支付表单
     *
     * @param orderId
     * @return
     */
    @GetMapping("/alipay/submit/{orderId}")
    public String createAlipayForm(HttpServletRequest request, @PathVariable("orderId") Long orderId) {
        String userId = AuthContextHolder.getUserId(request);
        return alipayService.createAlipayForm(userId, orderId);
    }
}

4.4.2 编写支付宝下单

4.4.2.1 业务接口

package com.atguigu.gmall.payment.service;

public interface AlipayService {

    /**
     * 调用支付宝接口生成支付表单
     * @param userId
     * @param orderId
     * @return
     */
    String createAlipayForm(String userId, Long orderId);
}

4.4.2.2 业务实现类

package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.atguigu.gmall.enums.model.PaymentStatus;
import com.atguigu.gmall.enums.model.PaymentType;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.model.PaymentInfo;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author: atguigu
 * @create: 2023-08-12 15:27
 */
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private PaymentInfoService paymentInfoService;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private AlipayClient alipayClient;

    /**
     * 调用支付宝接口生成支付表单
     *
     * @param userId
     * @param orderId
     * @return
     */
    @Override
    public String createAlipayForm(String userId, Long orderId) {
        try {
            //1.先保存本地交易记录
            //1.1 创建本地交易记录为本地交易记录属性赋值
            PaymentInfo paymentInfo = new PaymentInfo();
            //1.2 调用订单服务获取订单信息
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
            if (orderInfo != null) {
                //1.3 为本地交易记录中属性赋值
                paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
                paymentInfo.setOrderId(orderId);
                paymentInfo.setUserId(orderInfo.getUserId());
                paymentInfo.setPaymentType(PaymentType.ALIPAY.name());
                //paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
                //TODO 开发阶段设置为0.01 一分钱
                paymentInfo.setTotalAmount(new BigDecimal("0.01"));
                paymentInfo.setSubject(orderInfo.getTradeBody());
                paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
                paymentInfoService.savePaymentInfo(paymentInfo, PaymentType.ALIPAY.name());

                //2.调用支付宝接口(显示支付页面接口)
                //2.1 创建支付页面请求对象
                AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
                ///2.1.1 设置同步回调地址-给用户看支付成功页面
                request.setReturnUrl(AlipayConfig.return_payment_url);
                ///2.1.2 设置异步回调地址-支付宝给系统通知
                request.setNotifyUrl(AlipayConfig.notify_payment_url);

                //2.2 设置请求头参数:必传参数
                JSONObject bizContent = new JSONObject();
                //商户订单号,商家自定义,保持唯一性
                bizContent.put("out_trade_no", orderInfo.getOutTradeNo());
                //支付金额,最小值0.01元
                bizContent.put("total_amount", paymentInfo.getTotalAmount());
                //订单标题,不可使用特殊符号
                bizContent.put("subject", orderInfo.getTradeBody());
                //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
                bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
                //支付二维码过期时间
                bizContent.put("timeout_express", "10m");

                //2.3 调用支付宝接口
                request.setBizContent(bizContent.toString());
                AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
                //2.4 从响应对象获取支付表单页面
                if (response.isSuccess()) {
                    return response.getBody();
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("展示支付页面异常");
        }
        return null;
    }
}

4.4.3 前端页面

<ul class="payType">
   <a th:href="@{http://api.gmall.com/api/payment/alipay/submit/{orderId}(orderId=${orderInfo.id})}" target="_blank"><li><img src="./img/_/pay2.jpg"></li></a>
  
</ul>

5、支付后回调—同步回调

img

注意:将保存订单方法中延迟关闭订单消息时间设置为:600

5.1 控制器AlipayController

/**
 * 支付宝同步回调地址处理,重定向地址支付成功页面
 *
 * @param response
 */
@GetMapping("/alipay/callback/return")
public void redirectUrl(HttpServletResponse response) throws IOException {
    //重定向地址:http://payment.gmall.com/pay/success.html
    response.sendRedirect(AlipayConfig.return_order_url);
}

5.2 在web-all 项目中添加对应的返回控制器

PaymentHtmlController

/**
 * 支付成功页面渲染
 * @return
 */
@GetMapping("/pay/success.html")
public String paySuccessHtml() {
    return "/payment/success";
}

6、支付宝回调—异步回调

img

准备工作 https://natapp.cn/

  1. 使用内网穿透工具NetApp,将网关服务能通过公网地址进行访问.提供给支付宝进行异步回调支付结果接口.

  2. 申请免费隧道 端口:80

    image-20230510134355262

  3. 修改netapp目录下的配置文件config.ini

    [default]
    authtoken=申请免费隧道对应的authtoken   #对应一条隧道的authtoken
    
  4. 启动netapp软件 得到随机分配公网域名

  5. 修改nacos配置中心 service-payment-dev.yaml中异步回调地址 跟 随机分配域名一致

    image-20230314102354759

image-20230314102408255

异步回调有两个重要的职责:

确认并记录用户已付款通知电商模块。新版本的支付接口已经取消了同步回调的支付结果传递。所以用户付款成功与否全看异步回调。

接收到回调要做的事情:

1、 验证回调信息的真伪(避免出现“假通知”)

2、 验证用户付款的成功与否

3、 把新的支付状态写入支付信息表{paymentInfo}中。

4、 通知电商

5、 给支付宝返回回执。

6.1 控制器AlipayController

/**
 * 处理支付宝异步回调(支付宝通知支付系统告知用户支付结果)
 * @param mapParam
 * @return
 */
@PostMapping("/alipay/callback/notify")
public String notifyPaySuccess(@RequestParam Map mapParam){
    String retStr = alipayService.notifyPaySuccess(mapParam);
    return retStr;
}

6.2 AlipayService

/**
 * 处理支付宝异步回调(支付宝通知支付系统告知用户支付结果)
 * @param mapParam
 * @return
 */
String notifyPaySuccess(Map mapParam);
/**
 * 处理支付宝异步回调(支付宝通知支付系统告知用户支付结果)
 *
 * @param mapParam
 * @return
 */
@Override
public String notifyPaySuccess(Map mapParam) {
    try {
        log.info("支付宝提交异步通知参数:{}", mapParam);
        //1.对提交参数进行”验签“,对提交所有参数进行验证签名,避免出现提交参数篡改(避免出现”假“通知)”
        boolean signVerified = AlipaySignature.rsaCheckV1(mapParam, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
        if (!signVerified) {
            return "failure";
        }
        //2.验证订单是否为商家所有
        //2.1 得到支付宝提交商户订单编号
        String outTradeNo = (String) mapParam.get("out_trade_no");
        //2.2 根据订单编号+支付方式 查询本地交易记录
        PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
        if (paymentInfo == null) {
            return "failure";
        }
        //3.验证支付宝交易金额跟商家端订单金额是否一致
        //3.1 得到支付宝提交发生交易金额数
        String totalAmount = (String) mapParam.get("total_amount");
        //3.2 判断金额是否一致
        if (paymentInfo.getTotalAmount().compareTo(new BigDecimal(totalAmount)) != 0) {
            return "failure";
        }

        //4.验证appId是否为商户所有
        String appId = (String) mapParam.get("app_id");
        if (!appId.equals(AlipayConfig.app_id)) {
            return "failure";
        }

        //5.验证支付宝端交易状态 如果是“TRADE_SUCCESS”(用户付钱成功)
        String tradeStatus = (String) mapParam.get("trade_status");
        if ("TRADE_SUCCESS".equals(tradeStatus)) {
            //6.修改本地交易记录 包括:支付宝交易号跟本地交易记录关联;回调信息;支付状态:已支付
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setCallbackTime(new Date());
            paymentInfo.setCallbackContent(mapParam.toString());
            String tradeNo = (String) mapParam.get("trade_no");
            //只有用户付款成功后,才会将本地交易记录关联到支付宝端交易号
            paymentInfo.setTradeNo(tradeNo);
            paymentInfoService.updateById(paymentInfo);
            //7.TODO 订单服务需改订单状态改为“已支付”
            return "success";
        }

    } catch (Exception e) {
        log.error("[支付服务]处理支付宝异步回调异常:{}", e);
        throw new RuntimeException(e);
    }
    return "failure";
}

6.2.1 PaymentService

/**
 * 根据订单编号+支付方式 查询本地交易记录
 * @param outTradeNo
 * @param paymentType
 * @return
 */
PaymentInfo getPaymentInfo(String outTradeNo, String paymentType);

6.2.2 PaymentService实现类

/**
 * 根据订单编号+支付方式 查询本地交易记录
 *
 * @param outTradeNo
 * @param paymentType
 * @return
 */
@Override
public PaymentInfo getPaymentInfo(String outTradeNo, String paymentType) {
    LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
    queryWrapper.eq(PaymentInfo::getOutTradeNo, outTradeNo);
    return this.getOne(queryWrapper);
}

测试注意事项:

  • 修改com.atguigu.gmall.payment.service.impl.PaymentInfoServiceImpl#savePaymentInfo保存本地交易记录方法中本地交易支付金额image-20230129101922718
  • 修改nacos中支付服务配置文件中回调地址域名-改成自己启动后的域名

7、退款

商家前端有在线客服:https://www.easemob.com

退款发起时机:发起退款后顾客跟商家协商一致.

  • 消费者刚支付成功退款(商家未发货);恢复商品锁定库存;修改订单状态:关闭;修改本地交易记录状态:关闭;调用支付宝接口进行退款
  • 消费者支付成功,商家发货; 退货--要求顾客将货物寄回仓库;恢复库存;关闭订单,关闭交易记录;调用支付宝接口进行退款

直接在浏览器发起请求即可!

商户与客户协商一致的情况下,才可以退款!

YAPI接口地址:http://192.168.200.128:3000/project/11/interface/api/845

7.1 控制器

AlipayApiController

/**
 * 测试接口,用于支付宝退款
 * @param outTradeNo 订单编号
 * @return
 */
@GetMapping("/alipay/refund/{outTradeNo}")
public Result refund(@PathVariable("outTradeNo") String outTradeNo){
    alipayService.refund(outTradeNo);
    return Result.ok();
}

7.2 业务接口

AlipayService

/**
 * 进行支付宝退款业务处理
 * @param outTradeNo
 */
void refund(String outTradeNo);

7.2 业务实现类

AlipayServiceImpl

/**
 * 进行支付宝退款业务处理
 *
 * @param outTradeNo
 */
@Override
public void refund(String outTradeNo) {
    try {
        //1.验证本地交易记录状态=“已支付”,调用支付宝接口完成退款
        PaymentInfo paymentInfo = paymentInfoService.getPaymentInfo(outTradeNo, PaymentType.ALIPAY.name());
        if (paymentInfo != null && PaymentStatus.PAID.name().equals(paymentInfo.getPaymentStatus())) {
            //调用支付宝进行退款
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            JSONObject bizContent = new JSONObject();
            if (StringUtils.isNotBlank(paymentInfo.getTradeNo())) {
                bizContent.put("trade_no", paymentInfo.getTradeNo());
            }
            if (StringUtils.isNotBlank(paymentInfo.getOutTradeNo())) {
                bizContent.put("out_trade_no", paymentInfo.getOutTradeNo());
            }
            bizContent.put("refund_amount", paymentInfo.getTotalAmount());
            bizContent.put("out_request_no", "HZ01RF001");
            request.setBizContent(bizContent.toString());
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                if ("Y".equals(response.getFundChange())) {
                    //2.修改本地交易记录状态
                    paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
                    paymentInfoService.updateById(paymentInfo);

                    //3.TODO 远程调用订单服务 修改订单交易记录状态
                }
            }
        }
    } catch (Exception e) {
        log.error("[支付服务]订单:{},退款异常:{}", outTradeNo, e);
        throw new RuntimeException(e);
    }
}

如果要用以前关闭订单的队列,需要修改原来OrderInfoServiceImpl关闭订单业务增加判断

/**
 * 关闭订单
 *
 * @param orderId
 */
@Override
public void execExpiredOrder(Long orderId) {
    //1.查询订单状态 判断状态 是否为待支付
    OrderInfo orderInfo = this.getById(orderId);
    if (orderInfo != null && (OrderStatus.UNPAID.name().equals(orderInfo.getOrderStatus()) || OrderStatus.PAID.name().equals(orderInfo.getOrderStatus()))) {
        //2.修改为关闭
        this.updateOrderStatus(orderId, ProcessStatus.CLOSED);
    }
}