---
agentTools:
  projectIndex: https://rehook.readme.io/llms.txt
---

# Validate a coupon for applicability

# FetchAllClientCoupons

## Description
Validate coupon for a client.

## Endpoint
`POST /coupons/validate`

## Parameters
  - `coupon_details` (array of object): coupon details (mandatory)
    - `coupon_id` (string): coupon id or "bulk" (when using bulk user coupons) (mandatory)
    - `coupon_code` (string): coupon id (mandatory)
  - `source_id` (string): source id (optional)
  - `order` : Order details (optional)
    - `original_amount` (float64): original amount (mandatory)
    - `selling_amount` (float64): selling amount (mandatory)
    - `status` (string): status [pending, completed, reverted] (optional)
    - `shipping` (float64): shipping (optional)
    - `tax` (float64): tax (optional)
    - `payment_mode` (string): payment mode (optional)
    - `metadata`(object): metadata (optional)
      - `key1` (string): meta key 1
      - `key2` (float64): meta key 2
    - `items` (array of object): product items (optional)
      - `original_price` (float64): original price (mandatory)
      - `selling_price` (float64): original price (mandatory)
      - `product_id` (string): product id (mandatory)
      - `sku` (string): sku (optional)
      - `name` (string): name (optional)
      - `quantity` (float64): quantity (optional)
      - `metadata`(object): metadata (optional)
        - `itemkey1` (string): meta key 1
        - `itemkey2` (float64): meta key 2

## Responses
- `200 OK`

- `400 Bad Request`
    - `code` (error code): NOT_FOUND_CODE
    - `message` (error message): Campaign not found details
    - `details` (error details): Error details
    - `request_id` (string): Request ID

## Security
- Basic Authentication

## Authentication

This endpoint requires Basic Authentication.

- **Username**: `apiKey`
- **Password**: `apiSecret`

---

## Request Body Example

