//
//  pay.m
//  arena
//
//  Created by 张琳 on 2017/10/28.
//  Copyright © 2017年 江苏斑马软件技术有限公司. All rights reserved.
//

#import "pay.h"

@implementation pay

//导出模块
RCT_EXPORT_MODULE(pay);

//导出方法，桥接到js的方法返回值类型必须是void

//支付宝支付
RCT_EXPORT_METHOD(alipay:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.biz.pay.alipay" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    //主要这里必须使用主线程发送,不然有可能失效
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam]; //把收到的RN的参数用通知发送出去
    });
}


//支付宝支付
RCT_EXPORT_METHOD(wxpay:(NSDictionary *)param findEvents:(RCTResponseSenderBlock)callback)
{
    NSLog(@"原生接收到的RN参数:%@",param);
    
    NSMutableDictionary * finalParam = [[NSMutableDictionary alloc]init];
    [finalParam setValue:@"arena.biz.pay.wxpay" forKey:@"method"];
    [finalParam setValue:param forKey:@"params"];
    [finalParam setValue:@"rn" forKey:@"type"];
    
    
    void (^successBlock)(id) = ^(id result){
        callback(@[[NSNull null], result]);
    };
    
    void (^failedBlock)(id) = ^(id result){
        callback(@[result,[NSNull null]]);
    };
    
    NSDictionary *dic = @{@"success":successBlock , @"failed":failedBlock};
    
    //主要这里必须使用主线程发送,不然有可能失效
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dispatch" object:dic userInfo:finalParam]; //把收到的RN的参数用通知发送出去
    });
}

@end

