调用第三方接口——支付宝付款

devtools/2024/9/24 7:25:48/

沙箱环境是支付宝开放平台为开发者提供的用于接口开发及主要功能联调的模拟环境。

参考 登录 - 支付宝

在沙箱环境下,已经分配好了用于模拟测试的应用信息、商家信息、买家信息等

小程序文档 - 支付宝文档中心

 内网穿透(支付宝付款需要在公网进行检查网站)

这里使用natapp内网穿透工具,参考 NATAPP-内网穿透 基于ngrok的国内高速内网映射工具

1.创建前端页面

订单列表页 order.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>订单列表</title><style>tr{text-align: center;}</style>
</head>
<body><table border="1px" width="700px" height="200px"><tr><th>序号</th><th>订单号</th><th>订单名称</th><th>订单金额</th><th>下单时间</th><td>操作</td></tr><tr><td>1</td><td>202312100001</td><td>测试订单1</td><td>¥1.0</td><td>2023-12-10 10:30:21</td><td><a href="pay.html">去支付</a></td></tr><tr><td>2</td><td>202312100002</td><td>测试订单2</td><td>¥1.0</td><td>2023-12-10 10:30:21</td><td><a href="pay.html">去支付</a></td></tr><tr><td>3</td><td>202312100003</td><td>测试订单3</td><td>¥1.0</td><td>2023-12-10 10:30:21</td><td><a href="pay.html">去支付</a></td></tr></table>
</body>
</html>

确认付款页 pay.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>确认付款</title>   
</head>
<body><h2>确认付款</h2><form id="payForm" method="post" action="http://localhost:8080/alipay/pay"><p>订单号:<input type="text" name="orderNo" value="202312100001"></p><p>订单名称:<input type="text" name="subject" value="测试订单1"></p><p>付款金额:<input type="text" name="totalAmount" value="1.0"></p><p><input type="submit" value="付款"></p></form>
</body>
</html>

后端界面:
引入相关的依赖:

processor是为了将配置类中的信息,一次性读取注入到属性中:

        <!--   支付宝     --><dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.34.0.ALL</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></dependency>

application.yml 

# 支付宝配置信息
alipay:appId: xxxxxxxappPrivateKey: xxxxxxxalipayPublicKey: xxxxxxxsignType: RSA2charset: utf-8gatewayUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.donotifyUrl: xxxxxxxreturnUrl: http://127.0.0.1:5500/order.html

创建Alipay属性类:

java">@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayProperties {// 应用idprivate String appId;// 应用私钥private String appPrivateKey;// 支付宝公钥private String alipayPublicKey;// 签名方式private String signType;// 字符编码private String charset;// 支付宝网关private String gatewayUrl;// 异步通知地址private String notifyUrl;// 同步跳转地址,即支付完成后跳转的地址private String returnUrl;
}

 创建Alipay配置类

java">@Configuration
public class AlipayConfig {@Resourceprivate AlipayProperties alipayProperties;/*** 创建支付客户端*/@Beanpublic AlipayClient alipayClient(){AlipayClient alipayClient = new DefaultAlipayClient(alipayProperties.getGatewayUrl(),alipayProperties.getAppId(),alipayProperties.getAppPrivateKey(),"json",alipayProperties.getCharset(),alipayProperties.getAlipayPublicKey(),alipayProperties.getSignType());return alipayClient;}/*** 创建支付请求对象*/@Beanpublic AlipayTradePagePayRequest alipayTradePagePayRequest(){AlipayTradePagePayRequest payRequest = new AlipayTradePagePayRequest();payRequest.setNotifyUrl(alipayProperties.getNotifyUrl());payRequest.setReturnUrl(alipayProperties.getReturnUrl());return payRequest;}
}

创建Alipay控制类:

