Buy Now Flow for Instant Checkout

The Buy Now feature allows you to streamline the checkout experience by directing patients straight to the payment step, bypassing the cart entirely. This is ideal for campaigns, landing pages, or product highlights where you want to reduce friction and improve conversions.

This guide walks you through how to implement the Buy Now flow using CarePortals’ public API and a simple JavaScript function that dynamically creates a cart and redirects the patient to checkout.

Step 1: Add the Snippet

Insert the following code snippet into your site header or the page where you’d like to add the “Buy Now” button. This script defines the buyNowAction() function, which creates a cart and redirects the user to the checkout flow.

<script>
  const ORG = '{{organization_id}}';
  const CHECKOUT_URI = 'https://{{patient_portal_URL}}/checkouts/';
  window.carePortalsCheckoutQuery = 'start=signup';

  function buyNowAction(productIds) {
	  const myHeaders = new Headers();
    myHeaders.append('organization', ORG);
    myHeaders.append('Content-Type', 'application/json');

    const request = productIds.split(',').map(productId => ({
        quantity: 1,
        productId
    }));

    const raw = JSON.stringify(request);

    const requestOptions = {
      method: 'POST',
      headers: myHeaders,
      body: raw,
      redirect: 'follow',
    };

    fetch('https://public-api.portals.care/v2/carts', requestOptions)
      .then(async (res) => {
        if (!res.ok) {
          throw new Error('Failed to create cart');
        }
        const body = await res.json();
        window.location.href = CHECKOUT_URI + body._id + '?' + window.carePortalsCheckoutQuery;
      })
      .catch((error) => {
        // TODO: Handle error
        alert('Error: ' + error.message);
      });
  }
</script>

Step 2: Replace Variables

Replace the placeholder values with your own as follows:

  • {{organization_id}} - Your CarePortals organization ID, used to authenticate requests.

  • {{patient_portal_URL}} - Your organization’s patient portal domain.

  • {{productId}} - The unique ID of the product (e.g, 67bca5e6f670a8734cfdf96f). You can find this in CRM > Store Manager > Products.

If you’re unsure about the organization identifier or checkout URL, please reach out to our support team on Slack or email us at support@portals.care.

Step 3: Trigger the Action

Once the script is added, you can trigger the Buy Now flow from any button on your site by calling the buyNowAction() function and passing in the CarePortals product ID.

<button onClick="buyNowAction('productId')">Buy Now</button> 

To pass multiple products to checkout:

<button onClick="buyNowAction('productId1,productId2')">Buy Now</button> 

Once configured, customers clicking the "Buy Now" button will be automatically taken to checkout with their items added, streamlining the purchase process.

Related Articles

Want to learn more? Explore these related articles for more in-depth setup tips and advanced customizations.