Skip to main content

Payment Method Tokenization

Overview

Payment Method Tokenization is the exchange of payment data for a token representation provided by the payment gateway for PCI Compliance. This is an initial step in the payments lifecycle.

Prerequisites

There is one prerequisite resource that must be created before creating a Payment Method Tokenization.

           Object           Definition
Payment GatewayPayment Gateways track all the configuration settings that Chargent will use to proccess the payments and send requests to the gateway.

Create Methods

There are four methods for creating payment methods and tokens.

MethodDefinition
Create Payment MethodCreate a new Payment Method Tokenization with the Payment Gateway.
Import Payment Method TokensToDo
Multiple Gateway TokenizationCreate multiple Payment Method Tokenizations across multiple MIDs or multiple Payment Gateways.

Picklist Values

The following values may be sent for the picklist types.

PicklistValues
paymentMethodTypeCredit Card Bank Account
cardTypeVisa, Mastercard, Discover, American Express, MC Eurocard, UK Maestro, JCB Card, Diners Card
bankAccountTypeChecking, Savings, Business Checking

Create Payment Method

Credit Card

Apex Example
try {
// Get the payment gateway to use
ChargentBase__Gateway__c paymentGateway = [SELECT Id FROM ChargentBase__Gateway__c WHERE Name = 'Stripe Test' LIMIT 1];

// initialize variables
String phone = '{{$randomPhoneNumber}}';
String email = '{{$randomeEmail}}';

String cardNumber = '{{cardNumber}}';
String cardType = '{{cardType}}';
String cvv = '{{cvv}}';
String expiryMonth = String.valueOf(Date.today().month());
String expiryYear = String.valueOf(Date.today().year() + 3);

// credit card payment data
Map<String,Object> cardPaymentMethodMap = new Map<String, Object>();
cardPaymentMethodMap.put('cardType', cardType);
cardPaymentMethodMap.put('cardNumber', cardNumber);
cardPaymentMethodMap.put('expiryMonth', expiryMonth);
cardPaymentMethodMap.put('expiryYear', expiryYear);
cardPaymentMethodMap.put('cvv', cvv);
cardPaymentMethodMap.put('cardHolderName', 'Chargent Test');
cardPaymentMethodMap.put('cardHolderFirstName', 'Chargent');
cardPaymentMethodMap.put('cardHolderLastName', 'Test');

// billing address
Map<String,Object> addressMap = new Map<String, Object>();
addressMap.put('street', '123 Street St');
addressMap.put('state', 'California');
addressMap.put('city', 'San Francisco');
addressMap.put('postalcode', '94105');
addressMap.put('country', 'US');
addressMap.put('companyName', 'Chargent Test');

// payment method tokenization
Map<String,Object> paymentRequestMap = new Map<String, Object>();
paymentRequestMap.put('phone', phone);
paymentRequestMap.put('email', email);
paymentRequestMap.put('paymentGatewayId', paymentGateway.Id);
paymentRequestMap.put('cardPaymentMethod', cardPaymentMethodMap);
paymentRequestMap.put('address', addressMap);

// action service request
try {
Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('paymentMethods', new List<Object>{paymentRequestMap});
actionsMap.put('action', 'CreatePM');
actionsMap.put('feature', 'APEX');
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);

// process response
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
try {
System.debug('Payment Method Token create error.');
// unpack the message
List<Object> operationResponsesList = (List<Object>) actionResponseMap.get('operationResponsesList');
Map<String, Object> operationResponseItem = (Map<String, Object>) operationResponsesList.get(0);
// debug chargent error
Map<String, Object> errorItem = (Map<String, Object>) operationResponseItem.get('errorItem');
String chargentErrorType = (String) errorItem.get('errorType');
System.debug('Chargent Error Type: ' + chargentErrorType);
String chargentErrorMessage = (String) errorItem.get('message');
System.debug('Chargent Error Message: ' + chargentErrorMessage);
// debug gateway error
Map<String, Object> gatewayResponseMap = (Map<String, Object>) operationResponseItem.get('gatewayResponseMap');
Map<String, Object> gateway = (Map<String, Object>) gatewayResponseMap.get(paymentGateway.Id);
Map<String, Object> calloutResponse = (Map<String, Object>) gateway.get('webOutPut');
String status = (String) calloutResponse.get('Status');
System.debug('Gateway Status: ' + status);
String message = (String) calloutResponse.get('Message');
System.debug('Gateway Message: ' + message);
String reasonCode = (String) calloutResponse.get('ReasonCode');
System.debug('Gateway Code: ' + reasonCode);
} catch (Exception e) {
System.debug('Error unpacking Action Service response: ' + e.getMessage());
}
} else {
// Success
System.debug('Payment Method Token created successfully.');
}
} catch (Exception e) {
System.debug('Error creating Payment Method: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Payment Gateways: ' + e.getMessage());
}

