Add discount to order in web payment sdk using php

Please I am trying to pass a discount to an order using the PHP SDK and from the github documentation there are two scopes ‘Order’ and ‘Line_item’ but when I use the line the discount value is not applied but when I use Order option it is applied on all the line items, please how do I tie the discount to a particular line Item. Here is the generated data when I use Line Item option. as shown in the image below

Please I want to be able to tie a discount to a particular line item when I am using the LINE_ITEM scope.

Are you correctly applying the discount to the line item? For example:

$order_line_item_applied_discount = new \Square\Models\OrderLineItemAppliedDiscount('43534534534534');

$applied_discounts = [$order_line_item_applied_discount];
$order_line_item = new \Square\Models\OrderLineItem('1');
$order_line_item->setCatalogObjectId('UXTWXOHG5HBKM65MLIBUKMQF');
$order_line_item->setAppliedDiscounts($applied_discounts);

$order_line_item1 = new \Square\Models\OrderLineItem('1');
$order_line_item1->setCatalogObjectId('XDXZV3HITOA3XQM3AKGFSDKS');

$line_items = [$order_line_item, $order_line_item1];
$amount_money = new \Square\Models\Money();
$amount_money->setAmount(20);
$amount_money->setCurrency('USD');

$order_line_item_discount = new \Square\Models\OrderLineItemDiscount();
$order_line_item_discount->setUid('43534534534534');
$order_line_item_discount->setName('line');
$order_line_item_discount->setType('FIXED_AMOUNT');
$order_line_item_discount->setAmountMoney($amount_money);
$order_line_item_discount->setScope('LINE_ITEM');

$discounts = [$order_line_item_discount];
$order = new \Square\Models\Order('{{location_id}}');
$order->setCustomerId('{{customer_id}}');
$order->setLineItems($line_items);
$order->setDiscounts($discounts);

$body = new \Square\Models\CreateOrderRequest();
$body->setOrder($order);

$api_response = $client->getOrdersApi()->createOrder($body);

if ($api_response->isSuccess()) {
    $result = $api_response->getResult();
} else {
    $errors = $api_response->getErrors();
}
``` :slightly_smiling_face:

Please the discount code is it compulsory that should be created in my square dashboard? If it’s compulsory how do I go about applying a discount code created for a particular product outside square to an order in square… Thanks :pray: for your kind response…

Please this number: “43534534534534” is it the discount code?

$order_line_item1->setCatalogObjectId(‘XDXZV3HITOA3XQM3AKGFSDKS’);

and finally how do I get the CatalogObjectID as shown above

No, that’s the unique uid I generated when creating the discount. Do you have the Square generated discount_id? Also whats your application ID? :slightly_smiling_face:

Thanks :pray: issue resolved… I must say this that I am impressed with your Guys prompt response… Once again thanks :pray:

Awesome! Glad to hear it’s working! :slightly_smiling_face: