1. 程式人生 > >第一次接觸 java開發微信掃碼支付,獲取二維碼連結

第一次接觸 java開發微信掃碼支付,獲取二維碼連結

微信掃碼支付官方文件:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1

1.在官方文件中下載自己要有的sdk (java的SDK 是一個maven專案,它是不能釋出的)


2.在eclipse中引入下載的sdk專案

3.建立一個class,繼承sdk的WXPayConfig(這時候可能會報錯,因為WXPayConfig的方法訪問許可權是預設的,只能同包,本類訪問)

package ceshi.Model;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;


import com.github.wxpay.sdk.IWXPayDomain;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayUtil;


import ceshi.Service.impl.WXPayDomainSimpleImpl;
public class OrderRequest extends WXPayConfig{




    private byte[] certData;
    private static OrderRequest INSTANCE;


     public OrderRequest() throws Exception{
        String certPath = "C://Users/Administrator/Desktop/cert/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();
    }
     private String  nonce_str;
     
     
public String getNonce_str() {
nonce_str=WXPayUtil.generateNonceStr();
return nonce_str;
}


//
    public static OrderRequest getInstance() throws Exception{
        if (INSTANCE == null) {
            synchronized (OrderRequest.class) {
                if (INSTANCE == null) {
                    INSTANCE = new OrderRequest();
                }
            }
        }
        return INSTANCE;
    }


    public String getAppID() {
        return "1111111";
    }
    
    public String getAppSecret() {
        return "5111111111111111111111111111121c80f8";
    }


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


    public String getKey() {
        return "111111111111111111111";
    }
  //獲取商戶證書內容
    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }
//    HTTP(S) 連線超時時間,單位毫秒


    public int getHttpConnectTimeoutMs() {
        return 2000;
    }
//    HTTP(S) 讀資料超時時間,單位毫秒
    public int getHttpReadTimeoutMs() {
        return 10000;
    }


    public IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }


    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }


    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }


    @Override
    public int getReportWorkerNum() {
        return 1;
    }


    @Override
    public int getReportBatchSize() {
        return 2;
    }
}


4.建立class呼叫統一下單方法,成功返回的的是Map,需要用到Map中的code_url(二維碼連結),再將二維碼連結轉換成二維碼圖片

private OrderRequest orderRequest;
private WXPay wxpay;
private long time_stamp = WXPayUtil.getCurrentTimestamp();
private static Logger logger = Logger.getLogger("PayServiceImpl");


/**
* 掃碼支付 下單 out_trade_no;商戶訂單號total_fee;商戶訂單 金額
* @throws Exception 
*/
public Map<String, String> doUnifiedOrder(String out_trade_no, String total_fee) throws Exception {
orderRequest = orderRequest.getInstance();
wxpay = new WXPay(orderRequest, "http://test.letiantian.me/wxpay/notify");
Map<String, String> data = new HashMap<String, String>();
data.put("appid", orderRequest.getAppID());
data.put("mch_id", orderRequest.getMchID());
data.put("time_stamp", String.valueOf(time_stamp));
data.put("nonce_str", orderRequest.getNonce_str());
data.put("body", "代理商管理系統-掃碼支付");
data.put("out_trade_no", out_trade_no);// 商戶訂單號
data.put("device_info", "WEB");
data.put("fee_type", "CNY");
data.put("total_fee", total_fee);
data.put("spbill_create_ip", "127.0.0.1");// 終端IP 微信支付API的機器IP
data.put("notify_url", "http://test.letiantian.me/wxpay/notify");// 通知地址,非同步接收微信支付結果通知的回撥地址,通知url必須為外網可訪問的url,不能攜帶引數。
data.put("trade_type", "NATIVE");// 交易型別
//data.put("product_id", product_id);// 商品ID
// data.put("time_expire", "20170112104120");


Map<String, String> r = null;
try {
r = wxpay.unifiedOrder(data);
System.out.println(r);
System.out.println("url:" + r.get("code_url"));
} catch (Exception e) {
e.printStackTrace();
}


return r;


}

5.微信返回結果接收

@RequestMapping("/notify")
public String notify(String strxml,Model m) throws Exception {
Map<String, String> data = WXPayUtil.xmlToMap(strxml);
String return_code=data.get("return_code");
if(return_code.equals("SUCCESS")) {
m.addAttribute("res", "支付成功");
}else {
m.addAttribute("res", "支付失敗");
}
return "zhifu";
}


6.測試   main方法

PayController p=new PayController();

Integer f1=(int) (0.01*100);//支付0.01元
String f2=f1.toString();
try {
System.out.println(p.pagePay( "9874", "123451",f2));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}