Reputation: 48493
I am creating a first subscription with Stripe (for a Rails app) and I stuck with how long to create a subscription for a month.
Usually, when we create a subscription, it looks like this:
Stripe::Customer.create(description:email, plan: plan_id, card: stripe_card_token)
This means that for the respective user will be create a subscription where from the user's credit card will be automatically taken every month a respective mount of money.
But some users would like to pay their subscription for example only for a month, which means no recurring billing.
How to do that?
The documentation says that I should set up cancel_at_period_end on true and then the subscription will end up after the first month (which means that after the first month will be the subscription set up as cancelled).
But where should I set up this parameter?
Or - is my understanding of this problematic wrong and am I overlooking something?
Thank oyu
Upvotes: 1
Views: 725
Reputation: 5691
You can cancel the subscriptions using the following code:
# Set your secret key: remember to change this to your live secret key in production
# See your keys here https://dashboard.stripe.com/account/apikeys
Stripe.api_key = "sk_test_ypuqOnp64ATuGJlbgEwmAtzV"
customer = Stripe::Customer.retrieve("cus_3R1W8PG2DmsmM9")
customer.subscriptions.retrieve("sub_3R3PlB2YlJe84a").delete
By default, the cancellation takes effect immediately. If you would like to cancel the subscription immediately, but keep the subscription active until the end of the billing period (e.g., through the time the customer has already paid for), provide an at_period_end value of true:
customer.subscriptions.retrieve("sub_3R3PlB2YlJe84a").delete(:at_period_end => true)
In your case, if you wish to provide only one month subscription, you can consider it as one transection, and I would recommend you to do it with Subscription::Charge
as follows:
customer = Stripe::Customer.create(
:source => token,
:description => "Example customer"
)
# Charge the Customer instead of the card
Stripe::Charge.create(
:amount => 1000, # in cents
:currency => "usd",
:customer => customer.id
)
Upvotes: 0