Skip to main content
Version: Live

Functions

These functions mount the hosted fields and run payments and tokenization through them. Each one is on window.paytheory. The TypeScript declarations type every parameter and response on this page.


payTheoryFields​

Mounts the hosted fields on the page and connects them to Pay Theory.

paytheory.payTheoryFields({
apiKey: 'YOUR_API_KEY',
amount: 1000,
country: 'USA',
placeholders: PLACEHOLDERS,
styles: STYLES,
})
Keytypedescription
apiKeyStringRequired. Your Pay Theory API Key.
amountIntAmount of the payment, in cents. Pass it to have the SDK calculate the service fee and report it to the state observer. Change it later with updateAmount.
countryStringUSA or CAN. Match the merchant's country_code on the merchant object. Defaults to USA.
placeholdersObjectPlaceholder text, keyed by field name such as card-number or billing-zip.
stylesStyles objectHow the hosted fields look.

payTheoryFields returns a Promise. It resolves to { type: 'READY', body: true } when the fields are connected, or an error response.


transact​

Submits a payment with the mounted card or ACH fields, or creates a barcode with the cash fields.

// Amount in cents
const AMOUNT = 1000

// Optional details about the payor
const PAYOR_INFO = {
first_name: "Pat",
last_name: "Theodore",
email: "pat@example.com",
phone: "+15555550123",
personal_address: {
line1: "123 Main Street",
city: "Cincinnati",
region: "OH",
postal_code: "45209",
country: "USA"
}
}

const PAYMENT_METADATA = {
"student-name": "Pat Theodore"
};

const TRANSACTING_PARAMETERS = {
amount: AMOUNT,
payorInfo: PAYOR_INFO, // optional
metadata: PAYMENT_METADATA, // optional
feeMode: paytheory.MERCHANT_FEE, // optional
idempotencyKey: "order-1042", // optional
accountCode: "code-123456789", // optional
reference: "field-trip", // optional
invoiceId: "ptl_inv_2O0yTY3k5AIyDc8NQ2SvhC", // optional
receiptDescription: "School Technology Fees", // optional
sendReceipt: true, // optional
}

const result = await paytheory.transact(TRANSACTING_PARAMETERS)

Pass either payorInfo or payorId, not both. Pass either invoiceId or recurringId, not both. The SDK returns an INVALID_PARAM error for either pair.

Keytypedescription
amountIntRequired. Amount of the payment, in cents.
accountCodeStringCode for tracking a payment or group of payments. Shows on the transaction and in the Pay Theory portal.
billingInfoBilling infoBilling name and address. Required for cards if you don't mount the zip hosted field.
expandedResponseBooleanDefaults to false. With true, SUCCESS and FAILED responses return the full Transaction.
feeIntFee to charge, in cents. With SERVICE_FEE, pass the fee the state observer reports. Pay Theory checks it against the fee it calculates and returns an error if they don't match.
feeModeStringpaytheory.MERCHANT_FEE (default) or paytheory.SERVICE_FEE. With SERVICE_FEE, Pay Theory adds the fee to the amount and charges it to the payor.
healthExpenseTypeStringHealthcare category: HEALTHCARE, RX, VISION, CLINICAL, COPAY, DENTAL or TRANSIT.
idempotencyKeyStringYour key for this payment. If you retry with the same key and the same details, Pay Theory returns the first response instead of charging again. Cash payments ignore it. See Idempotency.
invoiceIdStringID of the invoice this payment pays.
level3DataSummaryLevel 3 data summaryLevel 3 summary data for the transaction.
metadataObjectKey-value data stored with the transaction.
oneTimeUseTokenBooleanDefaults to false. With true, Pay Theory sets the payment method's is_active to false after the payment, so it can't be charged again.
payorIdStringID of an existing payor.
payorInfoPayor infoDetails for a new payor.
receiptDescriptionStringDescription on the receipt. Defaults to "Payment from your merchant".
recurringIdStringID of the recurring payment this payment pays. The amount must be a multiple of the recurring amount per payment.
referenceStringYour own description of the payment. Shows on the transaction and in the Pay Theory portal.
sendReceiptBooleanPass true to email a receipt. The payor needs an email address, from payorInfo or the payor with payorId.

transact returns a Promise that resolves to an object with a type:

  • SUCCESS: The payment succeeded. Details are in body.
  • FAILED: The payment was declined. Details are in body.
  • CASH: The cash barcode was created. Details are in body.
  • ERROR: The SDK couldn't run the payment. The message is in error.

If you retry with the same idempotencyKey and the same details, transact returns the first response. With changed details, it returns an ERROR whose message includes IDEMPOTENCY_KEY_PAYLOAD_MISMATCH and the original transaction_id. Either way, no new payment is created. On the same page, you can retry after an ERROR or FAILED response. After SUCCESS or CASH, the fields are used up and transact returns ACTION_COMPLETE.

For a cash barcode, the SDK asks the browser for the payor's location to build the map URL. The first time, the browser asks the payor to allow it.


tokenizePaymentMethod​

Saves the card or bank account in the mounted fields as a payment method token, without charging it.

const TOKENIZE_PAYMENT_METHOD_PARAMETERS = {
payorInfo: PAYOR_INFO, // optional
metadata: { "system_id": "123456789" }, // optional
expandedResponse: true, // optional
}

const result = await paytheory.tokenizePaymentMethod(TOKENIZE_PAYMENT_METHOD_PARAMETERS)
Keytypedescription
payorInfoPayor infoDetails for a new payor. Don't combine with payorId.
payorIdStringID of an existing payor.
billingInfoBilling infoBilling name and address. Required for cards if you don't mount the zip hosted field.
metadataObjectKey-value data stored with the payment method token.
skipValidationBooleanDefaults to false. With true, Pay Theory saves the payment method without validating it first.
expandedResponseBooleanDefaults to false. With true, a TOKENIZED response returns the full payment method, and a processor rejection returns FAILED with the reason.

