支付寶開放平臺(tái)的demo地址:
成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、略陽網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、購物商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為略陽等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
我在集成支付寶SDK的時(shí)候遇到兩個(gè)問題:
第一個(gè)問題:rsa.h頭文件找不到
解決辦法:在building settings里搜索Header Search Paths 添加文件所在路徑?$(SRCROOT)/項(xiàng)目名/文件名
第二個(gè)問題:
Undefined symbols for architecture arm64:
"_EVP_DecodeBlock", referenced from:
? _dataFromBase64String in openssl_wrapper.o
"_BIO_free_all", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
? _rsa_verify_with_public_key_pem in openssl_wrapper.o
"_PEM_read_bio_RSA_PUBKEY", referenced from:
? _rsa_verify_with_public_key_pem in openssl_wrapper.o
"_RSA_verify", referenced from:
? _rsa_verify_with_public_key_pem in openssl_wrapper.o
"_BIO_new", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
? _rsa_verify_with_public_key_pem in openssl_wrapper.o
"_EVP_EncodeBlock", referenced from:
? _base64StringFromData in openssl_wrapper.o
"_PEM_read_bio_RSAPrivateKey", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
"_RSA_check_key", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
"_SHA1", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
? _rsa_verify_with_public_key_pem in openssl_wrapper.o
(maybe you meant: __Z24alisec_crypto_SHA1_blockP26alisec_crypto_shastate_st_Pmi)
"_RSA_sign", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
"_BIO_ctrl", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
? _rsa_verify_with_public_key_pem in openssl_wrapper.o
"_BIO_s_file", referenced from:
? _rsa_sign_with_private_key_pem in openssl_wrapper.o
? _rsa_verify_with_public_key_pem in openssl_wrapper.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
這是缺少libcrypto.a和libssl.a文件引起的,這兩個(gè)文件在支付寶的demo中都有,添加進(jìn)項(xiàng)目即可。
首先在對項(xiàng)目開啟支付功能,在項(xiàng)目的manifest.json文件中勾選APP模塊配置中的Payment支付模塊,并且勾選支付寶支付和下面列出的ios和Android
根據(jù)需要在h5跟APP端都做支付寶的喚起支付,喚起orderInfo 的格式在APP和h5上有很大的差異,在這一點(diǎn)上浪費(fèi)了一些時(shí)間,在此貼出支付寶的文檔
支付寶開發(fā)文檔
H5端喚起支付寶。所需要的參數(shù)是后端返回的一個(gè)form表單,獲取成功后端返回的內(nèi)容以后將表單內(nèi)容用v-html置入標(biāo)簽中,然后提交表單便會(huì)執(zhí)行喚起
針對于支付寶支付的使用,截取核心代碼如下
其實(shí)開發(fā)完成以后發(fā)現(xiàn)APP端喚起支付寶更為簡單,但是這個(gè)過程中踩過一個(gè)坑,我以為orderInfo的值可以參照文檔和后端給的一些鍵值對在前端自己拼接,但是總是喚起失敗,后來聯(lián)系支付寶的螞蟻技術(shù)支持中心的客服,經(jīng)過溝通以后,才知道這個(gè)orderInfo必須在后端用支付寶SDK生成以后返回給前端,這樣一來,果然喚起成功。 加粗樣式
利用WKWebView加載網(wǎng)頁,在app中處理H5頁面調(diào)用微信支付 ------ 實(shí)現(xiàn)WKNavigationDelegate協(xié)議
該方法是決定H5頁面是否允許跳轉(zhuǎn)的
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{
WKNavigationActionPolicy? actionPolicy =WKNavigationActionPolicyAllow;//允許
NSString*urlScheme = navigationAction.request.URL.scheme;
NSString*urlString = navigationAction.request.URL.absoluteString;
NSString *wxPre = @"";
NSURLRequest*request = navigationAction.request;
NSLog(@"urlScheme:%@",urlScheme);
NSLog(@"urlStr:%@",urlString);
if([urlString containsString:@"weixin://wap/pay?"]) {
? ? ?actionPolicy =WKNavigationActionPolicyCancel;//不允許跳轉(zhuǎn)
? ? ?NSURL*url = [NSURLURLWithString:urlString];
? ? ? ? ?if([[UIApplicationsharedApplication]respondsToSelector:@selector(openURL:options:completionHandler:)]){
? ? ? ? if(@available(iOS10.0, *)) {
? ? ? ? ? ? [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
? ? ? ? ? ? }];
? ? ? ? }else{
? ? ? ? ? ? // Fallback on earlier versions
? ? ? ? ? ? [[UIApplicationsharedApplication]openURL:url];
? ? ? ? }
? ? }else{
? ? ? ? [[UIApplication sharedApplication]openURL:url];
? ? }
}
decisionHandler(actionPolicy);//這句是必須加上的,不然會(huì)異常
}
很多小伙伴兒都遇到回跳的問題,點(diǎn)擊“完成”或“取消”跳到了Safari卻跳不回APP。
2.1、配置URL Types ??添加schemes:
2.2、配置?Referer
[requestsetValue:@"" forHTTPHeaderField:@"Referer"];
2.3、服務(wù)器回調(diào)的URL中一定要寫成 ? , 如果一定要拼接,可以讓后臺(tái)區(qū)分一下,如果是iOS 前面一定要寫成? ,不然跳不回APP。
注意: 此域名一定是H5授權(quán)的域名,如果是二級(jí)域名,可以寫成:aaa.xxxx.com
一般支付完 不管完成與否都要調(diào)一下回調(diào)地址來刷新支付頁面的,但問題是iOS的回調(diào)地址中一定要寫成? 這樣的格式才能跳回來,但是調(diào)回來后沒有執(zhí)行回調(diào)地址,頁面又刷新不了,或者跳回來后是個(gè)空白頁,這又產(chǎn)生了一個(gè)新的問題。當(dāng)點(diǎn)擊微信支付頁上的“完成”或“取消”返回APP時(shí),會(huì)調(diào)用?AppDelegate 中的?openURL: 方法
-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary *)options{?
// url.scheme 就是?,你只需要在這做一個(gè)判斷,如果是? 你就給外部發(fā)一個(gè)通知,那外部接收到這個(gè)通知,刷新界面就行了。
//刷新地址也就是支付完的回調(diào)地址,后臺(tái)可以把回調(diào)地址拼接在? 后面,例如: 。 那么?url 就是?? ,你向外部發(fā)通知時(shí)把?url 作為參數(shù)傳出去即可?
if ([url.scheme containsString:@""]) {
? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"weChatPayHandle" object:nil userInfo:@{@"url":url}];
}
return YES;
}
//微信支付后刷新主界面 (處理通知的方法 )
-(void)refreshOrderData:(NSNotification*)notifi{
NSDictionary*dic = notifi.userInfo;
NSString*url = [NSStringstringWithFormat:@"%@",dic[@"url"]];
NSRange range = [url rangeOfString:@""];
NSString*lastUrl = [urlsubstringFromIndex:range.length];
NSLog(@"%@",lastUrl);
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:lastUrl]]];
}
解決h5的支付寶支付問題 可以提供兩種解決辦法
第一種 調(diào)用支付寶網(wǎng)頁支付?
(問題 支付成功后回調(diào)到了游覽器,無法返回APP,解決如下)
//以WKWebView為例 ?
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
WKNavigationActionPolicy? actionPolicy =WKNavigationActionPolicyAllow;//允許
NSString*urlScheme = navigationAction.request.URL.scheme;
NSString*urlString = navigationAction.request.URL.absoluteString;
NSString *wxPre = @"";
NSURLRequest*request = navigationAction.request;
NSLog(@"urlScheme:%@",urlScheme);
NSLog(@"urlStr:%@",urlString);
//? 先判斷一下,找到需要跳轉(zhuǎn)的再做處理
if([urlSchemeisEqualToString:@"alipay"]) {
? ? //? 1.以?號(hào)來切割字符串
? ? NSArray* urlBaseArr = [urlStringcomponentsSeparatedByString:@"?"];
? ? NSString* urlBaseStr = urlBaseArr.firstObject;
? ? NSString* urlNeedDecode = urlBaseArr.lastObject;
? ? //? 2.將截取以后的Str,做一下URLDecode,方便我們處理數(shù)據(jù)
? //? NSMutableString * afterDecodeStr = [NSMutableString stringWithString:[self URLDecodedString:urlNeedDecode]];
? ? NSString*afterDecodeStr =WebURLDecodedString(urlNeedDecode);
? ? //? 3.替換里面的默認(rèn)Scheme為自己的Scheme
? ? NSString* afterHandleStr = [afterDecodeStrstringByReplacingOccurrencesOfString:@"alipays"withString:@"QEQuickEnjoyment"];
? //? 4.然后把處理后的,和最開始切割的做下拼接,就得到了最終的字符串
? ? NSString* finalStr = [NSStringstringWithFormat:@"%@?%@",urlBaseStr,WebURLEncode(afterHandleStr)];
? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? //? 判斷一下,是否安裝了支付寶APP(也就是看看能不能打開這個(gè)URL)
? ? ? ? if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:finalStr]]) {
? ? ? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:finalStr]];
? ? ? ? }
? ? });
? ? //? 2.這里告訴頁面不走了 -_-
? ? decisionHandler(WKNavigationActionPolicyCancel);
? ? return;
}
decisionHandler(actionPolicy);//這句是必須加上的,不然會(huì)異常
}
// OC 做URLEncode的方法
staticinlineNSString* WebURLEncode(NSString* str){
NSString*charactersToEscape =@"#[]@!$'()*+,;\"%{}|^~`";
NSCharacterSet*allowedCharacters = [[NSCharacterSetcharacterSetWithCharactersInString:charactersToEscape]invertedSet];
NSString *encodedUrl = [[str description] stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
return encodedUrl; }
staticinlineNSString*WebURLDecodedString(NSString* str)
{ ?return [str stringByRemovingPercentEncoding]; }
這樣h5處理支付寶就可以了
但是如果想在支付寶成功后app處理事件 處理如下:
回到Appdelegate的代理方法
-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary *)options{// 判斷一下這個(gè)host,safepay就是支付寶的if([url.host isEqualToString:@"safepay"]) {// 拿到這個(gè)鏈接的數(shù)據(jù),數(shù)據(jù)大概是這樣的/*
? ? mushRoomBox://safepay/?%7B%22memo%22:%7B%22result%22:%22%7B%5C%22alipay_trade_wap_pay_response%5C%22:%7B%5C%22code%5C%22:%5C%2210000%5C%22,%5C%22msg%5C%22:%5C%22Success%5C%22,%5C%22app_id%5C%22:%5C%222017020505522491%5C%22,%5C%22auth_app_id%5C%22:%5C%222017020505522491%5C%22,%5C%22charset%5C%22:%5C%22GBK%5C%22,%5C%22timestamp%5C%22:%5C%222018-11-09%2011:43:06%5C%22,%5C%22out_trade_no%5C%22:%5C%222018100001_simple_48f033a4a00ba19a0526ddbd4f96b036%5C%22,%5C%22total_amount%5C%22:%5C%220.01%5C%22,%5C%22trade_no%5C%22:%5C%222018110922001438371006963978%5C%22,%5C%22seller_id%5C%22:%5C%222088221175674834%5C%22%7D,%5C%22sign%5C%22:%5C%22T6jn7QSsvjjG%5C/HxknSl2lOdHj6zf7jebWEKkeGhEJaXCYFbRHMTsml7TNRhRACupmwDYUADQOynKNR%5C/QBXHgBxJRhYEE9SXVz4mp7sqSGPItzV4bVmZw5AG5UJzsuIh9O6QC%5C/QHc+FcA2i1zw46VDMUnbCCKat5q4ME9d3AUSMhYg31xO+1TZnraDfm0I%5C/OwAnA5iCTOhq%5C/fkm%5C/r4hsujdv9fTvmszgIJLMhN7yQhoGaOWs0vIQ6hoJoXmCXVapfY7ANsoKaR6hGJogH4GKGQi0vLk17W8s+LjcOUT%5C/zB6K6gN+fyCefm8FOJ+8g4GUAreuZDcW0MuGE1OOmaxDrXg==%5C%22,%5C%22sign_type%5C%22:%5C%22RSA2%5C%22%7D%22,%22ResultStatus%22:%229000%22,%22memo%22:%22%22%7D,%22requestType%22:%22safepay%22%7D
? ? *///? 然后,一樣的套路方法
NSString* urlNeedJsonStr = url.absoluteString;
NSArray* afterComStr = [urlNeedJsonStr componentsSeparatedByString:@"?"];/
/? 這個(gè)decode方法,在上面找哈NSString* lastStr = [self WebURLDecodedString:afterComStr.lastObject];//? 這個(gè)lastStr,其實(shí)是一個(gè)jsonStr,轉(zhuǎn)一下,就看到了數(shù)據(jù)
NSDictionary* dict = [self dictionaryWithJsonString:lastStr];//? dict的結(jié)構(gòu)差不多是這樣/*
? ? ? "memo": {
? ? ? ? ? ? ? "result":"訂單相關(guān)信息,如訂單號(hào),支付金額等等";
? ? ? ? ? ? ? "ResultStatus":"9000";
? ? ? },
? ? ? ******
? ? *///? 和支付寶SDK的返回結(jié)果一次,這個(gè)ResultStatus,就是我們要的數(shù)據(jù)//? 9000 :支付成功//? 8000 :訂單處理中//? 4000 :訂單支付失敗//? 6001 :用戶中途取消//? 6002 :網(wǎng)絡(luò)連接出錯(cuò)//? 這里的話,就可以根據(jù)狀態(tài),去處理自己的業(yè)務(wù)了}returnYES;}
-? (NSDictionary*)dictionaryWithJsonString:(NSString*)jsonString{
if(jsonString ==nil) {
return nil;?
}
NSData*jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError*err;NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:jsonData? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options:NSJSONReadingMutableContainerserror:err];
if(err)? ?
{NSLog(@"json解析失?。?@",err);
return nil;? ??
}return dic;
}
接支付寶提供的SDK,schemeStr參數(shù)傳入自己APP的URLScheme即可。 具體的? 手機(jī)網(wǎng)站支付轉(zhuǎn)Native支付
1.1、掉起支付寶APP
1.2、支付完成后,支付寶APP要返回到我們的APP里面來
- (void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{
BOOL isIntercepted = [[AlipaySDK defaultService] payInterceptorWithUrl:[navigationAction.request.URL absoluteString] fromScheme:@"WRWarehouseHasRice" callback:^(NSDictionary *result) {
? ? // 處理支付結(jié)果
? ? NSLog(@"%@", result);
? ? // isProcessUrlPay 代表 支付寶已經(jīng)處理該URL
? ? if([result[@"isProcessUrlPay"]boolValue]) {
? ? ? ? // returnUrl 代表 第三方App需要跳轉(zhuǎn)的成功頁URL
? ? ? ? NSString*urlStr = result[@"returnUrl"];
? ? }
}];
if(isIntercepted) {
? ? ? ? ?decisionHandler(WKNavigationActionPolicyCancel);?
?return;?
}
? ? ? ?decisionHandler(WKNavigationActionPolicyAllow);
}