沙箱支付
登录支付宝开放平台,选择底部沙箱支付
下载密钥生成工具
生成应用私钥与公钥,上传沙箱支付,获得支付宝公钥
配置支付通知与支付回调地址
SpringBoot配置
yml文件
这里的地址必须与沙箱配置的一样
controller
package com.zq.wx.controller;import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zq.wx.entity.DonaEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;//调用支付宝支付
@Controller
public class AlipayController {@Value("${alipay.appId}")private String appId;@Value("${alipay.appPrivateKey}")private String appPrivateKey;@Value("${alipay.charSet}")private String charSet;@Value("${alipay.alipayPublicKey}")private String alipayPublicKey;@Value("${alipay.signType}")private String signType;@Value("${alipay.getewayUrl}")private String getewayUrl;@Value("${alipay.notifyUrl}")private String notifyUrl;@Value("${alipay.returnUrl}")private String returnUrl;@Value("${alipay.format}")private String format;@ResponseBody@PostMapping("/alipay/pay")public String alipay(HttpSession session,@RequestBody DonaEntity donaEntity) throws AlipayApiException {//把dona_id项目id 放在session中session.setAttribute("dona_id",donaEntity.getDonaId());//生成订单号(支付宝的要求?)String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());String user = UUID.randomUUID().toString().replace("-","").toUpperCase();String OrderNum = time+user;//调用封装好的方法(给支付宝接口发送请求)return sendRequestToAlipay(OrderNum,donaEntity.getDonaMoney(),"测试商品");}/*参数1:订单号参数2:订单金额参数3:订单名称*///支付宝官方提供的接口private String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {//获得初始化的AlipayClientAlipayClient alipayClient = new DefaultAlipayClient(getewayUrl,appId,appPrivateKey,format,charSet,alipayPublicKey,signType);//设置请求参数AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//支付成功异步地址alipayRequest.setNotifyUrl(notifyUrl);alipayRequest.setReturnUrl(returnUrl);//商品描述(可空)String body="";alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","+ "\"total_amount\":\"" + totalAmount + "\","+ "\"subject\":\"" + subject + "\","+ "\"body\":\"" + body + "\","+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");//请求String result = alipayClient.pageExecute(alipayRequest).getBody();return result;}//异步通知地址@RequestMapping("/alipay/notifyUrl")public String notifyUrl(){return "alipay/notify";}//回调地址@RequestMapping("/alipay/returnUrl")public String returnUrl(){return "alipay/return";}
前端html ajax 请求
// 发送 AJAX 请求$.ajax({url: '/alipay/pay', // 后端接收数据的接口地址type: 'POST', // 请求类型,通常是 POSTcontentType: 'application/json', // 告诉后端你发送的是 JSON 格式的数据data: JSON.stringify({ // 将 JavaScript 对象转换为 JSON 字符串donaId: 1008111,donaMoney: 100.00,}),success: function(response) {// 请求成功时的回调函数,沙箱返回html代码,这里要跳转新页面const div = document.createElement('divform');div.innerHTML = response;document.body.appendChild(div);document.forms['punchout_form'].setAttribute('target', '_blank')document.forms['punchout_form'].submit()},error: function(xhr, status, error) {// 请求失败时的回调函数console.error('Error:', error);}});
ajax发送请求,页面跳转
输入沙箱买家测试号
支付成功,等待几秒,跳转自己的回调页面