ホーム

How to cancel a subscription with Stripe

0
0

I will show you how to cancel a subscription with Stripe. The code used here is php but if you know the logic you think that it will be helpful enough in other languages.

In order to cancel Subscription with Stripe, it is a member ID which starts with sub_. I think that this can be confirmed in the dashboard as follows. This is unique and unique one by one.

 

Run the subscription campaign with the member ID starting with sub_ above. It will cancel the subscription that will be carried out as follows.

 

$sub = \Stripe\Subscription::retrieve("sub_CY38MZuhjsTtRc");
$sub->cancel();

echo $sub;

 

The problem is how to dynamically acquire a member ID. How about this kind of method? I think that I made customer information when making a subscription, but I will save the e-mail address used in the description and the above-mentioned member ID created when making a subscription in the database, and in the form of cancellation of membership By acquiring the member ID from the mail address, identifying the member ID associated with the e-mail address, and executing the cancel processing of Subscription for that member ID. Even a mail address + a password is fine. Is not there a problem? For example, if you had created customer information and subscriptions as follows

 

//======================================================================
//Create customer information based on token information
//======================================================================
kk =  \Stripe\Customer::create(
  array(
  "description" => "E-mail address received from the form",
  "source" => "$token_from_js", //Customer information token
  )
);

//======================================================================
//Execute by making subscription for monthly payment based on customer information
//======================================================================
$subscription =  \Stripe\Subscription::create(
  array(
  "customer" => "$customer->id",
  "items" => array( array("plan" => "id-test",), )
  )
);

A mail address and a customer ID can be obtained as follows.

//======================================================================
//Confirm execution result with echo
//======================================================================
echo $subscription->id;
echo $customer -> description;

If you keep the above two variables in db, it seems that users can cancel their subscriptions themselves.

Pocket
LinkedIn にシェア

  • この記事を書いた人
  • 最新記事

藤沢瞭介(Ryosuke Hujisawa)

りょすけと申します。18歳からプログラミングをはじめ、今はフロントエンドでReactを書いたり、AIの勉強を頑張っています。off.tokyoでは、ハイテクやガジェット、それからプログラミングに関する情報まで、エンジニアに役立つ情報を日々発信しています!

-ホーム

Copyright© off.tokyo , 2021 All Rights Reserved Powered by AFFINGER5.