>sdks>javascript>Full example

Full example

A complete checkout page using the Snap Finance JavaScript SDK. Copy it, add the clientId and merchantId Snap gave you for the sandbox, and open it in your browser to try the whole journey.

  • Client-side
  • JavaScript
  • Copy and paste

Walkthrough

What each part does

The numbers match the comments in the file.

  1. 1

    Load the SDK

    The script tag goes in the <head>. Use the sandbox URL while testing and https://sdk.snapfinance.co.uk/v1/snapuk.min.js when you go live.

  2. 2

    Add the container

    An empty <div id="snap-uk-checkout"> where the button should appear on your checkout page.

  3. 3

    Initialise Snap

    Call snapuk.init once with your client ID. Use the sandbox or production IDs that match the script you load.

  4. 4

    Describe the order

    The transaction: your invoice number, delivery date, shipping cost, products and customer. See the field reference.

  5. 5

    Render the button

    Pass your merchant ID, the transaction and the callbacks you need. See when each callback fires.

  6. 6

    Confirm from your server

    On onSuccess, send the applicationId and token to your server and check them with the Status API before updating the order.

html
checkout.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Snap Finance checkout example</title>
    <!-- 1. Load the SDK (production: https://sdk.snapfinance.co.uk/v1/snapuk.min.js) -->
    <script src="https://sandbox-sdk.snapfinance.co.uk/v1/snapuk.min.js"></script>
  </head>

  <body>
    <!-- 2. The button renders here -->
    <div id="snap-uk-checkout"></div>

    <script>
      // 3. Initialise with the IDs Snap gave you for this environment
      const clientId = "<!-- ADD your clientId HERE -->";
      const merchantId = "<!-- ADD your merchantId HERE -->";

      snapuk.init(clientId);

      // 4. Describe the order
      const transaction = {
        invoiceNumber: "51174155",
        deliveryDate: "2023-11-05",
        shippingCost: 50.12,
        customer: {
          firstName: "John",
          lastName: "Smith",
          email: "johnsmith@snapfinance.co.uk",
          mobileNumber: "07712345678",
          houseNumber: "7",
          houseName: "",
          unit: "Flat 165",
          streetAddress: "The Quays",
          city: "Manchester",
          postcode: "M50 3BE"
        },
        products: [
          {
            productId: "iphone",
            description: "Apple iPhone 11 Pro 256GB",
            quantity: 2,
            price: 500.99
          }
        ]
      };

      // 5. Render the button and react to each step
      snapuk.checkout.button({
        merchantId: merchantId,
        transaction: transaction,
        onApplicationId: (applicationId, token) => {
          console.log("Application created", applicationId);
        },
        onApproved: (applicationId, token) => {
          console.log("Approved", applicationId);
        },
        onApprovedWithConditions: (applicationId, token) => {
          console.log("Approved with conditions", applicationId);
        },
        onDenied: (applicationId, token) => {
          console.log("Denied", applicationId);
        },
        onSuccess: async (applicationId, token) => {
          // 6. Confirm the status from your server before updating the order
          await fetch("/your-server/confirm-snap-order", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ applicationId, token })
          });
        },
        onClose: (applicationId, token) => {
          console.log("Window closed", applicationId);
        },
        onWithdrawn: (applicationId, token) => {
          console.log("Withdrawn", applicationId);
        },
        onUnverifiedAccount: (applicationId, token) => {
          console.log("Bank account not verified", applicationId);
        },
        onPaymentFailure: (applicationId, token) => {
          console.log("Deposit payment failed", applicationId);
        },
        onError: (applicationId, token, message) => {
          console.error("Snap error", message);
        }
      });
    </script>
  </body>
</html>

Checklist

Before you go live

Work through these once the sandbox journey works end to end.