-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement support for Orders * Upgrade guide * wip * Fix code styling * wip * wip * wip * wip * wip --------- Co-authored-by: driesvints <[email protected]>
- Loading branch information
1 parent
e93aece
commit 7ddef90
Showing
15 changed files
with
677 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
# Upgrade Guide | ||
|
||
Future upgrade notes will be placed here. | ||
|
||
## Upgrading To 1.3 From 1.x | ||
|
||
### New Order Model | ||
|
||
Lemon Squeezy for Laravel v1.3 adds a new `Order` model. In order for your webhooks to start filling these out, you'll need to run the relevant migration: | ||
|
||
```shell | ||
php artisan migrate | ||
``` | ||
|
||
And now your webhooks will start saving newly made orders. If you're overwriting your migrations, you'll need to create [this migration](./database/migrations/2023_01_16_000003_create_orders_table.php) manually. | ||
|
||
Previously made orders unfortunately need to be stored manually but we're planning on making a sync command in the future to make this more easily. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace LemonSqueezy\Laravel\Database\Factories; | ||
|
||
use Carbon\Carbon; | ||
use DateTimeInterface; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use LemonSqueezy\Laravel\Customer; | ||
use LemonSqueezy\Laravel\Order; | ||
|
||
class OrderFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = Order::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'billable_id' => rand(1, 1000), | ||
'billable_type' => 'App\\Models\\User', | ||
'lemon_squeezy_id' => rand(1, 1000), | ||
'customer_id' => rand(1, 1000), | ||
'product_id' => rand(1, 1000), | ||
'variant_id' => rand(1, 1000), | ||
'order_number' => rand(1, 1000), | ||
'currency' => $this->faker->randomElement(['USD', 'EUR', 'GBP']), | ||
'subtotal' => $subtotal = rand(400, 1000), | ||
'discount_total' => $discount = rand(1, 400), | ||
'tax' => $tax = rand(1, 50), | ||
'total' => $subtotal - $discount + $tax, | ||
'tax_name' => $this->faker->randomElement(['VAT', 'Sales Tax']), | ||
'receipt_url' => null, | ||
'ordered_at' => $orderedAt = Carbon::make($this->faker->dateTimeBetween('-1 year', 'now')), | ||
'refunded' => $refunded = $this->faker->boolean(75), | ||
'refunded_at' => $refunded ? $orderedAt->addWeek() : null, | ||
'status' => $refunded ? Order::STATUS_REFUNDED : Order::STATUS_PAID, | ||
]; | ||
} | ||
|
||
/** | ||
* Configure the model factory. | ||
*/ | ||
public function configure(): self | ||
{ | ||
return $this->afterCreating(function ($subscription) { | ||
Customer::factory()->create([ | ||
'billable_id' => $subscription->billable_id, | ||
'billable_type' => $subscription->billable_type, | ||
]); | ||
}); | ||
} | ||
|
||
/** | ||
* Mark the order as pending. | ||
*/ | ||
public function pending(): self | ||
{ | ||
return $this->state([ | ||
'status' => Order::STATUS_PENDING, | ||
'refunded' => false, | ||
'refunded_at' => null, | ||
]); | ||
} | ||
|
||
/** | ||
* Mark the order as failed. | ||
*/ | ||
public function failed(): self | ||
{ | ||
return $this->state([ | ||
'status' => Order::STATUS_FAILED, | ||
'refunded' => false, | ||
'refunded_at' => null, | ||
]); | ||
} | ||
|
||
/** | ||
* Mark the order as paid. | ||
*/ | ||
public function paid(): self | ||
{ | ||
return $this->state([ | ||
'status' => Order::STATUS_PAID, | ||
'refunded' => false, | ||
'refunded_at' => null, | ||
]); | ||
} | ||
|
||
/** | ||
* Mark the order as being refunded. | ||
*/ | ||
public function refunded(DateTimeInterface $refundedAt = null): self | ||
{ | ||
return $this->state([ | ||
'status' => Order::STATUS_REFUNDED, | ||
'refunded' => true, | ||
'refunded_at' => $refundedAt, | ||
]); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
database/migrations/2023_01_16_000003_create_orders_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::create('lemon_squeezy_orders', function (Blueprint $table) { | ||
$table->id(); | ||
$table->unsignedBigInteger('billable_id'); | ||
$table->string('billable_type'); | ||
$table->string('lemon_squeezy_id')->unique(); | ||
$table->string('customer_id'); | ||
$table->uuid('identifier')->unique(); | ||
$table->string('product_id'); | ||
$table->string('variant_id'); | ||
$table->integer('order_number')->unique(); | ||
$table->string('currency'); | ||
$table->integer('subtotal'); | ||
$table->integer('discount_total'); | ||
$table->integer('tax'); | ||
$table->integer('total'); | ||
$table->string('tax_name'); | ||
$table->string('status'); | ||
$table->string('receipt_url')->nullable(); | ||
$table->boolean('refunded'); | ||
$table->timestamp('refunded_at')->nullable(); | ||
$table->timestamp('ordered_at'); | ||
$table->timestamps(); | ||
|
||
$table->index(['billable_id', 'billable_type']); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('lemon_squeezy_orders'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace LemonSqueezy\Laravel\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Relations\MorphMany; | ||
use LemonSqueezy\Laravel\LemonSqueezy; | ||
|
||
trait ManagesOrders | ||
{ | ||
/** | ||
* Get all of the orders for the billable. | ||
*/ | ||
public function orders(): MorphMany | ||
{ | ||
return $this->morphMany(LemonSqueezy::$orderModel, 'billable')->orderByDesc('created_at'); | ||
} | ||
|
||
/** | ||
* Determine if the billable has purchased a specific product. | ||
*/ | ||
public function hasPurchasedProduct(string $productId): bool | ||
{ | ||
return $this->orders()->where('product_id', $productId)->where('status', static::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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.