tokenizePaymentMethod returns a Promise that resolves to an object with a type:

  • TOKENIZED: The payment method was saved. Details are in body.
  • FAILED: The processor rejected the payment method. Only returned with expandedResponse: true.
  • ERROR: The SDK couldn't save the payment method. The message is in error.

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


updateAmount​

Changes the amount on every mounted hosted field and recalculates the service fee.

// Amount in cents
const result = await paytheory.updateAmount(2000)

if (result !== true) {
console.error('Could not update the amount:', result.error)
}
Keytypedescription
amountIntThe new amount, in cents.

updateAmount returns a Promise that resolves to true, or an error response that starts with one of these codes:

  • INVALID_PARAM: The amount isn't a number, is 0 or has a decimal.
  • NOT_READY: The fields aren't ready yet.
  • NO_FIELDS: There are no hosted fields on the page.
  • SOCKET_ERROR: The SDK couldn't reconnect to Pay Theory.

After an update, the state observer fires with the new amount and fees.


Parameter schemas​

Payor info object​

Pay Theory creates a payor from this data and returns its ID as payor_id.

{
"first_name": "Pat",
"last_name": "Theodore",
"email": "pat@example.com",
"phone": "+15555550123",
"personal_address": {
"line1": "123 Main Street",
"city": "Cincinnati",
"region": "OH",
"postal_code": "45209",
"country": "USA"
}
}
Keytypedescription
first_nameStringPayor's first name.
last_nameStringPayor's last name.
emailStringPayor's email address. Needed for sendReceipt.
phoneStringPayor's phone number.
personal_addressAddressPayor's address.
same_as_billingBooleanWith true, Pay Theory uses the billing address as the payor's address. Pass only email and phone with it.

Billing info object​

The billing name and address for the payment method. Required for cards if you don't mount the billing hosted fields.

Keytypedescription
nameStringName on the card or account.
addressAddressBilling address.

Address object​

{
"line1": "123 Main Street",
"line2": "Suite 200",
"city": "Cincinnati",
"region": "OH",
"postal_code": "45209",
"country": "USA"
}
Keytypedescription
line1StringStreet address.
line2StringSecond address line.
cityStringCity.
regionStringState or province.
postal_codeStringPostal code.
countryStringCountry.

Level 3 data summary object​

Summary data for Level 3 card processing. All amounts are in cents.

Keytypedescription
tax_amtIntTotal tax amount.
tax_indStringHow tax applies: TAX_AMOUNT_PROVIDED, NOT_TAXABLE or NO_TAX_INFO_PROVIDED.
purch_idfrStringPurchase identifier.
order_numStringMerchant's order number.
discnt_amtIntTotal discount amount.
frght_amtIntTotal freight or shipping amount.
duty_amtIntTotal duty amount.
dest_postal_codeStringPostal code the goods ship to.
prod_descString[]Descriptions of the products in the transaction.

Responses​

Success response​

The body of a SUCCESS response. With expandedResponse: true, body is the full Transaction instead.

Keytypedescription
receipt_numberStringThe transaction ID.
amountIntAmount charged, in cents. Includes the service fee.
service_feeIntService fee, in cents.
last_fourStringLast four digits of the card or account number.
brandStringCard brand.
created_atStringWhen the transaction was created.
stateStringStatus of the transaction.
metadataObjectThe metadata you passed.
payor_idStringID of the payor.
payment_method_idStringID of the payment method token.

Failed response​

The body of a FAILED response. With expandedResponse: true, body is the full Transaction instead.

Keytypedescription
receipt_numberStringThe transaction ID.
last_fourStringLast four digits of the card or account number.
brandStringCard brand.
stateStringStatus of the transaction. This is FAILURE.
typeStringDescription of the failure.
payor_idStringID of the payor.
reasonObjectWhy the payment failed. See below.

Reason object

Keytypedescription
failure_codeStringFailure code. See Failure codes.
failure_textStringFailure message.

Cash response​

The body of a CASH response.

Keytypedescription
barcodeUrlStringURL of the barcode image.
mapUrlStringURL of a map of retail locations that accept the barcode.

Tokenized response​

The body of a TOKENIZED response. With expandedResponse: true, body is the full payment method instead.

Keytypedescription
payment_method_idStringID of the payment method token.
payor_idStringID of the payor.
last_fourStringLast four digits of the card or account number.
brandStringCard brand.
expirationStringCard expiration date, as MMYY.
payment_typeStringcard or ach.
metadataObjectThe metadata you passed.

Failed tokenization response​

The body of a FAILED response from tokenizePaymentMethod. Returned only with expandedResponse: true.

{
"type": "FAILED",
"body": {
"failure_code": "GENERIC_DECLINE",
"failure_text": "The transaction was declined for an unknown reason."
}
}
Keytypedescription
failure_codeStringFailure code. See Failure codes.
failure_textStringFailure message.

Error response​

An ERROR response has an error string instead of a body. The string starts with one of the error codes.

{
"type": "ERROR",
"error": "NOT_READY: Error message"
}

Expanded responses​

With expandedResponse: true, the SDK returns the same objects as the API:

  • transact: SUCCESS and FAILED bodies are a Transaction, with its payment_method and payor. status tells you which one you got.
  • tokenizePaymentMethod: a TOKENIZED body is a payment method, with its payor.

Use expanded responses when your system already stores the API's objects, so you don't need a second query.