还真道txt下载,蜜桃成熟时钟真,中兴微电子
前言
支付分app支付、h5支付、扫码支付等。app支付一般在app中使用,并且需要集成相应的支付sdk,h5支付多用于网页。如果你的app不想集成支付sdk,又想实现支付功能,你可以在项目中使用h5支付。本文主要讲述如何将h5支付封装成一个原生可调用的组件。
1.h5支付流程
注:以下为网页h5支付流程,原生调用需要修改部分流程
1.1 微信支付
微信中间页重新向到redirect_url
1.2 支付宝支付
2.原生封装思路
新开一个webview加载支付中间页,拦截中间页支付请求并唤起支付,然后关闭webview流程结束。
webview需要加到window(或者当前控制器的view上),并设置一个大小(肉眼不可见就行)。因为使用wkwebview时,webview不显示的情况下,h5请求会被挂起,会导致支付宝页面不能唤起支付请求。
3.代码实现
具体步骤见代码注释
@interface hjh5webpaymanager()<uiwebviewdelegate> @property (nonatomic,strong) uiwebview *paywebview; @property (nonatomic,strong) void(^sendpayresult)(hjh5sendwebpayresult); @end @implementation hjh5webpaymanager +(instancetype)sharedinstance{ static dispatch_once_t once ; static hjh5webpaymanager *_instace = nil; dispatch_once(&once, ^{ _instace = [[self alloc] init]; }); return _instace; } -(void)loadwebpaytransitionpage:(nsstring *)html handleblock:(void (^)(hjh5sendwebpayresult))handle{ nsmutableurlrequest *request = nil; if ([html hasprefix:@"https://wx.tenpay.com"]) { //微信安全域名 nsstring *wxscheme = @""; nsstring *referer = [nsstring stringwithformat:@"%@://",wxscheme]; //将redirect_url替换成scheme,微信支付完毕才能跳回app,否则会打开safari浏览器(因为redirect_url一般为一个http的地址) nsrange range = [html rangeofstring:@"redirect_url="]; nsstring *requrl; if (range.length>0) { requrl = [html substringtoindex:range.location+range.length]; requrl = [requrl stringbyappendingstring:referer]; }else{ requrl = [html stringbyappendingstring:[nsstring stringwithformat:@"&redirect_url=%@",referer]]; } request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:requrl] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0]; //设置授权域名,伪造referer头,因为微信中间页会检验referer头,并且referer对应的值需要包含安全域名 [request setvalue:referer forhttpheaderfield:@"referer"]; if (self.paywebview) { [self.paywebview removefromsuperview]; self.paywebview = nil; } self.paywebview = [[uiwebview alloc] initwithframe:cgrectmake(0, 0, 0.1, 0.1)]; self.sendpayresult = handle; [[uiapplication sharedapplication].keywindow addsubview:self.paywebview]; self.paywebview.delegate = self; [self.paywebview loadrequest:request]; }else if ([html hasprefix:@"<form"]){ //如果是支付宝,html对应的应该是一段form表单提交脚本,需要调用loadstring方法加载 if (self.paywebview) { [self.paywebview removefromsuperview]; self.paywebview = nil; } self.paywebview = [[uiwebview alloc] initwithframe:cgrectmake(0, 0, 0.1, 0.1)]; self.sendpayresult = handle; [[uiapplication sharedapplication].keywindow addsubview:self.paywebview]; self.paywebview.delegate = self; nsstring *paystr = html; nsstring *htmlstring = [nsstring stringwithformat:@"htmlstring:<html> \n" "<head> \n" "<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" /> \n" "<style type=\"text/css\"> \n" "body {font-size:16px;}\n" "</style> \n" "</head> \n" "<body>" "%@" "</body>" "</html>",paystr]; [self.paywebview loadhtmlstring:htmlstring baseurl:nil]; }else{ //非法html,返回错误 handle(hjh5sendwebpayresultother); return; } //容错处理,20秒没唤起支付,当错误处理。 __weak typeof(self) weakself = self; dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(20 * nsec_per_sec)), dispatch_get_main_queue(), ^{ if (weakself.sendpayresult) { weakself.sendpayresult(hjh5sendwebpayresultother); } [weakself endpayment]; }); } - (void)webview:(uiwebview *)webview didfailloadwitherror:(nserror *)error{ //页面加载失败,返回错误 if (self.sendpayresult) { self.sendpayresult(hjh5sendwebpayresultloadfail); } [self endpayment]; } - (bool)webview:(uiwebview *)webview shouldstartloadwithrequest:(nsurlrequest *)request navigationtype:(uiwebviewnavigationtype)navigationtype{ nsurl *url = request.url; nsstring *newurl = url.absolutestring; //拦截微信支付请求,并打开微信 if([newurl rangeofstring:@"weixin://wap/pay"].location != nsnotfound){ //判断是否能打开微信 if ([[uiapplication sharedapplication] canopenurl:url]) { if (@available(ios 10.0, *)){ [[uiapplication sharedapplication] openurl:url options:@{} completionhandler:nil]; }else{ [[uiapplication sharedapplication] openurl:url]; } if (self.sendpayresult) { self.sendpayresult(hjh5sendwebpayresultsuccess); } [self endpayment]; }else{ if (self.sendpayresult) { self.sendpayresult(hjh5sendwebpayresultsendfail); } [self endpayment]; } return no; }else if([newurl rangeofstring:@"alipay://alipayclient/?"].location != nsnotfound){ //拦截支付宝支付请求,并且替换fromappurlscheme参数为当前app的scheme,实现支付完毕返回app功能。 nsstring *alischeme = @"支付宝支付scheme,支付完毕可通过scheme返回到当前app"; newurl = [hjstringhelper decodeurl:newurl]; nsstring *parameterstring = [newurl stringbyreplacingoccurrencesofstring:@"alipay://alipayclient/?" withstring:@""]; nserror *error = nil; id dict = [nsjsonserialization jsonobjectwithdata:[parameterstring datausingencoding:nsutf8stringencoding] options:nsjsonreadingmutablecontainers error:&error]; if (!error) { if ([dict iskindofclass:[nsmutabledictionary class]]) { dict[@"fromappurlscheme"] = alischeme; nsdata *jsondata = [nsjsonserialization datawithjsonobject:dict options:nsjsonwritingprettyprinted error:&error]; if (!error) { parameterstring = [hjstringhelper escapeurl:[[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]]; nsstring *payurl = [nsstring stringwithformat:@"alipay://alipayclient/?%@",parameterstring]; dispatch_async(dispatch_get_main_queue(), ^{ //判断是否能打开支付宝 if ([[uiapplication sharedapplication] canopenurl:[nsurl urlwithstring:payurl]]) { if (@available(ios 10.0, *)){ [[uiapplication sharedapplication] openurl:[nsurl urlwithstring:payurl] options:@{} completionhandler:nil]; }else{ [[uiapplication sharedapplication] openurl:[nsurl urlwithstring:payurl]]; } if (self.sendpayresult) { self.sendpayresult(hjh5sendwebpayresultsuccess); } [self endpayment]; }else{ if (self.sendpayresult) { self.sendpayresult(hjh5sendwebpayresultsendfail); } [self endpayment]; } }); } } } return no; }else{ return yes; } } -(void)endpayment{ self.sendpayresult = nil; [self.paywebview removefromsuperview]; self.paywebview = nil; } @end
3.1入参说明
调用该方法唤起支付-(void)loadwebpaytransitionpage:(nsstring *)html handleblock:(void (^)(hjh5sendwebpayresult))handle.
其中html为微信中间页地址和支付宝form表单脚本。如:
微信: https://wx.tenpay.com ?xxxx
支付宝:<form id=" alipaysubmit " name="alipaysubmit" action=xxxx></form><script>document.forms[' alipaysubmit '].submit();</script>
见1.h5支付流程,微信下单之后可以获取中间页地址,支付则需要form表单提交加载中间页。
3.2错误处理
typedef ns_enum(nsuinteger,hjh5sendwebpayresult) { hjh5sendwebpayresultsuccess = 0, //唤起登录成功 hjh5sendwebpayresultloadfail, //支付页面加载失败 hjh5sendwebpayresultsendfail, //调起支付失败,可能是没添加未安装微信或者支付宝 hjh5sendwebpayresultother //其他 };
支付请求发送成功则表示这次h5支付发起完成,具体支付结果需要查询后台获得。所以需要对一些异常情况进行处理,比如页面加载失败,微信或支付宝未安装等异常进行处理。
4.说明
这种方案可以统一微信和支付宝h5支付的流程,并且隐式地显示支付中间页,不会影响h5单页面应用的路由。app不需要集成支付sdk,可以绕过苹果扫描代码。
由于支付宝支付流程改成和微信一样,所以支付宝网页支付功能被砍掉,只能通过打开支付宝app去支付。这也是这种方案的不足之处。
到此这篇关于ios实现h5支付(微信、支付宝)原生封装的文章就介绍到这了,更多相关ios h5支付内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
iOS 使用UITextField自定义搜索框 实现用户输入完之后“实时搜索”功能
网友评论