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