>sdks>javascript

JavaScript SDK

One script adds Snap Finance to your website: a checkout button that lets customers apply for finance in a window over your page, and product widgets that show what they could repay before they reach checkout.

  • Client-side
  • JavaScript
  • Drop-in
Checkout page

Checkout button

A Snap Finance button for your checkout. Customers apply in a window over your page, without leaving your site.

  • One script tag and a container element
  • Callbacks for every step of the application
  • Dark and light themes
Product pages

Product widgets

Banners next to your prices that show customers how much they could repay with Snap Finance.

  • Spread the cost (RBP) and Interest free (BNPL)
  • Uses the same script as the checkout button
  • Dark and light themes

Checkout button

How it works

What happens after the customer clicks the button, and the callbacks your page receives at each step.

  1. The customer chooses Snap
    01

    The customer chooses Snap

    At checkout, the customer clicks the Snap Finance button you render. The application opens in a window over your page, so they never leave your site.

  2. They tell us about themselves
    02

    They tell us about themselves

    The customer fills in the "About you" screen. Once they submit it, Snap creates the application.

    • onApplicationId
  3. They get an instant decision
    03

    They get an instant decision

    After they fill in the next two pages, Snap decides straight away. Approved With Conditions means that we need extra checks before the customer can carry on.

    • onApproved
    • onApprovedWithConditions
    • onDenied
  4. They pay the deposit and sign
    04

    They pay the deposit and sign

    The customer verifies their bank account, pays their deposit and signs the agreement.

    • onUnverifiedAccount
    • onPaymentFailure
  5. The application is complete
    05

    The application is complete

    The finance is in place and the window closes, returning the customer to your page.

    • onSuccess
  6. You confirm the order
    06

    You confirm the order

    Your server checks the application with the Status API, using the applicationId and token from the callbacks, before it updates the order.

    Status API

Guide

Implementation

Load the SDK, describe the order and render the checkout button.

Step 01 Import SDK Script

Import the script in the <head> section of your website, using the code snippet below. The script should load before your document body loads.
For production retailer integration replace with: https://sdk.snapfinance.co.uk/v1/snapuk.min.js
Testing first? See how to test your integration in the sandbox.
html
<head>
  <meta charset="utf-8">
  <!-- On production replace url with: https://sdk.snapfinance.co.uk/v1/snapuk.min.js -->
  <script
    src="https://sandbox-sdk.snapfinance.co.uk/v1/snapuk.min.js"
  ></script>
</head>

Step 02 Add button container

Add an empty element to your checkout page where the Snap Finance button should appear. The SDK renders the button inside it, so the id must be snap-uk-checkout.
html
<div id="snap-uk-checkout"></div>

Step 03 Initialize Snap

Once the script has loaded, the global snapuk object is available. Initialize Snap by calling snapuk.init(clientId) with the client ID Snap gave you.
You only need to call it once per page, even if you also use the product widgets.
Make sure to replace the client ID with your production one when ready to go live.
html
<script>
  snapuk.init(clientId);
</script>

Step 04 Create Transaction Data

Describe the order the customer is paying for: what they’re buying, what it costs and who they are.
FieldTypeDescription
invoiceNumberstringYour order reference. 1 to 10 letters, numbers, hyphens, underscores, dots or slashes.
deliveryDatestringWhen the order will be delivered, as YYYY-MM-DD.
shippingCostnumberShipping cost in pounds, e.g. 25.12.
productsarrayThe items in the order: productId, description, quantity and price in pounds.
customerobjectThe customer's name, email, mobileNumber and address: houseNumber or houseName, unit, streetAddress, city and postcode. Unused address fields can be empty strings.
javascript
const transaction = {
  invoiceNumber: '12345',
  deliveryDate: '2023-05-05',
  shippingCost: 25.12,
  products: [
    {
      productId: 'AMC47',
      quantity: 4,
      description: 'Item dose',
      price: 250.99
    }
  ],
  customer: {
    firstName: 'Joe',
    lastName: 'Bloggs',
    email: 'test@snapfinance.co.uk',
    mobileNumber: '07712345678',
    houseNumber: '7',
    houseName: '',
    unit: '',
    streetAddress: 'Somewhere St',
    city: 'London',
    postcode: 'SW1A 1AA'
  }
};

Step 05 Add Snap Checkout Button

Render the button with your merchant ID and the transaction. When the customer clicks it, the Snap application opens in a window over your page.
Pass a function for each callback you want to react to. Each one receives the applicationId and a token, which you use to check the application with the Status API. See Callbacks below for when each one fires.
Never update an order from a callback alone. Callbacks run in the customer’s browser, so always confirm the status from your server with the Status API first.
javascript
snapuk.checkout.button({
  merchantId: merchantId,
  transaction: transaction,
  onApplicationId: (applicationId, token) => {
    // The application has been created
  },
  onApproved: (applicationId, token) => {
    // The customer has been approved
  },
  onApprovedWithConditions: (applicationId, token) => {
    // Approved, subject to extra checks
  },
  onDenied: (applicationId, token) => {
    // The application was declined
  },
  onSuccess: (applicationId, token) => {
    // The application is complete: confirm it with the Status API, then update the order
  },
  onClose: (applicationId, token) => {
    // The customer closed the window
  },
  onWithdrawn: (applicationId, token) => {
    // The application was withdrawn
  },
  onUnverifiedAccount: (applicationId, token) => {
    // The bank account couldn't be verified
  },
  onPaymentFailure: (applicationId, token) => {
    // The deposit payment failed too many times
  },
  onError: (applicationId, token, message) => {
    // Something went wrong; applicationId may be missing at early stages
  },
});

Reference

Callbacks

The functions the button calls as the customer moves through their application.

When each callback fires

Every callback receives the applicationId and a token. A typical approved application fires onApplicationId, then onApproved, then onSuccess.
CallbackWhen it fires
onApplicationIdThe customer has submitted the "About you" screen and Snap has created the application.
onApprovedThe application has been approved.
onApprovedWithConditionsThe application has been approved, subject to extra checks.
onDeniedThe application has been declined.
onSuccessThe finance is approved and the agreement is in place. Confirm it with the Status API, then complete the order.
onCloseThe customer closed the Snap window, at any stage.
onWithdrawnThe application has been withdrawn.
onUnverifiedAccountSnap couldn’t verify the customer’s bank account.
onPaymentFailureThe deposit payment failed the maximum number of times.
onErrorSomething went wrong. Also receives a message; applicationId may be missing if the error happened before the application was created.

Confirm the status from your server

Callbacks run in the customer’s browser, so they tell you something happened but can’t be trusted on their own. Before you store an applicationId or update an order, send it and the token to your server and check the application with the Status API.
A completed application has the status 26 (PENDING_DEL), the expected status after onSuccess.

Customise

Theming

Match the checkout button to the look of your store.

Checkout button theming

The checkout button comes in two themes: DARK, the default, and LIGHT. Pick the one that suits your checkout page.
To use the light theme, set the theme property to LIGHT.
javascript
snapuk.checkout.button({
  merchantId: merchantId,
  transaction: transaction,
  theme: 'LIGHT',
  // ...your callbacks
});
Dark Snap Finance checkout buttonLight Snap Finance checkout button

Example

See it all together

A complete checkout page with the SDK, the transaction and every callback, explained part by part.