This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy pathInventory.php
113 lines (96 loc) · 3.18 KB
/
Inventory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Store;
class Inventory
{
// This would be a lookup in your database
private static $products = [
'increment' => [
'type' => 'good', 'name'=> 'Increment Magazine', 'attributes' => [ 'issue' ],
'sku' => [
'id' => 'increment-03',
'product' => 'increment',
'attributes' => [ 'issue' => 'Issue #3 “Development”' ],
'price' => 399, 'currency' => 'usd',
'inventory' => [ 'type' => 'infinite' ]
]
],
'pins' => [
'type' => 'good', 'name' => 'Stripe Pins', 'attributes' => [ 'set' ],
'sku' => [
'id' => 'pins-collector',
'product' => 'pins',
'attributes' => [ 'set' => 'Collector Set' ],
'price' => 799, 'currency' => 'usd',
'inventory' => [ 'type' => 'finite', 'quantity' => 500 ]
]
],
'shirt' => [
'type' => 'good', 'name' => 'Stripe Shirt', 'attributes' => ['size', 'gender'],
'sku' => [
'id' => 'shirt-small-woman',
'product' => 'shirt',
'attributes' => [ 'size' => 'Small Standard', 'gender' => 'Woman' ],
'price' => 999, 'currency' => 'usd',
'inventory' => [ 'type' => 'infinite' ]
]
]
];
public static function calculatePaymentAmount($items) {
$total = 0;
foreach ($items as $item) {
$total += self::getSkuPrice($item['parent']) * $item['quantity'];
}
return $total;
}
public static function listProducts() {
static $cachedProducts = null;
if ($cachedProducts) {
return $cachedProducts;
}
$ids = array_keys(self::$products);
$products = \Stripe\Product::all([ "ids" => $ids ]);
if (count($products->data) === count($ids)) {
$cachedProducts = self::withSkus($products);
return $cachedProducts;
}
// Products have not been created yet, do it one by one
foreach (self::$products as $id => $product) {
$p = $product;
$p['id'] = $id;
unset($p['sku']);
\Stripe\Product::create($p);
\Stripe\Sku::create($product['sku']);
}
$products = \Stripe\Product::all([ "ids" => $ids ]);
if (count($products->data) === count($ids)) {
$cachedProducts = self::withSkus($products);
return $cachedProducts;
}
// Stripe should already have thrown an Exception but just in case
throw new \RuntimeException("Couldn't retrieve nor create the products in Stripe.");
}
protected static function withSkus($products) {
foreach ($products->data as $i => $product) {
$products->data[$i]->skus = [ 'data' => [
\Stripe\Sku::retrieve(self::$products[$product->id]['sku']['id'])
]];
}
return $products;
}
public static function getSkuPrice($id) {
foreach (self::listProducts()->data as $product) {
if ($product->skus->data[0]->id == $id) {
return $product->skus->data[0]->price;
}
}
throw new \UnexpectedValueException('Unknown sku ID. Argument passed: ' . $id);
}
public static function getProduct($id) {
foreach (self::listProducts()->data as $product) {
if ($product->id == $id) {
return $product;
}
}
throw new \UnexpectedValueException('Unknown product ID. Argument passed: ' . $id);
}
}