//
//  OJAPayAliService.m
//  HiSports
//
//  Created by luxiaoming on 16/10/15.
//  Copyright © 2016年 com.ouj. All rights reserved.
//

#import "OJAPayAliService.h"
#import <AlipaySDK/AlipaySDK.h>
#import "OJAPayManager.h"


@interface OJAPayAliService ()

@property (nonatomic, copy) OJAPayCompletion payCompletionBlock;

@end

@implementation OJAPayAliService

- (BOOL)handleOpenUrl:(NSURL *)url {
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
//        NSLog(@"processOrderWithPaymentResult url:%@ resultDict:%@", url, resultDic);
        [self handleAliPayResult:resultDic];
    }];
    return YES;
}

- (void)payForPreOrder:(OJAPayPreOrderModel *)preOrderModel completion:(OJAPayCompletion)completion {
    self.payCompletionBlock = completion;
    [[AlipaySDK defaultService] payOrder:preOrderModel.orderString fromScheme:[OJAPayManager defaultManager].kAliAppScheme callback:^(NSDictionary *resultDic) {
        [self handleAliPayResult:resultDic];
    }];
}

- (void)handleAliPayResult:(NSDictionary *)resultDict {
//    NSLog(@"resultDict is %@", resultDict);
    NSInteger resultStatus = [resultDict[@"resultStatus"] integerValue];
    if (resultStatus == 9000) {
        if (self.payCompletionBlock) {
            self.payCompletionBlock(OJAPayTypeAli, nil);
        }
    } else {
        if (self.payCompletionBlock) {
            self.payCompletionBlock(OJAPayTypeAli, [resultDict description]);
        }
    }
}


@end
