From 86207ffb3b641ae42ab6bd30e605a0a5bdcbee2a Mon Sep 17 00:00:00 2001 From: David Carr Date: Thu, 9 Nov 2023 23:08:46 +0000 Subject: [PATCH 1/2] added withQuantity method to checkout --- src/Checkout.php | 12 ++++++++++++ tests/Feature/CheckoutTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Checkout.php b/src/Checkout.php index 4366083..5c6889b 100644 --- a/src/Checkout.php +++ b/src/Checkout.php @@ -42,6 +42,8 @@ class Checkout implements Responsable private ?int $customPrice = null; + private int $quantity = 1; + public function __construct(private string $store, private string $variant) { } @@ -145,6 +147,16 @@ public function withDiscountCode(string $discountCode): self return $this; } + public function withQuantity(int $quantity): self + { + $this->checkoutData['variant_quantities'] = [ + 'variant_id' => $this->variant, + 'quantity' => $quantity, + ]; + + return $this; + } + public function withCustomData(array $custom): self { if ( diff --git a/tests/Feature/CheckoutTest.php b/tests/Feature/CheckoutTest.php index 239a449..230f16c 100644 --- a/tests/Feature/CheckoutTest.php +++ b/tests/Feature/CheckoutTest.php @@ -98,3 +98,18 @@ expect($checkout->url()) ->toBe('https://lemon.lemonsqueezy.com/checkout/buy/variant_123'); }); + +it('can include quantities', function () { + $checkout = Checkout::make('store_24398', 'variant_123') + ->withName('John Doe') + ->withQuantity(2); + + Http::fake([ + 'api.lemonsqueezy.com/v1/checkouts' => Http::response([ + 'data' => ['attributes' => ['url' => 'https://lemon.lemonsqueezy.com/checkout/buy/variant_123']], + ]), + ]); + + expect($checkout->url()) + ->toBe('https://lemon.lemonsqueezy.com/checkout/buy/variant_123'); +}); From a23a4244c95d660844d59a6f7f681fe0094eaf70 Mon Sep 17 00:00:00 2001 From: David Carr Date: Fri, 15 Dec 2023 14:20:59 +0000 Subject: [PATCH 2/2] Update Checkout.php use array of arrays --- src/Checkout.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Checkout.php b/src/Checkout.php index 5c6889b..709eb94 100644 --- a/src/Checkout.php +++ b/src/Checkout.php @@ -150,8 +150,10 @@ public function withDiscountCode(string $discountCode): self public function withQuantity(int $quantity): self { $this->checkoutData['variant_quantities'] = [ - 'variant_id' => $this->variant, - 'quantity' => $quantity, + [ + 'variant_id' => (int) $this->variant, + 'quantity' => $quantity, + ], ]; return $this;