支付宝沙箱
支付宝沙箱#
1.创建应用
https://open.alipay.com/platform/appDaily.htm
2.配置类
package com.bs.sportinggoodsmall.config;
import java.io.FileWriter;
import java.io.IOException;
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String app_id = "2021000118697888";
// 商户私钥,您的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.baidu.com";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "http://localhost:8080/#/Order";
// 签名方式
public static String sign_type = "RSA2";
// 字符编码格式
public static String charset = "utf-8";
// 支付宝网关
public static String gatewayUrl = "https://openapi.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();
}
}
}
}
}
3.控制器
package com.bs.sportinggoodsmall.controller;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bs.sportinggoodsmall.config.AlipayConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author Zxy
* @Date 2020/11/24 16:39
* @Version 1.0
*/
@CrossOrigin
@Controller
public class PayController {
@RequestMapping("/pay")
@ResponseBody
public void payController(
HttpServletRequest request, HttpServletResponse response
) throws IOException, AlipayApiException {
// 获取初始化的AliPayClient
AlipayClient 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 = new String(request.getParameter("orderId").getBytes("ISO-8859-1"),"UTF-8");
//付款金额,必填
String total_amount = new String(request.getParameter("total").getBytes("ISO-8859-1"),"UTF-8");
//订单名称,必填
String subject = new String(request.getParameter("goodsTitle").getBytes("ISO-8859-1"),"UTF-8");
//商品描述,可空
// String body = new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ "body" +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
// + "\"total_amount\":\""+ total_amount +"\","
// + "\"subject\":\""+ subject +"\","
// + "\"body\":\""+ body +"\","
// + "\"timeout_express\":\"10m\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
//输出
response.setContentType("text/html;charset="+ AlipayConfig.charset);
response.getWriter().write(result);
response.getWriter().flush();
response.getWriter().close();
}
}
4.Vue中
<template>
<div>
<div v-html = "payGateWay">{{payGateWay}}</div>
</div>
</template>
<script>
export default {
name: 'orderAlipay',
data(){
return {
payGateWay: ''
}
} ,
mounted () {
this.payGateWay = this.$route.query.htmlData
this.$nextTick(()=> {
document.forms[0].submit()
})
}
}
</script>
<style scoped>
</style>
5.结算
@ResponseBody
@PostMapping(value = "/goumaiShopcartGoods")
public PayMoneyVo goumaiShopcartGoods(Integer goodsId, String buyerName,Integer goodsCount) {
QueryWrapper<Buyer> queryWrapper1=new QueryWrapper<>();
queryWrapper1.lambda().eq(Buyer::getBuyerName,buyerName);
Buyer buyer = buyerService.getOne(queryWrapper1);
Goods goods = goodsService.getById(goodsId);
Seller seller = sellerService.getById(goods.getSellerId());
Orders order = new Orders();
order.setOrdersId(System.currentTimeMillis());
order.setOrdersCreatetime(new Date());
order.setSellerTitle(seller.getSellerTitle());
order.setGoodsTitle(goods.getGoodsTitle());
order.setGoodsImg(goods.getGoodsDetailImg());
order.setGoodsPrice(goods.getGoodsPrice());
order.setGoodsCount(goodsCount);
order.setGoodsTitle(goods.getGoodsTitle());
order.setGoodsParameters(goods.getGoodsParameter());
order.setBuyerAdders(buyer.getBuyerAdders());
order.setGoodsId(goodsId);
order.setBuyerRealname(buyer.getBuyerRealname());
order.setBuyerPhone(buyer.getBuyerPhone());
order.setGoodsTotal(goodsCount*goods.getGoodsPrice());
order.setSellerId(seller.getSellerId());
order.setBuyerId(buyer.getBuyerId());
orderService.saveOrUpdate(order);
PayMoneyVo payMoneyVo=new PayMoneyVo();
payMoneyVo.setOrderId(order.getOrdersId());
payMoneyVo.setTotal(order.getGoodsTotal());
payMoneyVo.setGoodsTitle(order.getGoodsTitle());
return payMoneyVo;
}
public class PayMoneyVo {
Long orderId;//订单号
String goodsTitle;//订单名称
Double total;//付款金额
}
6.发请支付请求
vm.$http.post("/goumaiShopcartGoods", data).then(function (res) {
let paydata = new FormData();
paydata.append("orderId",res.data.orderId)
paydata.append("goodsTitle",res.data.goodsTitle)
paydata.append("total",res.data.total)
vm.$http.post("/pay",paydata).then(function (res) {
console.log(res.data)
let routerData = vm.$router.resolve({path:'/orderAlipay',query: {htmlData: res.data}})
// 打开新页面
window.open(routerData.href, '_ blank')
})
})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~