SDKInterface.m 9.86 KB
#include "SDKInterface.h"
#import <UIKit/UIKit.h>

#import "HaiBaoJuheSDK/EEEE_CalendarAllManager_FFFF.h"
#import "HaiBaoJuheSDK/UUUU_CalendarHuiTiaoNotification_VVVV.h"
#import "HaiBaoJuheSDK/IIII_CalendarInformation_JJJJ.h"

@implementation SDKInterface : Interface

int mExchange;
NSString *CalendarAppId;
NSString *CalendarAppKey;
NSString *CalendarLeakOrderUrl;

- (void)SDKinit:(UIViewController *) control
{

    [super SDKinit:control];
    
    [AAAA_HaiBaoJuheConfig_BBBB CCCC_getInstance_DDDD];
    [[EEEE_CalendarAllManager_FFFF GGGG_CalendarInstance_HHHH]IIII_CalendarChuShiHua_JJJJ];
    [[EEEE_CalendarAllManager_FFFF GGGG_CalendarInstance_HHHH]KKKK_CalendarYangShi_LLLL:@"sdk"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveLoginMessage:) name:AAAA_CalendarLoginNotification_BBBB object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HCKAssetsToolCallBack:) name:@"CalendarToolCallBack" object:nil];
    
    [[OOOO_HaiBaoJuheChecker_PPPP QQQQ_getInstance_RRRR] sendNotice:HaiBaoJuheSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:HaiBaoJuheSDK_NO_ERROR description:@"initSuccess" extendData:nil];
    
    NSString* channelId = [[AAAA_HaiBaoJuheConfig_BBBB CCCC_getInstance_DDDD] EEEE_getValueByKey_FFFF:@"gamechannelid"];
    if(channelId == nil || [channelId length] <= 0){
        channelId = @"unknown";
    }
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    //APPID APPKEY
    CalendarAppId = [[AAAA_HaiBaoJuheConfig_BBBB CCCC_getInstance_DDDD] EEEE_getValueByKey_FFFF:@"APP_ID"];
    CalendarAppKey =[[AAAA_HaiBaoJuheConfig_BBBB CCCC_getInstance_DDDD] EEEE_getValueByKey_FFFF:@"APP_KEY"];
    [[EEEE_CalendarAllManager_FFFF GGGG_CalendarInstance_HHHH]SSSS_CalendarDengLu_TTTT:CalendarAppId :CalendarAppKey :self.controller];
    
}

/**
 loginCheck
 */
- (void)receiveLoginMessage:(NSNotification *)notification
{
    NSLog(@"loginchek");
    HaiBaoJuheLoginInfo *loginInfo = [HaiBaoJuheLoginInfo alloc];
    loginInfo.openId = [[IIII_CalendarInformation_JJJJ getInstance]openId];
    loginInfo.sign = [[IIII_CalendarInformation_JJJJ getInstance] token];
    loginInfo.timestamp = [[IIII_CalendarInformation_JJJJ getInstance] timestamp];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[OOOO_HaiBaoJuheChecker_PPPP QQQQ_getInstance_RRRR] startCheck:loginInfo];
    });
    
}


-(NSString*)DataTojsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    jsonString = [jsonData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return jsonString;
}

/**
 苹果支付结果回调
 */

- (void)HCKAssetsToolCallBack:(NSNotification *)notification
{
    NSDictionary* payResultDic = notification.object;
    NSString* receiptStr = [payResultDic objectForKey:@"CalendarToolResult"];
    NSString* queryId = [payResultDic objectForKey:@"query_id"];
    
    //收到回调,保存queryid和data
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray* queryIdArray = [userDefaults objectForKey:@"queryIds"];
    NSMutableArray* tempQueryIdArray = [[NSMutableArray alloc] initWithCapacity:10];
    if (queryIdArray) {
    }else{
        queryIdArray = [[NSMutableArray alloc]initWithCapacity:10];
    }
    for (NSString* arrayQueryId in queryIdArray) {
        [tempQueryIdArray addObject:arrayQueryId];
    }
    [tempQueryIdArray addObject:queryId];
    [userDefaults setObject:tempQueryIdArray forKey:@"queryIds"];
    [userDefaults setObject:receiptStr forKey:queryId];
    
    NSDictionary* receiptDic = [NSDictionary dictionaryWithObjectsAndKeys:receiptStr,@"receipt",queryId,@"query_id", nil];
    NSString* result = [[NetCenter getInstance] httpPostSyn:CalendarLeakOrderUrl :receiptDic];
    
    if([@"success" isEqualToString:result]){//确认订单成功
        [[OOOO_HaiBaoJuheChecker_PPPP QQQQ_getInstance_RRRR] sendNotice:HaiBaoJuheSDKNotificationGzyazf notiName:@"success" statusCode:HaiBaoJuheSDK_NO_ERROR description:@"success" extendData:nil];
        //删除已保存的
        [userDefaults removeObjectForKey:queryId];
        [tempQueryIdArray removeObject:queryId];
        [userDefaults setObject:tempQueryIdArray forKey:@"queryIds"];
        [userDefaults removeObjectForKey:@"lastqueryid"];
    }
        [userDefaults synchronize];
}

/**
 支付 创建订单
 */
