Java tutorial
/* * Copyright 2016-2017 Shanghai Boyuan IT Services Ltd. * * 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.boyuanitsm.pay.alipay.util; import com.boyuanitsm.pay.alipay.config.AlipayConfig; import com.boyuanitsm.pay.alipay.sign.RSA; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /* * *??AlipaySubmit *????? *????HTML?HTTP? *3.3 *2012-08-13 * *????????,??? *??????? */ public class AlipaySubmit { /** * ????URL() */ private static final String ALIPAY_GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?"; /** * ??? * @param sPara ??? * @return ?? */ public static String buildRequestMysign(Map<String, String> sPara) { String prestr = AlipayCore.createLinkString(sPara); //?=???&?? String mysign = ""; if (AlipayConfig.sign_type.equals("RSA")) { mysign = RSA.sign(prestr, AlipayConfig.private_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("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + ALIPAY_GATEWAY_NEW + "_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>"); return sbHtml.toString(); } /** * ??(create_direct_pay_by_user), ?HTML?POST???? * * @param outTradeNo ?? * @param subject ??? * @param totalFee ? * @param body ??? * @return ???HTML */ public static String buildRequest(String outTradeNo, String subject, String totalFee, String body) { //??MAP Map<String, String> sParaTemp = new HashMap<>(); sParaTemp.put("service", AlipayConfig.create_direct_pay_by_user); sParaTemp.put("partner", AlipayConfig.partner); sParaTemp.put("seller_id", AlipayConfig.seller_id); sParaTemp.put("_input_charset", AlipayConfig.input_charset); sParaTemp.put("payment_type", AlipayConfig.payment_type); sParaTemp.put("notify_url", AlipayConfig.notify_url); sParaTemp.put("return_url", AlipayConfig.return_url); sParaTemp.put("anti_phishing_key", AlipayConfig.anti_phishing_key); sParaTemp.put("exter_invoke_ip", AlipayConfig.exter_invoke_ip); sParaTemp.put("out_trade_no", outTradeNo); sParaTemp.put("subject", subject); sParaTemp.put("total_fee", totalFee); sParaTemp.put("body", body); // return AlipaySubmit.buildRequest(sParaTemp, "post", ""); } /** * ???(refund_fastpay_by_platform_pwd), ?HTML?POST???? * * @param batchNo ? * @param batchNum * @param detailData ? * @return ???HTML */ public static String buildRequest(String batchNo, String batchNum, String detailData) { Map<String, String> sParaTemp = new HashMap<>(); sParaTemp.put("service", AlipayConfig.refund_fastpay_by_platform_pwd); sParaTemp.put("partner", AlipayConfig.partner); sParaTemp.put("_input_charset", AlipayConfig.input_charset); sParaTemp.put("notify_url", AlipayConfig.notify_url); sParaTemp.put("seller_user_id", AlipayConfig.seller_id); sParaTemp.put("refund_date", UtilDate.getDateFormatter()); sParaTemp.put("batch_no", batchNo); sParaTemp.put("batch_num", batchNum); sParaTemp.put("detail_data", detailData); return AlipaySubmit.buildRequest(sParaTemp, "post", ""); } /** * ?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 = ALIPAY_GATEWAY_NEW + "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(); } }