From 087b97f3647eb114fdabcea88a701939cea36a72 Mon Sep 17 00:00:00 2001 From: Josh Marshall Date: Fri, 22 Sep 2023 14:24:45 +1000 Subject: [PATCH] Add whole-order discount amount --- README.md | 1 + src/Action/CaptureAction.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/README.md b/README.md index 03c1113..950fb7d 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ $payment->setDetails([ ], // ... ], + 'square_discount' => 0, // Optional, sets the whole order discount where the total of the line items does not match the total amount above 'square_customer' => [ // Optional, sets this order to this customer 'email' => 'xx@xx.com', // Required 'given_name' => 'Customer First Name', // Optional diff --git a/src/Action/CaptureAction.php b/src/Action/CaptureAction.php index cf9d8ba..c7c43cf 100644 --- a/src/Action/CaptureAction.php +++ b/src/Action/CaptureAction.php @@ -181,6 +181,7 @@ public function execute($request) { $item_name = $model['square_item_name'] ?? false; $line_items = $model['square_line_items'] ?? []; + $order_discount = $model['square_discount'] ?? 0; if ($item_name) { $line_items[] = [ @@ -213,6 +214,19 @@ public function execute($request) { if (isset($model['customer'])) { $order->setCustomerId($this->getSquareCustomer($client, $model['customer'])); } + + if ($order_discount) { + $discount_amount_money = new \Square\Models\Money(); + $discount_amount_money->setAmount(round($order_discount * 100)); + $discount_amount_money->setCurrency($model['currency']); + $order_line_item_discount = new \Square\Models\OrderLineItemDiscount(); + $order_line_item_discount->setUid('discount'); + $order_line_item_discount->setName('Discount'); + $order_line_item_discount->setAmountMoney($discount_amount_money); + $order_line_item_discount->setScope('ORDER'); + $order->setDiscounts([$order_line_item_discount]); + } + $orderbody = new \Square\Models\CreateOrderRequest(); $orderbody->setOrder($order); $orderbody->setIdempotencyKey(uniqid());