Get White Label API

Bargain Chatbot API Integration Guide

 

 

Prerequisites: Retrieve Your Shop ID and API Token

Before you can use the API, you need two essential "ingredients":

  • Shop ID/User ID: This unique identifier for your shop is available in your DBargain Dashboard. Make sure to retrieve it and use it in your requests to interact with the API.
     
  • API Token: Your API token is required for authorization and is also available in your DBargain Dashboard. Ensure you securely store this token and include it in the Authorization header of your API calls.

 


 

Step 1: Add the Bargain Chatbot Script

To begin using the chatbot on your product page, you'll first need to load the necessary JavaScript file. This script handles the user interface and logic for the bargaining functionality.

 

How to Add the Script:

  1. Open the product page where you want the chatbot to appear.
  2. Insert the following script tag inside the HTML or just before the closing tag:

 

https://custom.dbargainapi.com/assets/js/script.js

 

  1. This script initializes the chatbot UI and connects it to your product page.

 

What This Does:

  • Loads the chatbot interface on the page, allowing customers to interact with it and start bargaining on selected products.

 


 

Step 2: Initialize the Bargain Chatbot

Once the script is loaded, you need to pass information about the product and user to the chatbot. This is done by setting up the options object, which includes product details, user information, and the "Add to Cart" functionality.

 

How to Initialize the Chatbot:

  1. Define a function for adding products to the cart. This function is triggered when a user finalizes a bargain:
function addToCartHandle() {
  // Your existing Add to Cart function logic goes here
}

 

        2. Next, create an options object that contains essential product and user information:

const options = {
  product_id: "YOUR_PRODUCT_ID",  // Unique ID of the product being viewed
  variant_id: "YOUR_VARIANT_ID",  // Variant ID (e.g., size or color)
  product_image: "YOUR_PRODUCT_IMAGE_URL",  // Image URL of the product
  product_name: "YOUR_PRODUCT_NAME",  // Name of the product
  product_link: "YOUR_PRODUCT_URL",  // Link to the product page
  userid: "YOUR_USER_ID",  // User ID (can be a logged-in user or session ID)
  token: "YOUR_API_TOKEN",  // Your API token for authenticating requests
  currency_symbol: "Rs.",  // The symbol for the currency (customizable)
  product_price: "PRODUCT_PRICE",  // Current price of the product
  cart_link: "YOUR_CART_URL",  // Link to your shopping cart page
  cart: "YOUR_CART_ARRAY (OPTIONAL)",  // Optional: Current cart items
  addToCartHandle: addToCartHandle,  // Function to handle adding items to cart
};

 

       3. Initialize the chatbot by calling initDbargain(options).

initDbargain(options);

 

What This Does:

  • Passes product and user information to the chatbot.
  • Ensures the chatbot knows which product and variant to apply discounts to.
  • Links the chatbot to your "Add to Cart" functionality so that when a deal is finalized, the product is added to the cart seamlessly.

 


 

Step 3: Fetch Bargain Discounts for Cart

After a user interacts with the chatbot and adds items to their cart, you can fetch the available bargains and apply discounts directly to the cart. The chatbot uses a cookie named dbargainuser to identify the user and their previous negotiations.

How to Fetch Discounts:

  1. Create a function to retrieve the bargains for the items in the user’s cart:
async function getBargainDiscounts(cart) {
  const dbargainuser = getCookie('dbargainuser');  // Fetch 'dbargainuser' cookie

  if (!dbargainuser) {
    console.log('dbargainuser cookie not found');
    return;
  }

  const postFields = {
    dbargainuserid: dbargainuser,  // User ID from cookie
    shopid: 'YOUR_USER_ID',  // Replace with your shop ID
    cart: cart  // Cart object containing product details
  };

  try {
    const response = await fetch('https://custom.dbargainapi.com/api/getBargainedList', {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer YOUR_API_TOKEN',  // Replace with your API token
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(postFields)
    });

    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching bargain discounts:', error);
  }
}

 

      2. You can then call this function when the cart is loaded on the cart page, or any time you need to update the bargain prices:

const dbargainuser = getCookie('dbargainuser');
if (dbargainuser) {
  getBargainDiscounts(yourCartObject).then(discounts => {
    console.log('Bargain discounts:', discounts);
  });
}

 

What This Does:

  • Retrieves the bargains a user has negotiated based on the items in their cart.
  • Allows you to display the discounted prices in the shopping cart or checkout page.

 


 

Step 4: Handle Cart Object

To ensure you pass the correct information to the API, the cart object needs to contain key details about each product, such as its ID and variant. You can modify your existing cart object like this:

How to Structure the Cart Object:

  1. Map over the existing cart items and format them for the bargaining API:

 

const cartArrayForBargain = cart.map(c => ({
  product_id: 'YOUR_PRODUCT_ID',  // Replace with actual product ID
  variation_id: 'YOUR_PRODUCT_VARIANT_ID'  // Replace with actual variant ID
}));

 

        2. The cartArrayForBargain array can then be passed to the getBargainDiscounts function.

 

What This Does:

  • Ensures that the cart data is formatted properly for the API to process and return relevant discounts.
  • Each cart item contains a product_id and a variation_id, which are required for the bargaining process.

 


 

Utility Functions

Get Cookie Value

The bargaining API relies on cookies to track users and their negotiation history. You’ll need a utility function to retrieve the value of specific cookies, such as the dbargainuser cookie.

function getCookie(name) {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(';').shift();
  return null;
}

 

What This Does:

  • Helps retrieve the value of cookies, which is critical for identifying the user in the bargaining process.
  • Ensures that the dbargainuser cookie is passed to the API for discount retrieval.

 


 

API Endpoints
 

Get Bargained Discounts
 

  • Endpoint: POST /api/getBargainedList
     
  • Description: This endpoint returns a list of discounts that a user has negotiated for the items in their cart.
     
  • Request Body:
    • dbargainuserid: The ID of the user making the request.
       
    • shopid: The ID of your shop.
       
    • cart: The cart object containing product and variant IDs.

       
  • Headers:
    • Authorization: Bearer YOUR_API_TOKEN
       
    • Content-Type: application/json

       

Destroy Bargained List
 

  • Endpoint: POST /api/destroy_bargained_list
     
  • Description: Clears the list of negotiated discounts, usually after a successful purchase or when a user abandons their cart.
     
  • Request Body:
    • dbargainuserid: The user ID.
       
    • shopid: Your shop ID.
       
    • cart: Cart details.

       
  • Headers:
    • Authorization: Bearer YOUR_API_TOKEN
       
    • Content-Type: application/json

 

B
O
O
K
A
M
E
E
T
I
N
G

Enquire Now

Sign up to our newsletter

Receive the latest news, exclusive discounts & offers straight to your inbox!

© All Right Reserved | Made with by Website99