com.funtl.framework.alipay.trade.util.AlipaySubmit.java Source code

Java tutorial

Introduction

Here is the source code for com.funtl.framework.alipay.trade.util.AlipaySubmit.java

Source

/*
 * Copyright 2015-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.funtl.framework.alipay.trade.util;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.funtl.framework.alipay.trade.common.AlipayConfig;
import com.funtl.framework.alipay.trade.common.MD5;
import com.funtl.framework.alipay.trade.pay.PayManager;
import com.funtl.framework.alipay.trade.util.httpClient.HttpProtocolHandler;
import com.funtl.framework.alipay.trade.util.httpClient.HttpRequest;
import com.funtl.framework.alipay.trade.util.httpClient.HttpResponse;
import com.funtl.framework.alipay.trade.util.httpClient.HttpResultType;

import org.apache.commons.httpclient.NameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/* *
 *??AlipaySubmit
 *?????
 *????HTML?HTTP?
 *3.3
 *2012-08-13
 *
 *????????,???
 *???????
 */

public class AlipaySubmit {

    /**
     * ???
     *
     * @param sPara ???
     * @return ??
     */
    public static String buildRequestMysign(Map<String, String> sPara) {
        String prestr = AlipayCore.createLinkString(sPara); //?=???&??
        String mysign = "";
        if (AlipayConfig.sign_type.equals("MD5")) {
            mysign = MD5.sign(prestr, AlipayConfig.key, AlipayConfig.input_charset);
        }
        return mysign;
    }

    /**
     * ????
     *
     * @param sParaTemp ??
     * @return ??
     */
    private static Map<String, String> buildRequestPara(Map<String, String> sParaTemp) {
        //???
        Map<String, String> sPara = AlipayCore.paraFilter(sParaTemp);
        //???
        String mysign = buildRequestMysign(sPara);

        //????????
        sPara.put("sign", mysign);
        sPara.put("sign_type", AlipayConfig.sign_type);

        return sPara;
    }

    /**
     * ?HTML?
     *
     * @param sParaTemp     ?
     * @param strMethod     ????post?get
     * @param strButtonName 
     * @return ???HTML
     */
    public static String buildRequest(Map<String, String> sParaTemp, String strMethod, String strButtonName) {
        //?
        Map<String, String> sPara = buildRequestPara(sParaTemp);
        List<String> keys = new ArrayList<String>(sPara.keySet());

        StringBuffer sbHtml = new StringBuffer();
        sbHtml.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" + "<html>\n"
                + "<head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
                + "<title>???</title>\n" + "</head>\n" + "<body>\n");
        sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\""
                + PayManager.HTTPS_MAPI_ALIPAY_COM_GATEWAY_DO + "?" + "_input_charset=" + AlipayConfig.input_charset
                + "\" method=\"" + strMethod + "\">");

        for (int i = 0; i < keys.size(); i++) {
            String name = (String) keys.get(i);
            String value = (String) sPara.get(name);

            sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
        }

        //submit???name
        sbHtml.append("<input type=\"submit\" value=\"" + strButtonName + "\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms['alipaysubmit'].submit();</script>");
        sbHtml.append("</body>\n" + "</html>");
        return sbHtml.toString();
    }

    /**
     * ?HTML?
     *
     * @param sParaTemp       ?
     * @param strMethod       ????post?get
     * @param strButtonName   
     * @param strParaFileName ???
     * @return ???HTML
     */
    public static String buildRequest(Map<String, String> sParaTemp, String strMethod, String strButtonName,
            String strParaFileName) {
        //?
        Map<String, String> sPara = buildRequestPara(sParaTemp);
        List<String> keys = new ArrayList<String>(sPara.keySet());

        StringBuffer sbHtml = new StringBuffer();

        sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\"  enctype=\"multipart/form-data\" action=\""
                + PayManager.HTTPS_MAPI_ALIPAY_COM_GATEWAY_DO + "?" + "_input_charset=" + AlipayConfig.input_charset
                + "\" method=\"" + strMethod + "\">");

        for (int i = 0; i < keys.size(); i++) {
            String name = (String) keys.get(i);
            String value = (String) sPara.get(name);

            sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
        }

        sbHtml.append("<input type=\"file\" name=\"" + strParaFileName + "\" />");

        //submit???name
        sbHtml.append("<input type=\"submit\" value=\"" + strButtonName + "\" style=\"display:none;\"></form>");

        return sbHtml.toString();
    }

    /**
     * HTTPPOST????
     * ??strParaFileNamestrFilePath
     * buildRequest("", "",sParaTemp)
     *
     * @param strParaFileName ???
     * @param strFilePath     
     * @param sParaTemp       ?
     * @return ??
     * @throws Exception
     */
    public static String buildRequest(String strParaFileName, String strFilePath, Map<String, String> sParaTemp)
            throws Exception {
        //?
        Map<String, String> sPara = buildRequestPara(sParaTemp);

        HttpProtocolHandler httpProtocolHandler = HttpProtocolHandler.getInstance();

        HttpRequest request = new HttpRequest(HttpResultType.BYTES);
        //?
        request.setCharset(AlipayConfig.input_charset);

        request.setParameters(generatNameValuePair(sPara));
        request.setUrl(
                PayManager.HTTPS_MAPI_ALIPAY_COM_GATEWAY_DO + "?" + "_input_charset=" + AlipayConfig.input_charset);

        HttpResponse response = httpProtocolHandler.execute(request, strParaFileName, strFilePath);
        if (response == null) {
            return null;
        }

        String strResult = response.getStringResult();

        return strResult;
    }

    /**
     * MAP??NameValuePair
     *
     * @param properties MAP
     * @return NameValuePair
     */
    private static NameValuePair[] generatNameValuePair(Map<String, String> properties) {
        NameValuePair[] nameValuePair = new NameValuePair[properties.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            nameValuePair[i++] = new NameValuePair(entry.getKey(), entry.getValue());
        }

        return nameValuePair;
    }

    /**
     * ?query_timestamp???
     * ??XML???SSL?
     *
     * @return 
     * @throws IOException
     * @throws DocumentException
     * @throws MalformedURLException
     */
    public static String query_timestamp() throws MalformedURLException, DocumentException, IOException {

        //query_timestamp?URL
        String strUrl = PayManager.HTTPS_MAPI_ALIPAY_COM_GATEWAY_DO + "?" + "service=query_timestamp&partner="
                + AlipayConfig.partner + "&_input_charset" + AlipayConfig.input_charset;
        StringBuffer result = new StringBuffer();

        SAXReader reader = new SAXReader();
        Document doc = reader.read(new URL(strUrl).openStream());

        List<Node> nodeList = doc.selectNodes("//alipay/*");

        for (Node node : nodeList) {
            // ?????
            if (node.getName().equals("is_success") && node.getText().equals("T")) {
                // ??
                List<Node> nodeList1 = doc.selectNodes("//response/timestamp/*");
                for (Node node1 : nodeList1) {
                    result.append(node1.getText());
                }
            }
        }

        return result.toString();
    }
}