Skip to content

Commit

Permalink
Don't allow multiple requests at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
tolu360 committed Oct 3, 2018
1 parent 95949b9 commit 1362051
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions ios/RNPaystack.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ @implementation RNPaystackModule
RCTPromiseResolveBlock _resolve;
RCTPromiseRejectBlock _reject;
NSString *publicKey;
BOOL requestIsCompleted;
}

- (instancetype)init {
Expand Down Expand Up @@ -112,6 +113,13 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
_reject = reject;
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

if(!requestIsCompleted) {
_reject(@"E_BUSY", @"Another request is still being processed, please wait", nil);
return;
}

requestIsCompleted = NO;

dispatch_async(dispatch_get_main_queue(), ^{
if (! [self cardParamsAreValid:params[@"cardNumber"] withMonth:params[@"expiryMonth"] withYear:params[@"expiryYear"] andWithCvc:params[@"cvc"]]) {

Expand Down Expand Up @@ -162,6 +170,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
forTransaction:transactionParams
onViewController:rootViewController
didEndWithError:^(NSError *error, NSString *reference){
requestIsCompleted = YES;
if (_reject) {
_reject(@"E_CHARGE_ERROR", @"Error charging card", error);
}
Expand All @@ -171,6 +180,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
NSLog(@"- RNPaystack: an OTP was requested, transaction has not yet succeeded");
}
didTransactionSuccess: ^(NSString *reference){
requestIsCompleted = YES;
// transaction may have succeeded, please verify on server
NSLog(@"- RNPaystack: transaction may have succeeded, please verify on server");
NSMutableDictionary *returnInfo = [self setReferenceMsg:reference];
Expand All @@ -181,7 +191,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
}];

} else {
// NSMutableDictionary *returnInfo = [self setErrorMsg:@"Invalid Card." withErrorCode:404];
requestIsCompleted = YES;

if (_reject) {
_reject(@"E_INVALID_CARD", @"Card is invalid", nil);
Expand All @@ -200,6 +210,13 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
_reject = reject;
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

if(!requestIsCompleted) {
_reject(@"E_BUSY", @"Another request is still being processed, please wait", nil);
return;
}

requestIsCompleted = NO;

dispatch_async(dispatch_get_main_queue(), ^{
if (! [self cardParamsAreValid:params[@"cardNumber"] withMonth:params[@"expiryMonth"] withYear:params[@"expiryYear"] andWithCvc:params[@"cvc"]]) {

Expand All @@ -225,6 +242,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
forTransaction:transactionParams
onViewController:rootViewController
didEndWithError:^(NSError *error, NSString *reference){
requestIsCompleted = YES;
if (_reject) {
_reject(@"E_CHARGE_ERROR", @"Error charging card", error);
}
Expand All @@ -234,6 +252,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
NSLog(@"- RNPaystack: an OTP was requested, transaction has not yet succeeded");
}
didTransactionSuccess: ^(NSString *reference){
requestIsCompleted = YES;
// transaction may have succeeded, please verify on server
NSLog(@"- RNPaystack: transaction may have succeeded, please verify on server");
NSMutableDictionary *returnInfo = [self setReferenceMsg:reference];
Expand All @@ -244,7 +263,7 @@ - (BOOL)cardParamsAreValid:(NSString *)cardNumber withMonth:(NSString *)expMonth
}];

} else {
// NSMutableDictionary *returnInfo = [self setErrorMsg:@"Invalid Card." withErrorCode:404];
requestIsCompleted = YES;

if (_reject) {
_reject(@"E_INVALID_CARD", @"Card is invalid", nil);
Expand Down

0 comments on commit 1362051

Please sign in to comment.