Bank Account

Apex Example
try {
// Get the payment gateway to use
ChargentBase__Gateway__c paymentGateway = [SELECT Id FROM ChargentBase__Gateway__c WHERE Name = 'Vantiv Test' LIMIT 1];

// initialize variables
String phone = '{{$randomPhoneNumber}}';
String email = '{{$randomeEmail}}';

String bankAccountType = 'Checking';
String bankInstitutionName = '{{$randomBankAccountName}}';
String bankAccountNumber = '{{bankAccountNumber}}';
String routingNumber = '{{routingNumber}}';
String bankAccountHolderName = 'Chargent Test';
String bankAccountHolderFirstName = 'Chargent';
String bankAccountHolderLastName = 'Test';

// credit card payment data
Map<String,Object> bankAccountPaymentMethodMap = new Map<String, Object>();
bankAccountPaymentMethodMap.put('bankAccountType', bankAccountType);
bankAccountPaymentMethodMap.put('bankInstitutionName', bankInstitutionName);
bankAccountPaymentMethodMap.put('bankAccountNumber', bankAccountNumber);
bankAccountPaymentMethodMap.put('routingNumber', routingNumber);
bankAccountPaymentMethodMap.put('bankAccountHolderName', bankAccountHolderName);
bankAccountPaymentMethodMap.put('bankAccountHolderFirstName', bankAccountHolderFirstName);
bankAccountPaymentMethodMap.put('bankAccountHolderLastName', bankAccountHolderLastName);

// billing address
Map<String,Object> addressMap = new Map<String, Object>();
addressMap.put('street', '123 Street St');
addressMap.put('state', 'California');
addressMap.put('city', 'San Francisco');
addressMap.put('postalcode', '94105');
addressMap.put('country', 'US');
addressMap.put('companyName', 'Chargent Test');

// payment method tokenization
Map<String,Object> paymentRequestMap = new Map<String, Object>();
paymentRequestMap.put('phone', phone);
paymentRequestMap.put('email', email);
paymentRequestMap.put('paymentGatewayId', paymentGateway.Id);
paymentRequestMap.put('bankAccountPaymentMethod', bankAccountPaymentMethodMap);
paymentRequestMap.put('address', addressMap);

// action service request
try {
Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('paymentMethods', new List<Object>{paymentRequestMap});
actionsMap.put('action', 'CreatePM');
actionsMap.put('feature', 'APEX');
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);

// process response
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
try {
System.debug('Payment Method Token create error.');
// unpack the message
List<Object> operationResponsesList = (List<Object>) actionResponseMap.get('operationResponsesList');
Map<String, Object> operationResponseItem = (Map<String, Object>) operationResponsesList.get(0);
// debug chargent error
Map<String, Object> errorItem = (Map<String, Object>) operationResponseItem.get('errorItem');
String chargentErrorType = (String) errorItem.get('errorType');
System.debug('Chargent Error Type: ' + chargentErrorType);
String chargentErrorMessage = (String) errorItem.get('message');
System.debug('Chargent Error Message: ' + chargentErrorMessage);
// debug gateway error
Map<String, Object> gatewayResponseMap = (Map<String, Object>) operationResponseItem.get('gatewayResponseMap');
Map<String, Object> gateway = (Map<String, Object>) gatewayResponseMap.get(paymentGateway.Id);
Map<String, Object> calloutResponse = (Map<String, Object>) gateway.get('webOutPut');
String status = (String) calloutResponse.get('Status');
System.debug('Gateway Status: ' + status);
String message = (String) calloutResponse.get('Message');
System.debug('Gateway Message: ' + message);
String reasonCode = (String) calloutResponse.get('ReasonCode');
System.debug('Gateway Code: ' + reasonCode);
} catch (Exception e) {
System.debug('Error unpacking Action Service response: ' + e.getMessage());
}
} else {
// Success
System.debug('Payment Method Token created successfully.');
}
} catch (Exception e) {
System.debug('Error creating Payment Method: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Payment Gateways: ' + e.getMessage());
}

