generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ninjaparade <[email protected]>
- Loading branch information
1 parent
5963a36
commit 6a17cd5
Showing
11 changed files
with
235 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Ninjaparade\StripeData\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Ninjaparade\StripeData\Data\Response\Prices\StripePriceData; | ||
use Ninjaparade\StripeData\Models\StripePrice; | ||
use Ninjaparade\StripeData\Stripe\StripeService; | ||
use Stripe\Exception\ApiErrorException; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
|
||
class SyncPricesCommand extends Command | ||
{ | ||
public $signature = 'stripe-data-sync:prices'; | ||
|
||
public $description = 'Command description'; | ||
|
||
protected ProgressBar $bar; | ||
|
||
public function __construct(protected StripeService $stripe) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @throws ApiErrorException | ||
*/ | ||
public function handle(): int | ||
{ | ||
$prices = $this->stripe->prices(); | ||
|
||
$count = $prices->data->count(); | ||
|
||
$this->bar = $this->output->createProgressBar($count); | ||
$this->info("Syncing $count records 🥷"); | ||
|
||
$this->bar->start(); | ||
|
||
$prices->data->each(function (StripePriceData $price) { | ||
|
||
StripePrice::query() | ||
->updateOrCreate([ | ||
'stripe_id' => $price->stripe_id, | ||
], $price | ||
->except('object') | ||
->except('stripe_id') | ||
->except('product') | ||
->toArray()); | ||
$this->bar->advance(); | ||
}); | ||
|
||
$this->bar->finish(); | ||
$this->info("Done Syncing $count records 🥷"); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Ninjaparade\StripeData\Data\Response\Prices; | ||
|
||
use Spatie\LaravelData\Attributes\DataCollectionOf; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\DataCollection; | ||
|
||
class StripePaginatedPriceData extends Data | ||
{ | ||
public function __construct( | ||
public readonly bool $has_more, | ||
#[DataCollectionOf(StripePriceData::class)] | ||
public readonly ?DataCollection $data, | ||
) { | ||
|
||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
namespace Ninjaparade\StripeData\Data\Response\Prices; | ||
|
||
use Carbon\Carbon; | ||
use Carbon\CarbonInterface; | ||
use Spatie\LaravelData\Attributes\MapInputName; | ||
use Spatie\LaravelData\Attributes\WithCast; | ||
use Spatie\LaravelData\Casts\DateTimeInterfaceCast; | ||
use Spatie\LaravelData\Data; | ||
|
||
class StripePriceData extends Data | ||
{ | ||
public function __construct( | ||
public readonly string $object, | ||
public readonly ?string $name, | ||
#[MapInputName('id')] | ||
public readonly string $stripe_id, | ||
public readonly string $product, | ||
public readonly bool $active, | ||
public readonly string $billing_scheme, | ||
public readonly string $currency, | ||
public readonly string|int $unit_amount, | ||
public readonly ?string $nickname, | ||
public readonly ?string $lookup_key, | ||
public readonly ?string $tax_behavior, | ||
public readonly string $type, | ||
// public readonly ?array $recurring, | ||
// public readonly ?array $metadata, | ||
|
||
#[WithCast(DateTimeInterfaceCast::class, format: 'U', type: Carbon::class), MapInputName('created')] | ||
public readonly CarbonInterface $created_at, | ||
) { | ||
} | ||
} | ||
|
||
/* | ||
"id" => "price_1Na3VOFw6aD3pDxKKHMZuJcH" | ||
"object" => "price" | ||
"active" => true | ||
"billing_scheme" => "per_unit" | ||
"created" => 1690838394 | ||
"currency" => "usd" | ||
"custom_unit_amount" => null | ||
"livemode" => false | ||
"lookup_key" => null | ||
"metadata" => [] | ||
"nickname" => null | ||
"product" => "prod_OMn5f7sun9043J" | ||
"recurring" => array:5 [ | ||
"aggregate_usage" => null | ||
"interval" => "year" | ||
"interval_count" => 1 | ||
"trial_period_days" => null | ||
"usage_type" => "licensed" | ||
] | ||
"tax_behavior" => "unspecified" | ||
"tiers_mode" => null | ||
"transform_quantity" => null | ||
"type" => "recurring" | ||
"unit_amount" => 900000 | ||
"unit_amount_decimal" => "900000" | ||
] | ||
*/ |
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,9 @@ | ||
<?php | ||
|
||
namespace Ninjaparade\StripeData\Enums; | ||
|
||
enum BillingType: string | ||
{ | ||
case OneTime = 'one_time'; | ||
case Recurring = 'recurring'; | ||
} |
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 Ninjaparade\StripeData\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Ninjaparade\StripeData\Enums\BillingType; | ||
|
||
class StripePrice extends Model | ||
{ | ||
protected $table = 'stripe_prices'; | ||
|
||
protected $guarded = ['id']; | ||
|
||
protected $casts = [ | ||
'type' => BillingType::class, | ||
// 'unit_amount' => 'integer', | ||
// 'recurring' => 'array', | ||
// 'metadata' => 'array', | ||
// 'active' => 'boolean', | ||
]; | ||
|
||
// | ||
protected $attributes = [ | ||
'metadata' => [], | ||
'recurring' => [], | ||
]; | ||
|
||
public function product(): BelongsTo | ||
{ | ||
return $this->belongsTo(StripeProduct::class); | ||
} | ||
} |
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 | ||
|
||
namespace Ninjaparade\StripeData\Stripe\Concerns; | ||
|
||
use Exception; | ||
use Ninjaparade\StripeData\Data\Response\Prices\StripePaginatedPriceData; | ||
use Ninjaparade\StripeData\Data\Response\Prices\StripePriceData; | ||
use Stripe\Exception\ApiErrorException; | ||
|
||
trait InteractsWithPrices | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
public function price(string $price_id): StripePriceData | ||
{ | ||
try { | ||
$product = $this->client()->prices->retrieve($price_id); | ||
} catch (ApiErrorException $e) { | ||
throw new Exception($e->getMessage()); | ||
} | ||
|
||
return StripePriceData::from($product->toArray()); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function prices(array $parameters = [], int $limit = 100): StripePaginatedPriceData | ||
{ | ||
try { | ||
$prices = $this->client()->prices->all(array_merge($parameters, [ | ||
'limit' => $limit, | ||
])); | ||
|
||
} catch (ApiErrorException $e) { | ||
throw new Exception($e->getMessage()); | ||
} | ||
|
||
return StripePaginatedPriceData::from($prices->toArray()); | ||
} | ||
} |
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