```json
{
  "coupon_details": [{
    "coupon_id": "coupm_rq4XiTPbrhq0d0i92EkSx",
    "coupon_code": "CUP-21"
  }],
  "source_id": "customerA_123",
  "order": {
    "order_id": "order_123",
    "status": "pending",	
    "original_amount": 190,
    "selling_amount": 135.97,
    "shipping": 5.0,
    "tax": 3.47,
    "payment_mode": "UPI",
    "metadata": {
      "created_by": "JohnDoe",
      "created_date": "2023-06-13T10:00:00",
      "source": "Website"
    },
    "items": [
      {
        "product_id": "ABC123",
        "sku": "SKU123",
        "name": "Example Product 1",
        "original_price": 190.99,
        "selling_price": 149.99,
        "quantity": 2,
        "metadata": {
          "color": "blue",
          "size": "L"
        }
      }
    ]
  }
}

**Rehook Platform API provides resource oriented URLs to work with your business data. Our API uses JSON for request and response. API errors are returned using standard HTTP response codes.**

# Introduction

Validate coupon API will enable you to validate an applied coupon code for it's redemption or applicability. Your application can consume this API at applicable touch points on your app or web.

On successful API call, Rehook will validate the coupon code and returns with a response body which specifies whether applied coupon code can redeemed or not.

**You can consume this API on following cases:**

1. When customer apply the coupon code on cart.
2. When customer entered the coupon code manually.
3. When customer modify the cart details after applying the coupon.
4. or any other check point as per your application purchase journey.

# API details

**API Endpoint:** <https://api.rehook.ai/coupons/validate>

**HTTP Method:** POST

# Prerequisites

1. API Key and Secret Key for your application
2. If you don't know the API key and secret key, please go to setting module on admin panel to view API details for your application. To know more about application setting, please visit [Application setting](https://rehook.readme.io/reference/getting-started-copy)
3. API key and secret key are auto generated and cannot be changed.

# Authentication

You need to provide the API key and Secret of your application in every new request as Authorization. Rehook use Basic authentication method to authenticate the API calls.

| Authorization | Value                          |
| :------------ | :----------------------------- |
| **Username**  | API key of your application    |
| **Password**  | Secret key of your application |

[block:image]
{
  "images": [
    {
      "image": [
        "https://files.readme.io/5e43567-Screenshot_2023-09-07_at_3.34.58_PM.png",
        "",
        ""
      ],
      "align": "center",
      "border": true
    }
  ]
}
[/block]

# Parameters

## Query Parameters

This API doesn't requires any query parameters. you have to provide the details in API body.

## Body Parameters

Please refer to the complete request body as given below.

```json
{
    "coupon_details": [
        {
            "coupon_id":"coupm_tfKRcTxedjr17FH3m267V",
            "coupon_code":"KS"
        }
    ],
    "source_id":"krish123",
    "order": {
        "order_id": "1223456",
        "status": "pending",
        "original_price_subtotal": 800,
        "selling_price_subtotal": 800,
        "shipping": 100,
        "tax":20,
        "payment_mode": "UPI",
        "metadata": {
            "cart_qty": "4"
        },
        "items": [
            {
                "product_id": "123",
                "sku": "",
                "name":"",
                "original_price": 200,
                "selling_price": 200,
                "quantity": 1,
                "shipping": 0,
                "metadata": {
                    "brand": "brand A",
                    "category": "grocery"
                }
            },
             {
                "product_id": "654",
                "sku": "",
                "name":"",
                "brand": "wrangler",
                "original_price": 200,
                "selling_price": 200,
                "quantity": 2,
                "shipping": 0,
                "metadata": {
                    "brand": "brand B",
                    "category": "vegetables"
                }
            }
        ]
    }
}
```

**Parameters without an object** (Optional)

[block:parameters]
{
  "data": {
    "h-0": "Filed name",
    "h-1": "Data type",
    "h-2": "Definition",
    "h-3": "Example",
    "0-0": "source_id",
    "0-1": "string",
    "0-2": "**Optional**  \nUnique customer Id from your application",
    "0-3": "Krishna_123"
  },
  "cols": 4,
  "rows": 1,
  "align": [
    "left",
    "left",
    "left",
    "left"
  ]
}
[/block]

**Order details object** (Optional)

[block:parameters]
{
  "data": {
    "h-0": "Field name",
    "h-1": "Data type",
    "h-2": "Definition",
    "h-3": "Example",
    "0-0": "order_id",
    "0-1": "string",
    "0-2": "**Optional**  \nUnique transaction Id from your application",
    "0-3": "rehook12345",
    "1-0": "original_price_subtotal",
    "1-1": "Int",
    "1-2": "**Mandatory**  \nSum of original price of all the items (MRP)  \n[selling_price * quantity]",
    "1-3": "500",
    "2-0": "selling_price_subtotal",
    "2-1": "float64",
    "2-2": "**Mandatory**  \nSum of selling price of all the items  \n[original_price * quantity]",
    "2-3": "300",
    "3-0": "status",
    "3-1": "string",
    "3-2": "**Optional**  \nStatus of transaction from your application. ",
    "3-3": "'pending', 'completed', 'reverted'",
    "4-0": "shipping",
    "4-1": "float64",
    "4-2": "**Optional**  \nshipping charge at an order",
    "4-3": "30",
    "5-0": "tax",
    "5-1": "float64",
    "5-2": "**Optional**  \ntotal tax amount applied at an order",
    "5-3": "20"
  },
  "cols": 4,
  "rows": 6,
  "align": [
    "left",
    "left",
    "left",
    "left"
  ]
}
[/block]

**Item details - Array of object** (Optional)

[block:parameters]
{
  "data": {
    "h-0": "Field name",
    "h-1": "Data type",
    "h-2": "Definition",
    "h-3": "Example",
    "0-0": "product_id",
    "0-1": "string",
    "0-2": "**Required**  \nProduct identification id from your application",
    "0-3": "abc_124",
    "1-0": "sku",
    "1-1": "string",
    "1-2": "**Optional**  \nAssigned SKU to a product from your application",
    "1-3": "124",
    "2-0": "name",
    "2-1": "string",
    "2-2": "**Optional**  \nname of the product",
    "2-3": "Coke",
    "3-0": "original_price",
    "3-1": "float64",
    "3-2": "**Required**  \noriginal price of the product or MRP",
    "3-3": "100",
    "4-0": "selling_price",
    "4-1": "float64",
    "4-2": "**Required**  \nselling price of the product",
    "4-3": "80",
    "5-0": "quantity",
    "5-1": "float64",
    "5-2": "**Optional**  \nquantity of the product in the cart",
    "5-3": "2",
    "6-0": "shipping",
    "6-1": "float64",
    "6-2": "**mandantory**  \nshipping charge at an item level (value is not mandantory but you have to this field in all the payload)",
    "6-3": "10"
  },
  "cols": 4,
  "rows": 7,
  "align": [
    "left",
    "left",
    "left",
    "left"
  ]
}
[/block]

**Coupon details object**(Mandatory)

[block:parameters]
{
  "data": {
    "h-0": "Filed name",
    "h-1": "Data type",
    "h-2": "Definition",
    "h-3": "Example",
    "0-0": "coupon_id",
    "0-1": "string",
    "0-2": "**Required**  \ncoupon id generated by Rehook against fixed coupon distributed. you will get the coupon_id against a coupon code in fetch API response",
    "0-3": "coupon_123",
    "1-0": "coupon_code",
    "1-1": "string",
    "1-2": "**Required**  \ncoupon code entered or applied by customer on your application",
    "1-3": "DIWALI100"
  },
  "cols": 4,
  "rows": 2,
  "align": [
    "left",
    "left",
    "left",
    "left"
  ]
}
[/block]

**Meta data** (Optional)

You can provide the meta data in order object or item object using key value pair

| meta data key name | meta data value |
| :----------------- | :-------------- |
| Key 1              | value 1         |
| Key 2              | value 2         |

Example:

| meta data key name | meta data value |
| :----------------- | :-------------- |
| brand              | Rehook          |
| category           | grocery         |

> 📘 Meta data -limitation
>
> 1. Meta data details are optional in the request body.
> 2. If you want to pass any other custom fields in the API payload, then such fields can be passed as key value pairs inside metadata object.
> 3. Our system will validate the meta data only if same meta data with key value pair with same data type is created in any campaign on rebook dashboard, otherwise, Rehook API will nullify the meta data provided in the request body.
> 4. Meta data object can be passed at order or item level.

# Sample request body

Below table shows what details to be shared in the request body in following cases.

[block:parameters]
{
  "data": {
    "h-0": "Case",
    "h-1": "Sub case",
    "h-2": "Request body",
    "h-3": "Response body",
    "0-0": "Logged in user",
    "0-1": "without validation",
    "0-2": "1. source_id or without source_id  \n2. coupon_details",
    "0-3": "**when source_id is passed: **Rehook will validate the source_id with coupon_code or coupon_id and return the validity status with coupon details for it's redemption.  \n**when source_id not passed**: Rehook will validate only order and item object with coupon code configuration and return the validity status with coupon details for it's redemption",
    "1-0": "Logged in user",
    "1-1": "with cart/item validation",
    "1-2": "1. source_id  \n  \n2. coupon_details  \n  \n3. order object with mandatory fields if validation to be done on order object.  \n**Mandatory fields:** original_price_subtotal and selling_price_subtotal is mandatory  \n  \n4. item object with mandatory fields if validation to be done on item object  \n**Mandatory fields:** original_price, selling_price, product_id are mandatory  \n  \n4. pass both cart and item object with mandatory fields if validation to be done on both objects.",
    "1-3": "Rehook will validate the source_id with  \ncoupon code or coupon_id and perform  \nvalidation check on cart or item object and  \nreturns the validity status with coupon details for it's redemption. ",
    "2-0": "Non logged in user",
    "2-1": "without validation",
    "2-2": "1. coupon_details",
    "2-3": "Rehook will validate only coupon  coupon code or coupon_id and returns validity status with coupon details for it's redemption. ",
    "3-0": "Non logged in user",
    "3-1": "with cart/item validation",
    "3-2": "1. coupon_details  \n  \n2. order object with mandatory fields if validation to be done on order object  \n**Mandatory fields:** original_price_subtotal and selling_price_subtotal are mandatory  \n  \n3. item object with mandatory fields if validation to be done on item object  \n**Mandatory fields:** original_price, selling_price, product_id are mandatory  \n  \n4. pass both cart and item object with mandatory fields if validation to be done on both objects.",
    "3-3": "Rehook will validate only coupon code or coupon_id and perform validation check on cart or item object and returns validity status with coupon details for it's redemption. "
  },
  "cols": 4,
  "rows": 4,
  "align": [
    "left",
    "left",
    "left",
    "left"
  ]
}
[/block]

**Sample request body and API behaviour details are given below for the above mentioned cases:**

## Complete payload and response

**Complete request body:**

```json
{
    "coupon_details": [
        {
            "coupon_id":"coupm_tfKRcTxedjr17FH3m267V",
            "coupon_code":"KS"
        }
    ],
    "source_id":"krish123",
    "order": {
        "order_id": "1223456",
        "status": "pending",
        "original_price_subtotal": 800,
        "selling_price_subtotal": 800,
        "shipping": 100,
        "tax":20,
        "payment_mode": "UPI",
        "metadata": {
            "cart_qty": "4"
        },
        "items": [
            {
                "product_id": "123",
                "sku": "",
                "name":"",
                "original_price": 200,
                "selling_price": 200,
                "quantity": 1,
                "shipping": 0,
                "metadata": {
                    "brand": "brand A",
                    "category": "grocery"
                }
            },
             {
                "product_id": "654",
                "sku": "",
                "name":"",
                "brand": "wrangler",
                "original_price": 200,
                "selling_price": 200,
                "quantity": 2,
                "shipping": 0,
                "metadata": {
                    "brand": "brand B",
                    "category": "vegetables"
                }
            }
        ]
    }
}
```

> 📘 API Behaviour:
>
> * Above payload contains the complete details of API payload for coupon validation
> * For assigned user coupons, passing source\_id is mandantory, otherwise system will return error message asking user to login to redeem the coupon code
> * In coupon details object, you have to share either coupon code or coupon id. Rehook will validate the coupon code if any of the fields are correct.
> * Coupon details are mandatory in the validation API call
> * For bulk user coupon - same coupon code can be passed in coupon\_code and coupon\_id fields.
> * Order object and Item object are optional, please provide the mandatory details of order and item if you want to validate the coupon codes based on cart and item validations.
> * All other parameters in cart validations and product validations is optional.
> * If you don't want to perform any validations on cart to item, then just provide coupon details in the request body with or without source\_id based on logged in status.
> * If validation is applied on mandatory parameters and such parameters are not passed in the request body, API will return the validation error messages.
> * If validation is applied on optional parameters and parameters are not passed in the request body, API will not return error, however validation error is only returned when optional parameters are passed.
> * If you want to pass any other custom fields in the API payload, then such fields can be passed as key value pairs inside metadata object.
> * We suggest you to pass the order and item object in all the API calls to avoid fraud coupon redemption.

**case 1: Response body with validation is true**

```json
{
    "coupon_code": "SELECTED",
    "coupon_code_id": "coupm_5lzR4H2wRja7sVGzZ9HOV",
    "coupon_name": "selected items",
    "coupon_terms_and_conditions": [
        ""
    ],
    "is_applicable": true,
    "coupon_savings": {
        "order": {
            "order_id": "1223456",
            "source_id": "KMN@123",
            "status": "pending",
            "original_price_subtotal": 800,
            "selling_price_subtotal": 800,
            "total_discount": 100,
            "discount_calculated_on_property": "selected_items_selling_price_subtotal",
            "total_amount": 700,
            "total_cashback": 0,
            "shipping": 100,
            "payment_mode": "UPI",
            "coupon_details": {},
            "metadata": {
                "cart_qty": "4"
            },
            "items": [
                {
                    "discount_value": 100,
                    "final_amount": 100,
                    "metadata": {
                        "brand": "brand A",
                        "category": "grocery"
                    },
                    "name": "",
                    "original_price": 200,
                    "product_id": "123",
                    "quantity": 1,
                    "selling_price": 200,
                    "shipping": 0,
                    "sku": ""
                },
                {
                    "brand": "wrangler",
                    "discount_value": 0,
                    "final_amount": 400,
                    "metadata": {
                        "brand": "brand B",
                        "category": "vegetables"
                    },
                    "name": "",
                    "original_price": 200,
                    "product_id": "654",
                    "quantity": 2,
                    "selling_price": 200,
                    "shipping": 0,
                    "sku": ""
                }
            ]
        }
    },
    "request_id": "c1a9dd45-a8b7-97be-a846-def9a0e18ddc"
}
```

**case 2: response body when validation is false**

```json
{
    "coupon_code": "WHOLECART",
    "coupon_code_id": "coupm_9ZbJ2ryi4ia2eyz6I7sRc",
    "coupon_name": "WHOLE CART",
    "coupon_terms_and_conditions": [
        ""
    ],
    "is_applicable": false,
    "message": "selling_price_subtotal should be greater than 3000.00",
    "coupon_savings": {
        "order": {
            "order_id": "",
            "source_id": "",
            "status": "",
            "selling_price_subtotal": 0,
            "total_discount": 0,
            "total_amount": 0,
            "total_cashback": 0,
            "shipping": 0,
            "coupon_details": {}
        }
    },
    "request_id": ""
}
```

**sample response fields and definition**

[block:parameters]
{
  "data": {
    "h-0": "Fields",
    "h-1": "Definition",
    "0-0": "coupon_code",
    "0-1": "Fixed coupon code configured on Rehook",
    "1-0": "coupon_code_id",
    "1-1": "coupon Id generated against a coupon code",
    "2-0": "coupon_name",
    "2-1": "[Return]name of the coupon configured on Rehook",
    "3-0": "coupon_description",
    "3-1": "[Return]description of the coupon",
    "4-0": "coupon_terms_and_conditions",
    "4-1": "[Return]terms and condition of the coupon. this array will have multiple values.",
    "5-0": "is_applicable",
    "5-1": "[Return]Indicates if coupon code is applicable or not (values: true/false)",
    "6-0": "**coupon_savings (array of objects)**",
    "6-1": "",
    "7-0": "**Order object details**",
    "7-1": "",
    "8-0": "total_amount",
    "8-1": "[Return]selling_price_subtotal - total_discount",
    "9-0": "total_discount",
    "9-1": "[Return]total_discount to be given for a coupon code",
    "10-0": "total_cashback",
    "10-1": "[Return]total cashback to be given for a coupon code",
    "11-0": "discount_calculated_on_property",
    "11-1": "[Return]specifies on which property discount to be applied  \n(values: selling_price_subtotal, original_price_subtotal, selected_items selling_price_subtotal, selected_items_original_price_subtotal, valid_cart_selling_price_subtotal, valid_cart_original_price_subtotal)",
    "12-0": "request id",
    "12-1": "[Return]API request id generated by Rehook for your request",
    "13-0": "**Item object details**",
    "13-1": "",
    "14-0": "discount_value",
    "14-1": "[Return]  \n  \n discount amount to be applied at an item. this value will exists only for selected items or whole cart with exclude items discount.",
    "15-0": "final_amount",
    "15-1": "[Return]  \n  \n Net amount of an item = (selling_price \\* quantity) - discount_value"
  },
  "cols": 2,
  "rows": 16,
  "align": [
    "left",
    "left"
  ]
}
[/block]

**Discount\_calculated\_on\_property - value and definition**

[block:parameters]
{
  "data": {
    "h-0": "value returned",
    "h-1": "definition",
    "0-0": "selling_price_subtotal",
    "0-1": "discount to be applied on selling_price_subtotal",
    "1-0": "original_price_subtotal",
    "1-1": "discount to be applied on original_price_subtotal",
    "2-0": "shipping",
    "2-1": "discount to be applied on shipping amount",
    "3-0": "selected_items_selling_price_subtotal",
    "3-1": "This is applicable only in case of selected items discount.  \ndiscount to be applied on eligible selected items selling price subtotal , in this case, rehook will return the item level discount_value in eligible item object",
    "4-0": "selected_items_original_price_subtotal",
    "4-1": "This is applicable only in case of selected items discount.  \ndiscount to be applied on eligible selected items original price subtotal, in this case, rehook will return the item level discount_value in eligible item object",
    "5-0": "valid_cart_selling_price_subtotal",
    "5-1": "This is applicable only in case of valid cart selling price subtotal (that means, system will apply discount on whole cart by excluding the specified items).  \nexample: Get 50% discount on whole excluding items from tobaco. ",
    "6-0": "valid_cart_original_price_subtotal",
    "6-1": "same as above but discount is applied on orginal_price_subtotal of eligible items "
  },
  "cols": 2,
  "rows": 7,
  "align": [
    "left",
    "left"
  ]
}
[/block]

### When customer is logged in

case 1: **Without any validations**

```json JSO
{
		"source_id": "101",
		"coupon_details": [{
			"coupon_id": "XYZ",
			"coupon_code": "RVKAC"
		}]
}
```

> 📘 API Behaviour:
>
> * If you want to validate coupon code applicable for a logged in customer without any validation on cart or item, you just need to provide the source\_id and coupon details in the API request body.
> * For above payload, Rehook API will validate the coupon code for given source id returns validity status with coupon details for it's redemption.

case 2: **With cart or item validations**

```json
Refer to complete payload
```

> 📘 API Behaviour:
>
> * If you want to validate coupon code applicable for a logged in customer with additional validation on cart or item, you need to provide the source\_id, coupon details and order or item object with mandatory fields in the API request body.
> * With reference to above payload, Rehook will validate the source\_id = 101 with coupon code or coupon\_id and perform validation check on cart or item object and returns the validity status with coupon details for it's redemption.

### For not signed in customers

Case 1: **without any validations**

```json JSON
{
	"coupon_details": [{
		"coupon_id": "XYZ",
		"coupon_code": "RVKAC"
	}]
}
```

> 📘 API Behaviour:
>
> * If you want to validate the coupon code for a not signed customers, you have to pass only coupon\_details in the request body
> * In this case, Rehook API will validate only given coupon code available for your applications without checking any validations on customer/order/Item and returns validity status with coupon details.

Case 2: **with order/item validations**

```json
Refer to complete payaload without source_id 	
```

> 📘 API Behaviour:
>
> * If you want to validate the coupon code for a not signed customers with cart or item validation while customer is active on your cart page, you have to pass the coupon details and cart or item object without source\_id
> * With reference to above payload, Rehook will validate only coupon code and perform additional validation on cart or item object and returns coupon validity status with coupon details for it's redemption.

# Discount types:

## Whole cart

In whole cart discount type, discount will be applied on whole cart. you can apply discount on following properties with cart or product validations.

1. Selling\_price\_subtotal
2. original\_price\_subtotal
3. shipping

Example: Buy min worth of Rs 5000 and Get Flat 30% off on MRP.

**Expected payload**

```json
{
    "coupon_details": [
        {
            "coupon_id":"coupm_tfKRcTxedjr17FH3m267V",
            "coupon_code":"FLAT50"
        }
    ],
    "source_id":"krish123",
    "order": {
        "order_id": "1223456",
        "status": "pending",
        "original_price_subtotal": 6400,
        "selling_price_subtotal": 6400,
        "shipping": 100,
        "tax":20,
        "payment_mode": "UPI",
        "metadata": {
            "cart_qty": "4"
        },
        "items": [
            {
                "product_id": "123",
                "sku": "",
                "name":"",
                "original_price": 3200,
                "selling_price":3200,
                "quantity": 1,
                "shipping": 0,
                "metadata": {
                    "brand": "brand A",
                    "category": "grocery"
                }
            },
             {
                "product_id": "654",
                "sku": "",
                "name":"",
                "brand": "wrangler",
                "original_price": 3200,
                "selling_price": 3200,
                "quantity": 1,
                "shipping": 0,
                "metadata": {
                    "brand": "brand B",
                    "category": "vegetables"
                }
            }
        ]
    }
}
```

> 📘 API Behaviour for above payload
>
> 1. In the payload, you have to pass the item details and order details for discount validations associated with a coupon code
> 2. In above example, discount to be applied on whole cart MRP (original\_price\_subtotal) when customer buys min order worth of Rs 5000 (this validation can be done on MRP or selling price based on configuration)
> 3. with above payload, Rehook will first check for discount validation on min order worth of 5000. since MRP or selling\_price\_subtotal is Rs 6400 which is greater than 5000, coupon validation is true
> 4. 30% discount will be applied on original\_price\_subtotal as per the configuration.
> 5. In response, system will return the following keys in the order object\
>    a. total\_discount : discount amount calculated (in this case, it will be 30 % of original\_price\_subtotal )\
>    b. total\_amount: selling\_price\_subtotal - total\_discount\
>    c. discount\_calculated\_on\_property : indicates on which property discount to be applied on
> 6. When whole cart discount is applied, total discount is returned in the order object. item level discount will not be returned.
> 7. In above payload, if order worth is less than 5000, then Rehook will return discount applicability as false

## Whole cart excluding

In this type, discount will be applied on whole cart by excluding specified product properties with cart or product validations.

example: Buy products worth Rs 5000 and Get 50% off on whole cart, discount is not applicable for items from tobaco category

expected payload:

```json
{
    "coupon_details": [
        {
            "coupon_id":"coupm_tfKRcTxedjr17FH3m267V",
            "coupon_code":"FLAT50"
        }
    ],
    "source_id":"krish123",
    "order": {
        "order_id": "1223456",
        "status": "pending",
        "original_price_subtotal":9600,
        "selling_price_subtotal":9600,
        "shipping": 100,
        "tax":20,
        "payment_mode": "UPI",
        "metadata": {
            "cart_qty": "4"
        },
        "items": [
            {
                "product_id": "123",
                "sku": "",
                "name":"",
                "original_price": 3200,
                "selling_price":3200,
                "quantity": 2,
                "shipping": 0,
                "metadata": {
                    "brand": "brand A",
                    "category": "grocery"
                }
            },
             {
                "product_id": "654",
                "sku": "",
                "name":"",
                "brand": "wrangler",
                "original_price": 3200,
                "selling_price": 3200,
                "quantity": 1,
                "shipping": 0,
                "metadata": {
                    "brand": "brand B",
                    "category": "tobaco"
                }
            }
        ]
    }
}
```

> 📘 API behaviour
>
> 1. In the payload, you have to pass the item details and order details for discount validations associated with a coupon code
>
> 2. In this example, discount will be applied on valid cart subtotal (MPR or selling price based on the configuration). system will not apply discount on products from tobaco category
>
> 3. with above payload, Rehook will first check for discount validation on min order worth of 5000. since MRP or selling\_price\_subtotal is Rs 6400 which is greater than 5000, discount validation is true
>
> 4. To apply discount, rehook will first calculate the valid cart subtotal (Eligible items selling price or original price \* quantity). In above example, only SKU: 123 is considered in the valid cart as SKU: 654 is from tobaco category which is to be excluded.
>
> 5. Hence, valid cart subtotal will be = selling price or orginal price of sku 123 *quantity (3200* 2) = 6400
>
> 6. Now, 50% discount will be applied on valid cart subtotal
>
> 7. In response, system will return the following keys in the order object.
>
>    a. total\_discount : discount amount calculated (in this case, it will be 30 % of original\_price\_subtotal )\
>    b. total\_amount: selling\_price\_subtotal - total\_discount\
>    c. discount\_calculated\_on\_property : In this case, value will be valid\_cart\_selling\_price\_subtotal or\
>    valid\_cart\_original\_price\_subtotal
>
> 8. In response, system will return the following keys in the item object\
>    a. discount\_value: item level discount amount. this value will exist only for applicable items. excluded\
>    items will have discount\_value as zero. system will first calculate the total discount\
>    on valid cart, then distribute discount amount at item level based on it's GMV\
>    b. final\_amount:  (Selling\_price  quantity) - discount\_value : in above example, final\_amount will be\
>    (3200 \* 2 ) - discount\_value

**Sample response body for above example:**

```json
{
    "coupon_code": "EXCLUDE",
    "coupon_code_id": "coupm_0EgJoMmnd2vzbhXAQGsZH",
    "coupon_name": "Exclude items",
    "coupon_terms_and_conditions": [
        ""
    ],
    "is_applicable": true,
    "coupon_savings": {
        "order": {
            "order_id": "1223456",
            "source_id": "KMN@123",
            "status": "pending",
            "original_price_subtotal": 9600,
            "selling_price_subtotal": 9600,
            "total_discount": 3200,
            "discount_calculated_on_property": "valid_cart_selling_price_subtotal",
            "total_amount": 6400,
            "total_cashback": 0,
            "shipping": 100,
            "payment_mode": "UPI",
            "coupon_details": {},
            "metadata": {
                "cart_qty": "4"
            },
            "items": [
                {
                    "discount_value": 3200,
                    "final_amount": 3200,
                    "metadata": {
                        "brand": "brand A",
                        "category": "grocery"
                    },
                    "name": "",
                    "original_price": 3200,
                    "product_id": "123S",
                    "quantity":2,
                    "selling_price": 3200,
                    "shipping": 0,
                    "sku": ""
                },
                {
                    "brand": "brand b",
                    "discount_value": 0,
                    "final_amount": 400,
                    "metadata": {
                        "brand": "brand B",
                        "category": "tobacco"
                    },
                    "name": "",
                    "original_price": 200,
                    "product_id": "654S",
                    "quantity": 2,
                    "selling_price": 200,
                    "shipping": 0,
                    "sku": ""
                }
            ]
        }
    },
    "request_id": "82eb1f97-f5a8-9d9d-b032-0e16a120bd62"
}
```

## Selected items

In this type, discount will be applied on selected items having specified product properties. selected items discount can be configured in following ways:

| Condition | definition                                                                                                                   | example                                                                                    | payload validation                                                                          |
| :-------- | :--------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ |
| AND       | In this case, discount will be applied when all the product properties specified are validated as true in the item object.   | Buy items from jeans category AND brand Levi's and Get 30% off                             | discount applicability will be true if item object have all the given product properties    |
| OR        | In this case, discount will be applied when either of product properties specified are validated as true in the item object. | example: Buy any items from grocery category OR vegetables category and Get 50% off on MRP | discount applicability will be true if item object have any of the given product properties |

**Selected items validations:**

Rehook supports validations on selected items property

| selected items property                    | validation                                                                                                            | example                                                       |
| :----------------------------------------- | :-------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------ |
| selected\_items\_total\_qty                | system will check if total quantities of given selected items in the payload are matching as per the condition        | Get 50% off on groceries, Buy min 5 qty                       |
| selected\_items\_selling\_price\_subtotal  | system will check if selling price subtotal of given selected items in the payload are matching as per the condition  | Buy worth Rs 2000 of groceries and Get 50%                    |
| selected\_items\_original\_price\_subtotal | system will check if original price subtotal of given selected items in the payload are matching as per the condition | Buy worth Rs 3000 of vegetables and Get 40% off               |
| AND condition with multiple properties     | system will check if all the given conditions are true or not                                                         | Buy worth Rs 5000, Get 40% off jeans on min purchase of 3 qty |

# Success Response:

If API call is successful, then Rehook will return the following HTTP success code with response body as given below:

| HTTP Code | Message |
| :-------- | :------ |
| 200       | Ok      |

# Error Responses

If API call is failed, then Rehook will return the following HTTP success code with response body as given below:

| HTTP Code | Message     |
| :-------- | :---------- |
| 400       | bad request |

**error response body**

| field name  | description                       | example                    |
| :---------- | :-------------------------------- | :------------------------- |
| Code        | Error code                        | 400                        |
| details     | Error details                     | Given coupon code is wrong |
| message     | error message                     | coupon\_code not found     |
| request\_id | API request Id assigned by Rehook | abc\_123                   |

**Below table shows possible list of cases with error messages**

[block:parameters]
{
  "data": {
    "h-0": "Case",
    "h-1": "Error message",
    "h-2": "Actions to take ",
    "0-0": "Wrong source id is passed",
    "0-1": "message: invalid_payload  \nerror details: invalid source_id",
    "0-2": "",
    "1-0": "Mandatory fields are not passed in the order or item object when validation check is applied on such fields or wrong field is passed. ",
    "1-1": "Rehook will return coupon code applicability as 'false' with order object message **'invalid payload, original_price_subtotal and selling_price_subtotal is required and should not be 0' **in the response body  \n  \nitem object message: **product_id is required**",
    "1-2": "",
    "2-0": "coupon is expired",
    "2-1": "Rehook will return coupon applicability as 'false' with message **'coupon is not available at this moment or expired' **in response body.",
    "2-2": "",
    "3-0": "coupon is not available for specific day or time",
    "3-1": "Rehook will return coupon applicability as 'false' with message **'coupon is not available at this moment or expired' **in response body.",
    "3-2": "",
    "4-0": "redemption limit exceeded",
    "4-1": "Rehook will return coupon applicability as 'false' with message **'coupon code redemption total_limit exceeded' **in the response body.",
    "4-2": "",
    "5-0": "coupon_code or coupon_id is wrong",
    "5-1": "message: coupon validation failed  \n  \nerror details: coupon not found, invalid coupon code or coupon id",
    "5-2": "",
    "6-0": "coupon_id or coupon_code missing in the request body",
    "6-1": "message: coupon validation failed  \n  \nerror details: coupon not found, invalid coupon code or coupon id",
    "6-2": "",
    "7-0": "User is not logged in and validating assigned user coupon",
    "7-1": "message: Invalid source_id  \n  \nerror details: please login/signup to redeem the coupon code",
    "7-2": "",
    "8-0": "API Key or secret key is wrong",
    "8-1": "message: Unathourized  \n  \nerror details: appId or app secret invalid",
    "8-2": "",
    "9-0": "for selected items validation, when validation is not satisfied",
    "9-1": "message: Discount validation is failed, please check terms and condition in the coupon tray'",
    "9-2": "",
    "10-0": "when selected items none of the product property is not found",
    "10-1": "message: Discount validation is failed, please check terms and condition in the coupon tray'",
    "10-2": "",
    "11-0": "when Discount validation is failed (in case of product validation )",
    "11-1": "sample message 1: selling_price should be greater than 200  \nsample message 2: cart_qty should be greater than 10",
    "11-2": ""
  },
  "cols": 3,
  "rows": 12,
  "align": [
    "left",
    "left",
    "left"
  ]
}
[/block]

# OpenAPI definition

````json
{
  "openapi": "3.0.0",
  "info": {
    "description": "This is a sample server Rehook server.",
    "title": "Rehook API",
    "termsOfService": "http://swagger.io/terms/",
    "contact": {
      "name": "Rehook API Support",
      "email": "support@rehook.io"
    },
    "version": "1.0"
  },
  "paths": {
    "/coupons/validate": {
      "post": {
        "security": [
          {
            "basicAuth": []
          }
        ],
        "description": "# FetchAllClientCoupons\n\n## Description\nValidate coupon for a client.\n\n## Endpoint\n`POST /coupons/validate`\n\n## Parameters\n  - `coupon_details` (array of object): coupon details (mandatory)\n    - `coupon_id` (string): coupon id or \"bulk\" (when using bulk user coupons) (mandatory)\n    - `coupon_code` (string): coupon id (mandatory)\n  - `source_id` (string): source id (optional)\n  - `order` : Order details (optional)\n    - `original_amount` (float64): original amount (mandatory)\n    - `selling_amount` (float64): selling amount (mandatory)\n    - `status` (string): status [pending, completed, reverted] (optional)\n    - `shipping` (float64): shipping (optional)\n    - `tax` (float64): tax (optional)\n    - `payment_mode` (string): payment mode (optional)\n    - `metadata`(object): metadata (optional)\n      - `key1` (string): meta key 1\n      - `key2` (float64): meta key 2\n    - `items` (array of object): product items (optional)\n      - `original_price` (float64): original price (mandatory)\n      - `selling_price` (float64): original price (mandatory)\n      - `product_id` (string): product id (mandatory)\n      - `sku` (string): sku (optional)\n      - `name` (string): name (optional)\n      - `quantity` (float64): quantity (optional)\n      - `metadata`(object): metadata (optional)\n        - `itemkey1` (string): meta key 1\n        - `itemkey2` (float64): meta key 2\n\n## Responses\n- `200 OK`\n\n- `400 Bad Request`\n    - `code` (error code): NOT_FOUND_CODE\n    - `message` (error message): Campaign not found details\n    - `details` (error details): Error details\n    - `request_id` (string): Request ID\n\n## Security\n- Basic Authentication\n\n## Authentication\n\nThis endpoint requires Basic Authentication.\n\n- **Username**: `apiKey`\n- **Password**: `apiSecret`\n\n---\n\n## Request Body Example\n\n```json\n{\n  \"coupon_details\": [{\n    \"coupon_id\": \"coupm_rq4XiTPbrhq0d0i92EkSx\",\n    \"coupon_code\": \"CUP-21\"\n  }],\n  \"source_id\": \"customerA_123\",\n  \"order\": {\n    \"order_id\": \"order_123\",\n    \"status\": \"pending\",\t\n    \"original_amount\": 190,\n    \"selling_amount\": 135.97,\n    \"shipping\": 5.0,\n    \"tax\": 3.47,\n    \"payment_mode\": \"UPI\",\n    \"metadata\": {\n      \"created_by\": \"JohnDoe\",\n      \"created_date\": \"2023-06-13T10:00:00\",\n      \"source\": \"Website\"\n    },\n    \"items\": [\n      {\n        \"product_id\": \"ABC123\",\n        \"sku\": \"SKU123\",\n        \"name\": \"Example Product 1\",\n        \"original_price\": 190.99,\n        \"selling_price\": 149.99,\n        \"quantity\": 2,\n        \"metadata\": {\n          \"color\": \"blue\",\n          \"size\": \"L\"\n        }\n      }\n    ]\n  }\n}\t",
        "tags": [
          "Coupons"
        ],
        "summary": "Validate a coupon for applicability",
        "operationId": "validate-coupons",
        "requestBody": {
          "$ref": "#/components/requestBodies/models.ClientOrderRequest"
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/models.CouponClientResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/models.ErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "servers": [
    {
      "url": "https://api.rehook.ai"
    }
  ],
  "components": {
    "requestBodies": {
      "models.ClientOrderRequest": {
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/models.ClientOrderRequest"
            }
          }
        },
        "description": "Request body",
        "required": true
      }
    },
    "securitySchemes": {
      "basicAuth": {
        "type": "http",
        "scheme": "basic"
      }
    },
    "schemas": {
      "models.ClientOrderRequest": {
        "type": "object",
        "required": [
          "coupon_details"
        ],
        "properties": {
          "coupon_details": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/models.Coupon"
            }
          },
          "order": {
            "$ref": "#/components/schemas/models.Order"
          },
          "source_id": {
            "type": "string",
            "example": "name:cust_123"
          }
        }
      },
      "models.Coupon": {
        "type": "object",
        "properties": {
          "coupon_code": {
            "type": "string"
          },
          "coupon_id": {
            "type": "string"
          }
        }
      },
      "models.CouponClientResponse": {
        "type": "object",
        "properties": {
          "coupon_code": {
            "type": "string"
          },
          "coupon_code_id": {
            "type": "string"
          },
          "coupon_description": {
            "type": "string"
          },
          "coupon_name": {
            "type": "string"
          },
          "coupon_savings": {
            "$ref": "#/components/schemas/models.ValidateDiscountResponse"
          },
          "coupon_terms_and_conditions": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "coupon_time_frame": {
            "$ref": "#/components/schemas/models.CouponTimeFrame"
          },
          "is_applicable": {
            "type": "boolean"
          },
          "message": {
            "type": "string"
          },
          "request_id": {
            "type": "string"
          }
        }
      },
      "models.CouponTimeFrame": {
        "type": "object",
        "properties": {
          "day_specific_validity": {
            "$ref": "#/components/schemas/models.DaySpecificValidity"
          },
          "end_date": {
            "type": "string"
          },
          "start_date": {
            "type": "string"
          }
        }
      },
      "models.DaySpecificValidity": {
        "type": "object",
        "properties": {
          "slots": {
            "$ref": "#/components/schemas/models.Slots"
          },
          "validation_days": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      },
      "models.ErrorResponse": {
        "type": "object",
        "properties": {
          "code": {
            "type": "integer",
            "example": 400
          },
          "details": {
            "type": "string",
            "example": "Invalid request."
          },
          "key": {
            "type": "string",
            "example": "invalid_request"
          },
          "message": {
            "type": "string",
            "example": "Invalid request."
          },
          "request_id": {
            "type": "string",
            "example": "1234567890"
          }
        }
      },
      "models.Order": {
        "type": "object",
        "required": [
          "customer_id",
          "order_id",
          "selling_price_subtotal",
          "source_id",
          "status"
        ],
        "properties": {
          "customer_id": {
            "type": "string",
            "example": "cust_123"
          },
          "discount": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "items": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "metadata": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "order_id": {
            "type": "string",
            "example": "order_123"
          },
          "original_price_subtotal": {
            "type": "number",
            "example": 100.2
          },
          "payment_mode": {
            "type": "string",
            "example": "UPI"
          },
          "selling_price_subtotal": {
            "type": "number",
            "example": 120.2
          },
          "shipping": {
            "type": "number",
            "example": 35
          },
          "source_id": {
            "type": "string",
            "example": "cust_123"
          },
          "status": {
            "type": "string",
            "example": "pending"
          },
          "tax": {
            "type": "number",
            "example": 23.7
          }
        }
      },
      "models.Slots": {
        "type": "object",
        "properties": {
          "validation_end_time": {
            "type": "string"
          },
          "validation_start_time": {
            "type": "string"
          }
        }
      },
      "models.ValidateDiscountResponse": {
        "type": "object",
        "properties": {
          "is_validated": {
            "type": "boolean"
          },
          "order": {
            "$ref": "#/components/schemas/models.Order"
          }
        }
      }
    }
  },
  "x-readme": {
    "explorer-enabled": true,
    "proxy-enabled": true
  },
  "_id": {
    "buffer": {
      "0": 100,
      "1": 202,
      "2": 3,
      "3": 244,
      "4": 245,
      "5": 103,
      "6": 98,
      "7": 0,
      "8": 68,
      "9": 56,
      "10": 181,
      "11": 45
    }
  }
}
````