Payment Method List

Apex Example
try {
// Get the payment gateway to use
ChargentBase__Gateway__c paymentGateway = [SELECT Id FROM ChargentBase__Gateway__c WHERE Name = 'Stripe Test' LIMIT 1];

// initialize variables
String phone = '{{$randomPhoneNumber}}';
String email = '{{$randomeEmail}}';

/*
* Credit Card Payment Method
*/

String cardNumber = '{{cardNumber}}';
String cardType = '{{cardType}}';
String cvv = '{{cvv}}';
String expiryMonth = String.valueOf(Date.today().month());
String expiryYear = String.valueOf(Date.today().year() + 3);

// credit card payment data
Map<String,Object> cardPaymentMethodMap = new Map<String, Object>();
cardPaymentMethodMap.put('cardType', cardType);
cardPaymentMethodMap.put('cardNumber', cardNumber);
cardPaymentMethodMap.put('expiryMonth', expiryMonth);
cardPaymentMethodMap.put('expiryYear', expiryYear);
cardPaymentMethodMap.put('cvv', cvv);
cardPaymentMethodMap.put('cardHolderName', 'Chargent Test');
cardPaymentMethodMap.put('cardHolderFirstName', 'Chargent');
cardPaymentMethodMap.put('cardHolderLastName', 'Test');

// billing address
Map<String,Object> creditCardAddressMap = new Map<String, Object>();
creditCardAddressMap.put('street', '123 Street St');
creditCardAddressMap.put('state', 'California');
creditCardAddressMap.put('city', 'San Francisco');
creditCardAddressMap.put('postalcode', '94105');
creditCardAddressMap.put('country', 'US');
creditCardAddressMap.put('companyName', 'Chargent Test');

// payment method tokenization
Map<String,Object> creditCardPaymentRequestMap = new Map<String, Object>();
creditCardPaymentRequestMap.put('phone', phone);
creditCardPaymentRequestMap.put('email', email);
creditCardPaymentRequestMap.put('paymentGatewayId', paymentGateway.Id);
creditCardPaymentRequestMap.put('cardPaymentMethod', cardPaymentMethodMap);
creditCardPaymentRequestMap.put('address', creditCardAddressMap);

/*
* Bank Account Payment Method
*/

String bankAccountType = 'Checking';
String bankInstitutionName = '{{$randomBankAccountName}}';
String bankAccountNumber = '{{bankAccountNumber}}';
String routingNumber = '{{routingNumber}}';
String bankAccountHolderName = 'Chargent Test';
String bankAccountHolderFirstName = 'Chargent';
String bankAccountHolderLastName = 'Test';

// credit card payment data
Map<String,Object> bankAccountPaymentMethodMap = new Map<String, Object>();
bankAccountPaymentMethodMap.put('bankAccountType', bankAccountType);
bankAccountPaymentMethodMap.put('bankInstitutionName', bankInstitutionName);
bankAccountPaymentMethodMap.put('bankAccountNumber', bankAccountNumber);
bankAccountPaymentMethodMap.put('routingNumber', routingNumber);
bankAccountPaymentMethodMap.put('bankAccountHolderName', bankAccountHolderName);
bankAccountPaymentMethodMap.put('bankAccountHolderFirstName', bankAccountHolderFirstName);
bankAccountPaymentMethodMap.put('bankAccountHolderLastName', bankAccountHolderLastName);

// billing address
Map<String,Object> bankAccountAddressMap = new Map<String, Object>();
bankAccountAddressMap.put('street', '123 Street St');
bankAccountAddressMap.put('state', 'California');
bankAccountAddressMap.put('city', 'San Francisco');
bankAccountAddressMap.put('postalcode', '94105');
bankAccountAddressMap.put('country', 'US');
bankAccountAddressMap.put('companyName', 'Chargent Test');

// payment method tokenization
Map<String,Object> bankAccountPaymentRequestMap = new Map<String, Object>();
bankAccountPaymentRequestMap.put('phone', phone);
bankAccountPaymentRequestMap.put('email', email);
bankAccountPaymentRequestMap.put('paymentGatewayId', paymentGateway.Id);
bankAccountPaymentRequestMap.put('bankAccountPaymentMethod', bankAccountPaymentMethodMap);
bankAccountPaymentRequestMap.put('address', bankAccountAddressMap);

// action service request
try {
Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('paymentMethods', new List<Object>{creditCardPaymentRequestMap, bankAccountPaymentRequestMap});
actionsMap.put('action', 'CreatePM');
actionsMap.put('feature', 'APEX');
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);

// process request
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
System.debug('Payment Methods Token create error.');
} else {
// Success
System.debug('Payment Methods Token created successfully.');
}
} catch (Exception e) {
System.debug('Error creating Payment Method: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Payment Gateways: ' + e.getMessage());
}

