Skip to content

Commit

Permalink
Add whole-order discount amount
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbmarshall committed Sep 22, 2023
1 parent bb26364 commit 087b97f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]', // Required
'given_name' => 'Customer First Name', // Optional
Expand Down
14 changes: 14 additions & 0 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 087b97f

Please sign in to comment.