Newer Version Available

This content describes an older version of this product. View Latest

Tokenization Service Apex Class Implementation

Use the tokenization service to hide sensitive customer payment method data. The Tokenization service uses PaymentMethodTokenizationRequest, PaymentMethodTokenizationResponse, and CardPaymentMethodRequest. Implement these classes in your payment gateway adapter.
Available in: Salesforce Spring '21 and later

PaymentMethodTokenizationRequest
Represents a request to tokenize a payment method.
This class holds information about the tokenize request. Gateway adapters use this information while constructing a tokenization JSON request, which is sent to the payment gateway.
PaymentMethodTokenizationRequest uses a constructor to build the tokenization request object in Salesforce. The PaymentMethodTokenizationRequest constructor takes no arguments. You can invoke it as follows:
1CommercePayments.PaymentMethodTokenizationRequest pmtr = new CommercePayments.PaymentMethodTokenizationRequest();
If you want to build a payment method tokenization request object, you can also invoke a constructor with arguments for the payment gateway ID. However, the constructor only works in a test environment, and throws an exception if used outside of the Apex test context. In the example below, we've provided a sample number.
1CommercePayments.PaymentMethodTokenizationRequest pmtr = new CommercePayments.PaymentMethodTokenizationRequest(00000585923);
CardPaymentMethodRequest

Contains details about the card used as a payment method for authorization, sale, or tokenization transaction requests. The gateway adapter uses this information to construct a transaction JSON request to send to the payment gateway. CardPaymentMethodRequest is available as the cardPaymentMethod field in the SaleApiPaymentMethodRequest Class, AuthApiPaymentMethodRequest Class, and PaymentMethodTokenizationRequest Class

CardPaymentMethodRequest uses a constructor to build the card payment method request object in Salesforce. The constructor takes cardCategory as an argument, which defines whether the card payment method is a credit card or debit card.
PaymentMethodTokenizationResponse
This class contains details about a tokenize transaction gateway response. PaymentMethodTokenizationResponse fields are populated from the response obtained from the payment gateway. This class is mapped to a response class in the Java layer, which the payments platform uses to construct the Tokenize gateway response shown to the user.

Implementing Tokenization Classes in Your Gateway Adapter

Add your tokenization classes to your payment gateway adapter. The following code retrieves PaymentMethodTokenizationRequest from the PaymentGatewayContext Class.

1commercepayments.PaymentMethodTokenizationRequest tokenizeRequest= (commercepayments.PaymentMethodTokenizationRequest)gatewayContext.getPaymentRequest()
2commercepayments.CardPaymentMethodRequest cardPaymentMethodRequest = tokenizeRequest.cardPaymentMethod;

You can then build a PaymentMethodTokenizationResponse object. In this example we’ve provided placeholder values for each of the setter methods.

1commercepayments.PaymentMethodTokenizationResponse tokenizeResponse = new commercepayments.PaymentMethodTokenizationResponse();
2tokenizeResponse.setGatewayToken("gatewayToken");
3tokenizeResponse.setGatewayTokenDetails("gatewayTokenDetails");
4tokenizeResponse.setGatewayAvsCode("avsCode");
5tokenizeResponse.setGatewayMessage("transaction message");
6tokenizeResponse.setGatewayResultCode("res_code");
7tokenizeResponse.setGatewayResultCodeDescription("result code description");
8tokenizeResponse.setSalesforceResultCodeInfo(getSalesforceResultCodeInfo(commercepayments.SalesforceResultCode.SUCCESS.name()));
9tokenizeResponse.setGatewayDate("02-04-2020");