Import Payment Method Tokens

Payment Method Token List

Apex Example
try {
// Get the payment gateway to use
ChargentBase__Gateway__c paymentGateway = [SELECT Id FROM ChargentBase__Gateway__c WHERE Name = 'Vantiv Test' LIMIT 1];

// initialize variables
String phone = '{{$randomPhoneNumber}}';
String email = '{{$randomeEmail}}';

/*
* Credit Card Payment Method
*/

String cardLast4 = '{{cardLast4}}';
String cardType = '{{cardType}}';
String expiryMonth = String.valueOf(Date.today().month());
String expiryYear = String.valueOf(Date.today().year() + 3);
String cardToken = '{{cardToken}}';

// billing address
Map<String,Object> creditCardAddressMap = new Map<String, Object>();
creditCardAddressMap.put('billingAddress', '123 Street St');
creditCardAddressMap.put('billingCity', 'San Francisco');
creditCardAddressMap.put('billingState', 'California');
creditCardAddressMap.put('billingPostalCode', '94105');
creditCardAddressMap.put('billingCountry', 'US');
creditCardAddressMap.put('billingEmail', email);
creditCardAddressMap.put('billingPhone', phone);

// payment method tokenization
Map<String,Object> creditCardTokenMap = new Map<String, Object>();
creditCardTokenMap.put('token', cardToken);
creditCardTokenMap.put('paymentGatewayId', paymentGateway.Id);

// credit card payment data
Map<String,Object> creditCardPaymentMethodDataMap = new Map<String, Object>();
creditCardPaymentMethodDataMap.put('cardPaymentMethodName', 'Chargent Credit Card');
creditCardPaymentMethodDataMap.put('paymentMethodType', 'Credit Card');
creditCardPaymentMethodDataMap.put('cardHolderName', 'Chargent Test');
creditCardPaymentMethodDataMap.put('cardHolderFirstName', 'Chargent');
creditCardPaymentMethodDataMap.put('cardHolderLastName', 'Test');
creditCardPaymentMethodDataMap.put('cardType', cardType);
creditCardPaymentMethodDataMap.put('cardExpirationMonth', expiryMonth);
creditCardPaymentMethodDataMap.put('cardExpirationYear', expiryYear);
creditCardPaymentMethodDataMap.put('cardLast4', cardLast4);
creditCardPaymentMethodDataMap.put('company', 'Chargent');
creditCardPaymentMethodDataMap.put('nickName', 'Chargent Test Credit Card');
creditCardPaymentMethodDataMap.put('token', creditCardTokenMap);
creditCardPaymentMethodDataMap.put('billing', creditCardAddressMap);

Map<String,Object> creditCardPaymentMethodMap = new Map<String, Object>();
creditCardPaymentMethodMap.put('cardPaymentMethod', creditCardPaymentMethodDataMap);

/*
* Bank Account Payment Method
*/

String bankAccountType = '{{bankAccountType}}';
String bankInstitutionName = '{{bankInstitutionName}}';
String routingNumber = '{{routingNumber}}';
String accountLast4 = '{{accountLast4}}';
String bankAccountHolderName = '{{bankAccountHolderName}}';
String bankAccountHolderFirstName = '{{bankAccountHolderFirstName}}';
String bankAccountHolderLastName = '{{bankAccountHolderLastName}}';

// billing address
Map<String,Object> bankAccountAddressMap = new Map<String, Object>();
bankAccountAddressMap.put('billingAddress', '123 Street St');
bankAccountAddressMap.put('billingCity', 'San Francisco');
bankAccountAddressMap.put('billingState', 'California');
bankAccountAddressMap.put('billingPostalCode', '94105');
bankAccountAddressMap.put('billingCountry', 'US');
bankAccountAddressMap.put('billingEmail', email);
bankAccountAddressMap.put('billingPhone', phone);

// payment method tokenization
Map<String,Object> bankAccountTokenMap = new Map<String, Object>();
bankAccountTokenMap.put('token', cardToken);
bankAccountTokenMap.put('paymentGatewayId', paymentGateway.Id);

// bank account payment data
Map<String,Object> bankAccountPaymentMethodDataMap = new Map<String, Object>();
bankAccountPaymentMethodDataMap.put('bankAccountPaymentMethodName', 'Chargent Bank Account');
bankAccountPaymentMethodDataMap.put('paymentMethodType', 'Bank Account');
bankAccountPaymentMethodDataMap.put('bankAccountHolderName', 'Chargent Test');
bankAccountPaymentMethodDataMap.put('bankAccountHolderFirstName', 'Chargent');
bankAccountPaymentMethodDataMap.put('bankAccountHolderLastName', 'Test');
bankAccountPaymentMethodDataMap.put('routingNumber', routingNumber);
bankAccountPaymentMethodDataMap.put('accountLast4', accountLast4);
bankAccountPaymentMethodDataMap.put('bankAccountType', bankAccountType);
bankAccountPaymentMethodDataMap.put('bankAccountValidatedOn', String.valueOf(Date.today()));
bankAccountPaymentMethodDataMap.put('bankInstitutionName', bankInstitutionName);
bankAccountPaymentMethodDataMap.put('company', 'Chargent');
bankAccountPaymentMethodDataMap.put('token', bankAccountTokenMap);
bankAccountPaymentMethodDataMap.put('billing', bankAccountAddressMap);

Map<String,Object> bankAccountPaymentMethodMap = new Map<String, Object>();
bankAccountPaymentMethodMap.put('bankAccountPaymentMethod', bankAccountPaymentMethodDataMap);

// action service request
try {
Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('data', JSON.serialize(new List<Object>{creditCardPaymentMethodMap, bankAccountPaymentMethodMap}));
actionsMap.put('action', 'Import');
actionsMap.put('feature', 'APEX');
System.debug('Actions Map: ' + JSON.serializePretty(actionsMap));
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);
System.debug('Action Response: ' + JSON.serialize(actionResponseMap));

// process request
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
System.debug('Payment Methods Token import error.');
} else {
// Success
System.debug('Payment Methods Token imported successfully.');
}
} catch (Exception e) {
System.debug('Error importing Payment Method Token: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Payment Gateways: ' + e.getMessage());
}

