前后端分离实现沙箱支付后,前端渲染后端传过来的form表单

 后端返回的form表单如下

<form name="punchout_form" method="post" action="...">

<input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;123450679&quot;,&quot;total_amount&quot;:&quot;16&quot;,&quot;subject&quot;:&quot;苹果&quot;,&quot;body&quot;:&quot;好吃&quot;,&quot;product_code&quot;:&quot;FAST_INSTANT_TRADE_PAY&quot;}">
<input type="submit" value="立即支付" style="display:none" >
</form>
<script>document.forms[0].submit();</script>

 前端接收数据后,

axios.get('/ali/pay')
	.then(function(response) {
		console.log(response);
		const div = document.createElement('divform');
		div.innerHTML = response.data;
		document.body.appendChild(div);  
		document.forms['punchout_form'].setAttribute('target', '_blank')
		document.forms['punchout_form'].submit()
}).catch(function(error) {
		// 处理错误情况
		console.log(error);
}).then(function() {
		// 总是会执行
});

punchout_form 是返回的form表单的name

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
支付沙箱环境是用于模拟支付宝正式环境的测试环境,可以进行支付支付的测试。本文将介绍如何使用Spring Boot实现支付沙箱支付后端案例。 1. 创建Spring Boot项目 使用IDEA或Eclipse创建一个新的Spring Boot项目,并添加以下依赖: ``` <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.9.5.ALL</version> </dependency> ``` 2. 配置支付沙箱环境 在支付宝开放平台注册并创建应用后,进入沙箱环境,获取沙箱环境的AppID、RSA私钥和公钥,并将其配置到项目中。 ``` #支付沙箱环境配置 alipay.gateway=https://openapi.alipaydev.com/gateway.do alipay.app-id=应用ID alipay.private-key=应用私钥 alipay.public-key=支付宝公钥 ``` 3. 创建支付支付接口 创建一个支付支付接口,用于生成支付支付的请求参数。 ``` @RestController public class AlipayController { @Autowired private AlipayProperties alipayProperties; @GetMapping("/alipay") public String alipay(HttpServletRequest request, HttpServletResponse response) throws Exception { //实例化客户端 AlipayClient alipayClient = new DefaultAlipayClient(alipayProperties.getGateway(), alipayProperties.getAppId(), alipayProperties.getPrivateKey(), "json", "utf-8", alipayProperties.getPublicKey(), "RSA2"); //创建API对应的request AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); //在公共参数中设置回跳和通知地址 alipayRequest.setReturnUrl(alipayProperties.getReturnUrl()); alipayRequest.setNotifyUrl(alipayProperties.getNotifyUrl()); //填充业务参数 alipayRequest.setBizContent("{\"out_trade_no\":\"" + 1 + "\"," + "\"total_amount\":\"" + 0.01 + "\"," + "\"subject\":\"" + "测试商品" + "\"," + "\"body\":\"" + "测试商品详情" + "\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); //调用SDK生成表单 String form = alipayClient.pageExecute(alipayRequest).getBody(); //直接将完整的表单html输出到页面 response.setContentType("text/html;charset=utf-8"); response.getWriter().write(form); response.getWriter().flush(); response.getWriter().close(); return null; } } ``` 4. 创建支付宝回调接口 创建一个支付宝回调接口,用于接收支付支付的异步通知。 ``` @RestController public class AlipayNotifyController { @PostMapping("/notify") public String notify(HttpServletRequest request) throws Exception { //获取支付宝POST过来反馈信息 Map<String, String> params = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) { String name = iter.next(); String[] values = requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } //乱码解决,这段代码在出现乱码时使用 valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8"); params.put(name, valueStr); } //调用SDK验证签名 boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayProperties.getPublicKey(), "utf-8", "RSA2"); if (signVerified) { //验证成功,处理业务逻辑 return "success"; } else { //验证失败 return "fail"; } } } ``` 5. 创建支付前端页面 创建一个支付前端页面,用于调用支付支付的接口。 ``` <html> <head> <meta charset="UTF-8"> <title>支付沙箱支付</title> </head> <body> <div> <button onclick="alipay()">支付支付</button> </div> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script> function alipay() { $.get("/alipay", function (data) { $("body").html(data); }) } </script> </body> </html> ``` 6. 测试支付沙箱支付 启动Spring Boot项目,访问支付前端页面,点击“支付支付”按钮,跳转到支付支付页面,输入支付宝账号和密码进行支付支付成功后,会触发支付宝回调接口中的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值