Springboot项目接入支付宝SDK

news/2024/10/5 7:59:27/

源码下载

config

java">import java.io.FileWriter;
import java.io.IOException;public class AlipayConfig {// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号public static String app_id = "";// 商户私钥,您的PKCS8格式RSA2私钥public static String merchant_private_key = "";// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。public static String alipay_public_key = "";// 服务器异步通知页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问public static String notify_url = "http://www.bing.com";// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问// 即支付成功之后,需要跳转到的页面,一般为网站的首页public static String return_url = "http://www.baidu.com";// 签名方式public static String sign_type = "RSA2";// 字符编码格式public static String charset = "utf-8";// 支付宝网关public static String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";// 日志存储路径public static String log_path = "C:\\";/*** 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)* @param sWord 要写入日志里的文本内容*/public static void logResult(String sWord) {FileWriter writer = null;try {writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");writer.write(sWord);} catch (Exception e) {e.printStackTrace();} finally {if (writer != null) {try {writer.close();} catch (IOException e) {e.printStackTrace();}}}}
}

AliPayController

java">import com.dysy.alipay.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Date;@CrossOrigin
@RequestMapping("/pay")
@Controller
public class AliPayController {@Autowiredprivate AlipayService alipayService;@GetMapping("/hello")public String hello() {return "index";}/*** 跳转到支付界面* @return* @throws Exception*/@GetMapping("/topay")@ResponseBodypublic String pay() throws Exception {String form = alipayService.toPay(String.valueOf(new Date().getTime()),720.0, "易购商城", "订单描述");return form;}
}

Service

AlipayService

java">service
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.dysy.alipay.config.AlipayConfig;
import org.springframework.stereotype.Service;@Service
public interface AlipayService {public String toPay(String orderId, double price, String orderName, String orderDesc) throws Exception;}

AlipayServiceImpl

java">import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.dysy.alipay.config.AlipayConfig;
import com.dysy.alipay.service.AlipayService;
import org.springframework.stereotype.Service;@Service
public class AlipayServiceImpl implements AlipayService {public String toPay(String orderId, double price, String orderName, String orderDesc) throws Exception{//获得初始化的AlipayClientAlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);//设置请求参数AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();alipayRequest.setReturnUrl(AlipayConfig.return_url);alipayRequest.setNotifyUrl(AlipayConfig.notify_url);//商户订单号,商户网站订单系统中唯一订单号,必填String out_trade_no = orderId;//付款金额,必填String total_amount = String.valueOf(price);//订单名称,必填String subject = orderName;//商品描述,可空String body = orderDesc;alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+ "\"total_amount\":\""+ total_amount +"\","+ "\"subject\":\""+ subject +"\","+ "\"body\":\""+ body +"\","+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");String form = "";AlipayTradePagePayResponse response = alipayClient.pageExecute(alipayRequest);if (response.isSuccess()) {form = alipayClient.pageExecute(alipayRequest).getBody();}// 这里返回的 form 是一个字符串,里面封装了支付的表单信息//(即 html 标签 和 javascript 代码),直接将这个 form 输出到页面即可。return form;}
}

util

FileUtils

java">import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;public class FileUtils {public static String readFileOfTxt(String path){StringBuilder sb = new StringBuilder();try(BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {char[] buf = new char[1024];int len = -1;while ((len = bufferedReader.read(buf)) != -1) {sb.append(new String(buf, 0, len));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return sb.toString();}
}

JSONUtils

java">package com.dysy.alipay.util;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;import java.util.Map;public class JSONUtils {private static final ObjectMapper mapper = new ObjectMapper();public static Map<String, Object> jsonToMap(String jsonStr) throws JsonProcessingException {Map<String, Object> map = mapper.readValue(jsonStr, Map.class);return map;}
}

templates

<form action="/pay/topay"><button type="submit">付款</button>
</form>

http://www.ppmy.cn/news/1473639.html

相关文章

uni-app-H5页面调用设备摄像头扫描二维码

应用场景&#xff1a;APK里面webView&#xff0c;访问用uniapp写的H5页面&#xff0c;需要调用设备摄像头扫描二维码 首先下载导入扫描插件&#xff1a;H5调用摄像头识别二维码&#xff08;原生H5调用&#xff0c;不需要任何sdk&#xff0c;本地扫描识别&#xff0c;不需要后端…

实现浏览器语音呼起及语音录入及下载

主要分布三部分: 第一部分:开始录音 ;第二部分:停止录音;第三部分:静默监听。 一、开始录音 代码如下: document.getElementById(startRecording).onclick = async function() {if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {try {au…

【代码随想录算法训练营第六十天|并查集、卡码网107.寻找可能存在的路径】

文章目录 卡码网107.寻找可能存在的路径 并查集基础内容还是看代码随想录 并查集 卡码网107.寻找可能存在的路径 纯并查集的基础应用&#xff0c;并查集只是看元素是否在同一个集合中&#xff0c;因此在加入的时候需要先查看两个元素是否已经在一个并查集中&#xff0c;如果不…

大数据处理系统架构特征

Storm之父Nathan Marz在《大数据系统构建&#xff1a;可扩展实时数据系统构建原理与最佳实践》一书中&#xff0c;提出了他认为大数据系统应该具有的属性。 1.鲁棒性和容错性&#xff08;Robust and Fault-tolerant&#xff09; 对大规模分布式系统来说&#xff0c;机器是不可…

golang线程池ants-实现架构

1、总体架构 ants协程池&#xff0c;在使用上有多种方式(使用方式参考这篇文章&#xff1a;golang线程池ants-四种使用方法)&#xff0c;但是在实现的核心就一个&#xff0c;如下架构图&#xff1a; 总的来说&#xff0c;就是三个数据结构&#xff1a; Pool、WorkerStack、goW…

基于深度学习LightWeight的人体姿态检测跌倒系统源码

一. LightWeight概述 light weight openpose是openpose的简化版本&#xff0c;使用了openpose的大体流程。 Light weight openpose和openpose的区别是&#xff1a; a 前者使用的是Mobilenet V1&#xff08;到conv5_5&#xff09;&#xff0c;后者使用的是Vgg19&#xff08;前10…

论文阅读之旋转目标检测ARC:《Adaptive Rotated Convolution for Rotated Object Detection》

论文link&#xff1a;link code&#xff1a;code ARC是一个改进的backbone&#xff0c;相比于ResNet&#xff0c;最后的几层有一些改变。 Introduction ARC自适应地旋转以调整每个输入的条件参数&#xff0c;其中旋转角度由路由函数以数据相关的方式预测。此外&#xff0c;还采…

【pearcmd】通过pearcmd.php 进行GetShell

https://cloud.tencent.com/developer/article/2204400 关于PHP 配置 register_argc_argv 小结 的一些研究文章。 应用例题 [NewStarCTF 2023 公开赛道]Include &#x1f350; <?phperror_reporting(0);if(isset($_GET[file])) {$file $_GET[file];if(preg_match(/flag|l…