Skip to content

Commit afbd6ec

Browse files
Fix order webhook handler (#62)
* Use Order::STATUS_PAID instead of static::STATUS_PAID * Adjust orders webhook handling * wip * Add identifier back * Update WebhookController.php * Update Order.php * Update WebhookController.php * Update WebhookController.php --------- Co-authored-by: Dries Vints <[email protected]>
1 parent b883f4f commit afbd6ec

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

database/migrations/2023_01_16_000003_create_orders_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function up(): void
2323
$table->integer('discount_total');
2424
$table->integer('tax');
2525
$table->integer('total');
26-
$table->string('tax_name');
26+
$table->string('tax_name')->nullable();
2727
$table->string('status');
2828
$table->string('receipt_url')->nullable();
2929
$table->boolean('refunded');

src/Concerns/ManagesOrders.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Relations\MorphMany;
66
use LemonSqueezy\Laravel\LemonSqueezy;
7+
use LemonSqueezy\Laravel\Order;
78

89
trait ManagesOrders
910
{
@@ -20,14 +21,14 @@ public function orders(): MorphMany
2021
*/
2122
public function hasPurchasedProduct(string $productId): bool
2223
{
23-
return $this->orders()->where('product_id', $productId)->where('status', static::STATUS_PAID)->exists();
24+
return $this->orders()->where('product_id', $productId)->where('status', Order::STATUS_PAID)->exists();
2425
}
2526

2627
/**
2728
* Determine if the billable has purchased a specific variant of a product.
2829
*/
2930
public function hasPurchasedVariant(string $variantId): bool
3031
{
31-
return $this->orders()->where('variant_id', $variantId)->where('status', static::STATUS_PAID)->exists();
32+
return $this->orders()->where('variant_id', $variantId)->where('status', Order::STATUS_PAID)->exists();
3233
}
3334
}

src/Http/Controllers/WebhookController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ public function handleOrderCreated(array $payload): void
8383
$order = $billable->orders()->create([
8484
'lemon_squeezy_id' => $payload['data']['id'],
8585
'customer_id' => $attributes['customer_id'],
86-
'product_id' => $attributes['product_id'],
87-
'variant_id' => $attributes['variant_id'],
86+
'product_id' => $attributes['first_order_item']['product_id'],
87+
'variant_id' => $attributes['first_order_item']['variant_id'],
88+
'identifier' => $attributes['identifier'],
8889
'order_number' => $attributes['order_number'],
8990
'currency' => $attributes['currency'],
9091
'subtotal' => $attributes['subtotal'],

src/Order.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ public function sync(array $attributes): self
179179
{
180180
$this->update([
181181
'customer_id' => $attributes['customer_id'],
182-
'product_id' => $attributes['product_id'],
183-
'variant_id' => $attributes['variant_id'],
182+
'product_id' => $attributes['first_order_item']['product_id'],
183+
'variant_id' => $attributes['first_order_item']['variant_id'],
184184
'order_number' => $attributes['order_number'],
185185
'currency' => $attributes['currency'],
186186
'subtotal' => $attributes['subtotal'],

0 commit comments

Comments
 (0)