本文實例為大家分享了java使用支付寶掃碼支付的具體代碼,供大家參考,具體內容如下
準備工作
首先開通支付寶沙箱的測試賬號,里面會有消費者賬戶和收款方賬戶
手機掃碼下載手機端app
基礎配置
所需jar包
alipayconfig
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package com.alipay.config; import java.io.filewriter; import java.io.ioexception; import java.util.resourcebundle; /* * *類名:alipayconfig *功能:基礎配置類 *詳細:設置帳戶有關信息及返回路徑 *修改日期:2017-04-05 *說明: *以下代碼只是為了方便商戶測試而提供的樣例代碼,商戶可以根據自己網站的需要,按照技術文檔編寫,并非一定要使用該代碼。 *該代碼僅供學習和研究支付寶接口使用,只是提供一個參考。 */ public class alipayconfig { //↓↓↓↓↓↓↓↓↓↓請在這里配置您的基本信息 // 應用id,您的appid,收款賬號既是您的appid對應支付寶賬號 public static string app_id = "2016080403162340"; // 商戶私鑰,您的pkcs8格式rsa2私鑰 public static string merchant_private_key = "miievaid2tulssmawg5+f4nzbexpnxi8nkqjpzeeaa=="; // 支付寶公鑰,查看地址:https://openhome.alipay.com/platform/keymanage.htm 對應appid下的支付寶公鑰。 public static string alipay_public_key = "miibijt26tltkar8s1erdwi25vibcmz7plmxvvumhf5tdbwfbmhus3qidaqab"; // 服務器異步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數,必須外網可以正常訪問 public static string notify_url = "http://localhost:8080/alipay.trade.page.pay-java-utf-8/notify_url.jsp"; // 頁面跳轉同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數,必須外網可以正常訪問 public static string return_url = "http://localhost:8080/exam/index/goumai"; // 簽名方式 public static string sign_type = "rsa2"; // 字符編碼格式 public static string charset = "utf-8"; // 支付寶網關 public static string gatewayurl = "https://openapi.alipaydev.com/gateway.do"; // 支付寶網關 public static string log_path = "e:\\"; //↑↑↑↑↑↑↑↑↑↑請在這里配置您的基本信息 /** * 寫日志,方便測試(看網站需求,也可以改成把記錄存入數據庫) * @param sword 要寫入日志里的文本內容 */ public static void logresult(string sword ) { filewriter writer = null ; try { writer = new filewriter(log_path + "alipay_log_" + system.currenttimemillis()+ ".txt" ); writer.write(sword); } catch (exception e) { e.printstacktrace(); } finally { if (writer != null ) { try { writer.close(); } catch (ioexception e) { e.printstacktrace(); } } } } } |
controller
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
|
//生成有二維碼,可供掃碼支付的頁面 @requestmapping (value = "alipay" ) public string alipay(httpservletresponse response,modelmap map,string chapterid,httpservletrequest request, string widout_trade_no,string widtotal_amount,string widsubject,string widbody) throws ioexception, alipayapiexception{ // string a,string urlname,string couname....+"&a="+a+"&urlname="+urlname+"&couname="+couname //獲得初始化的alipayclient alipayclient alipayclient = new defaultalipayclient(alipayconfig.gatewayurl, alipayconfig.app_id, alipayconfig.merchant_private_key, "json" , alipayconfig.charset, alipayconfig.alipay_public_key, alipayconfig.sign_type); //設置請求參數 alipaytradepagepayrequest alipayrequest = new alipaytradepagepayrequest(); alipayrequest.setreturnurl(alipayconfig.return_url+ "?chapterid=" +chapterid); alipayrequest.setnotifyurl(alipayconfig.notify_url); //付款id,必填 string out_trade_no = widout_trade_no; //付款金額,必填 string total_amount = widtotal_amount; total_amount=urldecoder.decode(total_amount, "utf-8" ); //轉碼 //訂單名稱,必填 string subject = widsubject; subject=urldecoder.decode(subject, "utf-8" ); //商品描述,可空 string body = widbody; alipayrequest.setbizcontent( "{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\"," + "\"timeout_express\":\"1m\"," + "\"product_code\":\"fast_instant_trade_pay\"}" ); //請求 string result = alipayclient.pageexecute(alipayrequest).getbody(); response.setcontenttype( "text/html; charset=utf-8" ); printwriter out = response.getwriter(); out.println(result); return null ; } |
支付成功的放回頁面(return_url)
成功后的返回路徑,走controller,詳見alipayconfig中的配置
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
|
//點擊購買,將課程存入購買表中 @requestmapping (value= "goumai" ) @responsebody public modelandview goumai(string chapterid,httpservletrequest req,string a,string urlname,string couname,modelmap map){ modelandview mav = new modelandview(); map<string,string> mapp1 = new hashmap<string,string>(); // sysusertab login_user = sysuserservice.getsysuserbyid(userid); httpsession session = req.getsession(); sysusertab login_user1 = (sysusertab) session.getattribute( "login_user" ); string userid = login_user1.getuserid(); // session.setattribute("login_user", login_user); mapp1.put( "userid" , userid); mapp1.put( "chapterid" , chapterid); int num = sysbuyservice.getbuycount(mapp1); if (num== 0 ){ mapp1.put( "buyid" , uuid.randomuuid().tostring().replace( "-" , "" )); sysbuyservice.insertbuy(mapp1); } //查詢課程內容 // string fanhui = showfh(req,chapterid,urlname,couname,map, a); mav.setviewname( "jsp/pay/paysuccess" ); return mav; } |
支付成功后,頁面跳轉至paysuccess.jsp頁面。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/han_xiaoxue/article/details/78528825