-
导入依赖
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java --> <!-- 导入支付宝的SDK--> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.9.28.ALL</version> </dependency>
-
编写整合成一个类(AlipayTemplate.java)
@ConfigurationProperties(prefix = "alipay") @Component @Data public class AlipayTemplate { //在支付宝创建的应用的id private String app_id = "YOUR-APPLICATION-ID"; // 商户私钥,您的PKCS8格式RSA2私钥 private String merchant_private_key = "YOUR-PRIVIATE-KEY"; // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。 private String alipay_public_key = "YOUR-PUBLIC-KEY"; // 服务器[异步通知]页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 // 支付宝会悄悄的给我们发送一个请求,告诉我们支付成功的信息 private String notify_url; // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 //同步通知,支付成功,一般跳转到成功页 private String return_url = "YOUR-URL"; // 签名方式 private String sign_type = "RSA2"; // 字符编码格式 private String charset = "utf-8"; // 支付宝网关; https://openapi.alipaydev.com/gateway.do // private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; 可上沙箱环境查看 private String gatewayUrl = "GATEWAY-URL"; public String pay(PayVo vo) throws AlipayApiException { //1、根据支付宝的配置生成一个支付客户端 AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type); //2、创建一个支付请求 //设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(return_url); alipayRequest.setNotifyUrl(notify_url); //商户订单号,商户网站订单系统中唯一订单号,必填 String out_trade_no = vo.getOut_trade_no(); //付款金额,必填 String total_amount = vo.getTotal_amount(); //订单名称,必填 String subject = vo.getSubject(); //商品描述,可空 String body = vo.getBody(); alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," + "\"total_amount\":\""+ total_amount +"\"," + "\"subject\":\""+ subject +"\"," + "\"body\":\""+ body +"\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); String result = alipayClient.pageExecute(alipayRequest).getBody(); //会收到支付宝的响应,响应的是一个页面,只要浏览器显示这个页面,就会自动来到支付宝的收银台页面 System.out.println("支付宝的响应:"+result); return result; } }
-
编写接口,调用整合好的类
@Controller public class PayWebController { @Resource AlipayTemplate alipayTemplate; @ResponseBody @GetMapping(value = "/payOrder",produces = "text/html") public String payOrder(@RequestParam(value = "subject") String subject, @RequestParam(value = "body") String body, @RequestParam(value = "total_amount") String total_amount){ PayVo payVo = new PayVo(); payVo.setTotal_amount(total_amount); payVo.setSubject(subject); payVo.setBody(body); payVo.setOut_trade_no(UUID.randomUUID().toString()); String pay = null; try { pay = alipayTemplate.pay(payVo); } catch (AlipayApiException e) { e.printStackTrace(); } System.out.println("pay"); System.out.println(pay); return pay; } }
此处搭配自己编写的页面输入相关数据进行测试
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style type="text/css"> * { padding: 0px; margin: 0px; } #box { display: flex; align-items: center; justify-content: center; width: 100%; height: 100vh; background-color: blue; } #innerbox { display: flex; align-items: center; justify-content: center; width: 400px; height: 300px; background-color: red; } input { float: right; width: 195px; height: 25px; font-size: 19px; outline: none; border: none; padding-left: 10px; margin-right: 10px; } span { display: inline-block; margin-left: 10px; font-weight: 500; } button { margin-top: 12px; width: 310.9px; height: 30px; } #logtitle { display: inline-block; } form{ height:auto; width: auto; } </style> </head> <body> <div id="box"> <div id="innerbox"> <div style="width: 90px;height: 129px;background-color: yellow;text-align: center;line-height: 129px;"> WELCOME </div> <div style="display: block;"> <form action="/payOrder " method="get"> <div> <span>订单名称:</span><input type="text" name="subject" maxlength="10" autofocus /><br> </div> <div style="margin-top: 10px;"> <span>订单主题:</span><input type="text" name="body" maxlength="20" /><br> </div> <div style="margin-top: 10px;"> <span>订单金额:</span><input type="text" name="total_amount" maxlength="10" /><br> </div> <button type="submit">submit</button> </form> </div> </div> </div> </body> </html>