Skip to content

Commit 6d4711e

Browse files
committed
create product explicitly
1 parent c98b659 commit 6d4711e

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/Console/Commands/CreateStripePlans.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public function handle()
4242
{
4343
Stripe\Stripe::setApiKey(config('services.stripe.secret'));
4444

45+
$this->info('Creating product ...');
46+
$this->createProduct();
47+
4548
$this->info('Creating user plans...');
4649
$this->createStripePlans(Spark::$plans);
4750

@@ -51,6 +54,38 @@ public function handle()
5154
$this->info('Finished');
5255
}
5356

57+
protected function getProductId()
58+
{
59+
return Spark::$details['product_id']
60+
?? strtolower(str_replace(' ', '-', Spark::$details['product']));
61+
}
62+
63+
/**
64+
* Try and create product in Stripe
65+
*
66+
* @param array $plans
67+
*/
68+
protected function createProduct()
69+
{
70+
$id = $this->getProductId();
71+
72+
try {
73+
Stripe\Product::retrieve($id);
74+
75+
$this->line('Stripe product ' . $id . ' already exists');
76+
} catch (\Exception $e) {
77+
Stripe\Product::create([
78+
'id' => $id,
79+
'name' => Spark::$details['product'],
80+
'statement_descriptor' => Spark::$details['vendor'],
81+
'unit_label' => 'JobAds',
82+
'type' => 'service',
83+
]);
84+
85+
$this->info('Stripe product created: ' . $id);
86+
}
87+
88+
}
5489
/**
5590
* Try and create plans in Stripe
5691
*
@@ -65,11 +100,7 @@ protected function createStripePlans($plans)
65100
Stripe\Plan::create([
66101
'id' => $plan->id,
67102
'nickname' => $plan->name,
68-
'product' => [
69-
'name' => Spark::$details['product'] . ' ' . $plan->name,
70-
'statement_descriptor' => Spark::$details['vendor'],
71-
'unit_label' => 'JobAds',
72-
],
103+
'product' => $this->getProductId(),
73104
'amount' => $plan->price * 100,
74105
'interval' => str_replace('ly', '', $plan->interval),
75106
'currency' => config('cashier.currency'),

0 commit comments

Comments
 (0)