sdk-alipay
spring boot下支付寶的開箱既用環境
使用場景
spring boot應用中需要接入支付寶
開始使用
pom.xml中引入依賴
1
2
3
4
5
|
<dependency> <groupId>net.guerlab</groupId> <artifactId>sdk-alipay-starter</artifactId> <version> 1.0 . 3 </version> </dependency> |
bootstrap.yml中增加配置
1
2
3
4
5
6
7
|
sdk: alipay: dev: true / false #默認 false ,為 true 表示使用沙箱環境 sign-type: RSA2 #簽名算法 app-id: #應用ID private -key: #應用私鑰 alipay- public -key: #支付寶公鑰 |
增加控制器實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.alibaba.fastjson.JSONObject; import com.alipay.api.AlipayClient; import com.alipay.api.request.AlipayTradeAppPayRequest; import com.alipay.api.request.AlipayTradePagePayRequest; import com.alipay.api.request.AlipayTradeWapPayRequest; import net.guerlab.sdk.alipay.controller.AlipayAbstractController; @RequestMapping ( "/pay/alipay" ) public class AlipayController extends AlipayAbstractController { @Autowired private AlipayClient client; //支付寶請求sdk客戶端 /** * 支付請求 */ @GetMapping ( "/app/{orderId}" ) public String app( @PathVariable Long orderId, HttpServletResponse httpResponse) { JSONObject data = new JSONObject(); data.put( "out_trade_no" , "201701010000001234" ); //商戶訂單號 data.put( "product_code" , "QUICK_MSECURITY_PAY" ); //產品碼, APP支付 QUICK_MSECURITY_PAY, PC支付 FAST_INSTANT_TRADE_PAY, 移動H5支付 QUICK_WAP_PAY data.put( "total_amount" , "0.01" ); //訂單金額 data.put( "subject" , "測試訂單" ); //訂單標題 //APP支付 AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); //PC支付 //AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); //移動H5支付 //AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.setNotifyUrl( "http://demo/pay/alipay/notify/1" ); //異步通知地址 request.setBizContent(data.toJSONString()); //業務參數 return client.sdkExecute(request).getBody(); } @PostMapping ( "/notify/{orderId}" ) public String notify( @PathVariable Long orderId, HttpServletRequest request) { if (!notify0(request.getParameterMap())) { //這里處理驗簽失敗 } request.getParameter( "trade_no" ); //獲取請求參數中的商戶訂單號 return "success" ; } } |
總結
以上所述是小編給大家介紹的spring boot 下支付寶的開箱既用環境,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.tuicool.com/articles/BfeIJbU