> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sezzle.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Direct Javascript SDK

The Javascript SDK can be used for a simple, lightweight integration, but it also includes an in-context mode which will host the Sezzle checkout in a modal iframe or pop-up window.

<CardGroup cols="2">
  <Card title="Checkouts" icon="cart-shopping-fast">
    Create checkouts and capture payments with Sezzle.
  </Card>

  <Card title="Integrations" icon="code-simple">
    Checkout in an iframe, pop-up window, or redirect to Sezzle.
  </Card>

  <Card title="Payments" icon="money-check-dollar-pen">
    Handle payment success, failure, or cancel with your Sezzle orders.
  </Card>

  <Card title="Sezzle Button" icon="badge-check">
    Render the Sezzle checkout button on your store.
  </Card>
</CardGroup>

## Include SDK code

Include the following script in the `<head>` section of the page.

```html theme={"system"}
<script
    type="text/javascript"
    src="https://checkout-sdk.sezzle.com/checkout.min.js"
></script>
```

## Checkout Configuration

The first requirement to get started with the direct JavaScript SDK is to configure a new Checkout object.

### Configuration Options

<Tabs>
  <Tab title="Template">
    ```javascript theme={"system"}
    const checkoutSdk = new Checkout({
      mode: string,
      publicKey: string,
      apiMode: string,
      apiVersion: string,
    });
    ```
  </Tab>

  <Tab title="Example">
    ```javascript theme={"system"}
    const checkoutSdk = new Checkout({
      mode: "popup",
      publicKey: "sz_pub_...",
      apiMode: "sandbox",
      apiVersion: "v2",
    });
    ```
  </Tab>

  <Tab title="Options">
    <ParamField path="mode" default="popup" type="string">
      Available options: `popup`, `iframe`, `redirect`

      <Warning>
        If you use `iframe` mode, add `*.sezzle.com` to your site's Content Security Policy (CSP) allowlist so the Sezzle checkout iframe can load.
      </Warning>

      <Note>
        * **popup** (recommended): works out of the box for most browser-based SDK integrations.
        * **iframe**: required when popups are blocked (e.g. inside a webview or embedded browser). Sezzle must enable iframe for your domain(s) first — submit your Merchant UUID and the domains to allow per environment (sandbox and production). For example: *please enable uat1.mysite.com, uat2.mysite.com in sandbox and [www.mysite.com](http://www.mysite.com), mysite.com in production*.
        * **redirect**: supported but generally less useful with the SDK, since the SDK's value is the in-context checkout and window messaging.
      </Note>
    </ParamField>

    <ParamField path="publicKey" type="string" required>
      Used when creating a checkout or capturing payment. Find your API keys at [https://dashboard.sezzle.com/merchant/settings/apikeys](https://dashboard.sezzle.com/merchant/settings/apikeys)
    </ParamField>

    <ParamField path="apiMode" default="live" type="string">
      Environment in which the checkout is to be completed

      Available options: `live`, `sandbox`
    </ParamField>

    <ParamField path="apiVersion" default="v2" type="string">
      The version of the Sezzle Checkout API the SDK will call. Use `v2` unless directed otherwise.

      Available options: `v2`
    </ParamField>
  </Tab>
</Tabs>

## Payment Option

This section covers how to present Sezzle as a payment method at checkout. Depending on your webpage's design, you might wish to display Sezzle as an alternative payment method (APM) button, a radio option in a list of payment methods, and/or as an alternative submission button. Below are some examples of how to implement the front-end component.

<Note>
  If you wish to use a different design from the options displayed below, please refer to our [Co-Branded Guidelines](https://sezzle.com/brand-assets/) for Approved Messaging and Sezzle logo acceptable use.
</Note>

### Sezzle in Payment Methods Table

Below is an example of Sezzle as a payment method radio.

<Tabs>
  <Tab title="Image">
    <Frame>
      <img src="https://mintcdn.com/sezzle/kTEIf2sm4ufO8y92/images/docs/guides/direct/payment-methods-table.png?fit=max&auto=format&n=kTEIf2sm4ufO8y92&q=85&s=762e57fa2bccb357a6cbc9ff3584ea60" alt="Payment Methods Table" width="299" height="176" data-path="images/docs/guides/direct/payment-methods-table.png" />
    </Frame>
  </Tab>

  <Tab title="Example Snippet">
    *This example snippet is for informational use only. Please follow the established HTML format of your existing payment methods.*

    ```html theme={"system"}
            <div class="payment-method-tile">
                <input type="radio" id="sezzlepay" value="sezzlepay"/>
                <label for="sezzlepay">
                    <div style="width: 100%; display: inline; align-items: center; justify-content: left;">
                        <img src="https://media.sezzle.com/branding/2.0/Sezzle_Logo_FullColor.svg" alt="Sezzle" style="height: 20px; width: fit-content; padding: 0px 4px 0 2px; position: relative; top: 4px;"/>
                        <span>Buy Now, Pay Later</span>
                    </div>
                </label>
            </div>
    ```
  </Tab>
</Tabs>

### Sezzle Button

The button option is available as a component of this SDK library. It is comprised of two parts: the configurable element plcaeholder and the render function.

#### Sezzle Button Configuration

Place the element snippet from the Template tab where you wish the Sezzle Button to be rendered on the page, then update the Options attributes as needed.

<Tabs>
  <Tab title="Image">
    <CardGroup cols={2}>
      <Card img="https://mintcdn.com/sezzle/kTEIf2sm4ufO8y92/images/docs/guides/direct/APM-button.png?fit=max&auto=format&n=kTEIf2sm4ufO8y92&q=85&s=6163d313f6d04988cfb870235ef5930c" width="1078" height="590" data-path="images/docs/guides/direct/APM-button.png">
        Sezzle as a button alongside other APMs
      </Card>

      <Card img="https://mintcdn.com/sezzle/kTEIf2sm4ufO8y92/images/docs/guides/direct/Submit%20button.png?fit=max&auto=format&n=kTEIf2sm4ufO8y92&q=85&s=df631a88ccfc6360d01f2961107e5ae0" width="1096" height="738" data-path="images/docs/guides/direct/Submit button.png">
        Sezzle as a Submit button as alternative to default button
      </Card>
    </CardGroup>
  </Tab>

  <Tab title="Template">
    ```html theme={"system"}
    <div id="sezzle-smart-button-container" style="text-align: center"></div>
    ```
  </Tab>

  <Tab title="Example">
    ```html theme={"system"}
    <div
        id="sezzle-smart-button-container"
        style="text-align: center"
        templateText="Pay with %%logo%%"
        borderType="semi-rounded"
        customClass="action,primary,checkout"
    ></div>
    ```
  </Tab>

  <Tab title="Options">
    <ParamField path="templateText" default="Checkout with %%logo%%" type="string">
      Text to appear inside the button. Use `%%logo%%` inside the text to display the Sezzle image
    </ParamField>

    <ParamField path="theme" type="string" default="light">
      The theme corresponds to your site's background color. If `theme`: `dark`, the button will be white with dark text. Else, the button will be dark purple with white text.

      Available options: `dark`, `light`
    </ParamField>

    <ParamField path="borderType" type="string" default="square">
      Available options: `square`, `semi-rounded`
    </ParamField>

    <ParamField path="customClass" type="string">
      Custom classes to be applied
    </ParamField>

    <ParamField path="paddingTop" default="1px" type="string">
      Blank space between the top of the content and the top edge of the button
    </ParamField>

    <ParamField path="paddingBottom" default="7px" type="string">
      Blank space between the bottom of the content and the bottom edge of the button
    </ParamField>

    <ParamField path="paddingLeft" default="30px" type="string">
      Blank space between the left side of the content and the left edge of the button
    </ParamField>

    <ParamField path="paddingRight" default="30px" type="string">
      Blank space between the right side of the content and the right edge of the button
    </ParamField>

    <ParamField path="sezzleImageWidth" default="84px" type="string">
      Width of the Sezzle logo within the button
    </ParamField>

    <ParamField path="sezzleImagePositionTop" type="string">
      CSS `top` offset for the Sezzle logo inside the button (e.g., `2px`).
    </ParamField>

    <ParamField path="sezzleImagePositionBottom" type="string">
      CSS `bottom` offset for the Sezzle logo inside the button (e.g., `2px`).
    </ParamField>

    <ParamField path="sezzleImagePositionLeft" type="string">
      CSS `left` offset for the Sezzle logo inside the button (e.g., `2px`).
    </ParamField>

    <ParamField path="sezzleImagePositionRight" type="string">
      CSS `right` offset for the Sezzle logo inside the button (e.g., `2px`).
    </ParamField>

    <ParamField path="letterSpacing" type="string">
      Spacing between the templateText letters.
    </ParamField>

    <ParamField path="width" type="string">
      Width of the button
    </ParamField>

    <ParamField path="height" default="4.2em" type="string">
      Height of the button.
    </ParamField>
  </Tab>
</Tabs>

#### Render the Sezzle Button

Add the following function to render the button when it is appropriate, such as when payment methods section loads, or when Sezzle is selected as a payment method. The parameter corresponds to the element created in the previous step.

```javascript theme={"system"}
checkoutSdk.renderSezzleButton("sezzle-smart-button-container");
```

## Initialize the Checkout

### Event Handlers

The SDK uses these event handlers to tell your site what's happening in the checkout. Implement each one to react to shopper actions like completing, cancelling, or running into an error.

<Tabs>
  <Tab title="Template">
    ```javascript theme={"system"}
    checkoutSdk.init({
      onClick: function () {
        event.preventDefault();
        checkoutSdk.startCheckout({...});
      },
      onComplete: function (response) {
        console.log(response.data);
      },
      onCancel: function () {
        console.log("Checkout cancelled.");
      },
      onFailure: function () {
        console.log("Checkout failed.");
      },
    });
    ```
  </Tab>

  <Tab title="Example">
    ```javascript expandable theme={"system"}
    checkoutSdk.init({
      onClick: function () {
        event.preventDefault();
        checkoutSdk.startCheckout({
          checkout_payload: {
            order: {
              intent: "AUTH",
              reference_id: "543645yg5tg5675686",
              description: "sezzle-store - #12749253509255",
              order_amount: {
                amount_in_cents: 10000,
                currency: "USD",
              },
            },
          },
        });
      },
      onComplete: function (response) {
        alert("Completed transaction. Capture started.");
        checkoutSdk
        .capturePayment(response.data.order_uuid, {
            capture_amount: {
            amount_in_cents: 10000,
            currency: "USD",
            },
            partial_capture: false,
        })
        .then((r) => {
            console.log(r);
        });
      },
      onCancel: function () {
        console.log("Checkout cancelled.");
      },
      onFailure: function () {
        console.log("Checkout failed.");
      },
    });
    ```
  </Tab>

  <Tab title="Options">
    <ParamField path="onClick" type="function" required>
      Runs when the shopper clicks the Sezzle button.

      Use this function to create the Sezzle checkout session and direct the shopper into that flow.

      <Note>
        See [Checkout Initialization](#checkout-initialization) section for `startCheckout` payload options.
      </Note>
    </ParamField>

    <ParamField path="onComplete" type="function" required>
      Runs when Sezzle checkout completes successfully. Use this to save the order and, if you used `intent: AUTH`, to capture the payment.

      <Note>
        See [Capture Payment](#capture-payment) section for `capturePayment` payload options.
      </Note>

      <Expandable title="parameters">
        <ParamField path="response" type="object">
          The checkout completion response

          <Expandable title="child attributes">
            <ParamField path="data" type="object">
              Checkout completion data

              <Expandable title="child attributes">
                <ParamField path="status" type="string">
                  `"success"`
                </ParamField>

                <ParamField path="checkout_uuid" type="string">
                  Checkout UUID
                </ParamField>

                <ParamField path="session_uuid" type="string">
                  Session UUID
                </ParamField>

                <ParamField path="order_uuid" type="string">
                  Order UUID
                </ParamField>
              </Expandable>
            </ParamField>

            <ParamField path="origin" type="string">
              Origin URL of the checkout window
            </ParamField>
          </Expandable>
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField path="onCancel" type="function" required>
      Runs when the shopper exits Sezzle checkout before completing payment. Use this to update the order status in your system.

      <Expandable title="parameters">
        <ParamField path="response" type="object">
          The cancel event response

          <Expandable title="child attributes">
            <ParamField path="data" type="object">
              Cancel event data

              <Expandable title="child attributes">
                <ParamField path="status" type="string">
                  `"cancel"`
                </ParamField>

                <ParamField path="checkout_uuid" type="string">
                  The UUID of the checkout that failed
                </ParamField>

                <ParamField path="session_uuid" type="string">
                  Session UUID
                </ParamField>

                <ParamField path="order_uuid" type="string">
                  Order UUID
                </ParamField>
              </Expandable>
            </ParamField>

            <ParamField path="origin" type="string">
              Origin URL of the checkout window
            </ParamField>
          </Expandable>
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField path="onFailure" type="function" required>
      Runs when Sezzle checkout fails to load or hits an error. Use this to update the order status and surface the failure to the shopper.

      <Expandable title="parameters">
        <ParamField path="response" type="object">
          The failure response

          <Expandable title="child attributes">
            <ParamField path="data" type="object">
              Error object or API error response. When `data` is an `Error`:

              <Expandable title="child attributes">
                <ParamField path="status" type="string">
                  `"failure"`
                </ParamField>

                <ParamField path="checkout_uuid" type="string">
                  The UUID of the checkout that failed
                </ParamField>

                <ParamField path="session_uuid" type="string">
                  Session UUID
                </ParamField>

                <ParamField path="order_uuid" type="string">
                  Order UUID
                </ParamField>

                <ParamField path="message" type="string">
                  Error message (e.g., `"Public Key is missing."`)
                </ParamField>
              </Expandable>
            </ParamField>

            <ParamField path="origin" type="string">
              Origin URL of the checkout window
            </ParamField>
          </Expandable>
        </ParamField>
      </Expandable>
    </ParamField>
  </Tab>
</Tabs>

### Checkout Initialization

<Tabs>
  <Tab title="Template">
    ```javascript theme={"system"}
    checkoutSdk.startCheckout({
      checkout_payload: {
        order: {
          intent: string,
          reference_id: string,
          description: string,
          order_amount: {
            amount_in_cents: integer,
            currency: string,
          },
        },
      },
    });
    ```
  </Tab>

  <Tab title="Example">
    ```javascript theme={"system"}
    checkoutSdk.startCheckout({
      checkout_payload: {
        order: {
          intent: "AUTH",
          reference_id: "543645yg5tg5675686",
          description: "sezzle-store - #12749253509255",
          order_amount: {
            amount_in_cents: 10000,
            currency: "USD",
          },
        },
      },
    });
    ```
  </Tab>

  <Tab title="Options">
    The `checkout_payload` schema mirrors the [Create Session](/docs/api/core/sessions/postv2session) request body. See that reference document for the full list of fields, types, and constraints.
  </Tab>
</Tabs>

<Note>
  Alternatively, start checkout by URL:

  ```javascript theme={"system"}
  checkout.startCheckout({
      checkout_url: "https://checkout.sezzle.com/?id=example",
  });
  ```
</Note>

The `startCheckout` method should be implemented in the checkout `onClick` handler. There are two ways to start checkout:

* **With a checkout payload** — pass the full session object inline (shown above). The cancel and complete URLs are optional for `iframe` and `popup` mode.
* **With an existing checkout URL** — call `startCheckout({ checkout_url })`. The SDK `mode` must match the `checkout_mode` you used when [creating the session](/docs/api/core/sessions/postv2session). For `iframe` and `popup`, include the parent window's `origin` in the cancel and complete URLs.

<Note>
  **Customer Tokenization:** the customer UUID isn't delivered through `onComplete`. To receive it, subscribe to the [customer.tokenized](/docs/api/core/webhooks/postv2webhooks#valid-webhook-events) webhook event.
</Note>

### Checkout completed by Payload

```javascript theme={"system"}
function onCompleteHandler(event) {
    var data = event.data || Object.create(null);

    console.log("session data:", data.session_uuid, data.order_uuid);
}

checkout.init({
    onComplete: onCompleteHandler,
});
```

### Checkout completed by URL

```javascript theme={"system"}
function onCompleteHandler(event) {
    var data = event.data || Object.create(null);

    console.log("checkout data:", data.checkout_uuid);
}

checkout.init({
    onComplete: onCompleteHandler,
});
```

### Capture Payment

<Note>
  Skip this step if you used `CAPTURE` intent when starting the checkout — Sezzle captures the order automatically.
</Note>

<Tabs>
  <Tab title="Template">
    ```javascript theme={"system"}
    var payload = {
        capture_amount: {
            amount_in_cents: integer,
            currency: string,
        },
    };

    checkout.capturePayment(data.order_uuid, payload);
    ```
  </Tab>

  <Tab title="Example">
    ```javascript theme={"system"}
    var payload = {
        capture_amount: {
            amount_in_cents: 5000,
            currency: "USD",
        },
    };

    checkout.capturePayment(data.order_uuid, payload);
    ```

    The capture payment method requires two parameters, the `order_uuid` and the payload as detailed in the [Capture Amount By Order Object](/docs/api/core/orders/postv2capturebyorder).
  </Tab>

  <Tab title="Options">
    The `capturePayment` request body mirrors the [Capture by Order](/docs/api/core/orders/postv2capturebyorder) request body. See that reference document for the full list of fields, types, and constraints.
  </Tab>
</Tabs>

## Installment Plan

```javascript theme={"system"}
const checkout = new Checkout({});
checkout.getInstallmentPlan(1000);
```

<Tabs>
  <Tab title="Template">
    ```json theme={"system"}
    {
        "schedule": string,
        "totalInCents": integer,
        "installments": [
            {
                "installment": integer,
                "amountInCents": integer,
                "dueDate": string
            }
        ]
    }
    ```
  </Tab>

  <Tab title="Example">
    ```json theme={"system"}
    {
        "schedule": "bi-weekly",
        "totalInCents": 1000,
        "installments": [
            {
                "installment": 1,
                "amountInCents": 250,
                "dueDate": "2020-10-14"
            },
            {
                "installment": 2,
                "amountInCents": 250,
                "dueDate": "2020-10-28"
            },
            {
                "installment": 3,
                "amountInCents": 250,
                "dueDate": "2020-11-11"
            },
            {
                "installment": 4,
                "amountInCents": 250,
                "dueDate": "2020-11-25"
            }
        ]
    }
    ```
  </Tab>

  <Tab title="Options">
    <ParamField path="schedule" default="bi-weekly" type="string">
      Payment cadence for installment plan
    </ParamField>

    <ParamField path="totalInCents" type="integer">
      Total order amount in cents
    </ParamField>

    <ParamField path="installments" type="array">
      Breakdown of each payment due

      <Expandable>
        <ParamField path="installment" type="integer">
          Number of installment in series
        </ParamField>

        <ParamField path="amountInCents" type="integer">
          Installment amount in cents
        </ParamField>

        <ParamField path="dueDate" type="string">
          Due date of the installment
        </ParamField>
      </Expandable>
    </ParamField>
  </Tab>
</Tabs>

This function will provide the installment details based on an amount in cents. An existing [checkout](#checkout-initialization) can be used, or a `checkout` without any configuration can also be used to quickly get installment details.
