第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添加接口

OrderApiController

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

OrderInfoService接口

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

OrderServiceImpl实现类

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

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 java.util.Map;

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

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

}

服务降级类:OrderDegradeFeignClient增加方法

@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.enums.model.OrderStatus;
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-05-09 15:53
 */
@Controller
public class PaymentController {


    @Autowired
    private OrderFeignClient orderFeignClient;

    /**
     * 渲染选择支付页面
     *
     * @param orderId
     * @return
     */
    @GetMapping("/pay.html")
    public String payChooseHtml(Model model, @RequestParam("orderId") Long orderId) {
        //1.远程调用获取订单信息
        OrderInfo orderInfo = orderFeignClient.getOrderInfoById(orderId);
        //2.判断订单状态 给静态页赋值
        if (orderInfo != null && OrderStatus.UNPAID.name().equals(orderInfo.getOrderStatus())) {
            model.addAttribute("orderInfo", orderInfo);
            return "/payment/pay";
        }
        throw new RuntimeException("订单状态异常!");
    }
}

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.Component;

/**
 * @author: atguigu
 * @create: 2023-05-09 15:33
 */
@Component
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::getOrderId, paymentInfo.getOrderId());
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        PaymentInfo info = this.getOne(queryWrapper);
        if (info != null) {
            return;
        }
        //2.保存本地交易记录
        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.payment.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: atguigu
 * @create: 2023-05-10 09:18
 */
@RestController
@RequestMapping("/api/payment/alipay")
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    /**
     * 调用支付宝接口产生支付页面
     *
     * @param orderId
     * @return
     */
    @GetMapping("/submit/{orderId}")
    public String createAlipayForm(@PathVariable("orderId") Long orderId) {
        String payForm = alipayService.createAlipayForm(orderId);
        return payForm;
    }
}

4.4.2 编写支付宝下单

4.4.2.1 业务接口

package com.atguigu.gmall.payment.service;

public interface AlipayService {
    /**
     * 调用支付宝接口产生支付页面
     *
     * @param orderId
     * @return
     */
    String createAlipayForm(Long orderId);
}

4.4.2.2 业务实现类

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

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.atguigu.gmall.enums.model.OrderStatus;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author: atguigu
 * @create: 2023-05-10 09:21
 */
@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private PaymentInfoService paymentInfoService;

    @Autowired
    private AlipayClient alipayClient;

    /**
     * 生成支付宝支付页面
     *
     * @param orderId 订单ID
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createAlipayForm(Long orderId) {
        try {
            //1.远程调用订单微服务获取订单信息 验证订单支付状态
            OrderInfo orderInfo = orderFeignClient.getOrderInfoById(orderId);
            if (orderInfo != null && OrderStatus.UNPAID.name().equals(orderInfo.getOrderStatus())) {
                //2.构建本地交易记录对象保存本地交易记录
                PaymentInfo paymentInfo = new PaymentInfo();
                //2.1 为本地交易记录封装属性
                paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
                paymentInfo.setOrderId(orderId);
                paymentInfo.setUserId(orderInfo.getUserId());
                paymentInfo.setPaymentType(PaymentType.ALIPAY.name());
                paymentInfo.setSubject(orderInfo.getTradeBody());
                paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
                //2.2 todo 支付金额测试阶段固定设置0.01   支付宝产生支付二维码固定设置0.01
                paymentInfo.setTotalAmount(new BigDecimal(0.01));
                //2.3 保存本地交易记录
                paymentInfoService.savePaymentInfo(paymentInfo, PaymentStatus.UNPAID.name());

                //3.调用支付宝-展示支付页面接口得到渲染支付页面html文本
                AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
                //异步接收地址,仅支持http/https,公网可访问  支付宝平台调用商城系统-用来通知商城支付结果回调地址
                request.setNotifyUrl(AlipayConfig.notify_payment_url);
                //同步跳转地址,仅支持http/https 用户付款后展示支付成功页面
                request.setReturnUrl(AlipayConfig.return_payment_url);
                /******必传参数******/
                JSONObject bizContent = new JSONObject();
                //商户订单号,商家自定义,保持唯一性
                bizContent.put("out_trade_no", orderInfo.getOutTradeNo());
                //支付金额,最小值0.01元
                bizContent.put("total_amount", 0.01);
                //订单标题,不可使用特殊符号
                bizContent.put("subject", orderInfo.getTradeBody());
                //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
                bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
                /*****`*可选参数******/
                bizContent.put("timeout_express", "10m");
                //扩展信息,按需传入
                JSONObject extendParams = new JSONObject();
                extendParams.put("orderId", orderId);
                bizContent.put("extend_params", extendParams);
                request.setBizContent(bizContent.toString());

                AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
                if (response.isSuccess()) {
                    System.err.println(response.getBody());
                    return response.getBody();
                }
            }
            return null;
        } catch (Exception e) {
            log.error("[支付服务],产生支付宝支付二维码页面异常:{},订单ID:{}", e, orderId);
            throw new RuntimeException(e);
        }
    }
}

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

/**
 * 处理支付宝支付成功后同步回调,将地址重定向到渲染支付成功html页面请求地址
 * @param response
 * @throws IOException 
 */
@GetMapping("/alipay/callback/return")
public void redirectUrl(HttpServletResponse response) throws IOException {
    response.sendRedirect(AlipayConfig.return_order_url);
}

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

PaymentController

/**
 * 渲染支付成功页面 反馈给用户
 * @return
 */
@GetMapping("/pay/success.html")
public String paySuccess(){
    return "/payment/success.html";
}

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 paramsMap 支付提交参数
 * @return "success" 支付宝收到该结果,停止通知 如果未响应"success"做到最大努力通知 额外通知7次
 */
