Documentation
Brick iOS SDK
Getting Started
Step 1: Install Brick
First, you need download Brick SDK
To add Brick SDK to your project:
- In the menubar click "File" then "Add file to YOUR_PROJECT"
- Select the "Brick" directory in the downloaded repository.
- Make sure 'Copy items into destination group's folder (if needed)' is checked.
- Click "Add"
- Click on your project, in Targets tab click in "Build Settings"
- In the "Header Search Paths" add link to include file of SDK such as "$SOURCE_ROOT/BrickSDK/include"
- In the "Library Search Paths" add link to file "BrickSDK.a"
Step 2: Configure your API key
You have to configure Brick with your public key. Specify it in your Appdelegate's -application:didFinishLaunchingWithOptions: method. See example below:
// AppDelegate.m
#import "AppDelegate.h"
#import <Brick/Brick.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Brick setPublishableKey:@"YOUR_PUBLIC_KEY"];
return YES;
}
@end
Step 3: Creating a one-time token
First, create the BrickCard object with the card details gathered from the user. BrickCard class stores the card details and also provide you with some method to validate it.
Next you need to create a one-time token based on the card. Having a BrickCard object you simply call a method to retrieve token. After successful token generation, you will have a token object in your success callback handler. You should then send a token to your backend and complete a charge.
Check the example below:
#import "YourViewController.h"
#import <Brick/Brick.h>
@implementation YourViewController
- (void) getToken {
BrickCard *card = [BrickCard new];
card.cardNumber = @"CARD_NUMBER";
card.cardCVC = @"CARD_CVC";
card.expMonth = @"EXP_MONTH";
card.expYear = @"EXP_YEAR";
[Brick getTokenFromCard:card completion:^(BrickToken *token, NSError *error){
if(!error){
NSLog(@"Your token: %@",token.token);
}
else {
NSLog(@"Error: %@",[[error userInfo] valueForKey: BRErrorMessageKey]);
}
}];
}
@end
Sending the token to your server
When you got the token from block method getTokenFromCard: you need to send it to your server. See the example blow:
// ViewController.m
- (void)sendTokenToServerWithToken:(BrickToken *)token
completion:(void (^)(NSString *response))completion {
NSURL *url = [NSURL URLWithString:@"https://your_api/push_token"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
NSString *body = [NSString stringWithFormat:@"brickToken=%@", token.token];
request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if (error) {
completion(nil);
} else {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
completion(responseString);
}
}];
}