【Stripe】商品IDから商品情報を取得する
※本ページのリンクにはプロモーションが含まれています。
こんにちは、Ryohei(@ityryohei)です!
本記事では、Stripeのダッシュボードで登録した商品のIDから商品情報を取得する方法をご紹介しています。Stripeは複数の言語に対応していますが、ここではPHPを例に解説しています。
PHPでStripeの商品IDから商品情報を取得したいんだけど、どうすればいいんだろう?
上記の疑問にお答えします。
では、解説していきます。Stripeをクライアント側で使用するための準備
Stripeをクライアント側で使用するためにライブラリのインストールとシークレットキー等の設定を行います。StripeのシークレットキーはStripeにサインインするとダッシュ―ボードに表示されています。
Striepライブラリをインストールする
composer require stripe/stripe-php
Stripeのシークレットキーを指定してオブジェクトをインスタンス化
<?php //ライブラリ読み込み require 'vendor/autoload.php'; //シークレットキー $seacret_key = 'sk_test_xxxxxxxxxx'; //インスタンス化 $stripe = new \Stripe\StripeClient([ "api_key" => $seacret_key , "stripe_version" => "2020-08-27" ]);
これでクライアント側からStripeの各オブジェクトにアクセスすることができます。
商品IDから商品情報を取得する
Stripeで登録した商品の商品IDから、商品情報を取得します。商品情報はProductオブジェクトにアクセスして取得することができます。
//ライブラリ読み込み require 'vendor/autoload.php'; //シークレットキー $seacret_key = 'sk_test_xxxxxxxxxx'; //シークレットキーを指定してインスタンス化 $stripe = new \Stripe\StripeClient([ "api_key" => $seacret_key , "stripe_version" => "2020-08-27" ]); //商品ID $product_id = 'prod_LP6tvOrc32uTuj'; //商品IDから商品情報を取得 $product = $stripe->products->retrieve($product_id); var_dump($product);
実行すると下記の情報を取得することができます。
object(Stripe\Product)#14 (18) {
["id"]=>
string(19) "prod_xxxxxxxxxx"
["object"]=>
string(7) "product"
["active"]=>
bool(true)
["attributes"]=>
array(0) {
}
["created"]=>
int(1648474314)
["description"]=>
string(36) "テスト商品の説明文です。"
["images"]=>
array(0) {
}
["livemode"]=>
bool(false)
["metadata"]=>
object(Stripe\StripeObject)#19 (0) {
}
["name"]=>
string(15) "テスト商品"
["package_dimensions"]=>
NULL
["shippable"]=>
NULL
["statement_descriptor"]=>
NULL
["tax_code"]=>
NULL
["type"]=>
string(7) "service"
["unit_label"]=>
NULL
["updated"]=>
int(1648474314)
["url"]=>
NULL
}
各プロパティにアクセスする場合は、オブジェクトと同様にアロー演算子でプロパティを指定します。
$product->id; $product->name;
これで商品IDから商品情報を取得することができます。
商品価格について
前項で取得した商品情報の出力結果を確認すると、商品価格がないことがわかります。商品価格はProductオブジェクトではなくPriceオブジェクトが保持しています。そのため商品価格を取得したい場合は、商品に登録されているprice_idからPriceオブジェクトにアクセスして取得する必要があります。
<?php //ライブラリ読み込み require 'vendor/autoload.php'; //シークレットキー $seacret_key = 'sk_test_xxxxxxxxxx'; //シークレットキーを指定してインスタンス化 $stripe = new \Stripe\StripeClient([ "api_key" => $seacret_key , "stripe_version" => "2020-08-27" ]); //商品ID $product_id = 'prod_xxxxxxxxxx'; //商品IDから商品情報を取得 $product = $stripe->products->retrieve($product_id); //PRICE ID $price_id = 'price_xxxxxxxxxx'; //PRICE IDから商品の価格情報を取得 $price = $stripe->prices->retrieve($price_id); var_dump($price);
実行すると下記の結果が返ります。
object(Stripe\Price)#25 (18) {
["id"]=>
string(30) "price_xxxxxxxxxx"
["object"]=>
string(5) "price"
["active"]=>
bool(true)
["billing_scheme"]=>
string(8) "per_unit"
["created"]=>
int(1648474314)
["currency"]=>
string(3) "jpy"
["livemode"]=>
bool(false)
["lookup_key"]=>
NULL
["metadata"]=>
object(Stripe\StripeObject)#30 (0) {
}
["nickname"]=>
NULL
["product"]=>
string(19) "prod_xxxxxxxxxx"
["recurring"]=>
NULL
["tax_behavior"]=>
string(11) "unspecified"
["tiers_mode"]=>
NULL
["transform_quantity"]=>
NULL
["type"]=>
string(8) "one_time"
["unit_amount"]=>
int(1000)
["unit_amount_decimal"]=>
string(4) "1000"
}
上記のunit_amountが商品価格です。ここでは1000円の価格を設定しています。
Priceオブジェクトは商品に紐づいているため、返り値にproduct_idが含まれています。商品情報と価格を取得したい場合は、price_idからPriceオブジェクトにアクセスし、返り値に含まれているproduct_idから商品情報を取得した方が良いかもしれませんね。
最後に
Stripeは世界中で利用されているグローバルな決済サービスです。初期費用等はなく決済時に手数料を支払うだけでセキュアな決済環境を導入することができます。日本でも徐々に知名度が上がってきていて、需要は増していくのではないかなと勝手に思ったりしてます。今後も決済が必要な場合に利用したいサービスですね。
以上、Stripeの商品IDから商品情報を取得する方法のご紹介でした!