- (void)SDKgzyazfWithGzyazfmentInfo:(HaiBaoJuheCreateOrderInfo *)payInfo
{
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSString* lastqueryid = [userDefaults objectForKey:@"lastqueryid"];
    
    CalendarLeakOrderUrl = [[NSString alloc] initWithFormat:@"%@?uid=%@&playerid=%@",
                                  [MMMM_HaiBaoJuheUtils_NNNN AAAA_createGzyazfUrl_BBBB],
                                  [payInfo userId],
                                  [payInfo playerId]];
    [userDefaults setObject:CalendarLeakOrderUrl forKey:@"lastCalendarLeakOrderUrl"];
    
    if (lastqueryid!=nil) {
        //创建订单前进行补单
        NSData* receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
        NSString* receiptStr = [receiptData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        if (receiptStr!=nil) {
            receiptStr = [receiptStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            //如果不为空,进行最后一次补单
            NSDictionary* receiptDic = [NSDictionary dictionaryWithObjectsAndKeys:receiptStr,@"receipt",lastqueryid,@"query_id", nil];
            NSString* result = [[NetCenter getInstance] httpPostSyn:CalendarLeakOrderUrl :receiptDic];
            if([@"success" isEqualToString:result]){//确认订单成功
                //删除已保存的
                [userDefaults removeObjectForKey:@"lastqueryid"];
            }
        }
    }
    
    mExchange = [payInfo exchange].intValue;
    [super SDKgzyazfWithGzyazfmentInfo:payInfo];
    CalendarLeakOrderUrl = [[NSString alloc] initWithFormat:@"%@?uid=%@&playerid=%@",
                                  [MMMM_HaiBaoJuheUtils_NNNN AAAA_createGzyazfUrl_BBBB],
                                  [payInfo userId],
                                  [payInfo playerId]];
    [userDefaults synchronize];
}

/**
 SDK
 */
- (void)SDKgotoUserCenter
{
}

- (void)SDKreport:(HaiBaoJuheReportInfo *)reportInfo
{
    NSLog(@"reportInfo");
    NSString* reportType = [reportInfo reportType];
    if (reportInfo) {
        if ([reportType isEqualToString:REPORT_ENTER]) {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                [self handlerLeakOrder];
            });
        }else if ([reportType isEqualToString:REPORT_CREATEROLE]){
        }
    }
}

/**
 SDK
 */
- (void)SDKexitSDK
{
    
}

- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
}

- (void)handleOpenurl:(NSURL *)url
{
}

- (void)receivePayMessage : (NSNotification *)notification;
{
    NSString *order_id = [notification object];
    NSLog(@"订单ID:%@", order_id);
}

//补单处理
- (void) handlerLeakOrder{
    NSUserDefaults* userDefaults2 = [NSUserDefaults standardUserDefaults];
    NSMutableArray* queryIdArray = [userDefaults2 objectForKey:@"queryIds"];
    
    NSLog(@"orderDic%@",queryIdArray);
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray* failQueryIdArray = [[NSMutableArray alloc]initWithCapacity:10];
    
    if(queryIdArray){
        for (NSString* queryId in queryIdArray) {
            NSString* receiptStr = [userDefaults valueForKey:queryId];
            NSDictionary* receiptDic = [NSDictionary dictionaryWithObjectsAndKeys:receiptStr,@"receipt",queryId,@"query_id", nil];
            NSString* lastCalendarLeakOrderUrl = [userDefaults objectForKey:@"lastCalendarLeakOrderUrl"];
            NSString* result;
            if (lastCalendarLeakOrderUrl!=nil) {
                 result = [[NetCenter getInstance] httpPostSyn:lastCalendarLeakOrderUrl :receiptDic];
            }else{
                 result = [[NetCenter getInstance] httpPostSyn:[MMMM_HaiBaoJuheUtils_NNNN AAAA_createGzyazfUrl_BBBB] :receiptDic];
            }
            if([@"success" isEqualToString:result]){//成功
                [userDefaults removeObjectForKey:queryId];
            }else{//补单失败
                [failQueryIdArray addObject:queryId];
            }
        }
        
        if([failQueryIdArray count] > 0){//含有补单失败项
            [userDefaults setObject:failQueryIdArray forKey:@"queryIds"];
        }else{//全部补单成功
            [userDefaults removeObjectForKey:@"queryIds"];
        }
        [userDefaults synchronize];
    }
}


- (void)receiveCreateOrder:(NSNotification *)notification
{
    NSLog(@"创建订单成功");
    AAAA_HaiBaoJuheGzyazfInfo_BBBB *payInfo = [notification object];
    
    NSString *queryId = [payInfo queryId];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self handlerLeakOrder];
    });
    
    NSString* products = [payInfo products];
    //保存订单号到本地
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:queryId forKey:@"lastqueryid"];
    [userDefaults synchronize];
    [[EEEE_CalendarAllManager_FFFF GGGG_CalendarInstance_HHHH] CalendarQueryOperation:self.controller CalendarHuoWuID:products CalendarJiaGe:[payInfo postAmount] CalendarHuoWuMing:[payInfo productName] CalendarDingDanHao:queryId];

}

-(void)paymentSuccess:(NSString *)accountId orderId:(NSString *)orderId currencyAmount:(double)amount currencyType:(NSString *)currencyType payType:(NSString *)payType custom:(NSArray *)customArray
{
}

@end