SpringBoot 支付宝付款接口类、支付异步回调函数模板
1.付款接口类
1.1.引入Maven依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.38.221.ALL</version>
</dependency>
1.2.将下面代码保存为AlipayTemplate.java
@Configuration
@ConfigurationProperties("alipay")
@Data
public class AlipayTemplate {
private String notifyUrl="";
private String returnUrl="";
private String serverUrl;//网关地址
private String appId;//应用ID
private String privateKey;//私钥
private String format="json";
private String charset="UTF-8";
private String alipayPublicKey;//公钥
private String signType="RSA2";
public String pay(String outTradeNo, BigDecimal totalAmount,String subject) throws AlipayApiException {
AlipayClient alipayClient = new DefaultAlipayClient(
serverUrl,//网关地址
appId,//应用ID
privateKey,//私钥
format,
charset,
alipayPublicKey,//公钥
signType//加密算法
);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//异步接收地址,仅支持http/https,公网可访问
request.setNotifyUrl(notifyUrl);
//同步跳转地址,仅支持http/https
request.setReturnUrl(returnUrl);
/******必传参数******/
JSONObject bizContent = new JSONObject();
//商户订单号,商家自定义,保持唯一性
bizContent.put("out_trade_no", outTradeNo);
//支付金额,最小值0.01元,保留两位小数向上取整
bizContent.put("total_amount", totalAmount.setScale(2, RoundingMode.UP));
//订单标题,不可使用特殊符号
bizContent.put("subject", subject);
//电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
/******可选参数******/
/**
* time_expire可选string(32)
* 【描述】订单绝对超时时间。
* 格式为yyyy-MM-dd HH:mm:ss。超时时间范围:1m~15d。
* 注:time_expire和timeout_express两者只需传入一个或者都不传,两者均传入时,优先使用time_expire。
* 【示例值】2016-12-31 10:05:01
*/
//bizContent.put("time_expire", LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plusMinutes(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//// 商品明细信息,按需传入
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);
//// 扩展信息,按需传入
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);
request.setBizContent(bizContent.toString());
AlipayTradePagePayResponse response = alipayClient.pageExecute(request,"POST");
// 如果需要返回GET请求,请使用
// AlipayTradePagePayResponse response = alipayClient.pageExecute(request,"GET");
String pageRedirectionData = response.getBody();
// System.out.println(pageRedirectionData);
return pageRedirectionData;
/*if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}*/
}
}
1.3.支付调用范例
@RequiredArgsConstructor
@Controller
public class PayWebController {
private final AlipayTemplate alipayTemplate;//依赖注入
@ResponseBody
@GetMapping(value = "/pay",produces = MediaType.TEXT_HTML_VALUE)
public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {//传入orderSn订单号
BigDecimal totalAmount = new BigDecimal(100);//支付金额
String subject = "支付测试";//支付标题
return alipayTemplate.pay(orderSn,totalAmount,subject);
}
}
2. 支付异步回调函数模板
@RequiredArgsConstructor
@Controller
public class payedListener {
private final AlipayTemplate alipayTemplate;
@ResponseBody
@PostMapping("/payed/notify")
public String handleAlipayed(@RequestParam Map<String,String> paramsMap) throws AlipayApiException {
try {
boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, alipayTemplate.getAlipayPublicKey(), alipayTemplate.getCharset(), alipayTemplate.getSignType()); //调用SDK验证签名
if(signVerified){
//验签成功,执行正常业务代码
return "success";
}else{
//验签失败
}
}catch (Exception ignored){
}
return "failure";
}
}