码上敲享录 > 网站支付常见问题分享 > 微信支付的SDK与DEMO(java)如何跑起来

微信支付的SDK与DEMO(java)如何跑起来

上一章章节目录下一章 2018-08-08已有5673人阅读 评论(0)

微信支付的SDK与DEMO(java)如何跑起来


解决方法:

有时候微信开发的时候下载下来的SDK与DEMO跑不起来,有的类缺失等等问题,可以参考项目下README.md文件,但按照里面很多东西都报错。现在我结合myeclipse总结一下。

1.下载SDK与DEMO(java):

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1


2.导入到myeclipse中,import—》选择Existing Maven Project,SDK与DEMO是java项目不是web项目。


3.在com.github.wxpay.sdk包下新建一个名字为MyConfig.java的类,实现WXPayConfig.java,再新建一个类WXPayDomainSimpleImpl.java(再复制以下内容进去)


4.在src/test/java中新建一个WXPayExample.java测试类,实现统一下单,参数要配置自己的。

MyConfig.java:

package com.github.wxpay.sdk;

import com.github.wxpay.sdk.IWXPayDomain;

import com.github.wxpay.sdk.WXPayConfig;

import java.io.*;

public class MyConfig extends 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 "wx238afc7b441c060c";

   }


   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;

   }


@Override

IWXPayDomain getWXPayDomain() {

// TODO Auto-generated method stub

return WXPayDomainSimpleImpl.instance();

}

}


WXPayDomainSimpleImpl.java:

package com.github.wxpay.sdk;

import org.apache.http.conn.ConnectTimeoutException;


import java.net.UnknownHostException;

import java.util.HashMap;

import java.util.Map;


/**

* Created by blaketang on 2017/6/16.

*/

public class WXPayDomainSimpleImpl implements IWXPayDomain {

   private WXPayDomainSimpleImpl(){}

   private static class WxpayDomainHolder{

       private static IWXPayDomain holder = new WXPayDomainSimpleImpl();

   }

   public static IWXPayDomain instance(){

       return WxpayDomainHolder.holder;

   }


   public synchronized void report(final String domain, long elapsedTimeMillis, final Exception ex) {

       DomainStatics info = domainData.get(domain);

       if(info == null){

           info = new DomainStatics(domain);

           domainData.put(domain, info);

       }


       if(ex == null){ //success

           if(info.succCount >= 2){    //continue succ, clear error count

               info.connectTimeoutCount = info.dnsErrorCount = info.otherErrorCount = 0;

           }else{

               ++info.succCount;

           }

       }else if(ex instanceof ConnectTimeoutException){

           info.succCount = info.dnsErrorCount = 0;

           ++info.connectTimeoutCount;

       }else if(ex instanceof UnknownHostException){

           info.succCount = 0;

           ++info.dnsErrorCount;

       }else{

           info.succCount = 0;

           ++info.otherErrorCount;

       }

   }


   public synchronized DomainInfo getDomain(final WXPayConfig config) {

       DomainStatics primaryDomain = domainData.get(WXPayConstants.DOMAIN_API);

       if(primaryDomain == null ||

               primaryDomain.isGood()) {

           return new DomainInfo(WXPayConstants.DOMAIN_API, true);

       }


       long now = System.currentTimeMillis();

       if(switchToAlternateDomainTime == 0){   //first switch

           switchToAlternateDomainTime = now;

           return new DomainInfo(WXPayConstants.DOMAIN_API2, false);

       }else if(now - switchToAlternateDomainTime < MIN_SWITCH_PRIMARY_MSEC){

           DomainStatics alternateDomain = domainData.get(WXPayConstants.DOMAIN_API2);

           if(alternateDomain == null ||

               alternateDomain.isGood() ||

               alternateDomain.badCount() < primaryDomain.badCount()){

               return new DomainInfo(WXPayConstants.DOMAIN_API2, false);

           }else{

               return new DomainInfo(WXPayConstants.DOMAIN_API, true);

           }

       }else{  //force switch back

           switchToAlternateDomainTime = 0;

           primaryDomain.resetCount();

           DomainStatics alternateDomain = domainData.get(WXPayConstants.DOMAIN_API2);

           if(alternateDomain != null)

               alternateDomain.resetCount();

           return new DomainInfo(WXPayConstants.DOMAIN_API, true);

       }

   }


   static class DomainStatics {

       final String domain;

       int succCount = 0;

       int connectTimeoutCount = 0;

       int dnsErrorCount =0;

       int otherErrorCount = 0;


       DomainStatics(String domain) {

           this.domain = domain;

       }

       void resetCount(){

           succCount = connectTimeoutCount = dnsErrorCount = otherErrorCount = 0;

       }

       boolean isGood(){ return connectTimeoutCount <= 2 && dnsErrorCount <= 2; }

       int badCount(){

           return connectTimeoutCount + dnsErrorCount * 5 + otherErrorCount / 4;

       }

   }

   private final int MIN_SWITCH_PRIMARY_MSEC = 3 * 60 * 1000;  //3 minutes

   private long switchToAlternateDomainTime = 0;

   private Map<String, DomainStatics> domainData = new HashMap<String, DomainStatics>();

}



WXPayExample.java


import java.util.HashMap;

import java.util.Map;


import com.github.wxpay.sdk.MyConfig;

import com.github.wxpay.sdk.WXPay;

import com.github.wxpay.sdk.WXPayConfig;


public class WXPayExample {


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

MyConfig config = new MyConfig();

       //WXPay wxpay = new WXPay(config);//正式

       WXPay wxpay = new WXPay(config,"http://yayihouse.com",false,true);//沙箱

       Map<String, String> data = new HashMap<String, String>();

       data.put("body", "腾讯充值中心-QQ会员充值");//商品描述

       data.put("out_trade_no", "2016090910595900000012");//商户订单号

       data.put("device_info", "");//设备号,PC网页或公众号内支付可以传"WEB"

       data.put("fee_type", "CNY");//标价币种,默认人民币:CNY

       data.put("total_fee", "1");//标价金额,单位为分,

       data.put("spbill_create_ip", "123.12.12.123");//APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP

       data.put("notify_url", "http://www.example.com/wxpay/notify");//异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。

       data.put("trade_type", "NATIVE");  //交易类型   JSAPI:公众号支付,NATIVE:扫码支付,APP:APP支付 此处指定为扫码支付

       data.put("product_id", "12");//trade_type=NATIVE时(即扫码支付),此参数必传。此参数为二维码中包含的商品ID,商户自行定义。


       try {

           Map<String, String> resp = wxpay.unifiedOrder(data);

           System.out.println(resp);

       } catch (Exception e) {

           e.printStackTrace();

       }


}


}


本文链接:http://yayihouse.com/yayishuwu/chapter/1448


27

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交