Skip to main content
Version: Lab (preview)

Tokenizing A Payment Method

This quickstart will show you how to securely tokenize a user's payment method so that it can be recalled in a future payment or through a recurring payment.

Before you start

In order to tokenize a payment method you need be set up to accept online payments.

Accepting Online Payments

Run tokenization method​

This function is used to tokenize a card or bank account. For further transactions, this token may be used in place of the real payment information, lowering the risk involved with keeping and transferring sensitive data.

javascript
// optionally provide details about the payor
const PAYOR_INFO = {
"first_name": "Some",
"last_name": "Body",
"email": "somebody@gmail.com",
}
const TOKENIZE_PAYMENT_METHOD_PARAMETERS = {
payorInfo: PAYOR_INFO, // optional
}

paytheory.tokenizePaymentMethod(TOKENIZE_PAYMENT_METHOD_PARAMETERS)

Optional parameters that can be passed into your payment method token​

These are the parameters that you can pass into the tokenizePaymentMethod function to tokenize a card or bank account.

Keytypedescription
payorInfoobjectsee the Payor Info object below for details
billingInfoobjectsee the Billing Info object below for details. This is required if you are not using the zip hosted field.
metadataobjectAn object that will be stored with the token and can be used to track the token.
payorIdstring
  • The Pay Theory payor ID to use for the payment. Allows for user to manage identities.
  • This cannot be used if also using the payorInfo parameter.
expandedResponsebooleanDefaults to false. With true, a successful response returns the full payment method, and a processor rejection returns FAILED with the reason.

The function returns a Promise that will contain an object with a key of type. You can expect the following values for type.

Promise return type and expected values​

TypeDescription
TOKENIZEDThe payment method was tokenized successfully and the token will be in the body key.
FAILEDThe processor rejected the payment method. The reason is in the body key. Only returned with expandedResponse: true.
ERRORThe transact call had an error while processing and the error details will be in the error key.

After the payment is successful you can see the token has been generated and this is how the body looks.

TokenizingPayments

Tokenized response​

This is the value of the body key in the response if the type is TOKENIZED:

Keytypedescription
payment_method_idstringThe Pay Theory id for the payment method token.
payor_idstringThe Pay Theory id for the payor that was used for the payment method token.
last_fourstringThe last four digits of the card number or account number.
brandstringThe brand of the card.
payment_typestringThe type of payment method tokenized. This will be card or ACH.
metadataJSONThe metadata that was passed in when tokenizing the payment method.

Failed response​

This is the value of the body key in the response if the type is FAILED:

Keytypedescription
failure_codestringThe failure code. See Failure codes.
failure_textstringThe failure message.

Without expandedResponse, the same rejection comes back as an ERROR that starts with SOCKET_ERROR: Token validation failed:.

Error Response​

If the type is ERROR, the response has an error key with a string. The string starts with one of the SDK error codes.


View Full Code​

View Full Code

Click below link to download the files.

Download