java wechat payment [unified order interface] and [order query interface] Call successful complete code and results

Keywords: xml SDK PHP github

The company wants to do wechat payment recently, but it hasn't done it before, but it's not difficult to do it with Alibaba cloud. I found a lot of posts on the Internet, and did five test projects in eclipse, but they didn't succeed in the test. Later, I got off the wechat SDK, and also made a test sample. During the test, I referred to: https://blog.csdn.net/t_james/article/details/84653827#comments

Step 1: download the SDK. https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1

The second step is to prepare the public address appid, MchID and key. https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=3_1 Get,

This is to be registered and applied by the company.

Step 3: open the payment mode. If you want to pay by scanning code (Native payment), please refer to: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=2_1

Step 4: import the project into eclipse, as shown in the following figure:

 

The following is the complete code. If you don't understand it, just one class:

package com.github.wxpay.test;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.github.wxpay.sdk.WxPayConfigImpl;

/**
 * Signature verification tool address:
 * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=20_1
 * 
 * Unified order API Description: 
 * https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
 * @author 123
 *
 */
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//unifiedOrder();
		orderQuery();
	}
	
	 /**
     * Unified order interface < br >
     * Scenario: public number payment, code scanning payment, APP payment
     */
	public static  void unifiedOrder (){
		try {
			//sdk class reference
			WxPayConfigImpl config = WxPayConfigImpl.getInstance();
			
			WXPay pyPay = new WXPay(config);
			
			//Order number (adjust to own production logic)
			String out_trade_no = config.getMchID()+"-" + System.currentTimeMillis(); 
			//Public address appid
	        String appid = config.getAppID(); 
	        //Business number 
	        String mch_id = config.getMchID();  
	        //key
	        String key = config.getKey();  
	        //random number
	        String nonce_str = WXPayUtil.generateNonceStr(); 
	        //Obtain the ip address of the initiating computer, which can be tested with Baidu and other substitutes.
	        String spbill_create_ip = "123.123.123.123";
	        //Callback interface (I don't need to use this machine for testing anyway, I'm free to make an accessible one)
	        String notify_url = "http://www.weixin.qq.com/wxpay/pay.php";
	        //Pattern
	        String trade_type = "NATIVE";//"JSAPI";
			
			SortedMap<String,String> packageParams = new TreeMap<String,String>();  
	        packageParams.put("appid", appid);
	        packageParams.put("mch_id", mch_id);
	        packageParams.put("nonce_str", nonce_str);
	        packageParams.put("body", "test-coke");  //(adjust to own name) format: Tencent recharge Center - QQ member recharge
	        packageParams.put("out_trade_no", out_trade_no);
	        packageParams.put("total_fee", "10"); //The unit of price is cent
	        packageParams.put("spbill_create_ip", spbill_create_ip);
	        packageParams.put("notify_url", notify_url);
	        packageParams.put("trade_type", trade_type);
	        
			Map runMap = pyPay.unifiedOrder(packageParams);
	        
	        System.out.println("=============returnXml==============");
			System.out.println(runMap.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//Send out:
//		<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//		<xml>
//		<appid> public address APPID</appid>
//		<body>test-coke</body>
//		< MCH ﹣ ID > own merchant number < / MCH ﹣ ID >
//		<nonce_str>rE8iTtCENf4VSOtOCZ0PIbkuuNklyFpw</nonce_str>
//		<notify_url>http://www.weixin.qq.com/wxpay/pay.php</notify_url>
//		< out trade no > own merchant No. - 1561942616146 < / out trade no >
//		<sign>907192B0875A02F199B45B7F30356644751DF6D4A2AFA77C0F3C226E4A60D276</sign>
//		<sign_type>HMAC-SHA256</sign_type>
//		<spbill_create_ip>123.123.123.123</spbill_create_ip>
//		<total_fee>10</total_fee>
//		<trade_type>NATIVE</trade_type>
//		</xml>
		
		//Result:
//		<xml><return_code><![CDATA[SUCCESS]]></return_code>
//		<return_msg><![CDATA[OK]]></return_msg>
//		<appid><! [CDATA[public address APPID]]></appid>
//		< MCH · ID > < CDATA [own merchant number]] >
//		<nonce_str><![CDATA[ayhkOwhZysjBQNR6]]></nonce_str>
//		<sign><![CDATA[021F45467BA5119C389D5F08D768CC5A635687057BBD6445E6B7A1BBB34F4AA9]]></sign>
//		<result_code><![CDATA[SUCCESS]]></result_code>
//		<prepay_id><![CDATA[wx010855154971807287de77f31587946700]]></prepay_id>
//		<trade_type><![CDATA[NATIVE]]></trade_type>
//		<code_url><![CDATA[weixin://wxpay/bizpayurl?pr=kMwACCs]]></code_url>
//		</xml>
		
		//Content after explanation
		//{nonce_str=ayhkOwhZysjBQNR6, code_url=weixin://wxpay/bizpayurl?pr=kMwACCs, appid= public number APPID, sign=021F45467BA5119C389D5F08D768CC5A635687057BBD6445E6B7A1BBB34F4AA9, trade_type=NATIVE, return_msg=OK, result_code=SUCCESS, mch_id= own merchant number, return_code=SUCCESS, mch_id=
		
	}
	
	/**
     * Query order interface < br >
     * Scenario: card payment, public number payment, code scanning payment, APP payment
     */
	public static void orderQuery(){
		try {
			//sdk class reference
			WxPayConfigImpl config = WxPayConfigImpl.getInstance();
			
			WXPay pyPay = new WXPay(config);
			
			//Order number 
			String out_trade_no = "Own merchant number-1561942616146"; 
			//appid  
	        String appid = config.getAppID();
	        //Business number 
	        String mch_id = config.getMchID();
	        //random number
	        String nonce_str = WXPayUtil.generateNonceStr(); 
	        //Pattern
	        String trade_type = "NATIVE";//"JSAPI";
			
			SortedMap<String,String> packageParams = new TreeMap<String,String>();  
	        packageParams.put("appid", appid);
	        packageParams.put("mch_id", mch_id);
	        packageParams.put("nonce_str", nonce_str);
	        packageParams.put("out_trade_no", out_trade_no);
	        
			Map runMap = pyPay.orderQuery(packageParams);
	        
	        System.out.println("=============returnXml==============");
			System.out.println(runMap.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Send out
//		<?xml version="1.0" encoding="UTF-8" standalone="no"?>
//		<xml>
//		<appid> public address APPID</appid>
//		< MCH ﹣ ID > own merchant number < / MCH ﹣ ID >
//		<nonce_str>epQ1ZECqQryQJv0ZOybGc8XIjWTJupNX</nonce_str>
//		< out trade no > own merchant No. - 1561942616146 < / out trade no >
//		<sign>366964520A9209243A714EAB55D46E542B56152E705814D4DB20FF1567511DA9</sign>
//		<sign_type>HMAC-SHA256</sign_type>
//		</xml>
		
		//Result:
//		<xml><return_code><![CDATA[SUCCESS]]></return_code>
//		<return_msg><![CDATA[OK]]></return_msg>
//		<appid><! [CDATA[public address APPID]]></appid>
//		< MCH · ID > < CDATA [own merchant number]] >
//		<device_info><![CDATA[]]></device_info>
//		<nonce_str><![CDATA[dewGq9USU04gFwUE]]></nonce_str>
//		<sign><![CDATA[94D3B1BFB852E19CB3F421DBC23D5A3AE5A7CBDC9448C8B146959036141EFE2F]]></sign>
//		<result_code><![CDATA[SUCCESS]]></result_code>
//		<total_fee>10</total_fee>
//		< out trade no > <! [CDATA [own merchant number - 1561942616146]]] >
//		<trade_state><![CDATA[NOTPAY]]></trade_state>
//		< trade  state  desc > <! [CDATA [order not paid]] >
//		</xml>
	}

}

Note: wxpayconfigmimpl and WXPayDomainSimpleImpl implementation refer to this brother's Weibo

https://blog.csdn.net/t_james/article/details/84653827#comments

In the code, [public number APPID], [merchant number], [key], [initiating computer ip] these are related to the contents of their own companies or their customers.

Posted by J-C on Fri, 01 Nov 2019 22:48:25 -0700