Let's complain:
The interface of Alipay and WeChat DEMO and documents are really too difficult to understand. It is messy and I don’t know where to find what I want. In recent days, our company needs to do similar developments. As a pioneer, I took the lead in solving the problem of integrating Java Alipay payment and WeChat payment interfaces.
Our working environment: JSP website + payment interface. The current Alipay interface is 20160912 and WeChat is V3 version. If you encounter a version upgrade, please contact the customer service staff of the relevant organization to upgrade.
This article introduces the JSP+Alipay interface, which is not original.
Be aware of newbies:
1. The interface address and parameters used in this article are the address of the sandbox. Whether you are using the sandbox or the official address, be sure to verify the use of the interface address and parameters, and do not take it for granted.
2. This article uses JAR package:
alipay-sdk-java20160912220103.jar
commons-codec-1.6.jar
commons-httpclient-3.0.1.jar
commons-logging-1.1.1.jar
fastjson-1.2.7.jar
json-lib-2.2.3-jdk13.jar
json.jar
jsp-api.jar
servlet-api.jar
illustrate:
alipay-sdk-java is the official payment SDK. Please download it at the official website. This article uses version 20160912
Home page call:
<body> <% CsPay pay=new CsPay(); pay.doPost(request, response); %> </body>
package com.luozhuang.alipay; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.internal.util.StringUtils; import com.alipay.api.request.AlipayTradeCancelRequest; import com.alipay.api.request.AlipayTradePayRequest; import com.alipay.api.request.AlipayTradePrecreateRequest; import com.alipay.api.request.AlipayTradeQueryRequest; import com.alipay.api.request.AlipayTradeRefundRequest; import com.alipay.api.request.AlipayTradeWapPayRequest; import com.alipay.api.response.AlipayTradeCancelResponse; import com.alipay.api.response.AlipayTradePayResponse; import com.alipay.api.response.AlipayTradePrecreateResponse; import com.alipay.api.response.AlipayTradeQueryResponse; import com.alipay.api.response.AlipayTradeRefundResponse; public class AlipayClientFactory { private static final AlipayClient client = new DefaultAlipayClient( Config.URL, Config.APPID, Config.RSA_RRIVATE_KEY, Config.FORMAT, Config.CHARSET, Config.ALIPAY_PUBLIC_KEY); public static AlipayClient getAlipayClientInstance() { return client; } /** * appAuthToken * If ISV calls the face-to-face payment interface instead of the merchant, the app_auth_token obtained after the merchant is authorized must be brought with him; if the merchant applies for face-to-face payment, he will pass null bizContent * Request parameters of JSON format merchant*/ // Mobile web payment website payment public String ydAndPc_Pay(Map<String, String> maps) throws AlipayApiException { AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest(); String NotifyUrl = maps.get("NotifyUrl"); String ReturnUrl = maps.get("ReturnUrl"); // Background callback if (!StringUtils.isEmpty(NotifyUrl)) { alipayRequest.setNotifyUrl(NotifyUrl); // Public parameter maps.remove("NotifyUrl"); // BizContent does not require the public parameter maps.remove("NotifyUrl"); } // Page callback if (!StringUtils.isEmpty(ReturnUrl)) { alipayRequest.setReturnUrl(ReturnUrl); // Public parameter maps.remove("ReturnUrl"); } String bizCon = JSON.toJSONString(maps); alipayRequest.setBizContent(bizCon); String form = ""; try { form = AlipayClientFactory.getAlipayClientInstance() .pageExecute(alipayRequest).getBody(); } catch (AlipayApiException e) { form = "err"; e.printStackTrace(); } // Call the SDK to generate a form return form; } // Query the order status public AlipayTradeQueryResponse query(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); } // Barcode payment public AlipayTradePayResponse pay(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradePayRequest request = new AlipayTradePayRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); } // Scan the code to pay public AlipayTradePrecreateResponse precreate(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); } // Order Revocation public AlipayTradeCancelResponse cancel(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradeCancelRequest request = new AlipayTradeCancelRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); } // Apply for a refund public AlipayTradeRefundResponse refund(String appAuthToken, String bizContent) throws AlipayApiException { AlipayTradeRefundRequest request = new AlipayTradeRefundRequest(); request.putOtherTextParam("app_auth_token", appAuthToken); request.setBizContent(bizContent); return AlipayClientFactory.getAlipayClientInstance().execute(request); } @SuppressWarnings("unused") public static void main(String[] args) { AlipayClientFactory c = new AlipayClientFactory(); try { Map<String, String> map = new HashMap<String, String>(); map.put("out_trade_no", "20160914113218"); String bizContent = JSON.toJSONString(map); System.err.println(bizContent); AlipayTradeQueryResponse rp = c.query(null, bizContent); } catch (AlipayApiException e) { e.printStackTrace(); } } } package com.luozhuang.alipay; public class Config { // Developer applies private key. Java configures PKCS8 format, and PHP/.Net language configures the original private key in the rsa_private_key.pem file. public static final String RSA_RRIVATE_KEY ="luozhuang"; // Interface request gateway. In-person payment, query, refund, and revocation interfaces are fixed values public static final String URL = "https://openapi.alipaydev.com/gateway.do"; // Merchant application APPID, as long as your application contains the face-person payment interface and is in the activate state, you can use the corresponding appid of this application. Developers can log in to the Open Platform-Management Center-Related Application to view public static final String APPID = "luozhuang"; // Encoded character set. Default utf-8 public static final String CHARSET = "utf-8"; // Returns format. Default json public static final String FORMAT = "json"; // Alipay public key is used to obtain synchronous return information for verification to verify whether it is the information sent by Alipay. public static final String ALIPAY_PUBLIC_KEY = "luozhuang"; } package com.luozhuang.alipay; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alipay.api.AlipayApiException; /** * Servlet implementation class CsPay */ public class CsPay extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // The parameters are currently written dead and written alive according to business needs Map<String, String> maps = new HashMap<String, String>(); maps.put("out_trade_no", UtilDate.getOrderNum()); maps.put("total_amount", "0.01"); maps.put("subject", "Iphone6 16G"); maps.put("body", "Iphone6 16G"); maps.put("product_code", "QUICK_WAP_PAY"); // The KEY of the following two parameters should not be written randomly with maps.put("ReturnUrl", "http://domain.com/CallBack/return_url.jsp"); maps.put("NotifyUrl", "http://domain.com/CallBack/notify_url.jsp"); try { AlipayClientFactory ali = new AlipayClientFactory(); String form = ali.ydAndPc_Pay(maps); if (!form.equals("err")) { response.setContentType("text/html;charset=utf-8"); response.getWriter().write(form);// Directly output the complete form html to the page response.getWriter().flush(); } } catch (AlipayApiException e) { e.printStackTrace(); } } } package com.luozhuang.alipay; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; public class CsUtil { /** * Sort all elements of the array and splice them into strings in the "parameter = parameter value" pattern * * @param params * Parameter group that needs to be sorted and participated in character splicing* @return String after splicing*/ public static String createLinkString(Map<String, String> params) { List<String> keys = new ArrayList<String>(params.keySet()); Collections.sort(keys); String prestr = ""; for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); String value = params.get(key); if (i == keys.size() - 1) {// When splicing, the last& character is not included prestr = prestr + key + "=" + value; } else { prestr = prestr + key + "=" + value + "&"; } } return prestr; } } package com.luozhuang.alipay; import java.util.Date; import java.util.Random; import java.text.SimpleDateFormat; import java.text.DateFormat; /* * * Class name: UtilDate * Function: Custom order class * Details: Tool class, which can be used to obtain system dates, order numbers, etc. * Version: 3.3 * Date: 2012-08-17 * Description: * The following code is just a sample code provided for convenience for merchant testing. Merchants can write according to the needs of their own website and according to technical documents, and it is not necessary to use this code. *This code is for learning and researching the Alipay interface only, and is only provided as a reference. */ public class UtilDate { /** Year, month, day, hour, minute, and second (no underscore) yyyMMddHHmmss */ public static final String dtLong = "yyyMMddHHmmss"; /** Full time yyyy-MM-dd HH:mm:ss */ public static final String simple = "yyyy-MM-dd HH:mm:ss"; /** Year, month, day, (no underscore) yyyyMMdd */ public static final String dtShort = "yyyyMMdd"; /** * Return the current time of the system (accurate to milliseconds), as a unique order number* @return * Current system time in yyyyMMddHHmmss format*/ public static String getOrderNum(){ Date date=new Date(); DateFormat df=new SimpleDateFormat(dtLong); return df.format(date); } /** * Get the current date of the system (accurate to milliseconds), format: yyyy-MM-dd HH:mm:ss * @return */ public static String getDateFormatter(){ Date date=new Date(); DateFormat df=new SimpleDateFormat(simple); return df.format(date); } /** * Get the current year, month, day of the system (accurate to the day), format: yyyyMMdd * @return */ public static String getDate(){ Date date=new Date(); DateFormat df=new SimpleDateFormat(dtShort); return df.format(date); } /** * Generate a random three-digit number* @return */ public static String getThree(){ Random rad=new Random(); return rad.nextInt(1000)+""; } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.