SDKInterface.m 6.87 KB
//
//  UnionInterface.m
//  PoolSdk
//
//  Created by winFan on 11/23/15.
//  Copyright (c) 2015 winFan. All rights reserved.
//
#include "SDKInterface.h"
#import <UIKit/UIKit.h>
#import "AppleIap.h"

@implementation SDKInterface : Interface


int mExchange;


/**
 初始化SDK
*/
- (void)SDKinit:(UIViewController *) control
{
    //必须继承
    [super SDKinit:control];
    
    [PoolConfig getInstance];
    NSLog(@"sdk chushihua");
    
    
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationInitSDK notiName:@"InitSuccess" statusCode:POOLSDK_NO_ERROR description:@"initSuccess" extendData:nil];
    
    NSString* appId = [[PoolConfig getInstance] getValueByKey:@"appId"];
    NSString* openKey = [[PoolConfig getInstance] getValueByKey:@"openKey"];
    NSString* channelId = [[PoolConfig getInstance] getValueByKey:@"channelId"];
    
    NSLog(@"appId:%@    \n openKey:%@",appId,openKey);
    [YDAccount initWithAppId:appId openKey:openKey];
    [YDTool initWithAppId:appId developKey:openKey];
    
    [YDAccount setDelegate:self];
    [YDTool setDelegate:self];
    [YDTool setChannelId:channelId];
}

/**
 登录SDK
 */
- (void)SDKloginSDK
{
    NSLog(@"调用了LoginSDK");
    [YDAccount autoRatate:YES];
    [YDAccount showWindow];
}

/**
 支付 创建订单
 */
- (void)SDKpayWithPaymentInfo:(PoolCreateOrderInfo *)payInfo
{
    mExchange = [payInfo exchange].intValue;
    [super SDKpayWithPaymentInfo:payInfo];
}

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

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


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

- (void)SDKlogoutSDK
{
    NSLog(@"切换账号");
    [YDAccount logout];
    [[PoolChecker getInstance] sendNotice:PoolSDKNotificationLogout notiName:@"logoutSuccess" statusCode:POOLSDK_NO_ERROR description:@"logoutSuccess" extendData:nil];
}

- (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];//补单失败的queryid集合
    
    if(queryIdArray){
        for (NSString* queryId in queryIdArray) {
            NSString* receiptStr = [userDefaults valueForKey:queryId];
            NSDictionary* receiptDic = [NSDictionary dictionaryWithObjectsAndKeys:receiptStr,@"receipt",queryId,@"query_id", nil];
            NSString* result = [[NetCenter getInstance] httpPostSyn:[PoolUtils createPayUrl] :receiptDic];
            if([@"success" isEqualToString:result]){//成功
                [userDefaults removeObjectForKey:queryId];
            }else{//补单失败
                [failQueryIdArray addObject:queryId];
            }
        }
        
        NSLog(@"order count:%lu",(unsigned long)[failQueryIdArray count]);
        if([failQueryIdArray count] > 0){//含有补单失败项
            [userDefaults setObject:failQueryIdArray forKey:@"queryIds"];
        }else{//全部补单成功
            [userDefaults removeObjectForKey:@"queryIds"];
        }
        [userDefaults synchronize];
    }
}


/**
 @return 商品名称
 */
- (NSString*)getProductNameByAmount:(NSString*)products amount:(NSString*)amount
{
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:products options:NSJSONWritingPrettyPrinted error:nil];
    if(jsonData){
        NSDictionary* productList = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
        if(productList){
            NSEnumerator* keysList = [productList keyEnumerator];
            id keyValue;
            while (keyValue = [keysList nextObject])
            {
                NSString* appleAmount = [[productList objectForKey:keyValue] objectForKey:@"amount"];
                if(appleAmount.intValue == amount.intValue){
                     NSString* productName = [[productList objectForKey:keyValue] objectForKey:@"name"];
                    return productName;
                }
            }
        }
    }
    NSLog(@"get product name error amount:%@",amount);
    return @"元宝";
}


- (void)receiveCreateOrder:(NSNotification *)notification
{
    NSLog(@"创建订单成功");
    PoolPayInfo *payInfo = [notification object];
    
    NSString *myamount = [payInfo postAmount];
    NSString *queryId = [payInfo queryId];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self handlerLeakOrder];
    });
    
    NSString *isOpenThirdPay = [payInfo isOpenThirdPay];
    NSLog(@"custom%@",[payInfo custom]);
    if([isOpenThirdPay isEqualToString:@"false"]){//apply
        NSString* products = [payInfo products];
        [[[AppleIap alloc] init]applePay:products amount:myamount queryId:queryId];
    }else{
        NSLog(@"queryId:%@",queryId);
        NSString* productNo = [[PoolConfig getInstance]getValueByKey:@"productId"];
        NSString* orderName = [self getProductNameByAmount:[payInfo products] amount:myamount];
        int amountValue = [myamount intValue] * 100;
        
        [YDTool tool:queryId orderName:orderName userId:[payInfo playerId] productNo:productNo amount:amountValue param:queryId];
    }
    
    
    int64_t delayInSeconds = 3.0;// 延迟的时间
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"chage state");
        [[PoolChecker getInstance] setOnlyOneTimePay:false];
    });
}


-(NSString*)getCurrentTimestamp{
    // 时间戳转时间
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
    NSString* timeStr = [[NSString alloc]initWithFormat:@"%.f",interval];
    return timeStr;
}


/**
 *优点SDK回调
 **/
//玩家登陆成功后回调
-(void)accountLogin:(YDUserAccount*)model{
    NSLog(@"login success");
    PoolLoginInfo *loginInfo = [PoolLoginInfo alloc];
    loginInfo.openId = [[NSString alloc]initWithFormat:@"%lld",[model accountId]];
    loginInfo.timestamp = [self getCurrentTimestamp];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[PoolChecker getInstance] startCheck:loginInfo];
    });
}
//玩家注册成功后回调
-(void)accountRegist:(YDUserAccount *)model{
    NSLog(@"reg success");
}

-(void)toolResult:(YDToolStatus)result data:(id)data
{
    NSLog(@"youdian pay callback");
}

@end