ToB企服应用市场:ToB评测及商务社交产业平台

标题: 付出宝沙箱版(什么是付出宝沙箱、配置付出宝沙箱、配置内网穿透、在Spring [打印本页]

作者: 冬雨财经    时间: 2024-11-28 22:12
标题: 付出宝沙箱版(什么是付出宝沙箱、配置付出宝沙箱、配置内网穿透、在Spring
0. 媒介

许多项目都需要有付出功能,但是对接第三方付出又需要企业资质,作为个人开发者,达不到这个条件,但付出功能是项目中一个很紧张的的部门,面试官也比较喜好问,如果面试官问与付出功能的相关的问题时,我们说由于没有企业资质,跳过了付出功能,那就错失了一次在面试官面前表现本身的机会
本日为大家介绍的付出宝沙箱正是为相识决这一痛点,通过使用付出宝沙箱,我们就能够在项目中模拟真正的付出,让我们的项目锦上添花(使用付出宝沙箱无需企业资质,只需要一个付出宝账号)
1. 什么是付出宝沙箱

付出宝沙箱(Alipay Sandbox)是付出宝为开发者提供的一个模拟真实付出宝情况的测试平台。开发者可以在沙箱情况中进行应用程序的开发和测试,无需使用真实的付出宝账户进行买卖业务

以下是付出宝沙箱的一些特点和用途:

付出宝沙箱对于希望集成付出宝付出服务的开发者来说是一个非常有用的工具,它能够资助开发者在不影响实际业务的情况下,高效、安全地完成付出功能的开发和测试工作
2. 配置付出宝沙箱

付出宝沙箱官网:沙箱应用 - 开放平台 (打开官网后需要用真实的付出宝账号进行登录)
2.1 沙箱应用的应用信息(获取app-id和gateway-url)

APPID 和 付出宝网关所在 字段下面会用到

2.2 沙箱账号的商家信息和买家信息

买家账号在付款时会用到

2.3 下载秘钥工具

下载所在:密钥工具下载 - 付出宝文档中央 (alipay.com)

   请不要安装在含有空格的目次路径下,否则会导致公私钥乱码的问题
  2.4 天生秘钥(获取private-key)


秘钥天生乐成的界面(应用私钥对应 private-key)

2.5 配置秘钥(获取alipay-public-key)

配置所在:沙箱应用 - 开放平台 (alipay.com)

点击自定义秘钥,再点击公钥模式的设置并检察

将刚才天生的应用公钥复制过来,接着点击生存按钮

最后点击确定按钮

将得到的付出宝公钥生存下来(付出宝公钥对应 alipay-public-key)
3. 配置内网穿透

3.1 使用cpolar实现内网穿透

我们使用 cpolar 来实现内网穿透,cpolar 的使用教程可参考我的另一篇博文:使用cpolar实现内网穿透,将Web项目部署到公网上
3.2 创建隧道(获取notify-url)

在欣赏器输入以下网址,创建隧道
  1. http://localhost:9200/#/tunnels/create
复制代码

创建隧道后会有一个公网所在,下面会用到

   cpolar 天生的公网所在在电脑重启之后可能会发生变化
  4. 在SpringBoot项目中怎样对接付出宝沙箱

本次演示的后端情况为:JDK 17.0.7 + SpringBoot 3.0.2
4.1 引入依赖(付出宝文档中央)

在项目的 pom.xml 文件中引入依赖(本次演示使用的是较新的版本,付出宝文档中央的版本是 4.34.0.ALL)
  1. <dependency>
  2.     <groupId>com.alipay.sdk</groupId>
  3.     <artifactId>alipay-sdk-java</artifactId>
  4.     <version>4.39.218.ALL</version>
  5. </dependency>
复制代码
完备的官方文档:通用版 - 付出宝文档中央 (alipay.com)

为了方便测试,我们额外引入 Spring Web 和 fastjson2 的依赖
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
复制代码
  1. <dependency>
  2.     <groupId>com.alibaba.fastjson2</groupId>
  3.     <artifactId>fastjson2</artifactId>
  4.     <version>2.0.53</version>
  5. </dependency>
复制代码
4.2 编写实体类

我们编写一个简单的实体类,用于测试
  1. /**
  2. * 订单类
  3. */
  4. public class Order {
  5.     /**
  6.      * 用于唯一标识一次支付请求,可以是订单号或与其他业务相关的唯一标识
  7.      */
  8.     private Long id;
  9.     /**
  10.      * 支付的总金额
  11.      */
  12.     private String totalAmount;
  13.     /**
  14.      * 支付时显示的商品描述
  15.      */
  16.     private String productDescription;
  17.     /**
  18.      * 支付时显示的商品名称
  19.      */
  20.     private String productName;
  21.     public Long getId() {
  22.         return id;
  23.     }
  24.     public void setId(Long id) {
  25.         this.id = id;
  26.     }
  27.     public String getTotalAmount() {
  28.         return totalAmount;
  29.     }
  30.     public void setTotalAmount(String totalAmount) {
  31.         this.totalAmount = totalAmount;
  32.     }
  33.     public String getProductDescription() {
  34.         return productDescription;
  35.     }
  36.     public void setProductDescription(String productDescription) {
  37.         this.productDescription = productDescription;
  38.     }
  39.     public String getProductName() {
  40.         return productName;
  41.     }
  42.     public void setProductName(String productName) {
  43.         this.productName = productName;
  44.     }
  45.     @Override
  46.     public String toString() {
  47.         return "Order{" +
  48.                 "id=" + id +
  49.                 ", totalAmount='" + totalAmount + '\'' +
  50.                 ", productDescription='" + productDescription + '\'' +
  51.                 ", productName='" + productName + '\'' +
  52.                 '}';
  53.     }
  54. }
复制代码
4.3 编写配置属性类

AlipayConfiguration.java
  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. @ConfigurationProperties(prefix = "alipay")
  5. public class AlipayConfiguration {
  6.     private String appId;
  7.     private String gatewayUrl;
  8.     private String privateKey;
  9.     private String alipayPublicKey;
  10.     private String notifyUrl;
  11.     private String returnUrl;
  12.     public String getAppId() {
  13.         return appId;
  14.     }
  15.     public void setAppId(String appId) {
  16.         this.appId = appId;
  17.     }
  18.     public String getGatewayUrl() {
  19.         return gatewayUrl;
  20.     }
  21.     public void setGatewayUrl(String gatewayUrl) {
  22.         this.gatewayUrl = gatewayUrl;
  23.     }
  24.     public String getPrivateKey() {
  25.         return privateKey;
  26.     }
  27.     public void setPrivateKey(String privateKey) {
  28.         this.privateKey = privateKey;
  29.     }
  30.     public String getAlipayPublicKey() {
  31.         return alipayPublicKey;
  32.     }
  33.     public void setAlipayPublicKey(String alipayPublicKey) {
  34.         this.alipayPublicKey = alipayPublicKey;
  35.     }
  36.     public String getNotifyUrl() {
  37.         return notifyUrl;
  38.     }
  39.     public void setNotifyUrl(String notifyUrl) {
  40.         this.notifyUrl = notifyUrl;
  41.     }
  42.     public String getReturnUrl() {
  43.         return returnUrl;
  44.     }
  45.     public void setReturnUrl(String returnUrl) {
  46.         this.returnUrl = returnUrl;
  47.     }
  48.     @Override
  49.     public String toString() {
  50.         return "AlipayConfiguration{" +
  51.                 "appId='" + appId + '\'' +
  52.                 ", gatewayUrl='" + gatewayUrl + '\'' +
  53.                 ", privateKey='" + privateKey + '\'' +
  54.                 ", alipayPublicKey='" + alipayPublicKey + '\'' +
  55.                 ", notifyUrl='" + notifyUrl + '\'' +
  56.                 ", returnUrl='" + returnUrl + '\'' +
  57.                 '}';
  58.     }
  59. }
复制代码

4.4 编写controller(设置notify-url和return-url)

AlipayController.java
  1. import cn.edu.scau.config.AlipayConfiguration;
  2. import cn.edu.scau.pojo.Order;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.alipay.api.AlipayApiException;
  5. import com.alipay.api.AlipayClient;
  6. import com.alipay.api.DefaultAlipayClient;
  7. import com.alipay.api.diagnosis.DiagnosisUtils;
  8. import com.alipay.api.domain.AlipayTradeRefundModel;
  9. import com.alipay.api.request.AlipayTradePagePayRequest;
  10. import com.alipay.api.request.AlipayTradeRefundRequest;
  11. import com.alipay.api.response.AlipayTradeRefundResponse;
  12. import jakarta.servlet.http.HttpServletRequest;
  13. import jakarta.servlet.http.HttpServletResponse;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.web.bind.annotation.*;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.Map;
  21. import static com.alipay.api.AlipayConstants.*;
  22. @Controller
  23. @RequestMapping("/alipay")
  24. public class AlipayController {
  25.     private final AlipayConfiguration alipayConfiguration;
  26.     public AlipayController(AlipayConfiguration alipayConfiguration) {
  27.         this.alipayConfiguration = alipayConfiguration;
  28.     }
  29.     @GetMapping("/pay")
  30.     public void pay(@RequestParam Long orderId, HttpServletResponse httpResponse) throws IOException {
  31.         // 创建默认的支付宝客户端实例
  32.         AlipayClient alipayClient = new DefaultAlipayClient(
  33.                 alipayConfiguration.getGatewayUrl(),
  34.                 alipayConfiguration.getAppId(),
  35.                 alipayConfiguration.getPrivateKey(),
  36.                 FORMAT_JSON,
  37.                 CHARSET_UTF8,
  38.                 alipayConfiguration.getAlipayPublicKey(),
  39.                 SIGN_TYPE_RSA2
  40.         );
  41.         AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
  42.         // 设置异步通知地址
  43.         alipayTradePagePayRequest.setNotifyUrl(alipayConfiguration.getNotifyUrl());
  44.         // 设置重定向地址
  45.         alipayTradePagePayRequest.setReturnUrl(alipayConfiguration.getReturnUrl());
  46.         Order order = new Order();
  47.         order.setId(orderId); // 商户订单号
  48.         order.setProductName("爱国者键盘"); // 商品名称
  49.         order.setProductDescription("键盘中的战斗机"); // 商品描述
  50.         order.setTotalAmount("0.01"); // 付款金额,必填
  51.         // 构造业务请求参数(如果是电脑网页支付,product_code是必传参数)
  52.         JSONObject jsonObject = new JSONObject();
  53.         jsonObject.put("out_trade_no", order.getId());
  54.         jsonObject.put("subject", order.getProductName());
  55.         jsonObject.put("body", order.getProductDescription());
  56.         jsonObject.put("total_amount", order.getTotalAmount());
  57.         jsonObject.put("product_code", "FAST_INSTANT_TRADE_PAY");
  58.         alipayTradePagePayRequest.setBizContent(jsonObject.toJSONString());
  59.         // 请求支付宝接口
  60.         String alipayForm;
  61.         try {
  62.             alipayForm = alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
  63.         } catch (AlipayApiException e) {
  64.             throw new RuntimeException(e);
  65.         }
  66.         // 将表单直接输出到页面,用户点击后会跳转到支付宝支付页面
  67.         httpResponse.setContentType("text/html;charset=" + CHARSET_UTF8);
  68.         httpResponse.getWriter().write(alipayForm);
  69.         httpResponse.getWriter().flush();
  70.         httpResponse.getWriter().close();
  71.     }
  72.     /**
  73.      * 处理支付宝异步通知的接口(注意这里必须是POST接口)
  74.      *
  75.      * @param request HTTP请求对象,包含支付宝返回的通知信息
  76.      * @return 返回给支付宝的确认信息
  77.      */
  78.     @PostMapping("/notify")
  79.     @ResponseBody
  80.     public String payNotify(HttpServletRequest request) {
  81.         System.err.println("====================payNotify====================");
  82.         // 获取支付宝返回的各个参数
  83.         Map<String, String[]> requestParameterMap = request.getParameterMap();
  84.         requestParameterMap.forEach((key, value) -> System.err.println(key + " = " + Arrays.toString(value)));
  85.         // 检查交易状态是否为成功
  86.         if ("TRADE_SUCCESS".equals(request.getParameter("trade_status"))) {
  87.             System.err.println("交易成功");
  88.             // 执行更新数据库中的订单状态等操作
  89.         }
  90.         System.err.println("====================payNotify====================");
  91.         System.err.println();
  92.         // 告诉支付宝,已经成功收到异步通知
  93.         return "success";
  94.     }
  95.     @GetMapping("/return")
  96.     public void payReturn(HttpServletRequest request, HttpServletResponse response) throws Exception {
  97.         System.out.println("====================payReturn====================");
  98.         // 获取支付宝返回的各个参数
  99.         Map<String, String[]> requestParameterMap = request.getParameterMap();
  100.         System.out.println("支付宝返回的参数:");
  101.         requestParameterMap.forEach((key, value) -> System.out.println(key + " = " + Arrays.toString(value)));
  102.         System.out.println("====================payReturn====================");
  103.         System.out.println();
  104.         // 设置响应的字符编码为UTF-8,防止中文乱码
  105.         response.setCharacterEncoding("UTF-8");
  106.         // 设置响应的内容类型为HTML,并指定字符编码为UTF-8
  107.         response.setContentType("text/html; charset=UTF-8");
  108.         // 重定向到指定的HTML页面(需要提前与前端沟通好支付成功后要跳转的页面)
  109.         response.sendRedirect("http://localhost:5173/paySuccess");
  110.     }
  111.     @GetMapping("/refund")
  112.     @ResponseBody
  113.     public String refund(@RequestParam(required = false) Long id, @RequestParam(required = false) String tradeNo) {
  114.         System.out.println("====================refund====================");
  115.         // 创建默认的支付宝客户端实例
  116.         AlipayClient alipayClient = new DefaultAlipayClient(
  117.                 alipayConfiguration.getGatewayUrl(),
  118.                 alipayConfiguration.getAppId(),
  119.                 alipayConfiguration.getPrivateKey(),
  120.                 FORMAT_JSON,
  121.                 CHARSET_UTF8,
  122.                 alipayConfiguration.getAlipayPublicKey(),
  123.                 SIGN_TYPE_RSA2
  124.         );
  125.         Order order = new Order();
  126.         order.setId(id);
  127.         order.setProductName("爱国者键盘");
  128.         order.setProductDescription("键盘中的战斗机");
  129.         order.setTotalAmount("0.01");
  130.         AlipayTradeRefundRequest alipayTradeRefundRequest = new AlipayTradeRefundRequest();
  131.         AlipayTradeRefundModel alipayTradeRefundModel = new AlipayTradeRefundModel();
  132.         // 设置商户订单号
  133.         if (id != null) {
  134.             alipayTradeRefundModel.setOutTradeNo(String.valueOf(order.getId()));
  135.         }
  136.         // 设置查询选项
  137.         List<String> queryOptions = new ArrayList<>();
  138.         queryOptions.add("refund_detail_item_list");
  139.         alipayTradeRefundModel.setQueryOptions(queryOptions);
  140.         // 设置退款金额
  141.         alipayTradeRefundModel.setRefundAmount("0.01");
  142.         // 设置退款原因说明
  143.         alipayTradeRefundModel.setRefundReason("正常退款");
  144.         // 设置流水订单号
  145.         if (tradeNo != null && !tradeNo.isEmpty()) {
  146.             alipayTradeRefundModel.setTradeNo(tradeNo);
  147.         }
  148.         alipayTradeRefundRequest.setBizModel(alipayTradeRefundModel);
  149.         // 执行请求
  150.         AlipayTradeRefundResponse alipayTradeRefundResponse = null;
  151.         int count = 1;
  152.         int limit = 3;
  153.         while (count <= limit) { // 最多重试3次
  154.             try {
  155.                 alipayTradeRefundResponse = alipayClient.execute(alipayTradeRefundRequest);
  156.                 break;
  157.             } catch (AlipayApiException e) {
  158.                 System.out.println("第" + count + "次重试");
  159.                 e.printStackTrace();
  160.                 count++;
  161.             }
  162.         }
  163.         if (count > limit || alipayTradeRefundResponse == null) {
  164.             return "退款失败,请联系客服人员";
  165.         }
  166.         System.out.println("alipayTradeRefundResponse.getBody() = " + alipayTradeRefundResponse.getBody());
  167.         System.out.println("====================refund====================");
  168.         System.out.println();
  169.         if (!alipayTradeRefundResponse.isSuccess()) {
  170.             // SDK版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
  171.             String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(alipayTradeRefundResponse);
  172.             System.out.println("diagnosisUrl = " + diagnosisUrl);
  173.             return "退款失败,请联系客服人员";
  174.         }
  175.         if ("N".equals(alipayTradeRefundResponse.getFundChange())) {
  176.             return "该订单已经退款,请勿重复操作";
  177.         }
  178.         return "退款成功";
  179.     }
  180. }
复制代码
  需要提前与前端沟通好付出乐成后要跳转的页面
  4.5 编写配置文件

   SpringBoot 应用程序的端口号需要与内网穿透中隧道的端口号保持一致
  application.yml
  1. alipay:
  2.   # app-id:支付宝开放平台中的APPID
  3.   app-id:
  4.   # gateway-url:支付宝开放平台中的支付宝网关地址
  5.   gateway-url:
  6.   # private-key:在支付宝开放平台秘钥工具中生成的应用私钥
  7.   private-key:
  8.   # alipay-public-key:支付宝开放平台中的支付宝公钥
  9.   alipay-public-key:
  10.   # notify-url:支付宝通过该URL通知交易状态变化
  11.   notify-url:
  12.   # return-url:用户完成支付后,支付宝会重定向到该URL
  13.   return-url: http://localhost:10005/alipay/return
  14. server:
  15.   port: 10005
复制代码
  cpolar 天生的公网所在在电脑重启之后可能会发生变化,需要更改 notify-url 属性
  5. 前端代码

本次演示使用的是由 Vue3 搭建的工程
5.1 vite.config.js

  1. import {fileURLToPath, URL} from 'node:url'
  2. import {defineConfig} from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. // https://vitejs.dev/config/
  5. export default defineConfig({
  6.   plugins: [
  7.     vue()
  8.   ],
  9.   resolve: {
  10.     alias: {
  11.       '@': fileURLToPath(new URL('./src', import.meta.url))
  12.     }
  13.   },
  14.   server: {
  15.     proxy: {
  16.       '/api': {
  17.         target: 'http://localhost:10005',
  18.         changeOrigin: true,
  19.         rewrite: (path) => path.replace('/api', '')
  20.       }
  21.     }
  22.   }
  23. })
复制代码
关键代码

5.2 src/utils/request.js

  1. import axios from 'axios'
  2. const request = axios.create({
  3.   baseURL: '/api',
  4.   timeout: 20000,
  5.   headers: {
  6.     'Content-Type': 'application/json;charset=UTF-8'
  7.   }
  8. })
  9. request.interceptors.request.use(
  10. )
  11. request.interceptors.response.use(response => {
  12.   if (response.data) {
  13.     return response.data
  14.   }
  15.   return response
  16. }, (error) => {
  17.   // Do something with response error
  18.   return Promise.reject(error)
  19. })
  20. export default request
复制代码
5.3 src/router/index.js

  1. import {createRouter, createWebHistory} from 'vue-router'
  2. import HomeView from '../views/HomeView.vue'
  3. const router = createRouter({
  4.   history: createWebHistory(import.meta.env.BASE_URL),
  5.   routes: [
  6.     {
  7.       path: '/',
  8.       name: 'home',
  9.       component: HomeView
  10.     }, {
  11.       path: '/paySuccess',
  12.       name: 'paySuccess',
  13.       component: () => import('../views/PaySuccess.vue')
  14.     }
  15.   ]
  16. })
  17. export default router
复制代码
5.4 src/views/HomeView.vue

  1. <template>
  2.   <div class="payment-container">
  3.     <input type="number" v-model="orderId" placeholder="订单号" class="input-field">
  4.     <button class="pay-button" @click="pay">支付</button>
  5.     <div ref="alipayForm" class="hidden-form"></div>
  6.   </div>
  7. </template>
  8. <script setup>
  9. import request from '@/utils/request.js'
  10. import { ref } from 'vue'
  11. const alipayForm = ref()
  12. const orderId = ref('')
  13. const pay = () => {
  14.   request
  15.       .get('/alipay/pay', {
  16.         params: {
  17.           orderId: orderId.value
  18.         }
  19.       })
  20.       .then((response) => {
  21.         let innerHtml = response.toString()
  22.         innerHtml = innerHtml.substring(0, innerHtml.indexOf('<script>'))
  23.         alipayForm.value.innerHTML = innerHtml
  24.         window.document.forms[0].submit()
  25.       })
  26.       .catch((error) => {
  27.         console.error('支付请求错误:', error)
  28.       })
  29. }
  30. </script>
  31. <style scoped>
  32. .payment-container {
  33.   display: flex;
  34.   flex-direction: column;
  35.   align-items: center;
  36.   justify-content: center;
  37.   min-height: 100vh;
  38.   background-color: #f0f8ff; /* 淡蓝色背景 */
  39.   font-family: Arial, sans-serif;
  40. }
  41. .input-field {
  42.   width: 300px; /* 输入框宽度 */
  43.   margin: 10px 0; /* 上下间距 */
  44.   padding: 15px; /* 内边距 */
  45.   border: 2px solid #c7e0ff; /* 边框颜色 */
  46.   border-radius: 5px; /* 圆角 */
  47.   outline: none; /* 去除默认轮廓 */
  48.   font-size: 16px; /* 字体大小 */
  49.   transition: border-color 0.3s; /* 平滑过渡效果 */
  50. }
  51. .input-field:focus {
  52.   border-color: #007bff; /* 聚焦时的边框颜色 */
  53. }
  54. .pay-button {
  55.   width: 333px; /* 按钮宽度比输入框略长 */
  56.   margin: 10px 0; /* 上下间距 */
  57.   padding: 15px; /* 内边距 */
  58.   border: none; /* 无边框 */
  59.   border-radius: 5px; /* 圆角 */
  60.   background-color: #007bff; /* 按钮颜色 */
  61.   color: white; /* 文字颜色 */
  62.   cursor: pointer; /* 鼠标指针样式 */
  63.   font-size: 16px; /* 字体大小 */
  64.   transition: background-color 0.3s; /* 平滑过渡效果 */
  65. }
  66. .pay-button:hover {
  67.   background-color: #0056b3; /* 悬停时更深的颜色 */
  68. }
  69. .hidden-form {
  70.   display: none; /* 隐藏表单 */
  71. }
  72. </style>
复制代码
关键代码如下

5.5 src/views/paySuccess.vue

  1. <template>
  2.   <div class="success-container">
  3.     <div class="success-icon">
  4.       <svg width="100" height="100" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  5.         <path
  6.             d="M12 2C6.48 2 2 6.48 2 12C2 17.52 6.48 22 12 22C17.52 22 22 17.52 22 12C22 6.48 17.52 2 12 2ZM12 20C7.59 20 4 16.41 4 12C4 7.59 7.59 4 12 4C16.41 4 20 7.59 20 12C20 16.41 16.41 20 12 20ZM16.59 7.58L10 14.17L7.41 11.59L6 13L10 17L18 9L16.59 7.58Z"
  7.             fill="#4CAF50"/>
  8.       </svg>
  9.     </div>
  10.     <h1>支付成功</h1>
  11.     <p class="success-message">您已完成支付,感谢您的使用</p>
  12.     <button class="btn" @click="returnHomePage">返回首页</button>
  13.   </div>
  14. </template>
  15. <script setup>
  16. import router from '@/router/index.js'
  17. const returnHomePage = () => {
  18.   router.push('/')
  19. }
  20. </script>
  21. <style scoped>
  22. .success-container {
  23.   display: flex;
  24.   flex-direction: column;
  25.   align-items: center;
  26.   justify-content: center;
  27.   height: 100vh;
  28.   background-color: #f4f8fa; /* 柔和的背景色调 */
  29. }
  30. .success-icon {
  31.   margin-bottom: 20px;
  32. }
  33. .success-icon svg {
  34.   fill: #4CAF50; /* 柔和的绿色调 */
  35. }
  36. h1 {
  37.   font-size: 2.5rem;
  38.   color: #333; /* 深色调的文字 */
  39.   margin: 0;
  40.   font-weight: 500;
  41. }
  42. .success-message {
  43.   font-size: 1.25rem;
  44.   color: #666; /* 稍浅的文字颜色 */
  45.   text-align: center;
  46.   max-width: 300px;
  47.   margin-top: 10px;
  48. }
  49. .btn {
  50.   display: inline-block;
  51.   padding: 14px 40px; /* 增加按钮的宽度和内边距 */
  52.   margin-top: 20px;
  53.   font-size: 1rem;
  54.   font-weight: 500;
  55.   color: #333; /* 深色调文字,确保在淡色背景下可见 */
  56.   background-image: linear-gradient(to right, #639cbc, #1c67a8); /* 更淡的渐变色 */
  57.   border: none;
  58.   border-radius: 25px;
  59.   box-shadow: 0 2px 4px rgba(50, 50, 93, 0.1), 0 1px 2px rgba(0, 0, 0, 0.08);
  60.   cursor: pointer;
  61.   transition: all 0.3s ease;
  62. }
  63. .btn:hover {
  64.   transform: translateY(-2px);
  65.   box-shadow: 0 4px 6px rgba(50, 50, 93, 0.1), 0 2px 4px rgba(0, 0, 0, 0.08);
  66. }
  67. .btn:focus {
  68.   outline: none;
  69.   box-shadow: 0 0 0 3px rgba(200, 200, 200, 0.5);
  70. }
  71. </style>
复制代码
6. 付出宝沙箱乐成付出的整个过程

   
  以下是乐成付出的整个过程的截图

付出暗码默认是111111




付出乐成后付出宝会返回一个流水订单号(trade_no),退款时需要用到流水订单号,可以将流水订单号生存到数据库中
7. 付出宝沙箱的退款操作

   
  在实际开发中,与商品有关的信息是需要从后端查出来的,为了方便测试,由前端传给后端(退款的具体代码参考本文的编写controller(设置notify-url和return-url)部门)

8. 可能遇到的问题

8.1 504 Gateway Time-out

   出现这种情况大概率是由于付出宝沙箱处于维护状态(常出现于节假日),可以过一段时间再进行实验
  

The gateway did not receive a timely response from the upstream server or application. Sorry for the inconvenience.
Please report this message and include the following information to us.
Thank you very much!
URL:http://excashier-sandbox.dl.alipaydev.com/standard/auth.htm?payOrderId=451a320ca38641729ed983e9ace73143.00Server:excashier-36.gz00z.sandbox.alipay.netDate:2024/10/05 14:38:26 Powered by Tengine
8.2 系统有点儿忙,一会儿再试试

   出现这种情况大概率是由于付出宝沙箱处于维护状态(常出现于节假日),可以过一段时间再进行实验
  


8.3 退款服务不可用(Service Currently Unavailable)

   执行退款操作时,如果付出宝沙箱返回以下信息,分析退款服务不可用,大概率是由于付出宝沙箱处于维护状态(常出现于节假日),可以过一段时间再进行实验
  
{“alipay_trade_refund_response”:{“msg”:“Service Currently Unavailable”,“code”:“20000”,“sub_msg”:“系统非常”,“refund_fee”:“0.00”,“sub_code”:“aop.ACQ.SYSTEM_ERROR”,“send_back_fee”:“0.00”},“sign”:“c5Ct9FwyzUNhUPPYXjM6FdCtILlIzAWwx3dPSC+do0uL2aVp0QPVjnFlkN2MDApwbeuQCoRRuAnlZMocVyP0P84cUmLKrptj2p+wfkfuJXGor0KVUH7CtttIrdTLMbMHfKaPvfBqUKLy/NwKuCwAyQP2va/KwEfBna3xlIhgTobu2EN3C1s3LRFvEK+9x08XNjomG1t2ponftQLj6dag5M/UpTTDswrZY4GB74bH9YbwN7bS8NiD4gUDn+ncTZ6sjPfnJbUKOvOSmaFAaf9WmYjznj0tjcBFqMYLIezZ8NtniUktxJHOJC6GHj88Y9eLccF2fvwrq+Oj6iBR+9eXqA==”}

8.4 退款操作遇到Read timed out错误

   执行退款操作时,如果程序抛出以下错误,是由于在与付出宝API进行通信时,读取操作超时了(可能是由于客户端与付出宝服务器之间的网络毗连不稳定或速度过慢,导致数据传输超时),可以进行有限制次数的重试
  com.alipay.api.AlipayApiException: java.net.SocketTimeoutException: Read timed out
at com.alipay.api.AbstractAlipayClient.doPost(AbstractAlipayClient.java:1015)
at com.alipay.api.AbstractAlipayClient._execute(AbstractAlipayClient.java:921)
at com.alipay.api.AbstractAlipayClient.execute(AbstractAlipayClient.java:395)
at com.alipay.api.AbstractAlipayClient.execute(AbstractAlipayClient.java:377)
at com.alipay.api.AbstractAlipayClient.execute(AbstractAlipayClient.java:372)
at com.alipay.api.AbstractAlipayClient.execute(AbstractAlipayClient.java:366)
9. 付出宝沙箱社区

社区所在:付出宝开发者社区-学习提问互动交流开放社区 (alipay.com)
在使用付出宝沙箱时,如果遇到了无法解决的问题,可以在社区中看看有没有解决办法
10. 付出宝沙箱手机端

除了用账号暗码进行付出,也可以使用手机扫描付出,现在付出宝沙箱在手机端仅提供 Android 版本
下载所在:沙箱工具 - 开放平台 (alipay.com)

11. 温馨提示

12. 完备的示例代码

完备的示例代码已经放到了 Gitee 上


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4