Skip to content

Commit 087b97f

Browse files
committed
Add whole-order discount amount
1 parent bb26364 commit 087b97f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ $payment->setDetails([
7474
],
7575
// ...
7676
],
77+
'square_discount' => 0, // Optional, sets the whole order discount where the total of the line items does not match the total amount above
7778
'square_customer' => [ // Optional, sets this order to this customer
7879
'email' => '[email protected]', // Required
7980
'given_name' => 'Customer First Name', // Optional

src/Action/CaptureAction.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public function execute($request) {
181181

182182
$item_name = $model['square_item_name'] ?? false;
183183
$line_items = $model['square_line_items'] ?? [];
184+
$order_discount = $model['square_discount'] ?? 0;
184185

185186
if ($item_name) {
186187
$line_items[] = [
@@ -213,6 +214,19 @@ public function execute($request) {
213214
if (isset($model['customer'])) {
214215
$order->setCustomerId($this->getSquareCustomer($client, $model['customer']));
215216
}
217+
218+
if ($order_discount) {
219+
$discount_amount_money = new \Square\Models\Money();
220+
$discount_amount_money->setAmount(round($order_discount * 100));
221+
$discount_amount_money->setCurrency($model['currency']);
222+
$order_line_item_discount = new \Square\Models\OrderLineItemDiscount();
223+
$order_line_item_discount->setUid('discount');
224+
$order_line_item_discount->setName('Discount');
225+
$order_line_item_discount->setAmountMoney($discount_amount_money);
226+
$order_line_item_discount->setScope('ORDER');
227+
$order->setDiscounts([$order_line_item_discount]);
228+
}
229+
216230
$orderbody = new \Square\Models\CreateOrderRequest();
217231
$orderbody->setOrder($order);
218232
$orderbody->setIdempotencyKey(uniqid());

0 commit comments

Comments
 (0)