Charge a Token

Assume you want to charge a token that is available as a variable token. Create the payment parameters with the token:

NSError *error;
PWPaymentParams *tokenParams = [provider.paymentParamsFactory 
    createTokenPaymentParamsWithAmount:amount 
                              currency:EUR 
                               subject:@"Charge test" 
                                 token:token
                                 error:&error];
		
if(tokenParams == nil) {
    // Something went wrong! 
    // To find out what, look at [error description] message
    NSLog(@"%@", [error description]);
} else {
   ...
}

If everything is ok, process the transaction in the else branch:

[provider createAndRegisterDebitTransactionWithParams:tokenParams 
    onSuccess:^(PWTransaction *transaction) {
        [provider debitTransaction:transaction
            onSuccess:^(PWTransaction *transaction) {
                 NSLog(@"Transaction successful");
            } onFailure:^(PWTransaction *transaction, NSError *error) {
                 NSLog(@"%@", [error description]);
            }
        ];
    } onFailure:^(PWTransaction *transaction, NSError *error) {
        NSLog(@"%@", [error description]);
    }
];

Create the token parameters:

String token = ...
double amount = ...
		
// create token parameters
PWPaymentParams tokenParams = null;
try {
    tokenParams = _binder.getPaymentParamsFactory().createTokenPaymentParams(
         amount, PWCurrency.EURO, "nosubject", token);
} catch (PWProviderNotInitializedException e) {
    e.printStackTrace();
    return;
} catch (PWException e) {
    e.printStackTrace();
    return;
}

...and register a transaction:

currentTokenization = false; // now we do a debit transaction
		
try {
    _binder.createAndRegisterDebitTransaction(tokenParams);
} catch(PWException e) {
   e.printStackTrace();
}

After a successful registration, the debit transaction has to be processed. Add an else branch to the creationAndRegistrationSucceeded callback:

...
else { // if(currentTokenization == false)
	// execute it
	try {
		_binder.debitTransaction(transaction);
	} catch (PWException e) {
		setStatusText("Invalid Transaction.");
		e.printStackTrace();
	}
}
...

Finally, you can implement the transactionSuccessful callback to show the charged amount if we did a successful debit on the token:

@Override
public void transactionSucceeded(PWTransaction transaction) {
    if(!currentTokenization) { 
         // our debit succeeded
    }
}