Starting a payment with Affirm on the web with a virtual card consists of creating a checkout form, tokenizing customer information securely, and generating a virtual card. You can then process the card through your existing payment gateway.

This guide shows you how to include `Affirm.js` and Affirm Checkout on your web page to create an Affirm virtual card.

## **Step 1: Set up Affirm.js**

Include the following script in the head section on every page on your site. This script should always be loaded directly from the Affirm domain.

Use only your sandbox API keys and domain for testing and development

This ensures that you don't accidentally modify your live transactions. Note that Affirm does _not_ generate real virtual cards in the sandbox environment. If applicable, please work with your technical account manager on live testing.

Embed Affirm.js
Open Recipe

[See here for supported locale and country codes](🔗).

Once you include the script above, a defined instance of Affirm will be created on your client. You will gain access to methods within the Affirm object to trigger multiple actions.

## **Step 2: Create the checkout object**

When a customer chooses Affirm to pay for a purchase, a [checkout object](🔗) containing the order details should be created with <code>affirm.checkout.open_vcn()</code> so that Affirm can render the checkout flow. When the customer completes their purchase, you will need to handle the callbacks or leverage redirects. See below for a sample checkout object:

### Method arguments

MethodData typeDescription
`checkoutObject` <span style="color:red">required</span>_object_The object containing the customer's checkout data. [See details](🔗).


  • Our virtual card product only supports modal mode. The Affirm checkout will open in a modal that floats on top of your checkout page. This is indicated in the checkout object when setting the mode parameter to modal.

  • Set the use_vcn parameter to true to enable card details to be generated on loan origination.

  • affirm.checkout() validates the required data in the checkout object.

## **Step 3: Launch checkout and handle callbacks**

After you create the checkout object, you can launch the Affirm checkout modal using the `affirm.checkout.open_vcn()` function. When the customer confirms their Affirm loan, we send a javascript callback on success or on error, depending on the outcome. In the event of a successful checkout, you will retrieve the card details and the `checkout_id` from the callback. See below for a template:


If you do not want to retrieve the card details from the client-side, we can always pass them back via the server-side only via our [Read Card API](🔗) (for PCI reasons). Please contact Merchant Help for further information.

### Returns

Returns callbacks that resolve in a `card_checkout`, `error_response` or `checkout_validation_error`. Those objects have either:

  • `card_checkout`: A card object was successfully created.

  • `error_response`: There was an error. Includes the `checkout_id` for the attempt and the `reason` why it failed.

  • `checkout_validation_error`: There was a validation error with the checkout request submitted.

### Retrieve full [card details](🔗) via success callback

### Alternative success callback: Omit [card details](🔗) and retrieve the `checkout_token` client-side and obtain card details via your server

## **Step 4: Card management**

Once you receive the card details from the checkout response or [Read Card](🔗) API, you can submit authorizations, captures, and refunds to the virtual card using your existing payment rails.