1、引入所需依赖
1. <!--支付依赖--> 2. <dependency> 3. <groupId>com.alipay.sdk</groupId> 4. <artifactId>alipay-sdk-java</artifactId> 5. <version>4.22.113.ALL</version> 6. </dependency>
2、登录支付宝开放平台,进入个人沙箱环境,这些信息需要填写在下面的配置类中。
3、查看密钥信息
4、编写一个工具类,相关信息在个人沙箱环境里面都有,自行修改(相关密钥可以点击系统默认密钥进行查看)
1. public class AlipayConfig { 2. /** 3. * 支付宝网关地址 4. */ 5. public static String serverUrl = "https://openapi.alipaydev.com/gateway.do"; 6. /** 7. * appId 8. */ 9. public static String appId = "2021000119697439"; 10. /** 11. * 应用私钥 后期替换成自己的私钥 12. */ 13. public static String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC3PgI3lAW40dRwevBzwxdUkKGhjVIX9K4zuUmCeF5PhWo11LGTmhjrTpBEq/BiBa/v1yZl+3/3/eU14aGxwE86qMnUEiweNVIclp9Gh2o85RiB2EGXyAi5wvTdwziZ50P2s/5VFzyhjjU/Pj6XKg+SShc1gyFsEfttGnMsvP3tm44XsxtRFvTZBqGV90sZ61H9DzRVxHf4dfhnRLJvRFVYC+2XdGFwP+1+UOhCdjOJla6wkrz9B0kvXB1XdAdJHuXTvisTRgBDnc8lY2tyA9RIfAceqHFHub1x0Q0V2HDgYcd2EllGCDP/MLeLAKbGC4H+bh6w57h3NkhbhA/ER8adAgMBAAECggEAGpWgdLfA7MNkcr0jTWgsZlRlXq7GDjutWkuzYn5cEGVmt1CYQrXEj77iJ1Ral54DnGQ2ae1G74+iRWLq+4qpXV8Vj3xk4Ujo6q6Styr1l0DsozyeJ3UlWVcNSybGMXCDoYyxey7KgtZg9kVf5PVi0JypqKgjNfz0YLU6kdIUouYbmz7HPBRQSV3mLclAUiJmbiVa7g6luV/PyNJFh3c1PITvsXah6gvOmOuARn3ZadHvS546AbLYmerVI0c3/Y7HOW5XyLnR+EzzqA+PXtXAV/RV67ywhb9KSEJBevsbPe83ZQkzkoXhKZFNhQb5QdmDXmNA6cRZ1hW6ohuhcQKBgQDlwzuAA3MHpQ3MLfyuUUD1o2fvQ9hymURm4BGzHrPlSz+3yWIuNyUKCfbtslqdGeHPXTlOaKtBiS2wrVbv/JaNA/Elzl+BZgkNCcb3tnsha7e522syHdgapdPrXyGSDN69mIZsWoRJQlGpRISJnd75XClZf3nGlr4g4hV9+dS3rwKBgQDMKtJUSxlb8rf9NZ/xOmklcLxBgFGo39noVS+FDz5O1P1o3I1LSHF+vOTkkqLTO4TrEtr2q0Ijb/xj65wX42i0Av2t63fbMEAkYTatih8Pve9LtX1G6nv2Fw9U/ZDOUKRdhPO2GDG59SifXEgDI4eV29AZhh4oqwRH7wz5neitcwKBgQCg2SR2YfPNjkcygZ87ibaoQFUqs5x73cB9JLA/bWcTRrK8v0b0g7ZyaLdtgEbfZJcozGKp+ayqnJZX8YHDUFk1L2VKykciF4SOlCGumRlqNDbS7kEFPM9lGGJbjKW70Ta+FGFGybeCceMpVMZUcSj/+P0Zopxl4nIIIz5YiuXYEQKBgDmC8n6WLQ2OoK9uFKDiIE5QpGcbG2WjsQFWRa2IrreMHdoVDqQfQwE9eoi8matA/98sD4wej+HloMYQz4aXI55ZnVYJl1iiDd9xlvJpjvhjO24rMJ/yW3rl4gbB6rGDW9mQIwqvZAqK+adyWkKwFnhrVAJsmkBAdFNDDneeOLXrAoGBAOEsOJ76yaxY3RGrvWnGm7aA/tyVLNtpz3Hj0T/zTN24bsKm5TwuobVq5jVvQ2UagBQRlGAl3mP6XiVv0NclDPRupSCOjhkbLlMw+B6rE5uV7HkxtrtHsSFaruehRI94K6tbGNQJdImqWjHKBIjqoHr8snLk41vXxxwyK2G38puD"; 14. /** 15. * 发送数据的格式 目前只能为json 16. */ 17. public static String format = "json"; 18. /** 19. * 设置字符集编码 目前只能为utf-8 20. */ 21. public static String charset = "utf-8"; 22. /** 23. * 支付宝公钥 后期替换成自己的支付宝公钥 24. */ 25. public static String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh2yYUlCpO6tNqNa+svqMJfnl1Ax9LrOAf8bNxexVPIJh6LF77ca4qVPpuYTbvQNuGEMDSP35uZ3g+Vch3WasIaYy/D36NfOVjPzrB2b3cXKbUSEjgRawnx15oyBj687tOKEJcPO9okGUgYr2/HkVQPVU0IpFZVRfcRk8BbadS7AOz38RtRYteY1bu+YVFWqf0O0WTILHAZeDpLqENhgHjmj1ATKdCPel0K2US/uCSe6rPJgclFHplEBKI9ysfTKfhEKNhj56w6CrG7XQ1MQHZI1V7rR2busYRntoHh/DB6CZSiv/X5L8+TxZvAtUCn+z6Ns+8e3LfG1/bq59OU0wIDAQAB"; 26. /** 27. * 支付宝签名 目前是 RSA2 28. */ 29. public static String signType = "RSA2"; 30. public static AlipayClient getAlipayClient() { 31. // 获得初始化的AlipayClient 32. return new DefaultAlipayClient(AlipayConfig.serverUrl, AlipayConfig.appId, 33. AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.publicKey, 34. AlipayConfig.signType); 35. } 36. }
4、编写controller,模拟请求接口
1. package com.lili.controller; 2. 3. import com.alipay.api.AlipayClient; 4. import com.alipay.api.domain.AlipayTradePayModel; 5. import com.alipay.api.request.AlipayTradePagePayRequest; 6. import com.lili.util.AlipayConfig; 7. import org.springframework.web.bind.annotation.PathVariable; 8. import org.springframework.web.bind.annotation.RequestMapping; 9. import org.springframework.web.bind.annotation.RequestParam; 10. import org.springframework.web.bind.annotation.RestController; 11. 12. import javax.servlet.http.HttpServletResponse; 13. import java.io.IOException; 14. 15. /** 16. * @author QiJingJing 17. * @since 2022/12/31 18. */ 19. @RestController 20. @RequestMapping("alipay") 21. public class PayController { 22. /** 23. * 支付请求接口 24. */ 25. @RequestMapping("pay/{orderId}/{cost}") // 订单id(不能重复)和消费金额 26. public void pay(@PathVariable String orderId,@PathVariable String cost, HttpServletResponse response){ 27. try { 28. AlipayClient alipayClient = AlipayConfig.getAlipayClient(); 29. // 设置请求参数 30. AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); 31. AlipayTradePayModel model = new AlipayTradePayModel(); 32. // 设定订单号 必须要写,且订单号不能重复,目前已经只是做测试,已经写死 33. model.setOutTradeNo(orderId); 34. // 设置订单金额 35. model.setTotalAmount(cost); 36. // 订单名字 37. model.setSubject("书籍订单"); 38. // 订单描述 39. model.setBody("这是一本书籍的消费订单"); 40. // 产品码 41. model.setProductCode("FAST_INSTANT_TRADE_PAY"); 42. // 设置参数 43. alipayRequest.setBizModel(model); 44. // 设置回调地址 45. String url = "http://localhost:8080/alipay/success?orderId="+orderId+""; 46. System.out.println(url); 47. alipayRequest.setReturnUrl(url); 48. String result = alipayClient.pageExecute(alipayRequest).getBody(); 49. response.setContentType("text/html;charset=utf-8"); 50. response.getWriter().println(result); 51. } catch (Exception e) { 52. e.printStackTrace(); 53. } 54. } 55. /** 56. * 支付成功后,回调页面 57. */ 58. @RequestMapping("success") 59. public void success(@RequestParam("orderId") String orderId, HttpServletResponse response) throws IOException { 60. response.setContentType("text/html;charset=utf-8"); 61. response.getWriter().println("订单Id为"+orderId+"的订单已经成功支付"); 62. } 63. }
5、浏览器访问http://localhost:8080/alipay/pay/23232/509
订单号为23232,金额为509元,回车后可以看到出现以下页面
点击订单详情可以看到我们订单的信息:
6、我们需要登录买家账号进行登录即可
7、登录完成后,会出现以下页面(余额不够的话,可以在上面页面进行充值即可)
8、支付完成后,即可成功跳入我们设置的回调地址