支付宝支付,相比较微信支付,封装了提供给开发者使用的接口,开发者可以在蚂蚁金服开放平台先把开发DEMO下载下来
手机网站内支付 下载地址:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.tFuJYY&treeId=54&articleId=106682&docType=1
在下载的DEMO中,可以找到需要调用的支付宝封装的架包
请自行引入到自己的项目中。
-----------------------------------------------------------------------------------------------------------------------1.支付宝 手机网页内 支付开发流程图-------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------2.根据流程图,前台页面----------------------------------------------------------------------------------------------------------------------
用户点击支付页面 userPayView.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>模拟支付页面--微信支付/支付宝支付</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <input type="text" value="" name="openID"/> <!-- 微信 所需OpenID --> <input type="text" value="" name="orderID"/><!-- 微信 支付宝[out_trade_no] --> <input type="number" value="" name="money"/><!-- 微信[分为单位,不允许小数] 支付宝[total_amount 元为单位,精确到小数点后2位] 商品价格 --> <input type="text" value="" name="subject"/><!-- 支付宝 商品的标题/交易标题/订单标题/订单关键字等。 --> <input type="text" value="" name="product_code"/><!-- 支付宝 销售产品码,商家和支付宝签约的产品码。该产品请填写固定值:QUICK_WAP_WAY。 --> <input type="text" value="" name="body"/><!-- 支付宝 商品描述 --> <button class="payButton">微信支付</button> <button class="alipayButton">支付宝支付</button> </body> <script type="text/javascript" src="/resources/bootstrap-3.3.5-dist/js/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="/wx/pay/pay.js"></script> </html>
View Code
点击事件 pay.js
$(document).ready(function(){ /** * ①支付宝支付按钮 */ $(".alipayButton").click(function(){ location.href = "/wx/PayAliOrder/aliPayOrder"; }); });
View Code
异步请求最后到达的页面 notify_url.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>支付宝支付--交易成功后页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> notify_url <br> </body> </html>
View Code
同步请求最后到达的页面 return_url.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>支付宝支付--用户确认支付后页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> return_url <br> </body> </html>
View Code
---------------------------------------------------------------------------------------------------------------------------3.支付宝配置实体 实体类---------------------------------------------------------------------------------------------------------------------
package net.shopxx.wx.payali; /** * 支付宝支付 配置参数实体 * @author SXD * */ public class AlipayConfig { /** * app_id * 支付宝分配给开发者的应用ID * 蚂蚁金服开放平台(open.alipay.com),在开发者中心创建登记你的应用,将获得应用唯一标识(APPID) * 32 */ public static String app_id = ""; /** * method * 接口名称 * 128 */ public static String method = "alipay.trade.wap.pay"; // /** * notify_url * 服务器异步通知页面路径 * 需http://或者https://格式的完整路径, * 不能加?id=123这类自定义参数,必须外网可以正常访问 * * 交易成功后,支付宝post请求notifyUrl(商户入参传入),返回异步通知参数。[商户网关地址] 是你的项目公网访问地址 * 256 */ public static String notify_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp"; /** * return_url * 页面跳转同步通知页面路径 * 需http://或者https://格式的完整路径, * 不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址 * * 用户确认支付后,支付宝get请求returnUrl(商户入参传入),返回同步返回参数 * 256 */ public static String return_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/return_url.jsp"; /** * 请求网关地址 * 用于SDK封装的签名和验签过程中使用 */ public static String URL = "https://openapi.alipay.com/gateway.do"; /** * charset * 请求使用的编码格式,如utf-8,gbk,gb2312等 * 10 */ public static String charset = "UTF-8"; /** * format * 返回格式 * 40 */ public static String format = "json"; /** * sign_type * 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2 */ public static String sign_type = "RSA2"; // 私钥 pkcs8格式的 public static String RSA_PRIVATE_KEY = ""; // 支付宝公钥 public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjrEVFMOSiNJXaRNKicQuQdsREraftDA9Tua3WNZwcpeXeh8Wrt+V9JilLqSa7N7sVqwpvv8zWChgXhX/A96hEg97Oxe6GKUmzaZRNh0cZZ88vpkn5tlgL4mH/dhSr3Ip00kvM4rHq9PwuT4k7z1DpZAf1eghK8Q5BgxL88d0X07m9X96Ijd0yMkXArzD7jg+noqfbztEKoH3kPMRJC2w4ByVdweWUT2PwrlATpZZtYLmtDvUKG/sOkNAIKEMg3Rut1oKWpjyYanzDgS7Cg3awr1KPTl9rHCazk15aNYowmYtVabKwbGVToCAGK+qQ1gT3ELhkGnf3+h53fukNqRH+wIDAQAB"; }
View Code
---------------------------------------------------------------------------------------------------------------------------4.支付宝支付 商户服务器 逻辑处理类-----------------------------------------------------------------------------------------------------
package net.shopxx.wx.payali; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.domain.AlipayTradeWapPayModel; import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.request.AlipayTradeCloseRequest; import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest; import com.alipay.api.request.AlipayTradeQueryRequest; import com.alipay.api.request.AlipayTradeRefundRequest; import com.alipay.api.request.AlipayTradeWapPayRequest; /** * 支付宝支付 逻辑处理页面 * @author SXD * */ @Controller @RequestMapping("/wx/PayAliOrder") public class PayAliOrderController { /** * ②支付宝支付 按钮点击后,找到这个方法,返回from表单 * @param request * @return */ @ResponseBody @RequestMapping("/aliPayOrder") public String aliPayOrder(HttpServletRequest request){ // SDK 公共请求类,包含公共请求参数,以及封装了签名与验签,开发者无需关注签名与验签 //利用AlipayConfig中配置好的公共参数,创建AlipayClient AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.app_id, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.sign_type); AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest(); // 获取请求支付的必填项信息 封装请求支付信息 AlipayTradeWapPayModel model = new AlipayTradeWapPayModel(); //必填 订单ID model.setOutTradeNo(request.getParameter("orderID")); //必填 商品名称 model.setSubject(request.getParameter("subject")); //必填 交易金额 model.setTotalAmount(request.getParameter("money")); //选填 商品描述 model.setBody(request.getParameter("body")); //必填 商家和支付宝签约的产品码 model.setProductCode(request.getParameter("product_code")); //将支付请求的信息集合 传递给支付请求 alipay_request.setBizModel(model); // 设置异步通知地址 alipay_request.setNotifyUrl(AlipayConfig.notify_url); // 设置同步地址 alipay_request.setReturnUrl(AlipayConfig.return_url); // form表单生产 String form = ""; try { /** * ③调用SDK生成表单 */ form = client.pageExecute(alipay_request).getBody(); /** * ④商户服务器将表单返回给网页 * 本示例中并未提供前台页面处理from表单的步骤,请自行完善 */ return form; } catch (AlipayApiException e) { e.printStackTrace(); } return "支付失败"; } /** * ⑤ ⑥设置功能:支付宝服务器同步通知页面 * 用户确认支付后 支付宝get请求的同步地址 * @return * @throws UnsupportedEncodingException * @throws AlipayApiException */ @RequestMapping("/returnURLDeal") public String returnURLDeal(HttpServletRequest request,Model model) throws UnsupportedEncodingException, AlipayApiException{ //获取支付宝GET过来反馈信息 Map<String,String> params = new HashMap<String,String>(); Map<String,String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } //乱码解决,这段代码在出现乱码时使用 valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8"); params.put(name, valueStr); } //调用SDK验证签名 boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.charset,AlipayConfig.sign_type); // TODO 验签成功后,按照支付结果通知中的描述,对支付结果中的业务内容进行二次校验,校验成功后在response中返回success并继续商户自身业务处理,校验失败返回failure if(signVerified) { //商户订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8"); //付款金额 String amount = new String(request.getParameter("amount").getBytes("ISO-8859-1"),"UTF-8"); model.addAttribute("info", "trade_no:"+trade_no+"<br/>out_trade_no:"+out_trade_no+"<br/>amount:"+amount); }else {// TODO 验签失败则记录异常日志,并在response中返回failure. model.addAttribute("info", "验签失败"); } return "wx/pay/return_url.jsp"; } /** * ⑦ ⑧设置功能:支付宝服务器异步通知页面 * 交易成功后,支付宝post请求异步回调地址 * @return * @throws UnsupportedEncodingException * @throws AlipayApiException */ @RequestMapping("/notifyURLDeal") public String notifyURLDeal(HttpServletRequest request,Model model) throws UnsupportedEncodingException, AlipayApiException{ //获取支付宝POST过来反馈信息 Map<String,String> params = new HashMap<String,String>(); Map<String,String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } //乱码解决,这段代码在出现乱码时使用 valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8"); params.put(name, valueStr); } //调用SDK验证签名 boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.charset,AlipayConfig.sign_type); //——请在这里编写您的程序(以下代码仅作参考)—— /* 实际验证过程建议商户务必添加以下校验: 1、需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号, 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额), 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email) 4、验证app_id是否为该商户本身。 */ if(signVerified) {//验证成功 //商户订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8"); //交易状态 String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8"); //交易总金额 String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8"); //收款方ID String seller_id = new String(request.getParameter("seller_id").getBytes("ISO-8859-1"),"UTF-8"); //支付宝给开发者分配的应用ID String app_id = new String(request.getParameter("app_id").getBytes("ISO-8859-1"),"UTF-8"); if(trade_status.equals("TRADE_FINISHED")){ //交易失败 //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知 }else if (trade_status.equals("TRADE_SUCCESS")){ //交易成功 //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //付款完成后,支付宝系统发送该交易状态通知 } model.addAttribute("info","success"); }else {//验证失败 model.addAttribute("info","fail"); //调试用,写文本函数记录程序运行情况是否正常 //String sWord = AlipaySignature.getSignCheckContentV1(params); //AlipayConfig.logResult(sWord); } return "wx/pay/notify_url.jsp"; } /** * ⑨查询交易状态 * @param request */ public void query(HttpServletRequest request) { //获得初始化的AlipayClient AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.app_id, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.sign_type); //设置请求参数 AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest(); try { //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8"); //请二选一设置 alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","+"\"trade_no\":\""+ trade_no +"\"}"); //请求 String result = client.execute(alipayRequest).getBody(); //其中包含了trade_status 交易当前状态 //System.out.println(result); } catch (UnsupportedEncodingException | AlipayApiException e) { e.printStackTrace(); } } /** * 退款 * @param request */ public void Refund(HttpServletRequest request) { //获得初始化的AlipayClient AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.app_id, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.sign_type); //设置请求参数 AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest(); try { //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8"); //请二选一设置 //需要退款的金额,该金额不能大于订单金额,必填 String refund_amount = new String(request.getParameter("amount").getBytes("ISO-8859-1"),"UTF-8"); //退款的原因说明 String refund_reason = new String(request.getParameter("reason").getBytes("ISO-8859-1"),"UTF-8"); //标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传 String out_request_no = new String(request.getParameter("request_no").getBytes("ISO-8859-1"),"UTF-8"); alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," + "\"trade_no\":\""+ trade_no +"\"," + "\"refund_amount\":\""+ refund_amount +"\"," + "\"refund_reason\":\""+ refund_reason +"\"," + "\"out_request_no\":\""+ out_request_no +"\"}"); //请求 String result = client.execute(alipayRequest).getBody(); //输出 //System.out.println(result); } catch (UnsupportedEncodingException | AlipayApiException e) { e.printStackTrace(); } } /** * 退款查询 * @param request */ public void refundQuery(HttpServletRequest request) { //获得初始化的AlipayClient AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.app_id, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.sign_type); //设置请求参数 AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest(); try { //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8"); //请二选一设置 //请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号,必填 String out_request_no = new String(request.getParameter("request_no").getBytes("ISO-8859-1"),"UTF-8"); alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," +"\"trade_no\":\""+ trade_no +"\"," +"\"out_request_no\":\""+ out_request_no +"\"}"); //请求 String result = client.execute(alipayRequest).getBody(); //输出 System.out.println(result); } catch (AlipayApiException | UnsupportedEncodingException e) { e.printStackTrace(); } } //交易关闭 public void close(HttpServletRequest request) { //获得初始化的AlipayClient AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.app_id, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.sign_type); //设置请求参数 AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest(); try { //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8"); //支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8"); //请二选一设置 alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," +"\"trade_no\":\""+ trade_no +"\"}"); //请求 String result = client.execute(alipayRequest).getBody(); //输出 System.out.println(result); } catch (UnsupportedEncodingException | AlipayApiException e) { e.printStackTrace(); } } }
View Code
支付宝手机网站 快速接入流程:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.zbVMYK&treeId=193&articleId=105285&docType=1
支付宝手机网站 异步接收通知:https://doc.open.alipay.com/doc2/detail.htm?treeId=203&articleId=105286&docType=1
支付宝手机网站 接入参数API:https://doc.open.alipay.com/doc2/detail.htm?treeId=203&articleId=105463&docType=1
--------------------------------------------------------------------------------------------------------------------------以上,支付宝 手机网页内支付 待补充,待定----------------------------------------------------------------------------------------------------------------