java支付宝开发-02-手机网站支付
源码已上传github,欢迎专注:https://github.com/shirayner/alipay-wap
一、基础部分
1.手机网站支付产品介绍
1.1 阅读官方介绍: 手机网站支付产品介绍
1.2 看完官方的介绍之后,可以做出如下总结:
通过接入此接口,能让商家接入支付宝的支付功能。具体支付流程如下:
(1)若用户已安装支付宝客户端,则在支付时会唤起支付宝客户端进行支付。
(2)若用户未安装支付宝客户端,则需要在网页中登录支付宝账号进行支付。
2.接入准备
参考:java支付宝开发-01-沙箱环境接入 ,配置好沙箱环境,并下载好沙箱钱包
二、代码实现
1.AlipayConfig
此文件主要存放支付宝的一些配置信息 ,大家需要 登录沙箱环境 ,找到相关配置参数,并据此修改配置文件。
package com.ray.alipaywap.config; /** * @author : shirayner * @date : 2018/3/9 * @time : 9:36 * @desc : 支付宝的相关配置 **/ public class AlipayConfig { //支付宝网关名-正式环境 //public static final String OPEN_API_DOMAIN="https://openapi.alipay.com/gateway.do"; //支付宝网关名-沙箱环境 public static final String OPEN_API_DOMAIN="https://openapi.alipaydev.com/gateway.do"; /** 一、应用相关 * */ /** * 1.应用ID */ public static final String APP_ID="2016091100487746"; /** * 2.应用私钥 */ public static final String APP_PRIVATE_KEY="应用私钥"; /** * 3.应用公钥 */ public static final String APP_PUBLIC_KEY="应用公钥"; /** * 4.支付宝公钥 */ public static final String ALIPAY_PUBLIC_KEY="支付宝公钥"; /** * 二、商家相关 */ /** * 1.app域名 * 需要改成自己应用的地址 */ public static final String APP_DOMAIN = "http://rayge.nat300.top"; /** * 2.回跳url * 即付款之后跳回到此页面。注:沙箱环境下,有bug,不会跳到此页面。正式环境下正常。 */ public static final String RETURN_URL = APP_DOMAIN + "/return_url.jsp"; /** * 3.通知url */ public static final String NOTIFY_URL = APP_DOMAIN + "/notify_url.jsp"; /** * 三、此部分不用修改 */ /** * 5.app_auth_token在ISV代替商家调用接口时使用,商家自己调用,可传null。 */ public static final String APP_AUTH_TOKEN=""; public static final String SIGN_TYPE = "RSA2"; public static final String CHAR_SET = "UTF-8"; public static final String FORMAT = "json"; }
View Code
2.service
2.1 接口——WapPayService
package com.ray.alipaywap.service; import com.alipay.api.response.AlipayTradeWapPayResponse; /** * @author : shirayner * @date : 2018/3/12 * @time : 10:48 * @desc : **/ public interface WapPayService { public AlipayTradeWapPayResponse pay(String content) throws Exception; }
View Code
2.2 实现类——WapPayServiceImpl
package com.ray.alipaywap.service.impl; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradeWapPayRequest; import com.alipay.api.response.AlipayTradeWapPayResponse; import com.ray.alipaywap.config.AlipayConfig; import com.ray.alipaywap.service.WapPayService; /** * @author : shirayner * @date : 2018/3/12 * @time : 11:02 * @desc : **/ public class WapPayServiceImpl implements WapPayService{ // 初始化alipayClient对象 public static AlipayClient alipayClient = new DefaultAlipayClient( AlipayConfig.OPEN_API_DOMAIN, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHAR_SET, AlipayConfig.ALIPAY_PUBLIC_KEY); /** * 手机网站支付 * @param content 订单信息 * @return * @throws AlipayApiException */ @Override public AlipayTradeWapPayResponse pay(String content) throws AlipayApiException { //1. 创建API对应的request AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest(); //2. 在公共参数中设置回跳和通知地址(应用提供给支付宝的请求路径),沙箱模式中不起作用(不知道是不是这个原因,支付宝技术客服告诉我正式上线后就没问题) alipayRequest.setReturnUrl(AlipayConfig.APP_DOMAIN+"/index.jsp"); alipayRequest.setNotifyUrl(AlipayConfig.NOTIFY_URL); // 填充业务参数 alipayRequest.setBizContent(content); //3.执行请求 AlipayTradeWapPayResponse alipayResponse = alipayClient .pageExecute(alipayRequest); return alipayResponse; } }
View Code
3. servlet-WapPayServlet
接收前台订单信息,并调用支付宝接口,来唤起支付宝app来进行支付。
package com.ray.alipaywap.controller; import com.alipay.api.response.AlipayTradeWapPayResponse; import com.ray.alipaywap.config.AlipayConfig; import com.ray.alipaywap.service.WapPayService; import com.ray.alipaywap.service.impl.WapPayServiceImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class WapPayServlet extends javax.servlet.http.HttpServlet { private WapPayService wapPayService; @Override public void init() throws ServletException { super.init(); wapPayService = new WapPayServiceImpl(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException { //1.设置将发送到客户端的响应的内容类型 resp.setContentType("text/html;charset="+ AlipayConfig.CHAR_SET); //1.1 String subject = request.getParameter("subject"); String total_amount = request.getParameter("total_amount"); String out_trade_no = String.valueOf(System.currentTimeMillis()); //1.2 //2.填充业务参数 String content="{" + " \"out_trade_no\":\""+out_trade_no+"\"," + " \"total_amount\":\""+total_amount+"\"," + " \"subject\":\""+subject+"\"," + " \"product_code\":\"QUICK_WAP_PAY\"" + " }"; //3.去支付 AlipayTradeWapPayResponse alipayResponse= null; try { alipayResponse = wapPayService.pay(content); } catch (Exception e) { e.printStackTrace(); } String form= alipayResponse.getBody(); System.out.println(form); //4.输出支付宝返回的表单页面 resp.setContentType("text/html;charset=" + AlipayConfig.CHAR_SET); resp.getWriter().write(form);//直接将完整的表单html输出到页面 resp.getWriter().flush(); resp.getWriter().close(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
View Code
4.前台JSP页面
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h2>订单详情</h2> </body> <form action="/wapPayServlet" method="post" > 商品: <input id="subject" type="text" name="subject" value="IphoneX 128G"/> <br><br> 总价: <input id="total_amount" type="text" name="total_amount" value="99.88"/> <br><br> <input type="submit" value="提交订单" /> <br><br> </form> </html>
View Code
5.web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>wapPayServlet</servlet-name> <servlet-class>com.ray.alipaywap.controller.WapPayServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>wapPayServlet</servlet-name> <url-pattern>/wapPayServlet</url-pattern> </servlet-mapping> </web-app>
View Code
三、参考资料