Obtain a Token

Create payment parameters for the data the user supplied:

PWPaymentParamsFactory *paramFactory = provider.paymentParamsFactory;
    
NSError *error;
PWPaymentParams *ccParams = [paramFactory 
    createCreditCardTokenizationParamsWithNumber:cNumber 
                                            name:holder 
                                      expiryYear:expYear 
                                     expiryMonth:expMonth 
                                             CVV:cvv 
                                           error:&error];

In the next lines, we handle the error that can occur if the card details are not valid.

if(ccParams == nil) {
    // Something went wrong! To find out what,
    // look at [error description] message
    NSLog(@"%@", [error description]);
} else {
    ...
}

In the else branch, fire the transaction to obtain the token.

// prepare to obtain token
[provider createAndRegisterObtainTokenTransactionWithParams:ccParams 
    onSuccess:^(PWTransaction *transaction) {
        // obtain token
        [provider obtainToken:transaction 
            onSuccess:^(NSString *token, PWTransaction *transaction) {
                NSLog(@"%@", token);
            }
            onFailure:^(PWTransaction *transaction, NSError *error) {
                NSLog(@"%@", [error description]);
            }
        ];
    } onFailure:^(PWTransaction *transaction, NSError *error) {
        NSLog(@"%@", [error description]);
    }
];

The token is returned in the success callback of obtainToken.

Create the payment params and register to obtain a token:

PWPaymentParams paymentParams = null;
try {
	paymentParams = _binder
			.getPaymentParamsFactory()
			.createCreditCardTokenizationParams(name,
                                                            type,
                                                         cNumber, 
                                                         expYear, 
                                                        expMonth, 
                                                             cvv);

} catch (PWProviderNotInitializedException e) {
	setStatusText("Error: Provider not initialized!");
	e.printStackTrace();
	return;
} catch (PWException e) {
	setStatusText("Error: Invalid Parameters!");
	e.printStackTrace();
	return;
}

Add a boolean currentTokenization field to the class to track if we currently do tokenization, set it to true, and create the transaction.

currentTokenization = true;

try {
	_binder.createAndRegisterObtainTokenTransaction(paymentParams);
} catch (PWException e) {
	setStatusText("Error: Could not contact Gateway!");
	e.printStackTrace();
}

As a next step, we need to listen for transaction events. Let your activity implement PWTokenObtainedListener and PWTransactionListener and add implementations for the required methods. In creationAndRegistrationSucceeded, we send the transaction to the gateway if we are currently doing tokenization:

@Override
public void creationAndRegistrationSucceeded(PWTransaction transaction) {
	// check if it is our tokenization transaction
	if(currentTokenization) {
		// execute it
		try {
			_binder.obtainToken(transaction);
		} catch (PWException e) {
			e.printStackTrace();
		}
	}
}

When you obtain the token, you can store it:

@Override
public void obtainedToken(String token, PWTransaction transaction) {
	// store the token.
}

Finally, add the activity as a TransactionListener and TokenObtainedListener in the service definition:

...
_binder.addTokenObtainedListener(TokenizationActivity.this);
_binder.addTransactionListener(TokenizationActivity.this);
...