之前测试一直显示系统繁忙,问了一下才旧版沙箱已经停止维护了,要登录新版沙箱沙箱支付
appId,gateway是等会在项目中会用到的,**选择自定义**,点击设置并查看。
可以发现生成了一个应用公钥,一个应用私钥,这两个记住他们的位置,等会都会用到。
注意!!!我们后面真正要用到的是这个支付宝公钥,应用公钥只是为了生成支付宝公钥,千万别搞错了。
将刚刚的应用公钥粘贴下来,自动生成支付宝公钥。
目前为止,appId,gateway,**,publicKey都已经有了,这四个是接下来必须的配置,我已经都标注了。
com.alipay.sdk
alipay-sdk-java
4.16.2.ALL
前面四个属性按照自己的配就可以
returnUrl指的是支付成功之后同步回调地址,这个不是必须的。
notifyUrl指的是异步回调地址,在支付成功之后,支付宝会给我们异步发送本次交易的详情。注意!!!这个地址必须是公网地址,因为是在本机上测试的,所以用了内网穿透,后面再说。
alipay.appId=9021000132618445
alipay.privateKey=MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCyg5po29pFfrFLiz1uHipN/EZSDbGHtvHn8kG1eTn5ypbEXLEaEu+fS2o1JWrVa68Lx8QKl8BlgBOfQONkQ73Z5hB9fCZQZWsbf+OUb+EO/S12G2Gzia9xfp2YtKm747ORCsYmreaH6/IlKTm0uVXQHObMz4MmO1Auk0ixBUgMx7ellM4u0vxmP19q1CtHuIQowdVYkp+kRoxZTIeqKihY84iAQlW7mxV0XN2nNfoIUXV+8AJt497azlOpZiLK7GPGDDuNUn1TZRB9pOHqHze9OpzQ2JriqrMAX+cioNk+r4z+HAA9+uKJ2nuFaZsqYLOx6LJ3CCxijpIlQVVAOPClAgMBAAECggEAPHCcXfZZ73+WrZomIVtrPV41vOBQBEAnpT17i+LQg0dZRoCPoeFZahiRjA7wHfRInvdy45c/l94GRrwkgTUCWX0whhv7qOq9meDx/7blfzqvIrbgnHX4hbe1yhcZBLlyeaApOL1WORzkrjpIBG1fpYeziRKVN+OLIUnSqcS9AT4m5U1mogsf0A/V3eVs2yCPdNT6w5cT99/2BLgBINW72iD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trDo4dDl66ud5v1qNSyj5/iLxLAw6oecDohGu9NSvttySTihLnuClKQmGePSVQmprVjL0erziF8qZP/ZzcbRyocNLGlyQWJo7CEPBbscYy0tENtBgv9mxi+9un0f5u9JIe6zf6FRX34+7tF0BBq5fVW//uuupSqffn30zX4RSaY/JymFwuWjxKZI4t3nO/iTJdu8FIhI+UddmTtsBh1YTe2BQeuFAavASH/WzOjk0d6gTzOFYXFswIrxrFD80FnCVttWfTiKeLWPoarxOGe5LoS2QQKA4+X8Yj3ze+tw0z4GB1lf+r+2oMy4Ad4F5J82C3572Vx7wIDAQAB
alipay.gateway=https://openapi-sandbox.dl.alipaydev.com/gateway.do
alipay.returnUrl=http://vfqahy.natappfree.cc/returnUrl
alipay.notifyUrl=http://vfqahy.natappfree.cc/returnUrl
@Data
@Component
@PropertySource("classpath:alipay.properties")
public class AlipayProperties {
/*
appId
*/
@Value("${alipay.appId}")
private String appId;
/*
应用私钥
*/
@Value("${alipay.privateKey}")
private String privateKey;
/*
支付宝公钥
*/
@Value("${alipay.publicKey}")
private String publicKey;
/*
网关
*/
@Value("${alipay.gateway}")
private String gateway;
/*
支付宝前端跳转 不需要是公网!!!
*/
@Value("${alipay.returnUrl}")
private String returnUrl;
/*
异步通知 公网!!!
*/
@Value("${alipay.notifyUrl}")
private String notifyUrl;
}
支付宝要求必须有一个订单号,所以我们就给他生成一个订单号orderNumber.
sendRequestToAlipay方法设置参数,向支付宝发送请求,里面这些参数约束是支付宝规定好的。
sendRequestToAlipay中的这个pageExecute是向支付宝发请求。
@Controller
public class PayController {
@Autowired
private AlipayProperties properties;
private final String CHARSET = "UTF-8";
private final String FORMAT = "JSON";
private final String SIGN_TYPE = "RSA2";
@ResponseBody
@RequestMapping("/pay/alipay")
public String alipay(@RequestParam("dona_money") float dona_money,
@RequestParam("dona_id") int dona_id) throws AlipayApiException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String timeStr = dateFormat.format(new Date());
String orderNumber = timeStr + new Random().nextInt(1000000);
//向支付宝发送请求
return sendRequestToAlipay(orderNumber,dona_money,"ghjk");
}
private String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException, AlipayApiException {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(properties.getGateway(), properties.getAppId(), properties.getPrivateKey(), FORMAT, CHARSET, properties.getPublicKey(), SIGN_TYPE);
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(properties.getReturnUrl());
alipayRequest.setNotifyUrl(properties.getNotifyUrl());
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;
}
}
为了方便,我直接在地址栏请求这个接口,如果正常,显示的页面如下,接着正常填写即可。
如果出现异常,很大概率是配置文件配置的不对,那就再检查一遍。
【可以看到那个页面,这个好像属于违规因为有二维码,发不出来】
在支付成功之后,会自动回调上面配置的alipay.notifyUrl那个地址。获得订单详情等等.....
这是地址必须是公网地址,不然打不过来。natapp内网穿透配置
@PostMapping("/notifyUrl")
public String payNotify(HttpServletRequest request) throws Exception {
if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
System.out.println("=========支付宝异步回调========");
Map params = new HashMap<>();
Map requestParams = request.getParameterMap();
for (String name : requestParams.keySet()) {
params.put(name, request.getParameter(name));
System.out.println(name + " = " + request.getParameter(name));
}
String tradeNo = params.get("out_trade_no");
String gmtPayment = params.get("gmt_payment");
String alipayTradeNo = params.get("trade_no");
String sign = params.get("sign");
String content = AlipaySignature.getSignCheckContentV1(params);
boolean checkSignature = AlipaySignature.rsa256CheckContent(content, sign, aliPayConfig.getAlipayPublicKey(), "UTF-8"); // 验证签名
// 支付宝验签
if (checkSignature) {
System.out.println("交易名称: " + params.get("subject"));
System.out.println("交易状态: " + params.get("trade_status"));
System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
System.out.println("商户订单号: " + params.get("out_trade_no"));
System.out.println("交易金额: " + params.get("total_amount"));
//自己的业务
}
}
return "success";
}