Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
Signed-off-by: ninjaparade <[email protected]>
  • Loading branch information
ninjaparade committed Dec 10, 2023
1 parent 5963a36 commit 6a17cd5
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": "^8.1",
"illuminate/contracts": "^10.0",
"lorisleiva/laravel-actions": "^2.7",
"spatie/laravel-data": "^3.9",
"spatie/laravel-package-tools": "^1.14.0",
"stripe/stripe-php": "^12.5"
Expand Down
1 change: 1 addition & 0 deletions database/migrations/create_stripe_data_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ return new class extends Migration
$table->string('nickname')->nullable();
$table->string('stripe_id');
$table->string('type');
$table->boolean('active')->default(true);
$table->string('billing_scheme');
$table->unsignedInteger('unit_amount');
$table->string('currency');
Expand Down
57 changes: 57 additions & 0 deletions src/Commands/SyncPricesCommand.php
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;
}
}
18 changes: 18 additions & 0 deletions src/Data/Response/Prices/StripePaginatedPriceData.php
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,
) {

}
}
65 changes: 65 additions & 0 deletions src/Data/Response/Prices/StripePriceData.php
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"
]
*/
9 changes: 9 additions & 0 deletions src/Enums/BillingType.php
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';
}
33 changes: 33 additions & 0 deletions src/Models/StripePrice.php
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);
}
}
42 changes: 42 additions & 0 deletions src/Stripe/Concerns/InteractsWithPrices.php
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());
}
}
9 changes: 5 additions & 4 deletions src/Stripe/Concerns/InteractsWithProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

namespace Ninjaparade\StripeData\Stripe\Concerns;

use Exception;
use Ninjaparade\StripeData\Data\Response\Products\StripePaginatedProductData;
use Ninjaparade\StripeData\Data\Response\Products\StripeProductData;
use Stripe\Exception\ApiErrorException;

trait InteractsWithProducts
{
/**
* @throws \Exception
* @throws Exception
*/
public function product(string $product_id): StripeProductData
{
try {
$product = $this->client()->products->retrieve($product_id);
} catch (ApiErrorException $e) {
throw new \Exception($e->getMessage());
throw new Exception($e->getMessage());
}

return StripeProductData::from($product->toArray());
}

/**
* @throws \Exception
* @throws Exception
*/
public function products(array $parameters = [], int $limit = 100): StripePaginatedProductData
{
Expand All @@ -32,7 +33,7 @@ public function products(array $parameters = [], int $limit = 100): StripePagina
'limit' => $limit,
]));
} catch (ApiErrorException $e) {
throw new \Exception($e->getMessage());
throw new Exception($e->getMessage());
}

return StripePaginatedProductData::from($products->toArray());
Expand Down
2 changes: 2 additions & 0 deletions src/Stripe/StripeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Ninjaparade\StripeData\Data\Config\StripeConfig;
use Ninjaparade\StripeData\Stripe\Concerns\InteractsWithCustomers;
use Ninjaparade\StripeData\Stripe\Concerns\InteractsWithInvoices;
use Ninjaparade\StripeData\Stripe\Concerns\InteractsWithPrices;
use Ninjaparade\StripeData\Stripe\Concerns\InteractsWithProducts;
use Stripe\StripeClient;

class StripeService
{
use InteractsWithCustomers;
use InteractsWithInvoices;
use InteractsWithPrices;
use InteractsWithProducts;

public function __construct(protected StripeConfig $config)
Expand Down
2 changes: 2 additions & 0 deletions src/StripeDataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Relations\Relation;
use Ninjaparade\StripeData\Commands\SyncCustomersCommand;
use Ninjaparade\StripeData\Commands\SyncPricesCommand;
use Ninjaparade\StripeData\Commands\SyncProductsCommand;
use Ninjaparade\StripeData\Data\Config\StripeConfig;
use Ninjaparade\StripeData\Models\StripeCustomer;
Expand Down Expand Up @@ -53,6 +54,7 @@ protected function getCommands(): array
[
SyncCustomersCommand::class,
SyncProductsCommand::class,
SyncPricesCommand::class,
] : [];
}

Expand Down

0 comments on commit 6a17cd5

Please sign in to comment.