Skip to content

Instantly share code, notes, and snippets.

@Jose-Alvarado
Last active August 29, 2015 14:05
Show Gist options
  • Save Jose-Alvarado/91ccdb432eef3151aa5e to your computer and use it in GitHub Desktop.
Save Jose-Alvarado/91ccdb432eef3151aa5e to your computer and use it in GitHub Desktop.
Code that worked with test ach info, but not with production
- (IBAction)onSaveBankAccountInfo:(id)sender {
if (saveOnce == YES)
return;
saveOnce = YES;
NSString *accountName = self.txtAccountValue.text;
if (accountName.length == 0 ) {
[[[UIAlertView alloc] initWithTitle:@"Incorrect data"
message:@"Please enter the account holder's name"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil] show];
saveOnce = NO;
return;
}
NSString *rountingNumber = self.txtRoutingNumber.text;
if (rountingNumber.length == 0) {
[[[UIAlertView alloc] initWithTitle:@"Incorrect data"
message:@"Please enter the routing number"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil] show];
saveOnce = NO;
return;
}
NSString *accountNumber = self.txtAccountNumber.text;
if (accountNumber.length == 0) {
[[[UIAlertView alloc] initWithTitle:@"Incorrect data"
message:@"Please enter the account number"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil] show];
saveOnce = NO;
return;
}
NSDictionary *achInfo;
if ([[PSApiClient instance] isProductionBuild]) {
achInfo = @{@"country":@"US", @"routing_number":rountingNumber, @"account_number":accountNumber};
} else {
achInfo = @{@"country":@"US", @"routing_number":rountingNumber, @"account_number":accountNumber};
}
NSURL *url = [NSURL URLWithString:@"https://api.stripe.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setDefaultHeader:@"Authorization" value:@"Basic key"];
NSDictionary *dataDictionary = @{@"bank_account[country]": @"US",
@"bank_account[routing_number]" : rountingNumber,
@"bank_account[account_number]" : accountNumber};
httpClient.parameterEncoding = AFFormURLParameterEncoding;
[httpClient postPath:@"/v1/tokens" parameters:dataDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary* jsonFromData = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSString *achToken = jsonFromData[@"id"];
NSDictionary *result = @{@"ach_token" : achToken, @"account_holder_name" : accountName};
[self sendACHInfoToServer:result];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
saveOnce = NO;
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
[[[UIAlertView alloc] initWithTitle:@"Invalid ACH Info" message:@"Please try again" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
}];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
Code where we tried to fix the problem
NSDictionary *achInfo = @{@"country":@"US", @"routing_number":rountingNumber, @"account_number":accountNumber};
// NSURL *url = [NSURL URLWithString:@"https://api.stripe.com"];
// AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// [httpClient setDefaultHeader:@"Authorization" value:@"sk_live_key"];
// [httpClient setAuthorizationHeaderWithToken:@"sk_live_key"];
NSDictionary *dataDictionary = @{@"bank_account[country]": @"US",
@"bank_account[routing_number]" : rountingNumber,
@"bank_account[account_number]" : accountNumber};
// NSArray *bank_account = @[@"US", rountingNumber, accountNumber];
// NSDictionary *testing = @{@"bank_account": @{@"country": @"US", @"routing_number": rountingNumber, @"account_number":accountNumber}};
// httpClient.parameterEncoding = AFJSONParameterEncoding;
NSURL *url = [NSURL URLWithString:@"https://api.stripe.com/v1/tokens"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[request setHTTPMethod:@"POST"];
[request setValue:@"Basic sk_live_key" forHTTPHeaderField:@"Authorization"];
[request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
// [request setValue:@"US" forKey:@"bank_account[country]"];
// [request setValue:rountingNumber forKey:@"bank_account[routing_number]"];
// [request setValue:accountNumber forKey:@"bank_account[account_number]"];
NSMutableData *postBody = [NSMutableData data];
NSData *plainData = [@"bank_account[country]: US" dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
[postBody appendData:[base64String dataUsingEncoding:NSUTF8StringEncoding]];
NSData *plainData2 = [@"bank_account[routing_number]: %@" dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String2 = [plainData2 base64EncodedStringWithOptions:0];
[postBody appendData:[[NSString stringWithFormat:base64String2, rountingNumber] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *plainData3 = [@"bank_account[account_number]: %@" dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String3 = [plainData3 base64EncodedStringWithOptions:0];
[postBody appendData:[[NSString stringWithFormat:base64String3, accountNumber] dataUsingEncoding:NSUTF8StringEncoding]];
// [request setValue:[NSString stringWithFormat:@"%d", [postBody length]] forKey:@"Content-Length"];
[request setHTTPBody: postBody];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON responseObject: %@ ",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (error == nil)
{
NSDictionary *jsonObject=[NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:nil];
NSLog(@"%@", jsonObject);
} else {
NSLog(@"here");
NSLog(@"error - %@", error.description);
}
// [httpClient postPath:@"/v1/tokens" parameters:dataDictionary success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
// NSDictionary* jsonFromData = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
// NSString *achToken = jsonFromData[@"id"];
// NSDictionary *result = @{@"ach_token" : achToken, @"account_holder_name" : accountName};
// [self sendACHInfoToServer:result];
//
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
// saveOnce = NO;
// NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
// [[[UIAlertView alloc] initWithTitle:@"Invalid ACH Info" message:@"Please try again" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
//
// }];
Error message: In produciton {
error = {
message = "Invalid ASCII characters found in API key: \"\\xB2Ib\\xBD\\xEE\\x10\\x06z\\xE4\\xB3\\x05=\\x1A\\xD3\\xC6\\xB3\\x9BE\\xB2#\\f\". For assistance, please contact [email protected].";
type = "invalid_request_error";
};
}
@Jose-Alvarado
Copy link
Author

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment