Skip to content

Commit 9c18b86

Browse files
committed
group plans by product
1 parent 28ca22d commit 9c18b86

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

src/Console/Commands/CreateStripePlans.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class CreateStripePlans extends Command
2323
*/
2424
protected $description = 'Creates plans in Stripe based on the plans defined in Spark';
2525

26+
/**
27+
* Product Stripe IDs
28+
*
29+
* @var array
30+
*/
31+
private $productStripeIds = [];
2632
/**
2733
* Create a new command instance.
2834
*
@@ -42,8 +48,8 @@ public function handle()
4248
{
4349
Stripe\Stripe::setApiKey(config('services.stripe.secret'));
4450

45-
$this->info('Creating product ...');
46-
$this->createProduct();
51+
$this->info('Fetch products...');
52+
$this->fetchProducts();
4753

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

57-
protected function getProductId()
63+
/**
64+
* Try and create product in Stripe
65+
*
66+
* @param array $plans
67+
*/
68+
protected function fetchProducts()
5869
{
59-
return Spark::$details['stripe_product_id']
60-
?? strtolower(str_replace(' ', '-', Spark::$details['product']));
70+
try {
71+
/** @var \Stripe\Product[] $products */
72+
$products = Stripe\Product::all();
73+
foreach ($products as $product) {
74+
$this->productStripeIds[] = $product->id;
75+
}
76+
77+
$this->info('Fetched products');
78+
} catch (\Stripe\Error\InvalidRequest | \Stripe\Exception\InvalidRequest $e) {
79+
$this->line('Unable to fetch products');
80+
}
81+
6182
}
6283

6384
/**
64-
* Try and create product in Stripe
85+
* Create product in Stripe if needed and return the id
6586
*
6687
* @param array $plans
6788
*/
68-
protected function createProduct()
89+
protected function getProductId($name = null)
6990
{
70-
$id = $this->getProductId();
91+
$name = $name ?? Spark::$details['product'];
92+
93+
$id = strtolower(str_replace(' ', '-', $name));
94+
95+
$this->info('Creating looking up id for: '.$id);
96+
97+
if (in_array($id, $this->productStripeIds)) {
98+
return $id;
99+
}
100+
101+
$this->info('Creating product: '.$id);
71102

72103
try {
73-
Stripe\Product::create([
104+
$product = Stripe\Product::create([
74105
'id' => $id,
75-
'name' => Spark::$details['product'],
106+
'name' => $name,
76107
'statement_descriptor' => Spark::$details['vendor'],
77108
'unit_label' => Spark::$details['unit_label'] ?? null,
78109
'type' => 'service',
79110
]);
80111

112+
$this->productStripeIds[] = $product->id;
113+
81114
$this->info('Stripe product created: ' . $id);
82115
} catch (\Stripe\Error\InvalidRequest | \Stripe\Exception\InvalidRequest $e) {
83116
$this->line('Stripe product ' . $id . ' already exists');
84117
}
85118

119+
return $id;
86120
}
121+
87122
/**
88123
* Try and create plans in Stripe
89124
*
@@ -101,7 +136,7 @@ protected function createStripePlans($plans)
101136
Stripe\Plan::create([
102137
'id' => $plan->id,
103138
'nickname' => $plan->name,
104-
'product' => $this->getProductId(),
139+
'product' => $this->getProductId($plan->attribute('product')),
105140
'amount' => $plan->price * 100,
106141
'interval' => str_replace('ly', '', $plan->interval),
107142
'currency' => config('cashier.currency'),
@@ -112,7 +147,7 @@ protected function createStripePlans($plans)
112147

113148
$this->info('Stripe plan created: ' . $plan->id);
114149
} catch (\Stripe\Error\InvalidRequest | \Stripe\Exception\InvalidRequest $e) {
115-
$this->line('Stripe plan ' . $plan->id . ' already exists');
150+
$this->line('Stripe plan ' . $plan->id . ' already exists: '.$e->getMessage());
116151
}
117152
}
118153
}

0 commit comments

Comments
 (0)