//
// YOYOAlipayWXManage.m
// YOYOLive
//
// Created by 宋文龙 on 2020/5/8.
// Copyright © 2020 twksky. All rights reserved.
//
#import "YOYOAlipayWXManage.h"
#import "YoYo乐园-Swift.h"
@interface YOYOAlipayWXManage ()<WKNavigationDelegate>
@property (nonatomic ,strong) WKWebView *webView;
@end
@implementation YOYOAlipayWXManage
+ (instancetype)shareInstance {
static YOYOAlipayWXManage *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[YOYOAlipayWXManage alloc] init];
[manager setupWebView];
});
return manager;
}
- (void)handleWeixinWithUrl:(NSURL *)url {
//h5微信回调无法得知支付成功还是失败,直接当成功,刷新页面金额
[[NSNotificationCenter defaultCenter] postNotificationName:@"kPaySuccessNotification" object:nil];
}
- (void)handleAliPayWithUrl:(NSURL *)url {
NSString * urlNeedJsonStr = url.absoluteString;
NSArray * afterComStr = [urlNeedJsonStr componentsSeparatedByString:@"?"];
// decode
NSString * lastStr = [self URLDecodedString:afterComStr.lastObject];
// 这个lastStr,其实是一个jsonStr,转一下,就看到了数据
NSDictionary * dict = [self dictionaryWithJsonString:lastStr];
if ([dict[@"memo"][@"ResultStatus"] isEqual:@"9000"]) { // 支付成功
[self.webView removeFromSuperview];
[[NSNotificationCenter defaultCenter] postNotificationName:@"kPaySuccessNotification" object:nil];
}
// dict的结构差不多是这样
/*
"memo": {
"result":"订单相关信息,如订单号,支付金额等等";
"ResultStatus":"9000";
},
******
*/
// 和支付宝SDK的返回结果一次,这个ResultStatus,就是我们要的数据
// 9000 :支付成功
// 8000 :订单处理中
// 4000 :订单支付失败
// 6001 :用户中途取消
// 6002 :网络连接出错
}
- (void)setupWebView {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.preferences = [[WKPreferences alloc] init];
config.preferences.javaScriptEnabled=YES;
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
self.webView.navigationDelegate = self;
}
- (void)alertAlipayWX {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
return ;
}];
UIAlertAction *weChatAction = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self weChatPay];
}];
UIAlertAction *aliPayAction = [UIAlertAction actionWithTitle:@"支付宝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self alipay];
}];
[alertVC addAction:weChatAction];
[alertVC addAction:aliPayAction];
[alertVC addAction:cancelAction];
[Global_APP_CurrentViewController presentViewController:alertVC animated:YES completion:nil];
}
- (void)weChatPay {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
if (_isWebVip) {
params[@"catalogCode"] = _webVipArray.firstObject;
} else {
YOChargeDetailModel *model = self.modelArray[_selectIndex];
params[@"catalogCode"] = model.catalogCode;
}
[YOHttpManager OC_POSTWithUrl:OCRequest.Request_Charge_OC params:params success:^(YOHttpModel * _Nonnull model) {
if ([model.code isEqualToString:@"S_OK"]) {
NSString *urlStr = model.data[@"url"];
// 1.以= 号来切割字符串
NSArray * urlBaseArr = [urlStr componentsSeparatedByString:@"="];
NSString * urlBaseStr = urlBaseArr.firstObject;
NSString * urlNeedDecode = urlBaseArr.lastObject;
// 2.将截取以后的Str,做一下URLDecode,方便我们处理数据
NSMutableString * afterDecodeStr = [NSMutableString stringWithString:[self URLDecodedString:urlNeedDecode]];
// 3.添加redirect_url
[afterDecodeStr appendString:@"&redirect_url=www.shuyuapp.com://"];
// 4.URLCode
NSString *urlCodeStr = [self URLEncodeString:afterDecodeStr];
// 最终合并url
NSString *lastUrlStr = [NSString stringWithFormat:@"%@=%@", urlBaseStr ,urlCodeStr];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:lastUrlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[self.webView loadRequest:request];
// [Global_APP_CurrentViewController.view addSubview:self.webView];
}else{
[Global_APP_CurrentViewController showMessage:model.message];
}
} failure:^(NSInteger jrCode, NSString * _Nonnull msg) {
[Global_APP_CurrentViewController showMessage:msg];
}];
}
- (void)alipay {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
if (_isWebVip) {
params[@"catalogCode"] = _webVipArray.lastObject;
} else {
YOChargeDetailModel *model = self.modelArray[_selectIndex+5];
params[@"catalogCode"] = model.catalogCode;
}
[YOHttpManager OC_POSTWithUrl:OCRequest.Request_Charge_OC params:params success:^(YOHttpModel * _Nonnull model) {
if ([model.code isEqualToString:@"S_OK"]) {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.data[@"url"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[self.webView loadRequest:request];
// [Global_APP_CurrentViewController.view addSubview:self.webView];
}else{
[Global_APP_CurrentViewController showMessage:model.message];
}
} failure:^(NSInteger jrCode, NSString * _Nonnull msg) {
[Global_APP_CurrentViewController showMessage:msg];
}];
}
#pragma mark - WKNavigation Delegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// 先判断一下,找到需要跳转的再做处理,支付宝
if ([navigationAction.request.URL.scheme isEqualToString:@"alipay"]) {
decisionHandler(WKNavigationActionPolicyCancel);
// 1.以?号来切割字符串
NSArray * urlBaseArr = [navigationAction.request.URL.absoluteString componentsSeparatedByString:@"?"];
NSString * urlBaseStr = urlBaseArr.firstObject;
NSString * urlNeedDecode = urlBaseArr.lastObject;
// 2.将截取以后的Str,做一下URLDecode,方便我们处理数据
NSMutableString * afterDecodeStr = [NSMutableString stringWithString:[self URLDecodedString:urlNeedDecode]];
// 3.替换里面的默认Scheme为自己的Scheme
NSString * afterHandleStr = [afterDecodeStr stringByReplacingOccurrencesOfString:@"alipays" withString:@"yoyoAlipay"];
// 4.然后把处理后的,和最开始切割的做下拼接,就得到了最终的字符串
NSString * finalStr = [NSString stringWithFormat:@"%@?%@",urlBaseStr, [self URLEncodeString: afterHandleStr]];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:finalStr]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:finalStr]];
} else {
[Global_APP_CurrentViewController showMessage:@"未安装支付宝,无法支付"];
}
return;
}
// 微信
if ([navigationAction.request.URL.scheme isEqualToString:@"weixin"]) {
decisionHandler(WKNavigationActionPolicyCancel);
if ([[UIApplication sharedApplication] canOpenURL:navigationAction.request.URL]) {
[[UIApplication sharedApplication] openURL:navigationAction.request.URL];
} else {
[Global_APP_CurrentViewController showMessage:@"未安装微信,无法支付"];
}
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
// OC 做URLEncode的方法
- (NSString *)URLEncodeString:(NSString*)str {
NSString *unencodedString = str;
NSString *encodedString = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedString;
}
- (NSString*)URLDecodedString:(NSString*)str {
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err)
{
NSLog(@"json解析失败:%@",err);
return nil;
}
return dic;
}
@end
iOS支付宝微信h5支付
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...