LoginSignup
3
1

More than 1 year has passed since last update.

Stripe Checkoutを利用した決済で、ゲスト顧客の住所や氏名を取得する方法

Posted at

Stripe Checkoutでは、会員機能のない通販サイトなど向けにゲスト注文の機能が用意されています。

ゲスト顧客として注文を処理した場合、Customer APIから顧客のデータを取得することができません。

そのため、注文時に入力された氏名や住所は、Checkoutのセッション情報から取得します。

Checkoutのセッション情報を取得する

Checkoutのセッション情報は、Session IDを利用して取得します。

const session = await stripe.checkout.sessions.retrieve('cs_test_xxxx');

2022/03/30以降の注文では、取得したデータの中にcustomer_detailsが含まれています。

{
      customer_details: {
        address: {
          city: null,
          country: 'JP',
          line1: null,
          line2: null,
          postal_code: null,
          state: null
        },
        email: 'test@example.com',
        name: 'test name',
        phone: null,
        tax_exempt: 'none',
        tax_ids: []
      }
      ...
}

このデータを利用して、発送業務や顧客からの問い合わせのサポートなどを行うことができます。

2022/03/29以前の注文で、データを取得する方法

2022/03/29以前に完了したセッションでは、customer_detailsnullになる場合があります。

そのため、以下のようにレスポンスを拡張してデータを取得する必要があります。

      const session = await stripe.checkout.sessions.retrieve('cs_test_xxxxx', {
        expand: [
          'payment_intent.payment_method'
        ]
      });
      console.log(session.payment_intent.payment_method.billing_details);

console.logの中身

    {
      address: {
        city: null,
        country: 'JP',
        line1: null,
        line2: null,
        postal_code: null,
        state: null
      },
      email: 'test@example.com',
      name: 'test name',
      phone: null
    }

Webhookでデータを取得する

WebhookでCheckoutのセッション完了時に受信するイベント(checkout.session.completed)でも、このcustomer_detailsは取得できます。

Webhookの場合、body.data.object.customer_detailsとすこしネストが深い場所にありますので、ご注意ください。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1