Skip to content

Commit

Permalink
fix http response parser (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
jukiginanjar authored Dec 10, 2018
1 parent 5224ba0 commit 000aa1a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
73 changes: 40 additions & 33 deletions MidtransCoreKit/MidtransCoreKit/MidtransNetworkOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,45 +131,52 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[object setObject:[_responseData SNPvalidateUTF8] forKey:kMTNetworkOperationResponse];
[[NSNotificationCenter defaultCenter] postNotificationName:MTNetworkingDidFinishRequest object:object];

if (responseObject[MIDTRANS_CORE_STATUS_CODE]) {
NSInteger code = [responseObject[MIDTRANS_CORE_STATUS_CODE] integerValue];
if (code/100 == 2) {
if (self.callback) self.callback(responseObject, nil);
}
else if (code == 400) {
id statusMessage = responseObject[@"status_message"];
id validationMessage = responseObject[@"validation_messages"];

NSMutableDictionary *userInfo = [NSMutableDictionary new];
if (statusMessage) {
userInfo[NSLocalizedDescriptionKey] = statusMessage;
if ([responseObject isKindOfClass:[NSArray class]] || [responseObject isKindOfClass:[NSMutableArray class]]) {
if (self.callback) self.callback(responseObject, nil);

} else {
if (responseObject[MIDTRANS_CORE_STATUS_CODE]) {
NSInteger code = [responseObject[MIDTRANS_CORE_STATUS_CODE] integerValue];
if (code/100 == 2) {
if (self.callback) self.callback(responseObject, nil);
}

if (validationMessage) {
userInfo[NSLocalizedFailureReasonErrorKey] = validationMessage;
else if (code == 400) {
id statusMessage = responseObject[@"status_message"];
id validationMessage = responseObject[@"validation_messages"];

NSMutableDictionary *userInfo = [NSMutableDictionary new];
if (statusMessage) {
userInfo[NSLocalizedDescriptionKey] = statusMessage;
}

if (validationMessage) {
userInfo[NSLocalizedFailureReasonErrorKey] = validationMessage;
}

NSError *error = [NSError errorWithDomain:MIDTRANS_ERROR_DOMAIN
code:code
userInfo:userInfo];
if (self.callback) self.callback(nil, error);
}
else {
id statusMessage = responseObject[@"status_message"];
NSMutableDictionary *userInfo = [NSMutableDictionary new];
if (statusMessage) {
userInfo[NSLocalizedDescriptionKey] = statusMessage;
}

NSError *error = [NSError errorWithDomain:MIDTRANS_ERROR_DOMAIN
code:code
userInfo:userInfo];
if (self.callback) self.callback(nil, error);
}

NSError *error = [NSError errorWithDomain:MIDTRANS_ERROR_DOMAIN
code:code
userInfo:userInfo];
if (self.callback) self.callback(nil, error);
}
else {
id statusMessage = responseObject[@"status_message"];
NSMutableDictionary *userInfo = [NSMutableDictionary new];
if (statusMessage) {
userInfo[NSLocalizedDescriptionKey] = statusMessage;
}

NSError *error = [NSError errorWithDomain:MIDTRANS_ERROR_DOMAIN
code:code
userInfo:userInfo];
if (self.callback) self.callback(nil, error);
if (self.callback) self.callback(responseObject, nil);
}
}
else {
if (self.callback) self.callback(responseObject, nil);
}



[self finish];
}
Expand Down
43 changes: 21 additions & 22 deletions MidtransDemo/MidtransDemo/MDOrderViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,28 @@ - (void)viewDidLoad {
self.progressHUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
self.progressHUD.textLabel.text = @"Loading...";

// NSString *clientkey;
// NSString *merchantServer;
// CC_CONFIG.paymentType = [[MDOptionManager shared].ccTypeOption.value integerValue];
// switch (CC_CONFIG.paymentType) {
// case MTCreditCardPaymentTypeOneclick:
// clientkey = @"VT-client-E4f1bsi1LpL1p5cF";
// merchantServer = @"https://rakawm-snap.herokuapp.com";
// CC_CONFIG.tokenStorageEnabled = NO;
// break;
// default:
// clientkey = @"SB-Mid-client-txZHOj6jPP0_G8En";
// merchantServer = @"https://dev-mobile-store.herokuapp.com/";
// CC_CONFIG.tokenStorageEnabled = YES;
// break;
// }
// [CONFIG setClientKey:clientkey
// environment:MidtransServerEnvironmentSandbox
// merchantServerURL:merchantServer];

[CONFIG setClientKey:@"VT-client-yrHf-c8Sxr-ck8tx"
environment:MidtransServerEnvironmentProduction
merchantServerURL:@"https://midtrans-mobile-snap.herokuapp.com"];
NSString *clientkey;
NSString *merchantServer;
CC_CONFIG.paymentType = [[MDOptionManager shared].ccTypeOption.value integerValue];
switch (CC_CONFIG.paymentType) {
case MTCreditCardPaymentTypeOneclick:
clientkey = @"VT-client-E4f1bsi1LpL1p5cF";
merchantServer = @"https://rakawm-snap.herokuapp.com";
CC_CONFIG.tokenStorageEnabled = NO;
break;
default:
clientkey = @"SB-Mid-client-txZHOj6jPP0_G8En";
merchantServer = @"https://dev-mobile-store.herokuapp.com/";
CC_CONFIG.tokenStorageEnabled = YES;
break;
}
[CONFIG setClientKey:clientkey
environment:MidtransServerEnvironmentSandbox
merchantServerURL:merchantServer];

// [CONFIG setClientKey:@"VT-client-yrHf-c8Sxr-ck8tx"
// environment:MidtransServerEnvironmentProduction
// merchantServerURL:@"https://midtrans-mobile-snap.herokuapp.com"];

//forced to use token storage
UICONFIG.hideStatusPage = NO;
Expand Down

0 comments on commit 000aa1a

Please sign in to comment.