微信青蛙刷臉支付——Android
使用設(shè)備: 天之河刷臉設(shè)備(開放版)
基礎(chǔ)配置
需要用到 微信刷臉支付 SDK
將下載的 微信刷臉支付 SDK 放入工程 libs 下澜建,并添加該依賴
-
在 application 中初始化微信刷臉 SDK
/** * 初始化微信刷臉SDK */ private void initFaceSDK() { WxPayFace.getInstance().initWxpayface(context, new IWxPayfaceCallback() { @Override public void response(Map map) throws RemoteException { Log.d("initWxpayface: " + map.toString()); String returnCode = (String) map.get(RETURN_CODE); String returnMsg = (String) map.get(RETURN_MSG); if (String.valueOf(EnumWxFacePayPublicErrorCode.SYSTEMERROR).equals(returnCode)){ ToastUtils.showShort("系統(tǒng)異常,請重新打開應(yīng)用重試"); }else if (String.valueOf(EnumWxFacePayPublicErrorCode.SUCCESS).equals(returnCode)){ }else { ToastUtils.showShort(returnMsg); } } }); }
刷臉支付
- 調(diào)用 SDK 獲取后臺需要的 Rawdata
WxPayFace.getInstance().getWxpayfaceRawdata(new IWxPayfaceCallback() {
@Override
public void response(Map map) throws RemoteException {
Log.d("getWxpayfaceRawdata: " + map.toString());
if (null == map) {
return;
}
String code = (String) map.get(RETURN_CODE);
String msg = (String) map.get(RETURN_MSG);
String rawdata = map.get(RAWDATA).toString();
if (code == null || rawdata == null || !code.equals(EnumWxFacePayPublicErrorCode.SUCCESS.toString())) {
new RuntimeException("調(diào)用返回非成功信息,return_msg:" + msg + " ").printStackTrace();
return;
}
getAuthInfo(rawdata);//調(diào)用自己后臺接口
}
});
-
調(diào)用自己后臺接口奴迅,獲取驗證信息
private String appid; private String mch_id; private String sub_mch_id; private String store_id; private String store_name; private String device_id; private String auth_info; private String expires_in; private String out_trade_no; private String nonce_str;
-
使用第二布獲取到的驗證信息調(diào)用 wxfacepay_app 的刷臉功能
Map<String, Object> map = new HashMap<>(); map.put("appid", authInfoRsp.getAppid()); map.put("mch_id", authInfoRsp.getMch_id()); map.put("store_id", authInfoRsp.getStore_id()); map.put("out_trade_no", authInfoRsp.getOut_trade_no()); map.put("total_fee", "1"); map.put("face_authtype", EnumFaceAuthType.FACEPAY.toString()); map.put("authinfo", authInfoRsp.getAuth_info()); map.put("ask_face_permit", "0"); map.put("ask_ret_page", "0"); map.put("ignore_update_pay_result", "0"); WxPayFace.getInstance().getWxpayfaceCode(map, new IWxPayfaceCallback() { @Override public void response(Map info) throws RemoteException { Log.d("getWxpayfaceCode: " + info.toString()); if (info == null) { new RuntimeException("調(diào)用返回為空").printStackTrace(); return; } //用戶退出了人臉識別 if (info.get("return_code").equals(String.valueOf(EnumWxFacePayPublicErrorCode.EnumGetFaceCodeErrorCode.USER_CANCEL))) { } //用戶選擇掃碼支付 else if (info.get("return_code").equals(String.valueOf(EnumWxFacePayPublicErrorCode.EnumGetFaceCodeErrorCode.SCAN_PAYMENT))) { startCodeScanner(); } //人臉識別成功 else if (info.get("return_code").equals(String.valueOf(EnumWxFacePayPublicErrorCode.SUCCESS))) { String openid = info.get("openid").toString(); // openid String faceCode = info.get("face_code").toString(); // 人臉憑證材义,用于刷臉支付 facePay(openid, faceCode, authInfoRsp.getOut_trade_no());//調(diào)用自己后臺刷臉支付接口 } } });
-
請求后臺刷臉支付接口 facepay
Map<String, Object> map = new HashMap<>(); map.put("store_id", sotreId); map.put("total", view.getTotalStr()); map.put("openId", openId); map.put("faceCode", faceCode); map.put("out_trade_no", outTradeNo); map.put("nonce_str", nonceStr); String dataAddSaleStr = buildRequestDataForSign(map, loginRsp.getKey()); String sign = MD5Utils.MD5Upper(dataAddSaleStr); RequestBase requestBase = new RequestBase(loginRsp.getAppid(), Constants.WX_FACEPAY, map, sign); homeModel.facePay(requestBase) .subscribe(new BaseObserver<FacePayRsp>() { @Override protected void onSuccees(BaseResponse<FacePayRsp> t) throws Exception { updateWxpayfaceBannerState(true); } @Override protected void onCodeError(BaseResponse<FacePayRsp> t) throws Exception { updateWxpayfaceBannerState(false); } @Override protected void onFailure(String errorMsg, boolean isNetWorkError) throws Exception { updateWxpayfaceBannerState(false); } });
-
得到支付結(jié)果后調(diào)用 SDK 中 updateWxpayfaceBannerState() 關(guān)閉刷臉界面均抽,并處理自己業(yè)務(wù)邏輯
HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("banner_state", 0); // 設(shè)置運營位狀態(tài) 0: 可展示運營圖片 1: 不可展示運營圖片 必填 WxPayFace.getInstance().updateWxpayfaceBannerState(map, new IWxPayfaceCallback() { @Override public void response(Map info) throws RemoteException { if (info == null) { new RuntimeException("調(diào)用返回為空").printStackTrace(); view.facePayResult(false); return; } String code = (String) info.get("return_code"); // 錯誤碼 String msg = (String) info.get("return_msg"); // 錯誤碼描述 if (code == null || !code.equals("SUCCESS")) { new RuntimeException("調(diào)用返回非成功信息,return_msg:" + msg + " ").printStackTrace(); view.facePayResult(false); return; } /* 在這里處理您自己的業(yè)務(wù)邏輯: 執(zhí)行到這里說明用戶已經(jīng)確認(rèn)支付結(jié)果且成功了嫁赏,此時刷臉支付界面關(guān)閉其掂,您可以在這里選擇跳轉(zhuǎn)到其它界面 */ view.facePayResult(isPaySuccess); } });