1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-alipay/ios/Alipay.m

100 lines
3.4 KiB
Mathematica
Raw Normal View History

2020-07-07 17:44:10 +08:00
#import "Alipay.h"
2020-07-07 20:10:43 +08:00
#import <AlipaySDK/AlipaySDK.h>
2020-07-07 17:44:10 +08:00
@implementation Alipay
2020-07-07 20:10:43 +08:00
{
NSString *alipayScheme;
RCTResponseSenderBlock alipayCallBack;
}
2020-07-07 17:44:10 +08:00
RCT_EXPORT_MODULE()
2020-07-07 20:10:43 +08:00
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOpenURL:) name:@"RCTOpenURLNotification" object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)handleOpenURL:(NSNotification *)aNotification
{
NSString * aURLString = [aNotification userInfo][@"url"];
NSURL * aURL = [NSURL URLWithString:aURLString];
if ([aURL.host isEqualToString:@"safepay"]) {
//
[[AlipaySDK defaultService] processOrderWithPaymentResult:aURL standbyCallback:^(NSDictionary *resultDic) {
if (self->alipayCallBack != nil) {
self->alipayCallBack([[NSArray alloc] initWithObjects:resultDic, nil]);
self->alipayCallBack = nil;
}
2020-07-08 22:32:18 +08:00
NSLog(@"result-->1 = %@",resultDic);
2020-07-07 20:10:43 +08:00
}];
//
[[AlipaySDK defaultService] processAuth_V2Result:aURL standbyCallback:^(NSDictionary *resultDic) {
2020-07-08 22:32:18 +08:00
NSLog(@"result-->2 = %@", resultDic);
2020-07-07 20:10:43 +08:00
// auth code
NSString *result = resultDic[@"result"];
NSString *authCode = nil;
if (result.length>0) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:10];
break;
}
}
//
if (self->alipayCallBack != nil) {
self->alipayCallBack([[NSArray alloc] initWithObjects:resultArr, nil]);
self->alipayCallBack = nil;
}
}
NSLog(@"授权结果 authCode = %@", authCode?:@"");
}];
}
return NO;
}
RCT_EXPORT_METHOD(setAlipayScheme:(NSString *)scheme){
alipayScheme = scheme;
}
2020-07-13 22:01:03 +08:00
2020-07-07 20:10:43 +08:00
RCT_EXPORT_METHOD(alipay:(NSString *)info callback:(RCTResponseSenderBlock)callback)
{
alipayCallBack = callback;
dispatch_async(dispatch_get_main_queue(), ^{
2020-07-13 22:18:45 +08:00
[[AlipaySDK defaultService] payOrder:info fromScheme: alipayScheme callback:^(NSDictionary *resultDic) {
2020-07-13 22:01:03 +08:00
callback([[NSArray alloc] initWithObjects:resultDic, nil]);
}];
});
}
RCT_EXPORT_METHOD(authInfo:(NSString *)info callback:(RCTResponseSenderBlock)callback)
{
alipayCallBack = callback;
dispatch_async(dispatch_get_main_queue(), ^{
[[AlipaySDK defaultService] auth_V2WithInfo:info fromScheme:alipayScheme callback:^(NSDictionary *resultDic) {
2020-07-07 20:10:43 +08:00
callback([[NSArray alloc] initWithObjects:resultDic, nil]);
}];
});
}
/*!
* [warn][tid:main][RCTModuleData.mm:68] Module Alipay requires main queue setup since it overrides `init` but doesn't implement `requiresMainQueueSetup`.
* In a future release React Native will default to initializing all native modules on a background thread unless explicitly opted-out of.
*/
+ (BOOL)requiresMainQueueSetup
2020-07-07 17:44:10 +08:00
{
2020-07-07 20:10:43 +08:00
return YES;
2020-07-07 17:44:10 +08:00
}
@end