Multiple Gateway Tokenization

Payment Method

Apex Example
try {
// Get the payment gateways to use
List<String> paymentGatewayNames = new List<String>{'Stripe Test', 'Vantiv Test', 'Forte Test'};
List<ChargentBase__Gateway__c> paymentGatewayList = [SELECT Id FROM ChargentBase__Gateway__c WHERE Name IN :paymentGatewayNames];

// initialize variables
String paymentGatewayId = paymentGatewayList[0].Id;
String paymentGatewayId2 = paymentGatewayList[1].Id;
String paymentGatewayId3 = paymentGatewayList[2].Id;

String phone = '{{$randomPhoneNumber}}';
String email = '{{$randomeEmail}}';

String cardNumber = '{{cardNumber}}';
String cardType = '{{cardType}}';
String cvv = '{{cvv}}';
String expiryMonth = String.valueOf(Date.today().month());
String expiryYear = String.valueOf(Date.today().year() + 3);

// credit card payment data
Map<String,Object> cardPaymentMethodMap = new Map<String, Object>();
cardPaymentMethodMap.put('cardType', cardType);
cardPaymentMethodMap.put('cardNumber', cardNumber);
cardPaymentMethodMap.put('expiryMonth', expiryMonth);
cardPaymentMethodMap.put('expiryYear', expiryYear);
cardPaymentMethodMap.put('cvv', cvv);
cardPaymentMethodMap.put('cardHolderName', 'Chargent Test');
cardPaymentMethodMap.put('cardHolderFirstName', 'Chargent');
cardPaymentMethodMap.put('cardHolderLastName', 'Test');

// billing address
Map<String,Object> addressMap = new Map<String, Object>();
addressMap.put('street', '123 Street St');
addressMap.put('state', 'California');
addressMap.put('city', 'San Francisco');
addressMap.put('postalcode', '94105');
addressMap.put('country', 'US');
addressMap.put('companyName', 'Chargent Test');

// payment method tokenization
Map<String,Object> paymentRequestMap = new Map<String, Object>();
paymentRequestMap.put('phone', phone);
paymentRequestMap.put('email', email);
paymentRequestMap.put('paymentGatewayId', paymentGatewayId);
paymentRequestMap.put('paymentGatewayIds', new List<String>{paymentGatewayId2, paymentGatewayId3});
paymentRequestMap.put('multipleTokenization', true);
paymentRequestMap.put('cardPaymentMethod', cardPaymentMethodMap);
paymentRequestMap.put('address', addressMap);

// action service request
try {
Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('paymentMethods', new List<Object>{paymentRequestMap});
actionsMap.put('action', 'CreatePM');
actionsMap.put('feature', 'APEX');
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);

// process request
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
System.debug('Payment Methods Tokens create error.');
} else {
// Success
System.debug('Payment Methods Tokens created successfully.');
}
} catch (Exception e) {
System.debug('Error creating Payment Method: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Payment Gateways: ' + e.getMessage());
}

