java支付寶開發-02-手機網站支付

源碼已上傳github,歡迎專一:https://github.com/shirayner/alipay-wap

1、基礎部分

1.手機網站支付產品介紹

1.1 閱讀官方介紹: 手機網站支付產品介紹html

1.2 看完官方的介紹以後,能夠作出以下總結:java

經過接入此接口,能讓商家接入支付寶的支付功能。具體支付流程以下:git

(1)若用戶已安裝支付寶客戶端,則在支付時會喚起支付寶客戶端進行支付。github

(2)若用戶未安裝支付寶客戶端,則須要在網頁中登陸支付寶帳號進行支付。web

 

2.接入準備

 參考:java支付寶開發-01-沙箱環境接入  ,配置好沙箱環境,並下載好沙箱錢包json

 

 

2、代碼實現

1.AlipayConfig

此文件主要存放支付寶的一些配置信息 ,你們須要  登陸沙箱環境  ,找到相關配置參數,並據此修改配置文件。api

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、應用相關
     *
     */

    /**
     * 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="支付寶公鑰";



    /**
     * 2、商家相關
     */

    /**
     * 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";


    /**
     * 3、此部分不用修改
     */
    /**
     * 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來進行支付。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.jspjsp

<%@ 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

 

 

3、參考資料

1.SpringBoot_03_依賴本地jaride

相關文章
相關標籤/搜索