Wechat Pay — SDK and DEMO download

See firstREADME.md

Java SDK for wechat Pay


The API given in the developer document of wechat Pay is encapsulated.

Com. Making. Wxpay. SDK. Wxpay under class provides a corresponding method:

The method name instructions
microPay Credit card payment
unifiedOrder Unified order
orderQuery Query order
reverse Cancel the order
closeOrder Close the order
refund To apply for a refund
refundQuery Query a refund
downloadBill Download statement
report Transaction security
shortUrl Convert short link
authCodeToOpenid License code Query openID
  • Note:
  • The certificate file cannot be stored in the virtual directory of the Web server, but should be stored in a directory with access control to prevent it from being downloaded by others
  • It is recommended to change the certificate file name to something complex and not easy to guess
  • Merchant servers should do a good job of virus and Trojan protection, not to be stolen by illegal intrusions certificate files
  • Please keep the merchant payment key and public account SECRET properly to avoid key leakage
  • Parameters forMap<String, String>Object, as well as the return typeMap<String, String>
  • The method internally converts the parameters to containappid,mch_id,nonce_str,sign\_typeandsignThe XML
  • The HMAC-SHA256 and MD5 signatures are optional
  • When the returned data is obtained via HTTPS request, the necessary processing is done (such as verifying the signature and throwing an exception if the signature is incorrect).
  • For downloadBill, the Map is returned regardless of success and contains bothreturn_codeandreturn_msgIf successful, wherereturn_codeforSUCCESSAnd the otherdataCorresponding statement data

The sample

The configuration class MyConfig:

import com.github.wxpay.sdk.WXPayConfig;
import java.io.*;

public class MyConfig implements WXPayConfig{

    private byte[] certData;

    public MyConfig(a) throws Exception {
        String certPath = "/path/to/apiclient_cert.p12";
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getAppID(a) {
        return "wx8888888888888888";
    }

    public String getMchID(a) {
        return "12888888";
    }

    public String getKey(a) {
        return "88888888888888888888888888888888";
    }

    public InputStream getCertStream(a) {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs(a) {
        return 8000;
    }

    public int getHttpReadTimeoutMs(a) {
        return 10000; }}Copy the code

Unified order:

import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;

public class WXPayExample {

    public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("body"."Tencent Recharge Center -QQ Member recharge");
        data.put("out_trade_no"."2016090910595900000012");
        data.put("device_info"."");
        data.put("fee_type"."CNY");
        data.put("total_fee"."1");
        data.put("spbill_create_ip"."123.12.12.123");
        data.put("notify_url"."http://www.example.com/wxpay/notify");
        data.put("trade_type"."NATIVE");  // Pay by scanning code is specified here
        data.put("product_id"."12");

        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            System.out.println(resp);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Order enquiry:

import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;

public class WXPayExample {

    public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no"."2016090910595900000012");

        try {
            Map<String, String> resp = wxpay.orderQuery(data);
            System.out.println(resp);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Refund enquiries:

import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;

public class WXPayExample {

    public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no"."2016090910595900000012");

        try {
            Map<String, String> resp = wxpay.refundQuery(data);
            System.out.println(resp);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Download statement:

import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;

public class WXPayExample {

    public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("bill_date"."20140603");
        data.put("bill_type"."ALL");

        try {
            Map<String, String> resp = wxpay.downloadBill(data);
            System.out.println(resp);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

The use of other apis is similar.

Downloading the statement in compressed format is not currently supported, but you can use the SDK to generate the XML data for the request:

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

import java.util.HashMap;
import java.util.Map;

public class WXPayExample {

    public static void main(String[] args) throws Exception {

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> data = new HashMap<String, String>();
        data.put("bill_date"."20140603");
        data.put("bill_type"."ALL");
        data.put("tar_type"."GZIP");

        try {
            data = wxpay.fillRequestData(data);
            System.out.println(WXPayUtil.mapToXml(data));
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

When you receive a payment result notification, you need to verify the signature by doing this:


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

import java.util.Map;

public class WXPayExample {

    public static void main(String[] args) throws Exception {

        String notifyData = "..."; // Payment result notification data in XML format

        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);

        Map<String, String> notifyMap = WXPayUtil.xmlToMap(notifyData);  // Convert to map

        if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {
            // The signature is correct
            // Do the processing.
            // Note the special situation: the order has been refunded, but after receiving the notice of payment success, the order status on the merchant side should not be changed from refund to payment success
        }
        else {
            // A signature error. If there is no sign field in the data, it is also considered a signature error}}}Copy the code

HTTPS request Optional HMAC-SHA256 and MD5 signature:

import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayConstants; public class WXPayExample { public static void main(String[] args) throws Exception { MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config, WXPayConstants.SignType.HMACSHA256); / /... }}Copy the code

To use a Sandbox environment:

import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayConstants; public class WXPayExample { public static void main(String[] args) throws Exception { MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5, true); / /... }}Copy the code