//
//  JSUMSCallback.m
//  qm-mposkit
//
//  Created by 夏迎霖 on 2017/7/12.
//  Copyright © 2017年 Marc Shilling. All rights reserved.
//

#import "JSUMSCallback.h"
#import <JSUMSSDK/JSUMSSDK.h>

@implementation JSUMSCallback



/**
 *  处理左收右付传过来的数据,并进行封装
 *
 *  @param application
 *  @param url
 *  @param sourceApplication
 *  @param annotation
 *
 *  @return
 */
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    /** 结果处理示例代码 **/
    
    /* 注意返回结果只可能为2种类型: 订单消费/订单撤销/订单同步 返回结果为OrderThirdResEntity;
     订单批量查询 返回结果为OrderThirdBatchQueryResEntity;
     请根据示例代码分别进行判断 */
    
    //调用Api中的方法将URL转换为id类型
    id entity = [JSUMSApi UrlSplitToEntity:url];
    //订单消费/订单撤销/订单同步结果
    if ([entity isMemberOfClass:[OrderThirdResEntity class]]) {
        OrderThirdResEntity *tempEntity = entity;
        /* 通过判断是否有signData判断是否出错 */
        if (tempEntity.signData) {
            //初始化RSA-SHA1类
            RSASH1Impl *rsa = [[RSASH1Impl alloc]init];
            //将entity中的字段按文档中提到的规则排序
            NSString *afterSortSignString = [JSUMSUtil sortSignString:tempEntity];
            //验签
            BOOL isCorrect = [rsa verifyString:afterSortSignString withSign:tempEntity.signData];
            //如果签名正确
            if (isCorrect) {
                NSLog(@"签名正确.");
                // to do
            }
            //如果签名错误，则数据可能被篡改
            else {
                NSLog(@"签名错误.");
                RIButtonItem *OKButton = [RIButtonItem itemWithLabel:@"确定"];
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"RSA签名错误" cancelButtonItem:nil otherButtonItems:OKButton, nil];
                [alert show];
                return YES;
                // to do
            }
            RIButtonItem *OKButton = [RIButtonItem itemWithLabel:@"确定"];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"操作成功" cancelButtonItem:nil otherButtonItems:OKButton, nil];
            [alert show];
        }
        //报错
        else {
            /* 通过得到resultCode和resultMsg分别得到错误码和错误信息 */
            NSString *errMsg = [NSString stringWithFormat:@"%@ %@",tempEntity.resultCode,tempEntity.resultMsg];
            RIButtonItem *OKButton = [RIButtonItem itemWithLabel:@"确定"];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:errMsg cancelButtonItem:nil otherButtonItems:OKButton, nil];
            [alert show];
        }
    }
    //批量查询结果(下列处理与上同)
    else if ([entity isMemberOfClass:[OrderThirdBatchQueryResEntity class]]) {
        OrderThirdBatchQueryResEntity *tempEntity = entity;
        if (tempEntity.signData) {
            //验签
            RSASH1Impl *rsa = [[RSASH1Impl alloc]init];
            NSString *afterSortSignString = [JSUMSUtil sortSignString:tempEntity];
            BOOL isCorrect = [rsa verifyString:afterSortSignString withSign:tempEntity.signData];
            if (isCorrect) {
                NSLog(@"签名正确.");
                // to do
            }else {
                NSLog(@"签名错误.");
                RIButtonItem *OKButton = [RIButtonItem itemWithLabel:@"确定"];
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"RSA签名错误" cancelButtonItem:nil otherButtonItems:OKButton, nil];
                [alert show];
                return YES;
                // to do
            }
            RIButtonItem *OKButton = [RIButtonItem itemWithLabel:@"确定"];
            NSError *err;
            //下列代码为将批量查询结果显示,可根据需要进行处理
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tempEntity.orders options:0 error:&err];
            NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
            NSString *msgString = [NSString stringWithFormat:@"totalSize:%@ orders:%@",tempEntity.totalSize,jsonString];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msgString cancelButtonItem:nil otherButtonItems:OKButton, nil];
            [alert show];
        }else {
            NSString *errMsg = [NSString stringWithFormat:@"%@ %@",tempEntity.resultCode,tempEntity.resultMsg];
            RIButtonItem *OKButton = [RIButtonItem itemWithLabel:@"确定"];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:errMsg cancelButtonItem:nil otherButtonItems:OKButton, nil];
            [alert show];
        }
        
    }
    return YES;
}

@end
