Skip to content

Commit

Permalink
Fix order webhook handler (#62)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
calebporzio and driesvints authored Nov 20, 2023
1 parent b883f4f commit afbd6ec
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function up(): void
$table->integer('discount_total');
$table->integer('tax');
$table->integer('total');
$table->string('tax_name');
$table->string('tax_name')->nullable();
$table->string('status');
$table->string('receipt_url')->nullable();
$table->boolean('refunded');
Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/ManagesOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Relations\MorphMany;
use LemonSqueezy\Laravel\LemonSqueezy;
use LemonSqueezy\Laravel\Order;

trait ManagesOrders
{
Expand All @@ -20,14 +21,14 @@ public function orders(): MorphMany
*/
public function hasPurchasedProduct(string $productId): bool
{
return $this->orders()->where('product_id', $productId)->where('status', static::STATUS_PAID)->exists();
return $this->orders()->where('product_id', $productId)->where('status', Order::STATUS_PAID)->exists();
}

/**
* Determine if the billable has purchased a specific variant of a product.
*/
public function hasPurchasedVariant(string $variantId): bool
{
return $this->orders()->where('variant_id', $variantId)->where('status', static::STATUS_PAID)->exists();
return $this->orders()->where('variant_id', $variantId)->where('status', Order::STATUS_PAID)->exists();
}
}
5 changes: 3 additions & 2 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public function handleOrderCreated(array $payload): void
$order = $billable->orders()->create([
'lemon_squeezy_id' => $payload['data']['id'],
'customer_id' => $attributes['customer_id'],
'product_id' => $attributes['product_id'],
'variant_id' => $attributes['variant_id'],
'product_id' => $attributes['first_order_item']['product_id'],
'variant_id' => $attributes['first_order_item']['variant_id'],
'identifier' => $attributes['identifier'],
'order_number' => $attributes['order_number'],
'currency' => $attributes['currency'],
'subtotal' => $attributes['subtotal'],
Expand Down
4 changes: 2 additions & 2 deletions src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ public function sync(array $attributes): self
{
$this->update([
'customer_id' => $attributes['customer_id'],
'product_id' => $attributes['product_id'],
'variant_id' => $attributes['variant_id'],
'product_id' => $attributes['first_order_item']['product_id'],
'variant_id' => $attributes['first_order_item']['variant_id'],
'order_number' => $attributes['order_number'],
'currency' => $attributes['currency'],
'subtotal' => $attributes['subtotal'],
Expand Down

0 comments on commit afbd6ec

Please sign in to comment.