【Stripe】CHECKOUT_SESSION_IDからセッションオブジェクトを取得する
※本ページのリンクにはプロモーションが含まれています。
こんにちは、Ryohei(@ityryohei)です!
本記事では、StripeのCHECKOUT_SESSION_IDからセッションオブジェクトを取得する方法をご紹介しています。
Stripeのsession_idからセッションオブジェクトを取得したいんだけど、どうすればいいんだろう?
上記の疑問にお答えします。Stripeは複数のプログラミング言語に対応していますが、本記事ではPHPを使用しています。
では、解説していきます。Stripeの準備
Stripeをクライアントで使用するため、Stripeのライブラリをインストールしてインスタンスを生成します。インスタンス生成時に指定するStripeのシークレットキーはダッシュ―ボードから取得できます。
Striepライブラリをインストールする
composerでStripeのライブラリをインストールします。
composer require stripe/stripe-php
コマンド一発なので簡単にインストールできますね。
Stripeのインスタンスを生成する
Stripeライブラリがインストールできたら、Stripeを使用するファイルにライブラリ群を読み込み、インスタンスを生成します。
<?php //ライブラリ読み込み require 'vendor/autoload.php'; //シークレットキー $seacret_key = 'sk_test_xxxxxxxxxx'; //インスタンス生成 $stripe = new \Stripe\StripeClient($seacret_key);
以上でStripeの準備は完了です。
CHECKOUT_SESSION_IDからセッションオブジェクトを取得する
StripeのCHECKOUT_SESSION_IDからセッションオブジェクトを取得します。セッションオブジェクトはStripeClientのretrieveメソッドで取得できます。
Stripeの決済後、下記のページにリダイレクトされた場合を想定します。
https://localhost/stripe/thanks/?session_id=cs_test_xxxxxxxxxx
クエリ文字列のsession_idの値からセッションオブジェクトを取得します。
<?php //ライブラリ読み込み require 'vendor/autoload.php'; //シークレットキー $seacret_key = 'sk_test_xxxxxxxxxx'; //インスタンス生成 $stripe = new \Stripe\StripeClient($seacret_key); //CHECKOUT_SESSION_ID得 $id = $_GET['sesssion_id']; //セッションオブジェクト取得 $session = $stripe->checkout->sessions->retrieve($id);
取得したセッションオブジェクトは下記のような情報を持っています。
Session Object
object(Stripe\Checkout\Session)#5055 (40) {
["id"]=>
string(66) "cs_test_xxxxxxxxxx"
["object"]=>
string(16) "checkout.session"
["after_expiration"]=>
NULL
["allow_promotion_codes"]=>
bool(false)
["amount_subtotal"]=>
int(1000)
["amount_total"]=>
int(1000)
["automatic_tax"]=>
object(Stripe\StripeObject)#5060 (2) {
["enabled"]=>
bool(false)
["status"]=>
NULL
}
["billing_address_collection"]=>
string(4) "auto"
["cancel_url"]=>
string(18) "https://stripe.com"
["client_reference_id"]=>
NULL
["consent"]=>
NULL
["consent_collection"]=>
NULL
["currency"]=>
string(3) "jpy"
["customer"]=>
NULL
["customer_creation"]=>
string(11) "if_required"
["customer_details"]=>
object(Stripe\StripeObject)#5061 (6) {
["address"]=>
object(Stripe\StripeObject)#5069 (6) {
["city"]=>
NULL
["country"]=>
string(2) "JP"
["line1"]=>
NULL
["line2"]=>
NULL
["postal_code"]=>
NULL
["state"]=>
NULL
}
["email"]=>
string(21) "taro.tanaka@example.com"
["name"]=>
string(15) "TARO TANAKA"
["phone"]=>
NULL
["tax_exempt"]=>
string(4) "none"
["tax_ids"]=>
array(0) {
}
}
["customer_email"]=>
NULL
["expires_at"]=>
int(1649652544)
["livemode"]=>
bool(false)
["locale"]=>
string(4) "auto"
["metadata"]=>
object(Stripe\StripeObject)#5065 (0) {
}
["mode"]=>
string(7) "payment"
["payment_intent"]=>
string(27) "pi_xxxxxxxxxx"
["payment_link"]=>
string(30) "plink_xxxxxxxxxx"
["payment_method_options"]=>
array(0) {
}
["payment_method_types"]=>
array(1) {
[0]=>
string(4) "card"
}
["payment_status"]=>
string(4) "paid"
["phone_number_collection"]=>
object(Stripe\StripeObject)#5070 (1) {
["enabled"]=>
bool(false)
}
["recovered_from"]=>
NULL
["setup_intent"]=>
NULL
["shipping"]=>
NULL
["shipping_address_collection"]=>
NULL
["shipping_options"]=>
array(0) {
}
["shipping_rate"]=>
NULL
["status"]=>
string(8) "complete"
["submit_type"]=>
string(4) "auto"
["subscription"]=>
NULL
["success_url"]=>
string(18) "https://stripe.com"
["total_details"]=>
object(Stripe\StripeObject)#5077 (3) {
["amount_discount"]=>
int(0)
["amount_shipping"]=>
int(0)
["amount_tax"]=>
int(0)
}
["url"]=>
NULL
}
セッションオブジェクトは多くの情報を持っています。DB等の処理で使用される名前やメールアドレス、合計金額等のプロパティは下記のようにアクセス取得できます。
//名前 $session->customer_details->name //メールアドレス $session->customer_details->email, //小計 $session->amount_subtotal; //合計 $session->amount_total;
最後に
Stripeで簡単に決済フォームを作成することができるStripe Checkoutは、サンクスページにリダイレクトする形で自前のWEBサイトにsession_idを渡すことができます。そのため決済完了→独自のシステムを実行という処理が構築しやすかったです。Stripe Elementsを使って独自のフォームに組み込むこともできるので、案件の内容や予算に応じて決済フォームを使い分けできるのもStripeの魅力。また決済が必要な案件に携わる機会があれば使わせてもらおうと思います。
以上、StripeでCHECKOUT_SESSION_IDからセッションオブジェクトを取得する方法の