微信支付JAVA版的安装与调试

(1) MyConfig.java

package com.github.wxpay.sdk;
import com.github.wxpay.sdk.WXPayConfig;
import java.io.*;
public class MyConfig extends WXPayConfig{
   
    private byte[] certData;
    public MyConfig() throws Exception {
   
    	//String certPath = "d:/tomcat/ssqc/webapps/wxpay-sdk/cert/apiclient_cert.p12";
        /*String certPath = "C:/Tomcat 8.5/webapps/wxpay-sdk/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();*/
    	//上面六行注释,要修改WXPay.java,注释掉63行
    }
    public String getAppID() {
   
        return "wx8397f8696b538317";
    }
    public String getMchID() {
   
        return "1473426802";
    }
    public String getKey() {
   
        return "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb";
    }
    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 null;
//	}
	//README.MD漏了这个类型:
	public IWXPayDomain getWXPayDomain() {
   
        // 这个方法需要这样实现, 否则无法正常初始化WXPay
    IWXPayDomain iwxPayDomain = new IWXPayDomain() {
   
        public void report(String domain, long elapsedTimeMillis, Exception ex) {
   
    
        }
        public DomainInfo getDomain(WXPayConfig config) {
   
            return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);
        }
    };
    return iwxPayDomain;
  }//public IWXPayDomain getWXPayDomain
}//public class MyConfig extends WXPayConfig

(2) WXPayExample.java

package com.github.wxpay.sdk;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
//import com.google.zxing.*;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
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", "2020113010595900000012");
        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.gzchenlijun.com/notify.php");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付//APP
        data.put("product_id", "12");
        try {
   
            Map<String, String> resp = wxpay.unifiedOrder(data);
            System.out.println(resp);
        } catch (Exception e) {
   
            e.printStackTrace();
        }    
    }//public static void main
    public void dopay(String order_id,String price,String jdpath) throws Exception {
   
        MyConfig config = new MyConfig();
        WXPay wxpay = new WXPay(config);
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "广州软件研究所");
        data.put("out_trade_no", order_id);
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", price);
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://www.gzchenlijun.com/notify.php");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id", "12");
        try {
   
            //发起支付
            Map<String, String> resp = wxpay.unifiedOrder(data); 
            System.out.println(resp);   
            System.out.println("jdpath2="+jdpath);
            //获取二维码URL
            String code_url = resp.get("code_url");
            //根据url生成二维码
            String text = code_url; 
            int width = 300; 
            int height = 300; 
            //二维码的图片格式 
            String format = "gif"; 
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); 
            //内容所使用编码 
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); 
            //生成二维码 
            //File outputFile = new File("D:/Tomcat 7.0/webapps/wxpay-sdk"+"/img/"+"/"+order_id+".gif");
            System.out.println("jdpath="+jdpath);
            File outputFile = new File(jdpath+"/img/"+"/"+order_id+".gif");
            //二维码图片放置的目录jdpath是首页index.jsp生成的
            if(!outputFile.exists()){
   
            QRUtil.writeToFile(bitMatrix, format, outputFile); 
          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值