Payment Method List

Apex Example
try {
// Get the payment gateways to use
List<String> paymentGatewayNames = new List<String>{'Stripe Test', 'Vantiv Test', 'Forte Test'};
List<ChargentBase__Gateway__c> paymentGatewayList = [SELECT Id FROM ChargentBase__Gateway__c WHERE Name IN :paymentGatewayNames];

// initialize variables
String paymentGatewayId = paymentGatewayList[0].Id;
String paymentGatewayId2 = paymentGatewayList[1].Id;
String paymentGatewayId3 = paymentGatewayList[2].Id;

String phone = '{{$randomPhoneNumber}}';
String email = '{{$randomeEmail}}';

/*
* Credit Card Payment Method
*/

String cardNumber = '{{cardNumber}}';
String cardType = '{{cardType}}';
String cvv = '{{cvv}}';
String expiryMonth = String.valueOf(Date.today().month());
String expiryYear = String.valueOf(Date.today().year() + 3);

// credit card payment data
Map<String,Object> cardPaymentMethodMap = new Map<String, Object>();
cardPaymentMethodMap.put('cardType', cardType);
cardPaymentMethodMap.put('cardNumber', cardNumber);
cardPaymentMethodMap.put('expiryMonth', expiryMonth);
cardPaymentMethodMap.put('expiryYear', expiryYear);
cardPaymentMethodMap.put('cvv', cvv);
cardPaymentMethodMap.put('cardHolderName', 'Chargent Test');
cardPaymentMethodMap.put('cardHolderFirstName', 'Chargent');
cardPaymentMethodMap.put('cardHolderLastName', 'Test');

// billing address
Map<String,Object> creditCardAddressMap = new Map<String, Object>();
creditCardAddressMap.put('street', '123 Street St');
creditCardAddressMap.put('state', 'California');
creditCardAddressMap.put('city', 'San Francisco');
creditCardAddressMap.put('postalcode', '94105');
creditCardAddressMap.put('country', 'US');
creditCardAddressMap.put('companyName', 'Chargent Test');

// payment method tokenization
Map<String,Object> creditCardPaymentRequestMap = new Map<String, Object>();
creditCardPaymentRequestMap.put('phone', phone);
creditCardPaymentRequestMap.put('email', email);
creditCardPaymentRequestMap.put('paymentGatewayId', paymentGatewayId);
creditCardPaymentRequestMap.put('paymentGatewayIds', new List<String>{paymentGatewayId2, paymentGatewayId3});
creditCardPaymentRequestMap.put('multipleTokenization', true);
creditCardPaymentRequestMap.put('cardPaymentMethod', cardPaymentMethodMap);
creditCardPaymentRequestMap.put('address', creditCardAddressMap);

/*
* Bank Account Payment Method
*/

String bankAccountType = 'Checking';
String bankInstitutionName = '{{$randomBankAccountName}}';
String bankAccountNumber = '{{bankAccountNumber}}';
String routingNumber = '{{routingNumber}}';
String bankAccountHolderName = 'Chargent Test';
String bankAccountHolderFirstName = 'Chargent';
String bankAccountHolderLastName = 'Test';

// credit card payment data
Map<String,Object> bankAccountPaymentMethodMap = new Map<String, Object>();
bankAccountPaymentMethodMap.put('bankAccountType', bankAccountType);
bankAccountPaymentMethodMap.put('bankInstitutionName', bankInstitutionName);
bankAccountPaymentMethodMap.put('bankAccountNumber', bankAccountNumber);
bankAccountPaymentMethodMap.put('routingNumber', routingNumber);
bankAccountPaymentMethodMap.put('bankAccountHolderName', bankAccountHolderName);
bankAccountPaymentMethodMap.put('bankAccountHolderFirstName', bankAccountHolderFirstName);
bankAccountPaymentMethodMap.put('bankAccountHolderLastName', bankAccountHolderLastName);

// billing address
Map<String,Object> bankAccountAddressMap = new Map<String, Object>();
bankAccountAddressMap.put('street', '123 Street St');
bankAccountAddressMap.put('state', 'California');
bankAccountAddressMap.put('city', 'San Francisco');
bankAccountAddressMap.put('postalcode', '94105');
bankAccountAddressMap.put('country', 'US');
bankAccountAddressMap.put('companyName', 'Chargent Test');

// payment method tokenization
Map<String,Object> bankAccountPaymentRequestMap = new Map<String, Object>();
bankAccountPaymentRequestMap.put('phone', phone);
bankAccountPaymentRequestMap.put('email', email);
bankAccountPaymentRequestMap.put('paymentGatewayId', paymentGatewayId);
bankAccountPaymentRequestMap.put('paymentGatewayIds', new List<String>{paymentGatewayId2, paymentGatewayId3});
bankAccountPaymentRequestMap.put('multipleTokenization', true);
bankAccountPaymentRequestMap.put('bankAccountPaymentMethod', bankAccountPaymentMethodMap);
bankAccountPaymentRequestMap.put('address', bankAccountAddressMap);

// action service request
try {
Map<String, Object> actionsMap = new Map<String, Object>();
actionsMap.put('paymentMethods', new List<Object>{creditCardPaymentRequestMap, bankAccountPaymentRequestMap});
actionsMap.put('action', 'CreatePM');
actionsMap.put('feature', 'APEX');
String actionResponse = ChargentBase.ActionService.getService().performAction(JSON.serialize(actionsMap));
Map<String, Object> actionResponseMap = (Map<String, Object>) JSON.deserializeUntyped(actionResponse);

// process request
if (actionResponseMap.get('operationSuccess') == false) {
// Failure
System.debug('Payment Methods Tokens create error.');
} else {
// Success
System.debug('Payment Methods Tokens created successfully.');
}
} catch (Exception e) {
System.debug('Error creating Payment Method: ' + e.getMessage());
}
} catch (Exception e) {
System.debug('Error getting Payment Gateways: ' + e.getMessage());
}