//
//  AliPay.m
//  JZPlus_RN
//
//  Created by 丁乐 on 2020/6/15.
//

#import "AliPay.h"
#import <AlipaySDK/AlipaySDK.h>

@interface AliPay()

@property (nonatomic,strong)NSDictionary *data;

//  成功的回调
@property (nonatomic,copy)RCTPromiseResolveBlock success;

//  失败的回调
@property (nonatomic,copy)RCTPromiseRejectBlock faild;

@end

@implementation AliPay


static AliPay *instance = nil;

+(instancetype)shareInstance{
    @synchronized(self) {
        if (instance == nil) {
            instance = [[AliPay alloc]init];
        }
    }
    return instance;
}



+(BOOL)applicationOpenUrl:(NSURL *)url{
    
    if ([url.host isEqualToString:@"safepay"]) {
        
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDict) {
            
            NSInteger resultStatus = [resultDict[@"resultStatus"] integerValue];
            if (resultStatus == 9000) {
                [AliPay shareInstance].success(resultDict);
            }else{
                NSString *msg = resultDict[@"memo"];
                [AliPay shareInstance].faild(msg, msg, nil);
            }
        }];
    }
    return YES;
}


-(void)alipay:(NSDictionary *)data success:(RCTPromiseResolveBlock)success faild:(RCTPromiseRejectBlock)faild{
    
    self.data = data;
    self.success = success;
    self.faild = faild;
    
    // 将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = [data objectForKey:@"payInfo"];
    NSString *appScheme = @"alipay";
    
    if (orderString == nil || appScheme == nil) {
        self.faild(@"缺少参数", @"缺少参数", nil);
        return;
    }
    
    //日志输出
    
    [[AlipaySDK defaultService] payOrder:orderString
                              fromScheme:appScheme
                                callback:^(NSDictionary *resultDict) {
                                    
                                    NSInteger resultStatus = [resultDict[@"resultStatus"] integerValue];
                                    if (resultStatus == 9000) {
                                        success(resultDict);
                                    }else{
                                        NSString *msg = resultDict[@"memo"];
                                        faild(msg, msg, nil);
                                    }
                                }
     ];
}


@end
