1,首先注冊芝麻認證開發平台
https://b.zmxy.com.cn/product/productDetail.htm?productId=w1010100000000002978&scene=shop
2,查看芝麻認證文檔中心
https://b.zmxy.com.cn/technology/openDoc.htm?LEFT_MENU_MODE=LEFT_BLANK&id=566
開始認證方式 1:商戶有自己的 App 使用 native 的方式喚起支付寶客戶端(可選)
這個原理是 跳轉至自己手機的支付寶進去認證 不需要調用SDK
A:先判斷手機是否安裝支付寶
B:支付寶需要傳入 真實姓名,身份證號碼,手機號(可選,以需求而定)
C: 首先將param傳給后台服務器,服務器返回一串字符串(如下)
"https://zmopenapi.zmxy.com.cn/openapi.do?charset=UTF-8&method=zhima.customer.certification.certify&sign=oPsjcCksTxwm3Gm0eVSIZ11TZAB69I4bt9xhW6x1Y6qFpuF6Xbmtw1XIW9G2J2ogvlpoIUgsLSpq%2BQjNG7CWbiyMfIj1%2BctCkEFIBWSFN%2Bs30anlfT1GMVfQWJWOtwP%2Fiseid2HWObREOO3DZ9MlKzwJWBHEnX5WLXkvq3M%2F5KA%3D&version=1.0&app_id=300000271&sign_type=RSA&platform=zmop¶ms=T1mDX729nz5ul0kcJ7%2BGt%2FSQwKv4yO%2BbqP8kgHeaAQW80wwP4PvxG27XyP0CondPH7x4Wz1UmH4YfATijBpUEVy6XpnFDoELd00Yr5za4G%2FR08QpaSb1fZYHiQCOEu1CLJC7nqGIQKuNZ0EcJQPd3D5%2FRtv9SrjDFLZb%2But3d%2Fs%3D";
ps:后台生成這段字符串 一定要掉2個接口,否則生成的字符串無法認證
zhima.customer.certification.initialize 認證初始化 服務端調用
zhima.customer.certification.certify 生成認證請求 URL 服務端調用
D: 移動端再拼接固定的url跳轉至支付寶
NSString *alipayUrl = [NSString stringWithFormat:@"alipays://platformapi/startapp?appId=20000067&url=%@",
[self URLEncodedStringWithUrl:url]];(url是后台返回的字符串,必須Encoded)
F:當認證成功/失敗需要跳轉至app
移動端設置:
需要在.info 里面的 URL types -->URL Schemes里面添加一個標識,最好與項目匹配
weilvapp:// (weilvapp這個就是設置在URL Schemes里面的標識)將這句話發給后台服務端,讓他設置在return_url這個字段返回
ps:如果認真成功/失敗無法自動回到app,檢查下URL types -->URL Schemes 不需要后面再拼接什么
E:當認證成功或失敗回到app時,芝麻認證都會返回一個狀態
在appdelagete里面獲取
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
[url absoluteString] 這樣可以拿到返回來的字符串 進行轉字符串
再將里面的params 和 sign進行截取 截取方法如下:然后再將截取后的字符串傳給后台即可
NSArray *strarray = [url componentsSeparatedByString:@"="];
NSString *param = [strarray objectAtIndex:1];
NSArray *paramSrr = [param componentsSeparatedByString:@"&"];
NSString *params =[paramSrr firstObject];
NSString *sign =[strarray lastObject];
NSMutableDictionary *paraDic = @{}.mutableCopy;
[paraDic setObject:params forKey:@"params"];
[paraDic setObject:sign forKey:@"sign"];
代碼如下:
NSString *alipayUrl = [NSString stringWithFormat:@"alipays://platformapi/startapp?appId=20000067&url=%@",
[self URLEncodedStringWithUrl:url]];(url是后台返回的字符串,必須Encoded)
if ([self canOpenAlipay]) {//是否安裝支付寶
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:alipayUrl] options:@{} completionHandler:^(BOOL success) {//跳轉到支付寶芝麻認證
}];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"是否下載並安裝支付寶完成認證?"
delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
[alertView show];
}
//字符串encode
- (NSString *)URLEncodedStringWithUrl:(NSString* )url {
NSString *encodedString = (NSString* )CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)url,NULL,(CFStringRef) @"!'();:@&=+$,%#[]|",kCFStringEncodingUTF8));
return encodedString;
}
//是否安裝支付寶
- (BOOL)canOpenAlipay {//
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alipays://"]];
}
//如果沒有安裝支付寶 就跳轉至蘋果市場下載支付寶
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *appstoreUrl = @"itms-apps://itunes.apple.com/app/id333206289";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appstoreUrl] options:@{} completionHandler:nil];
}else{
}
}