Skip to content

Commit

Permalink
group plans by product
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed Nov 20, 2019
1 parent 28ca22d commit 9c18b86
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/Console/Commands/CreateStripePlans.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class CreateStripePlans extends Command
*/
protected $description = 'Creates plans in Stripe based on the plans defined in Spark';

/**
* Product Stripe IDs
*
* @var array
*/
private $productStripeIds = [];
/**
* Create a new command instance.
*
Expand All @@ -42,8 +48,8 @@ public function handle()
{
Stripe\Stripe::setApiKey(config('services.stripe.secret'));

$this->info('Creating product ...');
$this->createProduct();
$this->info('Fetch products...');
$this->fetchProducts();

$this->info('Creating user plans...');
$this->createStripePlans(Spark::$plans);
Expand All @@ -54,36 +60,65 @@ public function handle()
$this->info('Finished');
}

protected function getProductId()
/**
* Try and create product in Stripe
*
* @param array $plans
*/
protected function fetchProducts()
{
return Spark::$details['stripe_product_id']
?? strtolower(str_replace(' ', '-', Spark::$details['product']));
try {
/** @var \Stripe\Product[] $products */
$products = Stripe\Product::all();
foreach ($products as $product) {
$this->productStripeIds[] = $product->id;
}

$this->info('Fetched products');
} catch (\Stripe\Error\InvalidRequest | \Stripe\Exception\InvalidRequest $e) {
$this->line('Unable to fetch products');
}

}

/**
* Try and create product in Stripe
* Create product in Stripe if needed and return the id
*
* @param array $plans
*/
protected function createProduct()
protected function getProductId($name = null)
{
$id = $this->getProductId();
$name = $name ?? Spark::$details['product'];

$id = strtolower(str_replace(' ', '-', $name));

$this->info('Creating looking up id for: '.$id);

if (in_array($id, $this->productStripeIds)) {
return $id;
}

$this->info('Creating product: '.$id);

try {
Stripe\Product::create([
$product = Stripe\Product::create([
'id' => $id,
'name' => Spark::$details['product'],
'name' => $name,
'statement_descriptor' => Spark::$details['vendor'],
'unit_label' => Spark::$details['unit_label'] ?? null,
'type' => 'service',
]);

$this->productStripeIds[] = $product->id;

$this->info('Stripe product created: ' . $id);
} catch (\Stripe\Error\InvalidRequest | \Stripe\Exception\InvalidRequest $e) {
$this->line('Stripe product ' . $id . ' already exists');
}

return $id;
}

/**
* Try and create plans in Stripe
*
Expand All @@ -101,7 +136,7 @@ protected function createStripePlans($plans)
Stripe\Plan::create([
'id' => $plan->id,
'nickname' => $plan->name,
'product' => $this->getProductId(),
'product' => $this->getProductId($plan->attribute('product')),
'amount' => $plan->price * 100,
'interval' => str_replace('ly', '', $plan->interval),
'currency' => config('cashier.currency'),
Expand All @@ -112,7 +147,7 @@ protected function createStripePlans($plans)

$this->info('Stripe plan created: ' . $plan->id);
} catch (\Stripe\Error\InvalidRequest | \Stripe\Exception\InvalidRequest $e) {
$this->line('Stripe plan ' . $plan->id . ' already exists');
$this->line('Stripe plan ' . $plan->id . ' already exists: '.$e->getMessage());
}
}
}
Expand Down

0 comments on commit 9c18b86

Please # to comment.