本文实例为大家分享了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