@PostMapping("/callback/notify")
public String notifyGmallPayResult(@RequestParam Map<String, String> paramsMap){
    String result = alipayService.notifyGmallPayResult(paramsMap);
    return result;
}

6.2 AlipayService

/**
 * 处理用户支付成功后,支付宝异步通知用户支付结果接口
 * @param paramsMap 支付提交参数
 * @return "success" 支付宝收到该结果,停止通知 如果未响应"success"做到最大努力通知 额外通知7次
 */
String notifyGmallPayResult(Map<String, String> paramsMap);
/**
 * 处理用户支付成功后,支付宝异步通知用户支付结果接口
 *
 * @param paramsMap 支付提交参数
 * @return "success" 支付宝收到该结果,停止通知 如果未响应"success"做到最大努力通知 额外通知7次
 */
@Override
public String notifyGmallPayResult(Map<String, String> paramsMap) {
    try {
        //1.业务校验
        // ①验证签名-保证支付响应数据确实是支付平台,保证提交参数网络传输中没有被篡改.防止出现"假通知" RSA非对称加密
        boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, AlipayConfig.alipay_public_key, AlipayConfig.charset, AlipayConfig.sign_type); //调用SDK验证签名
        if (signVerified) {
            // ②验证订单是否为商家,通过订单查询订单金额,商户端本地交易记录金额跟支付宝端金额是否一致
            String outTradeNo = paramsMap.get("out_trade_no");
            PaymentInfo paymentInfo = paymentInfoService.getPaymentInfoByOutTradeNo(outTradeNo, PaymentType.ALIPAY.name());
            // ③验证APPId是否为商家的
            if (paymentInfo != null) {
                BigDecimal totalAmount = paymentInfo.getTotalAmount();
                String aliPayTotalAmount = paramsMap.get("total_amount");
                //TODO 前提测试中 保存本地交易记录金额 0.01   生成支付宝支付二维码设置金额 0.01
                if (totalAmount.compareTo(new BigDecimal(aliPayTotalAmount)) == 0) {
                    //④判断支付宝支付交易结果
                    String tradeStatus = paramsMap.get("trade_status");
                    if("TRADE_SUCCESS".equals(tradeStatus)){
                        //2.如果支付宝响应支付结果:成功,修改本地交易记录状态
                        this.paySuccess(paymentInfo, paramsMap);
                        return "success";
                    }
                }
            }
        }
    } catch (AlipayApiException e) {
        return "failure";
    }


    return "failure";
}

/**
 * 修改本地交易记录状态
 *
 * @param paymentInfo
 * @param paramsMap
 */
private void paySuccess(PaymentInfo paymentInfo, Map<String, String> paramsMap) {
    //1.更新本地交易记录 跟 支付宝交易记录关联 后续进行日常对账
    //1.1 支付宝支付交易编号凭据
    String tradeNo = paramsMap.get("trade_no");
    paymentInfo.setTradeNo(tradeNo);
    paymentInfo.setCallbackTime(new Date());
    paymentInfo.setCallbackContent(paramsMap.toString());
    //2.更新本地交易记录状态
    paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
    paymentInfoService.updateById(paymentInfo);

    //3.TODO 发送消息到MQ通知订单微服务修改订单状态
}

6.2.1 PaymentService

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

6.2.2 PaymentService实现类

/**
 * 根据订单编号+付款方式查询本地交易记录
 *
 * @param outTradeNo
 * @param payType
 * @return
 */
@Override
public PaymentInfo getPaymentInfo(String outTradeNo, String payType) {
    LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(PaymentInfo::getOutTradeNo, outTradeNo);
    queryWrapper.eq(PaymentInfo::getPaymentType, payType);
    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 orderId
 * @return
 */
@GetMapping("/refund/{orderId}")
public Result refund(@PathVariable("orderId") Long orderId){
    alipayService.refund(orderId);
    return Result.ok();
}

7.2 业务接口

AlipayService

/**
 * 退款处理
 * @param orderId
 * @return
 */
boolean refund(Long orderId);

7.2 业务实现类

AlipayServiceImpl

/**
 * 双方协商一致后进行退款
 *
 * @param orderId
 * @return
 */
@Override
public void refund(Long orderId) {
    try {
        //1.将本地交易记录关闭
        LambdaUpdateWrapper<PaymentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PaymentInfo::getOrderId, orderId);
        updateWrapper.eq(PaymentInfo::getPaymentType, PaymentType.ALIPAY.name());
        PaymentInfo paymentInfo = paymentInfoService.getOne(updateWrapper);
        if (paymentInfo != null && PaymentStatus.PAID.name().equals(paymentInfo.getPaymentStatus())) {
            //2.调用支付宝退款接口完成退款处理
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            JSONObject bizContent = new JSONObject();
            if (StringUtils.isNotBlank(paymentInfo.getTradeNo())) {
                bizContent.put("trade_no", paymentInfo.getTradeNo());
            }
            bizContent.put("refund_amount", paymentInfo.getTotalAmount());
            bizContent.put("out_request_no", UUID.randomUUID().toString());
            bizContent.put("refund_reason", "双方协商一致,退款!");
            request.setBizContent(bizContent.toString());

            AlipayTradeRefundResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                String fundChange = response.getFundChange();
                if ("Y".equals(fundChange)) {
                    //退款成功后,修改本地交易记录状态
                    updateWrapper.set(PaymentInfo::getPaymentStatus, PaymentStatus.CLOSED);
                    paymentInfoService.update(updateWrapper);
                }
            }
        }
    } catch (Exception e) {
        log.error("[支付服务]退款异常:{}, 订单ID:", e, orderId);
        throw new RuntimeException(e);
    }
}