本文为本人原创文章,未经本人允许不得转载。博主含有支付宝扫码支付跟启动支付宝app两种支付java源码,如有需要可以加博主qq:3063715971(15RBM 帮助你实现支付功能)
如今不少项目都需要接入支付功能,比如租房网站,销售网站等,那么我们就得熟悉支付宝的java接口。
首先我们需要在蚂蚁金服开放平台获取,你应用所需要的APPID,秘钥跟公钥,测试用的沙箱账号,下载沙箱支付宝。生产环境下的代码跟测试环境的代码没有什么区别,只是测试环境的支付宝是金额是无限的,生产环境是需要企业号并且支付收费。
其次我们需要配置一个叫AlipayConfig.java的配置文件类,该类含有appid,秘钥跟公钥,还有服务器异步通知页面路径,支付成功的跳转路径。
对应的java代码
@RequestMapping(value="/gethouse",method=RequestMethod.POST)//获取要租的房子
public String getOneHouse(HttpServletRequest request) {
HttpSession session = request.getSession();
//判断是否登录
String id=(String) session.getAttribute("id");
if(StringUtil.isEmpty(id)) return "login";
Calendar now = Calendar.getInstance();
//获取提交信息
String customer_id=request.getParameter("customer_id");
String house_id=request.getParameter("house_id");
String description=request.getParameter("description");
String starttime=request.getParameter("starttime");
System.out.println(customer_id+house_id+description+starttime);
Date d=new Date();
String t=String.valueOf(now.get(Calendar.YEAR))+String.valueOf(now.get(Calendar.MONTH)+1)
+String.valueOf(now.get(Calendar.DAY_OF_MONTH)+1)+String.valueOf(now.get(Calendar.HOUR_OF_DAY))
+String.valueOf(now.get(Calendar.MINUTE))+String.valueOf(now.get(Calendar.SECOND))
;
session.setAttribute("hid", house_id);
session.setAttribute("starttime", starttime);
String WIDout_trade_no=t;//商户订单号 :
System.out.println(WIDout_trade_no);
String WIDsubject=house_id;//订单名称
String WIDtotal_amount="200";//订单价格
String WIDbody="商品描述";//商品描述
request.setAttribute("WIDout_trade_no", WIDout_trade_no);//WIDout_trade_no
request.setAttribute("WIDsubject",WIDsubject);
request.setAttribute("WIDtotal_amount", WIDtotal_amount);
request.setAttribute("WIDbody", WIDbody);
return "pay";
}
对应的jsp代码:pay.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="com.alipay.config.*"%>
<%@ page import="com.alipay.api.*"%>
<%@ page import="com.alipay.api.request.*"%>
<%
//获得初始化的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);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
String out_trade_no =(String) request.getAttribute("WIDout_trade_no");
System.out.println(out_trade_no);
//付款金额,必填
String total_amount =(String) request.getAttribute("WIDtotal_amount");
//订单名称,必填
String subject =(String) request.getAttribute("WIDsubject");
//商品描述,可空
String body = (String) request.getAttribute("WIDbody");
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"body\":\""+ body +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明
//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
// + "\"total_amount\":\""+ total_amount +"\","
// + "\"subject\":\""+ subject +"\","
// + "\"body\":\""+ body +"\","
// + "\"timeout_express\":\"10m\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
//请求
String result = alipayClient.pageExecute(alipayRequest).getBody();
//System.out.println(out_trade_no);
//System.out.println(total_amount);
//输出
out.println(result);
%>