Manual iOS Token Generation

Access the API directly.

If you'd prefer to access the API directly rather than going through a view like the default iOS Simplify instructions. These instructions will allow you to request a token directly from the Simplify API.

The iOS SDK by Simplify allows you to create a card token (one time use token representing card details) in your iOS app to send to a server to enable it to make a payment. By creating a card token, Simplify allows you to avoid sending card details to your server. The SDK can help with formatting and validating card information before the information is tokenized.

These three easy steps will allow you to collect card information, retrieve a card token from Simplify with the card information, and send the card token to your server in order to charge a card.

1. Add iOS Framework

Add Framework and Bundle to your project

Download the Simplify.Framework and Simplify.bundle and add both of these to your project. Include it by adding it to your "Framework" grouping in XCode and then ensuring that it is only added to your build target. Make sure you have selected "Copy items into destination group's folder".

You will need to enable Objective C functionality (i.e. categories) in external libraries. You accomplish this by adding the "-ObjC" linker flag in your project settings.

Setting API keys

After logging into your account, your API keys can be located at: Settings -> API Keys

To set your public API key, do the following wherever you need to import the Simplify Framework:

#import <Simplify/Simplify.h>

NSError *error;

//If you're just going to use the SIMSimplify without the need for a view, instantiate like this:
SIMSimplify *simplify = [[SIMSimplify alloc] initWithPublicKey:@"YOUR_PUBLIC_API_KEY" error:&error];

//Handle error;

The SDK will determine Live or Sandbox mode based upon the public key.

2. Collect Card Information

Collecting Card Information With SIMChargeCardViewController

You can directly input card information in the SIMSimplify object to create the token without the use of SIMChargeCardViewController or SIMChargeCardModel but there will not be any validation of the card information.

Alternatively, you can use the SIMChargeCardModel to hold and validate card information. You can create a SIMChargeCardModel like this:

NSError *error;
SIMChargeCardModel *chargeCardModel = [[SIMChargeCardModel alloc] initWithPublicKey:@"YOUR_PUBLIC_API_KEY" error:&error];

if (error) {
    //Handle publicKey error
} else {
    chargeCardModel.delegate = self;
}

Then you can input credit card information for each field. Each of these methods have some validations for what can be input. Use the isCardChargePossible method to determine if all three fields are valid.

[chargeCardModel updateCardNumberWithString:@"555555555555554444"];
BOOL isCardNumberValid = [chargeCardModel isCardNumberValid];

[self.chargeCardModel updateCVCNumberWithString:@"123"];
BOOL isCardCVCValid = [chargeCardModel isCVCCodeValid];

[self.chargeCardModel updateExpirationDateWithString:@"20200101"];
BOOL isCardExpirationDateValid = [chargeCardModel isExpirationDateValid];

BOOL isChargePossible = [chargeCardModel isCardChargePossible];

You can assign any class that implements the SIMChargeCardModelDelegate protocol as the delegate for SIMChargeCardModel, and it handles the success (tokenProcessed method) or failure (tokenFailedWithError method) of the token creation. You need only implement these protocols to process them. You can use the SIMChargeCardModel to actually retreive a token as well by calling retreive token and processing the callbacks.

[chargeCardModel retrieveToken];

#pragma mark SIMChargeCardModelDelegate callback methods

- (void)tokenFailedWithError:(NSError *)error {

    //Process error

}

-(void)tokenProcessed:(SIMCreditCardToken *)token {

    //Utilize token

}

3. Create a Card Token

Create a single-use token

Before you can actually submit a payment to Simplify, you must generate a Card Token. This removes the burden of storing or processing credit card details from you as a merchant. Once you have generated a card token using iOS SDK, you can charge the card using the token. You should never store or attempt to reuse single-use tokens to prevent possible security issues.

You can create a SIMSimplify yourself and utilize it. You must pass a completion handler block to the createCardToken method.

#import <Simplify/SIMSimplify.h>

//Put your Simplify Public Key in your instantiation
NSError *error;
SIMSimplify *simplify = [[SIMSimplify alloc] initWithPublicKey:@"YOUR_PUBLIC_API_KEY" error:nil];

[simplify createCardTokenWithExpirationMonth:self.expirationMonth
                                expirationYear:self.expirationYear
                                    cardNumber:self.cardNumber
                                           cvc:self.cvcCode
                              completionHander:^(NSString *cardToken, NSError *error) {

if (error) {

    NSLog(@"error:%@", error);

    //Handle the error

} else {

    NSLog(@"token: %@", cardToken);

    //Use the card token

}

}];

Charging a Card

You can charge a card by submitting a card token generated by the iOS SDK to your own server. You can not complete a payment from your iOS app without having a server because you should not put your private key on the iOS app. Here we will show how to charge a card using the card token you generated in the previous step. In the previous step, you created a one-time use card token, and now you will submit that token to your server. To complete a payment, you need to use that token with one of the Simplify Commerce SDKs to create a Charge.

The example below shows how to create a block completion handler for the SIMSimplify class. You will have to handle both the error condition and the successful token generation.


[simplify createCardTokenWithExpirationMonth:self.expirationMonth
                              expirationYear:self.expirationYear
                                  cardNumber:self.cardNumber
                                         cvc:self.cvcCode
                                     address:self.address
                            completionHander:^(NSString *cardToken, NSError *error) {

if (error) {

    NSLog(@"error:%@", error);

    //Utilize the provided alert screen
    NSLog(@"Credit Card Token Failed with error:%@", error.localizedDescription);
    UIImageView *blurredView = [UIImage blurImage:self.view.layer];
    NSString *errorDescription = @"There was a problem with the token.\nPlease try again.";
    SIMResponseViewController *viewController = [[SIMResponseViewController alloc] initWithBackground:blurredView
                                                                                         primaryColor:self.primaryColor
                                                                                                title:@"Failure."
                                                                                          description:errorDescription];
    [self presentViewController:viewController animated:YES completion:nil];

} else {


    NSURL *url= [NSURL URLWithString:@"https://Your_server/charge.rb"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    [request setHTTPMethod:@"POST"];
    NSString *postString = @"simplifyToken=";

    postString = [postString stringByAppendingString:token.token];

    NSLog(@"postString:%@", postString);
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    NSError *error;

    //Process Request on your own server

    if (error) {
        NSLog(@"error:%@", error);
        //Handle your server error

    } else {

        //Handle your server's response
    }

}

You will need to implement the details to send the request to your server and process the payment. Your server will need an endpoint that can receive a card token so that you can use the token to make a charge.

For more information on how to set up your payment system, see the API documentation and our tutorial.