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.

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 `affirm.checkout()` so that Affirm can render the checkout flow. See below for a sample checkout object:

️ Note:

  • 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 don't want to retrieve the card details from the client-side, we can always pass them back via the server-side only (for PCI reasons). Please contact Merchant Help for further information.

## Step 4: Card management

Once you receive the card details from the checkout response, you can submit authorizations, captures, and refunds to the virtual card using your existing payment rails.

You can also retrieve card details via your server using our [Read Card](🔗) API endpoint and the associated `checkout_id`.