Android微信刷脸支付开发文档
官方地址: 微信开放平台开发指引sdk下载
一、程序启动时微信初始化
/**
* 初始化
*
*/
Map<String, String> m1 = new HashMap<>();
WxPayFace.getInstance().initWxpayface(this, m1, new IWxPayfaceCallback() {
@Override
public void response(Map info) throws RemoteException {
if (info == null) {
showToast("调用返回为空, 请查看日志");
new RuntimeException("调用返回为空").printStackTrace();
return false;
}
String code = (String) info.get("return_code");
String msg = (String) info.get("return_msg");
showToast("初始化完成");
}
});
二、获取微信数据、sdk调用凭证
/**
* 获取rawdata
*
*/
WxPayFace.getInstance().getWxpayfaceRawdata(new IWxPayfaceCallback() {
@Override
public void response(final Map info) throws RemoteException {
if (info == null) {
showToast("调用返回为空, 请查看日志");
new RuntimeException("调用返回为空").printStackTrace();
return false;
}
String code = (String) info.get("return_code");
String msg = (String) info.get("return_msg");
String rawdata = info.get("rawdata");
//获取调用凭证authInfo
getAuthInfo(rawdata);
}
});
public void getAuthInfo(String rawData){
//这里调用后台接口获取authInfo
Retrofitutils.getInstance()
.create(ApiService.class)
.getAuthInfo(mapInfo)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Base_observer() {
@Override
public void onSubscribe(Disposable d) {
super.onSubscribe(d);
disposableList.add(d);
}
@Override
public void onSuccess(@NonNull String s) {
BeanPayInfo beanPayInfo = new Gson().fromJson(s, BeanPayInfo.class);
onSuccessAuthInfo(beanPayInfo);
}
@Override
public void onFailed(String e) {
dismissLoading();
Log.e("Errors", "onFailed" + e);
Toast.makeText(PayFaceActivity.this, e, Toast.LENGTH_SHORT).show();
// onFailedAuthInfo(e);
}
});
}
});
}
public void onSuccessAuthInfo(@NonNull BeanPayInfo beanPayInfo) {
BeanPayInfo.DataBean data = beanPayInfo.getData();
//服务商公众号id
appId = data.getAppId();
int expiresIn = data.getExpiresIn();//authInfo有效期 在有效期内可以不获取authInfo
mchId = data.getMchId(); //服务商商户号
String subAppId = data.getSubAppId();//子商户公众账号
String subMchId = data.getSubMchId();//子商户公众账号
storeId = data.getStoreId();// 门店编号
String tradeNo = data.getTradeNo();//订单号
String deviceCode = data.getDeviceCode();//设备号
int money= data.getMoney();//金额
getWxpayface(money, appId, mchId, subAppId, subMchId, storeId, tradeNo, deviceCode);//掉起微信app
}
三、 启动微信刷脸app支付,发起订单
Map<String, String> mapCode = new HashMap<>();
mapCode.put("appid", appId); // 公众号,必填
mapCode.put("mch_id", mchId); // 商户号,必填
mapCode.put("sub_appid", subAppId); // 子商户公众账号ID(非服务商模式不填)
mapCode.put("sub_mch_id", subMchId); // 子商户号(非服务商模式不填)
mapCode.put("store_id", storeId); // 门店编号,必填
mapCode.put("authinfo", AuthInfoUtils.getInstance().getAuthInfo()); // 调用凭证,详见上方的接口参数
mapCode.put("out_trade_no", tradeNo); // 商户订单号, 必填
mapCode.put("total_fee", totalFee1 + ""); // 订单金额(数字),单位:分,必填
mapCode.put("face_authtype", "FACEPAY"); // FACEPAY:人脸凭证,常用于人脸支付 FACEPAY_DELAY:延迟支付 必填
mapCode.put("ask_face_permit", "0"); // 展开人脸识别授权项,详情见上方接口参数,必填
mapCode.put("ask_ret_page", "1"); // 是否展示微信支付成功页,可选值:"0",不展示;"1",展示,非必填
WxPayFace.getInstance().getWxpayfaceCode(mapCode , new IWxPayfaceCallback() {
@Override
public void response(final Map info) throws RemoteException {
if (info == null) {
showToast("调用返回为空, 请查看日志");
new RuntimeException("调用返回为空").printStackTrace();
return false;
}
String code = (String) info.get("return_code");
String msg = (String) info.get("return_msg");
String facecode = (String) info.get("face_code");
String openid = (String) info.get("openid");
String subOpenid = (String) info.get("sub_openid");
....
//如果成功调用后台接口进行支付下单
//进行发起订单支付micropay
}
});
四、更新支付结果,结束支付
/*
* 调用后台接口查询订单状态orderquery
*
*/
/**
* 更新微信支付结果
*
*/
HashMap payResultHash = new HashMap();
payResultHash.put("payresult", "");
....
WxPayFace.getInstance().updateWxpayfacePayResult(payResultHash, new IWxPayfaceCallback() {
@Override
public void response(final Map info) throws RemoteException {
if (info == null) {
showToast("调用返回为空, 请查看日志");
new RuntimeException("调用返回为空").printStackTrace();
return false;
}
String code = (String) info.get("return_code");
String msg = (String) info.get("return_msg");
if (TextUtils.equals(returnCode, "USER_QUERY_CANCEL")) {
mResultTxt.setText("请与商户确认支付结果");
} else {
mResultTxt.setText("支付完成");
}
}
});