支付宝开放平台的demo地址:
成都创新互联公司专注于企业成都营销网站建设、网站重做改版、略阳网站定制设计、自适应品牌网站建设、H5技术、购物商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为略阳等各大城市提供网站开发制作服务。
我在集成支付宝SDK的时候遇到两个问题:
第一个问题:rsa.h头文件找不到
解决办法:在building settings里搜索Header Search Paths 添加文件所在路径 $(SRCROOT)/项目名/文件名
第二个问题:
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文件引起的,这两个文件在支付宝的demo中都有,添加进项目即可。
首先在对项目开启支付功能,在项目的manifest.json文件中勾选APP模块配置中的Payment支付模块,并且勾选支付宝支付和下面列出的ios和Android
根据需要在h5跟APP端都做支付宝的唤起支付,唤起orderInfo 的格式在APP和h5上有很大的差异,在这一点上浪费了一些时间,在此贴出支付宝的文档
支付宝开发文档
H5端唤起支付宝。所需要的参数是后端返回的一个form表单,获取成功后端返回的内容以后将表单内容用v-html置入标签中,然后提交表单便会执行唤起
针对于支付宝支付的使用,截取核心代码如下
其实开发完成以后发现APP端唤起支付宝更为简单,但是这个过程中踩过一个坑,我以为orderInfo的值可以参照文档和后端给的一些键值对在前端自己拼接,但是总是唤起失败,后来联系支付宝的蚂蚁技术支持中心的客服,经过沟通以后,才知道这个orderInfo必须在后端用支付宝SDK生成以后返回给前端,这样一来,果然唤起成功。 加粗样式
利用WKWebView加载网页,在app中处理H5页面调用微信支付 ------ 实现WKNavigationDelegate协议
该方法是决定H5页面是否允许跳转的
- (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;//不允许跳转
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);//这句是必须加上的,不然会异常
}
很多小伙伴儿都遇到回跳的问题,点击“完成”或“取消”跳到了Safari却跳不回APP。
2.1、配置URL Types 添加schemes:
2.2、配置 Referer
[requestsetValue:@"" forHTTPHeaderField:@"Referer"];
2.3、服务器回调的URL中一定要写成 , 如果一定要拼接,可以让后台区分一下,如果是iOS 前面一定要写成 ,不然跳不回APP。
注意: 此域名一定是H5授权的域名,如果是二级域名,可以写成:aaa.xxxx.com
一般支付完 不管完成与否都要调一下回调地址来刷新支付页面的,但问题是iOS的回调地址中一定要写成 这样的格式才能跳回来,但是调回来后没有执行回调地址,页面又刷新不了,或者跳回来后是个空白页,这又产生了一个新的问题。当点击微信支付页上的“完成”或“取消”返回APP时,会调用 AppDelegate 中的 openURL: 方法
-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary *)options{
// url.scheme 就是 ,你只需要在这做一个判断,如果是 你就给外部发一个通知,那外部接收到这个通知,刷新界面就行了。
//刷新地址也就是支付完的回调地址,后台可以把回调地址拼接在 后面,例如: 。 那么 url 就是 ,你向外部发通知时把 url 作为参数传出去即可
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的支付宝支付问题 可以提供两种解决办法
第一种 调用支付宝网页支付
(问题 支付成功后回调到了游览器,无法返回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);
// 先判断一下,找到需要跳转的再做处理
if([urlSchemeisEqualToString:@"alipay"]) {
// 1.以?号来切割字符串
NSArray* urlBaseArr = [urlStringcomponentsSeparatedByString:@"?"];
NSString* urlBaseStr = urlBaseArr.firstObject;
NSString* urlNeedDecode = urlBaseArr.lastObject;
// 2.将截取以后的Str,做一下URLDecode,方便我们处理数据
// NSMutableString * afterDecodeStr = [NSMutableString stringWithString:[self URLDecodedString:urlNeedDecode]];
NSString*afterDecodeStr =WebURLDecodedString(urlNeedDecode);
// 3.替换里面的默认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(也就是看看能不能打开这个URL)
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:finalStr]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:finalStr]];
}
});
// 2.这里告诉页面不走了 -_-
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(actionPolicy);//这句是必须加上的,不然会异常
}
// 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{// 判断一下这个host,safepay就是支付宝的if([url.host isEqualToString:@"safepay"]) {// 拿到这个链接的数据,数据大概是这样的/*
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:@"?"];/
/ 这个decode方法,在上面找哈NSString* lastStr = [self WebURLDecodedString:afterComStr.lastObject];// 这个lastStr,其实是一个jsonStr,转一下,就看到了数据
NSDictionary* dict = [self dictionaryWithJsonString:lastStr];// dict的结构差不多是这样/*
"memo": {
"result":"订单相关信息,如订单号,支付金额等等";
"ResultStatus":"9000";
},
******
*/// 和支付宝SDK的返回结果一次,这个ResultStatus,就是我们要的数据// 9000 :支付成功// 8000 :订单处理中// 4000 :订单支付失败// 6001 :用户中途取消// 6002 :网络连接出错// 这里的话,就可以根据状态,去处理自己的业务了}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参数传入自己APP的URLScheme即可。 具体的 手机网站支付转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) {
// 处理支付结果
NSLog(@"%@", result);
// isProcessUrlPay 代表 支付宝已经处理该URL
if([result[@"isProcessUrlPay"]boolValue]) {
// returnUrl 代表 第三方App需要跳转的成功页URL
NSString*urlStr = result[@"returnUrl"];
}
}];
if(isIntercepted) {
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款