准备工作:
1.首先,想要了解微信支付的流程,我们先通过微信开发平台,根据我们的需求,找到所需文档;
2.通过需求了解到,需要创建移动应用,根据自己的需求开通商户,并且开通微信支付功能;
3.根据开发文档下载java版的sdk,当然这一般情况下服务器端做的,可以通过pom.xml文件了解sdk详情;
4.所需值的路径或出处:
appId:应用appId
mactchId :商户号
key:商户平台->账号中心->账户设置->API安全->然后自己设置
证书路径:商户平台->账号中心->账户设置->API安全->下载证书
开发过程
a.添加依赖:
compile 'com.github.wxpay:WXPay-SDK-Java:0.0.4'
b.然后实现WXPayConfig 类;
public class WXConfig implements WXPayConfig {
private byte[] certData;
public WXConfig() throws Exception {
String certPath = iExec.getCertPath() + "/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();
}
@Override
public String getAppID() {
return "wx234444";
}
@Override
public String getMchID() {
return "11111111";
}
@Override
public String getKey() {
return "kluDqARvZg3LLNfffffffffffffffffffffff";
}
@Override
public InputStream getCertStream() {
ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
@Override
public int getHttpConnectTimeoutMs() {
return 8000;
}
@Override
public int getHttpReadTimeoutMs() {
return 10000;
}
}
c.申请支付接口:
WXConfig config = null;
try {
config = new WXConfig();
} catch (Exception e) {
e.printStackTrace();
}
WXPay mPay = new WXPay(config);
d.最后调用微信支付接口,可以通过sdk中README.md了解;
微信开发文档
通过native支付文档中模式2可以知道整个的步骤;
对于编代码简单来说:
1,统一订单,展示二维码
public void unitOrder() {
Map<String, String> data = new HashMap<String, String>();
data.put("body", "commodity");
final String outTradeNo = getOutTradeNo();
data.put("out_trade_no", outTradeNo);
data.put("device_info", "roy");
data.put("fee_type", "CNY");
data.put("total_fee", "1");
String ip = IpUtils.getIpAddress(mContext);
Log.e("www", "ip = " + ip);
data.put("spbill_create_ip", ip);
data.put("notify_url", "http://www.example.com/wxpay/notify");
data.put("trade_type", "NATIVE"); // 此处指定为扫码支付
data.put("product_id", "12");
Log.e("www", "data = " + data.toString());
new Thread() {
@Override
public void run() {
super.run();
Map<String, String> resp = null;
try {
resp = mPay.unifiedOrder(data);
Log.e("www", "resp = " + resp.toString());
String result_code_value = resp.get(RESULT_CODE_KEY);
Log.e("www", "result_code_value = " + result_code_value);
if (RESULT_SUCCESS.equals(result_code_value)) {
String codeUrl = resp.get(CODE_URL_KEY);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("www", "Exception = " + e.getMessage());
}
}
}.start();
}
2,查询订单
/***查询订单**/
public void queryPay(String outTradeNo) {
Map<String, String> data = new HashMap<String, String>();
data.put("out_trade_no", outTradeNo);
new Thread() {
@Override
public void run() {
super.run();
try {
Map<String, String> resp = mPay.orderQuery(data,1000, 1000);
Log.e("www", "queryPay , resp = " + resp.toString());
String tradeStateValue = resp.get(TRADE_STATE_KEY);
String outTradeNo = resp.get(OUT_TRADE_NO);
boolean isSucess = tradeStateValue.equals(RESULT_SUCCESS);
listener.onOrderQuery(isSucess,outTradeNo);
udpsender.getInstance().push("139.196.34.81", 7001, "queryPay , resp = " + resp.toString());
} catch (Exception e) {
e.printStackTrace();
Log.e("www", "Exception = " + e.getMessage());
udpsender.getInstance().push("139.196.34.81", 7001, "queryPay , Exception = " + e.getMessage());
}
}
}.start();
}