java">package net.wanho.controller;import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.wanho.properties.AlipayProperties;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;@RestController
@RequestMapping("/alipay")
@CrossOrigin
@Slf4j
public class AlipayController {@Resourceprivate AlipayClient alipayClient;@Resourceprivate AlipayTradePagePayRequest payRequest;@Resourceprivate AlipayProperties alipayProperties;/*** 支付方法*/@SneakyThrows@PostMapping("/pay")public void pay(String orderNo, String subject, Double totalAmount, HttpServletResponse response) {//封装业务参数JSONObject bizContent = new JSONObject();bizContent.put("out_trade_no", orderNo);//订单号bizContent.put("subject", subject); //订单名称bizContent.put("total_amount", totalAmount); //订单总金额bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); // 产品码,固定值bizContent.put("timeout_express", "15m"); //超时时间,15minpayRequest.setBizContent(bizContent.toString());//向支付宝发起支付请求String result = alipayClient.pageExecute(payRequest).getBody();//将结果返回给前端,进入支付用户登录界面response.setContentType("text/html;charset=utf-8");response.getWriter().print(result);}/*** 异步通知*/@SneakyThrows@PostMapping("/notify")public String notify(@RequestParam Map<String, String> params) {System.out.println(params);/*** 进行验签操作,防止签名被窜改*/boolean flag = AlipaySignature.rsaCheckV1(params, alipayProperties.getAlipayPublicKey(), alipayProperties.getCharset(), alipayProperties.getSignType());if (!flag) {log.info("验证签名失败!");return "fail";}/*** 判断返回的订单号、订单总金额是否与发送的一致,防止数据被篡改*/String out_trade_no = params.get("out_trade_no");String total_amount = params.get("total_amount");/*** 判断支付宝返回的状态是否正常*/String trade_status = params.get("trade_status");if (!trade_status.equals("TRADE_SUCCESS")) {log.info("交易失败!");return "fail";}/*** 更新相应的订单状态,例如将订单标记为已支付、更新库存等*/
//        orderService.updateOrderStatus();return "success";}}

实现效果:


http://www.ppmy.cn/devtools/39963.html

相关文章

2024年4月12日饿了么春招实习试题【第三题】-题目+题解+在线评测,2024.4.12,饿了么机试【Kruskal 算法, 最小生成树】

2024年4月12日饿了么春招实习试题【第三题】-题目题解在线评测&#xff0c;2024.4.12&#xff0c;饿了么机试 &#x1f3e9;题目一描述&#xff1a;样例1样例2解题思路一&#xff1a;[Kruskal 算法](https://baike.baidu.com/item/%E5%85%8B%E9%B2%81%E6%96%AF%E5%8D%A1%E5%B0%…

Reactor Netty 其他-响应式编程-018

&#x1f917; ApiHug {Postman|Swagger|Api...} 快↑ 准√ 省↓ GitHub - apihug/apihug.com: All abou the Apihug apihug.com: 有爱&#xff0c;有温度&#xff0c;有质量&#xff0c;有信任ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace The Nex…

【CTF MISC】XCTF GFSJ0510 this_is_flag Writeup(信息收集)

this_is_flag Most flags are in the form flag{xxx}, for example:flag{th1s_!s_a_d4m0_4la9} 解法 大多数标志的形式都是flag{xxx}&#xff0c;例如&#xff1a;flag{th1s_!s_a_d4m0_4la9} Flag flag{th1s_!s_a_d4m0_4la9}声明 本博客上发布的所有关于网络攻防技术的文章…

Ansible常用变量【上】

转载说明&#xff1a;如果您喜欢这篇文章并打算转载它&#xff0c;请私信作者取得授权。感谢您喜爱本文&#xff0c;请文明转载&#xff0c;谢谢。 在Ansible中会用到很多的变量&#xff0c;Ansible常用变量包括以下几种&#xff1a; 1. 自定义变量——在playbook中用户自定义…

vue根据文字动态判断溢出...鼠标悬停显示el-tooltip展示

使用自定义el- tooltip 组件 定义 Tooltip是一种小型弹出框&#xff0c;它显示有关特定页面元素的信息&#xff0c;例如按钮、链接或图标。Tooltip通常以半透明的气泡形式呈现&#xff0c;并出现在页面元素的旁边或下方。 它可以改善用户体验&#xff0c;使用户更容易理解页面…

事务的基础

&#x1f4dd;个人主页&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;面经 ⛺️稳中求进&#xff0c;晒太阳 事务的基础 1&#xff09;事务 事务是&#xff1a;一组操作的集合 &#xff0c;他是不可分割的工作单位。事务会把所有操作作为一个整体一起向系统提…

XXE-lab靶场搭建

源码下载地址 https://github.com/c0ny1/xxe-lab1.php_xxe 直接放在php web页面下即可运行。 2.java_xxe java_xxe是serlvet项目&#xff0c;直接导入eclipse当中即可部署运行。 3.python_xxe: 安装好Flask模块python xxe.py 4.Csharp_xxe 直接导入VS中运行 phpstudy…

【实验】根据docker部署nginx并且实现https

环境准备 systemctl stop firewalld setenforce 0 安装docker #安装依赖包 yum -y install yum-utils device-mapper-persistent-data lvm2 #设置阿里云镜像 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #安装最新版…