微信支付 Java 官方Demo

不少人說微信支付沒有提供Java版的官方Demo
這話說對了一半, 能夠拿來直接Run的Java程序的確沒提供.

可是針對每個功能都提供了java code的小例子, 只不過這些例子所有放在一個README.md文件裏了html

咱們會下載到 WxPayAPI_JAVA_v3.zip 這個包是吧?java

解壓縮, 在java-sdk-v3裏面git


我直接把這裏面的內容貼出來了哦github

---------------------------------------------分割線------------------------------------------------算法

微信支付 Java SDK
------
對[微信支付開發者文檔](https://pay.weixin.qq.com/wiki/doc/api/index.html)中給出的API進行了封裝。
com.github.wxpay.sdk.WXPay類下提供了對應的方法:

|方法名 | 說明 |
|--------|--------|
|microPay| 刷卡支付 |
|unifiedOrder | 統一下單|
|orderQuery | 查詢訂單 |
|reverse | 撤銷訂單 |
|closeOrder|關閉訂單|
|refund|申請退款|
|refundQuery|查詢退款|
|downloadBill|下載對帳單|
|report|交易保障|
|shortUrl|轉換短連接|
|authCodeToOpenid|受權碼查詢openid|


* 參數爲`Map<String, String>`對象,返回類型也是`Map<String, String>`。
* 方法內部會將參數會轉換成含有`appid`、`mch_id`、`nonce_str`、`sign\_type`和`sign`的XML;
* 默認使用MD5進行簽名;
* 經過HTTPS請求獲得返回數據後會對其作必要的處理(例如驗證簽名,簽名錯誤則拋出異常)。
* 對於downloadBill,不管是否成功都返回Map,且都含有`return_code`和`return_msg`。若成功,其中`return_code`爲`SUCCESS`,另外`data`對應對帳單數據。

api

安裝maven:微信

<dependency>
    <groupId>com.github.wxpay</groupId>
    <artifactId>wxpay-sdk</artifactId>
    <version>0.0.3</version>
</dependency>
-----------------------------------------如下爲示例----------------------------------------

配置類MyConfig
app

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

public class MyConfig implements WXPayConfig{

    private byte[] certData;

    public MyConfig() 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() {
        return "wx8888888888888888";
    }

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

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

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

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}

統一下單

maven

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", "騰訊充值中心-QQ會員充值");
        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");  // 此處指定爲掃碼支付
        data.put("product_id", "12");

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

}

訂單查詢微信支付

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();
        }
    }

}
退款查詢
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();
        }
    }

}

下載對帳單

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();
        }
    }

}

暫時不支持下載壓縮格式的對帳單,但可使用該SDK生成請求用的XML數據

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();
        }
    }

}

收到支付結果通知時,須要驗證簽名,能夠這樣作

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 = "...."; // 支付結果通知的xml格式數據

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

        Map<String, String> notifyMap = WXPayUtil.xmlToMap(notifyData);  // 轉換成map

        if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {
            // 簽名正確
            // 進行處理。
            // 注意特殊狀況:訂單已經退款,但收到了支付結果成功的通知,不該把商戶側訂單狀態從退款改爲支付成功
        }
        else {
            // 簽名錯誤,若是數據裏沒有sign字段,也認爲是簽名錯誤
        }
    }

}

HTTPS請求默認使用MD5算法簽名,若須要使用HMAC-SHA256

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);
        // ......
    }
}
若須要使用sandbox環境
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);
        // ......
    }

}

所需的jar包只有一個:wxpay-sdk-0.0.3.jar固然若是你須要看源碼和文檔的話, 能夠再加上下面兩個:wxpay-sdk-0.0.3-javadoc.jarwxpay-sdk-0.0.3-sources.jar這3個jar 包都在 java-sdk-v3\target 下面