HOMEBLOG ⟩ Stripe 定期決済で次回の請求日を取得する

Stripe 定期決済で次回の請求日を取得する

Stripeのサブスクリプションを使用する際に、次回の請求日を知りたい場合

$stripe->subscriptions->retrieve($subscr_id);

で取得できる値から知ることができます。

Subscription Retrieve で取得できる値

$stripe->subscriptions->retrieve で、IDを指定すると以下のような値が返ってきます。
※一部抜粋

{
  "id": "サブスクID",
  "object": "subscription",
  "application_fee_percent": null,
  "cancel_at": null,
  "cancel_at_period_end": false,
  "canceled_at": null,
  "created": 1494900949,
  "current_period_end": 1605492949,
  "current_period_start": 1602814549,
  "customer": "顧客ID",
  "discount": null,
  "ended_at": null,
  "items": {
  },
  "schedule": null,
  "start_date": 1494900949,
  "status": "active",
  "transfer_data": null,
  "trial_end": null,
  "trial_start": null
}

参考:The subscription object

期間終了日を取得する

ストライプのサブスクリプションは、請求期間の終了日に新しい請求書が作成されます。
current_period_endは、サブスクリプション終了日のタイムスタンプを保存しているので、current_period_endの値を取得すれば、次回の請求日を知ることができます。

//$vにサブクスIDを格納
$subsc_result = $stripe->subscriptions->retrieve($v);
//定期決済が有効か判定
if( $subsc_result->ended_at == null ){
  $current_period = $subsc_result->current_period_end; //終了日を取得
  $current_period = date('Y年m月d日',$current_period); //タイムスタンプを年月日に変換
  var_dump($current_period); // ●年●月●日 の値が取得できる
}