> ## 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** mode will work out-of-the-box, and Sezzle recommends it for most browser-based SDK integrations.
        If your checkout runs inside a webview or embedded browser where popups may be blocked, use **iframe** mode instead. **iframe** mode will not work properly without first contacting Sezzle. For security reasons, Sezzle must enable **iframe** for your domain(s). To have it enabled, please submit a request with your Sezzle Merchant UUID and a list of domains to be allowed per environment (production and sandbox). For example, [*please enable uat1.mysite.com, uat2.mysite.com in sandbox and www.mysite.com, mysite.com in production*](http://www.mysite.com).
        **redirect** mode is also supported, but it is generally a better fit for manual integrations because the SDK's main value is handling the in-context checkout experience and window messaging between your site and Sezzle.
      </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">
      Sezzle Checkout SDK Version

      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">
      Negative space between top of content and edge of button
    </ParamField>

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

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

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

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

    <ParamField path="sezzleImagePositionTop" type="string">
      Position of the Sezzle logo from top.
    </ParamField>

    <ParamField path="sezzleImagePositionBottom" type="string">
      Position of the Sezzle logo from bottom.
    </ParamField>

    <ParamField path="sezzleImagePositionLeft" type="string">
      Position of the Sezzle logo from left.
    </ParamField>

    <ParamField path="sezzleImagePositionRight" type="string">
      Position of the Sezzle logo from right.
    </ParamField>

    <ParamField path="letterSpacing" type="string">
      Spacing between the templateText letter.
    </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 requires the following event handlers that can be used to extend features in your application.

<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>
      Sezzle Button is clicked by the user.

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

    <ParamField path="onComplete" type="function" required>
      Sezzle payment is successfully completed. A successfully completed Sezzle checkout will trigger an event to the `onComplete` handler. The event should include a data object with data relevant to the start checkout input parameter.
    </ParamField>

    <ParamField path="onCancel" type="function" required>
      Sezzle payment is cancelled. If the user exits the Sezzle checkout for any reason, the `onCancel` handler will be executed.
    </ParamField>

    <ParamField path="onFailure" type="function" required>
      Sezzle payment has failed. If there is an error loading the Sezzle checkout page, the `onFailure` handler will be executed.
    </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">
    <ParamField path="order" type="object">
      The order for this session

      <Expandable title="child attributes">
        <ParamField path="intent" type="string">
          If your checkout flow requires the user to confirm their checkout on your site after being approved by Sezzle, use “AUTH” as your intent. If you prefer the checkout be captured immediately, use “CAPTURE”.

          Available options: `AUTH`, `CAPTURE`
        </ParamField>

        <ParamField path="reference_id" type="string">
          Your reference ID for this order (must contain only alphanumeric characters, dashes (-), and underscores (\_))
        </ParamField>

        <ParamField path="description" type="string">
          Your description for this order
        </ParamField>

        <ParamField path="order_amount" type="object">
          The total amount of the order

          <Expandable title="child attributes">
            <ParamField path="amount_in_cents" type="integer">
              The amount of the item in cents
            </ParamField>

            <ParamField path="currency" default="USD" type="string">
              The 3 character currency code as defined by ISO 4217
            </ParamField>
          </Expandable>
        </ParamField>
      </Expandable>
    </ParamField>
  </Tab>
</Tabs>

<Note>
  Alternatively, start checkout by URL:

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

Start checkout should be implemented in the checkout `onClick` handler. There are two methods for hosting a checkout.

**Use a checkout payload as detailed in the Session Object**

* The cancel and complete URLs are not required for `iframe` and `popup` mode.

**Use an existing checkout URL**

* The `mode` used when configuring the SDK checkout must match the `checkout_mode` when [creating a session](/docs/api/core/sessions/postv2session).
* The parent window `origin` must be provided in the cancel and complete urls when the `checkout_mode` is `iframe` or `popup`.

<Note>
  **Customer Tokenization** This is not supported in the `onComplete` event. To receive a customer UUID, subscribe to the [customer.tokenized](/docs/api/core/webhooks/postv2webhooks#valid-webhook-events) 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>
  Capturing an order is not required if the `CAPTURE` intent was used when creating the checkout.
</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">
    <ParamField path="capture_amount" type="object">
      The amount to capture on this order

      <Expandable title="child attributes">
        <ParamField path="amount_in_cents" type="integer">
          The amount in cents
        </ParamField>

        <ParamField path="currency" default="USD" type="string">
          The 3 character currency code as defined by ISO 4217
        </ParamField>
      </Expandable>
    </ParamField>
  </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.
