项目里集成微信aar包,将微信刷脸的app安装到有微信专门摄像头的设备上。
微信人脸识别
1,初始化操作:(先在application类中初始化)
private void initWxFacePay() {
WxPayFace.getInstance().initWxpayface(this, new IWxPayfaceCallback() {
@Override
public void response(Map info)throws RemoteException {
if (!WxCallBack.successInfo(getApplicationContext(), info)) {
ToastUtil.show(getApplicationContext(), "微信支付初始化失败");
}
}
});
}
2.获取rawData数据。
private void getWxPayRawData() {
WxPayFace.getInstance().getWxpayfaceRawdata(new IWxPayfaceCallback() {
@Override
public void response(Map info)throws RemoteException {
if (!WxCallBack.successInfo(mContext, info)) {
toast("getWxPayRawData()调用失败");
return;
}
String rawData = (String) info.get(RETURN_RAWDATA);
if (rawData ==null) {
toast("rawData为空");
return;
}
mPresenter.getAuthInfo(devoceId, rawData, storeId, storeName);
}
});
}
3.拿到rawData数据与公司后端进行交互 拿到authorInfo数据,(authorInfo很关键)
后端写个接口,安卓这边把相应的数据传给他。
4.人脸识别
private void doFaceRecognize(String type) {
HashMap params2 =new HashMap();
params2.put(FACE_AUTHTYPE, type);
params2.put(APPID, appId);
params2.put(MCH_ID, machId);
params2.put(STORE_ID, storeId);
params2.put(AUTHINFO, mAuthInfo);
WxPayFace.getInstance().getWxpayfaceUserInfo(params2, new IWxPayfaceCallback() {
@Override
public void response(final Map info)throws RemoteException {
if (!WxCallBack.successInfo(mContext, info)) {
toast("getWxpayfaceUserInfo()调用失败");
return;
}
final String openid = (String) info.get(OPENID);
final String nickname = (String) info.get(NICKNAME);
final String faceCode = (String) info.get(FACE_CODE);
if (!TextUtils.isEmpty(openid)) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent intent =new Intent(mContext, X5WebActivity.class);
intent.putExtra("openid", openid);
intent.putExtra("authInfo", mAuthInfo);
startActivity(intent);
stopWxpayface();
}
});
}
}
});
}
调用微信的getWxpayfaceUserInfo方法会自动打开微信app的摄像头,有两种模式FACEID-ONCE和FACEID-LOOP。ONCE模式是立马调起摄像头,LOOP模式是微信摄像头在后台进行活体检测,当有人在摄像头前,他检测到以后才会调起摄像头。
识别成功后可以拿到faceCode和openId
刷脸支付
1.前面几步与人脸识别前面相同 先初始化,再获取rawData,再拿authInfo,
2.进行支付操作
private void doPay() {
HashMap params =new HashMap();
params.put(APPID, appId);
params.put(MCH_ID, machId);
params.put(STORE_ID, storeId);
params.put(OUT_TRADE_NO, payNum ="zrlcs" + (System.currentTimeMillis() /100000));
params.put(TOTAL_FEE, fee);
params.put(FACE_AUTHTYPE, facePay);
params.put(ASK_FACE_PERMIT, facePermit);
params.put(AUTHINFO, mAuthInfo);
params.put(ASK_RET_PAGE, retPage);
params.put(FACTORY, true);
WxPayFace.getInstance().getWxpayfaceCode(params, new IWxPayfaceCallback() {
@Override
public void response(Map info)throws RemoteException {
if (!WxCallBack.successInfo(mContext, info)) {
toastError(getResources().getString(R.string.pay_error));
return;
}
mFaceCode = (String) info.get(FACE_CODE);
mPresenter.getBackendData(mOpenId, mFaceCode, payNum);
}
});
}
调用微信的getWxpayfaceCode方法会自动跳到微信的刷脸支付页面,刷脸成功后会返回faceCode
此时需要拿着这个faceCode和openId与公司后端进行交互,后端返回一个成功或失败的状态
3,支付成功返回你自己的app
private void backApp() {
final Map params =new HashMap<>();
params.put(APPID, appId);
params.put(MCH_ID, machId);
params.put(STORE_ID, storeId);
params.put(AUTHINFO, mAuthInfo);
params.put(PAY_RESULT, "SUCCESS");
WxPayFace.getInstance().updateWxpayfacePayResult(params, new IWxPayfaceCallback() {
@Override
public void response(Map info)throws RemoteException {
if (!WxCallBack.successInfo(mContext, info)) {
toastError(getResources().getString(R.string.pay_error) +",updateWxpayfacePayResult()调用失败");
}
isPaySuccess =true;
}
});
}
拿到后端状态后(这里直接填入了SUCCESS),调用微信的updateWxpayfacePayResult就可以更新结果并返回自己的页面了。
贴几个用到的工具类
public static boolean successInfo(Context context, Map info) {
if (info ==null) {
// ToastUtil.show(context, "接口返回的info为空");
return false;
}
String code = (String) info.get(RETURN_CODE);
String msg = (String) info.get(RETURN_MSG);
if (code ==null || !code.equals(WxfacePayCommonCode.VAL_RSP_PARAMS_SUCCESS)) {
// ToastUtil.show(context, "调用返回非成功信息" + msg);
return false;
}
return true;
}
网友评论