Skip to content

Instantly share code, notes, and snippets.

@g105b
Last active July 31, 2023 13:23
Show Gist options
  • Save g105b/f6ce0e4cf898f4d43bc127bbc1a93d0f to your computer and use it in GitHub Desktop.
Save g105b/f6ce0e4cf898f4d43bc127bbc1a93d0f to your computer and use it in GitHub Desktop.
Stripe usage
<?php
namespace App\Payment;
use Iterator;
use Stripe\Collection;
use Stripe\Price as StripePrice;
use Stripe\StripeClient;
use Stripe\Product as StripeProduct;
/** @implements Iterator<Product> */
class ProductList implements Iterator {
/** @var array<Product> */
private array $productArray;
public function __construct(
private readonly StripeClient $stripe,
) {
$this->productArray = [];
$this->rewind();
/** @var Collection<StripeProduct> $productCollection */
$productCollection = $this->stripe->products->all();
foreach($productCollection as $product) {
if(!$product->active) {
continue;
}
/** @var StripePrice $price */
$price = $this->stripe->prices->retrieve($product->default_price);
array_push(
$this->productArray,
new Product(
$product->id,
$product->name,
new Price(
$price->id,
$price->unit_amount,
Currency::fromString($price->currency),
)
),
);
}
}
public function key():int {
return key($this->productArray);
}
public function rewind():void {
reset($this->productArray);
}
public function next():void {
next($this->productArray);
}
public function valid():bool {
return isset($this->productArray[key($this->productArray)]);
}
public function current():Product {
return current($this->productArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment