Reputation: 1229
I'm still pretty new to Django and am trying to set up recurring payments via Stripe. I'm using Django 2.0 and have successfully set up a single charge test case. However, I'm unfamiliar with how to create recurring payments, and require it for the project I'm working on.
For the single payment, I have the following:
Views
stripe.api_key = settings.STRIPE_SECRET_KEY
def checkout(request):
"""Stripe check out"""
new_tier = models.PaymentTier(
level = "Tier 3",
year = 2018
)
if request.method == "POST":
token = request.POST.get("stripeToken")
try:
charge = stripe.Charge.create(
amount = 2000,
currency = "usd",
source = token,
description = "Tier 3 subscription for Elite Fitness"
)
new_tier.charge_id = charge.id
except stripe.error.CardError as ce:
return False, ce
else:
new_tier.save()
return redirect("thank_you_page")
def payment_form(request):
"""Render stripe payment form template"""
context = {"stripe_key": settings.STRIPE_PUBLIC_KEY}
return render(request, "stripe-template.html", context)
def thank_you_page(request):
"""Successful payment processed"""
return render(request,'thank_you_page.html')
stripe-template.html
<form action="checkout/" method="POST"> {% csrf_token %}
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key={{stripe_key}} # Make sure to wrap the variable name with double {}
data-amount="2000"
data-name="company name here"
data-description="Elite Fitness Subscription"
data-image="picture.png"
data-currency="usd">
</script>
</form>
I've had a difficult time finding anything online which covers recurring payments specifically. If anybody knows how to set them up (even through dj-stripe
or pinax
) any help will be very greatly appreciated.
Upvotes: 7
Views: 5451
Reputation: 3339
In 2022 I would suggest using the latest Stripe Checkout and Stripe Checkout session API. In your view you can create a new checkout session, and redirect straight to it (no JS required). Something roughly like this:
def checkout(request):
session = stripe.checkout.Session.create(
success_url=redirect_url + "?stripe=success",
cancel_url=redirect_url + "?stripe=cancel",
mode="subscription",
# `client_reference_id` will come back in the webhook,
# making it easier to look up the associated object
client_reference_id=obj.uuid,
payment_method_types=["card"],
line_items=[
{
"price": subscription_price_id,
"quantity": 1,
}
],
)
return HttpResponseRedirect(session.url, status=303)
Depending on your app, you'll probably also want to use the Customer Portal sessions (like Checkout sessions, but let's existing customers upgrade/cancel/etc.) and a webhook endpoint (if you need to do something when subscriptions are created or cancelled). A little bit longer writeup about that in Django Forge docs and the Stripe billing quickstart is also a good link still (updated to use these APIs, just not specific to Django).
Upvotes: 0
Reputation: 24260
As of 2020 it's actually recommended to use PaymentIntents
for this. Stripe has an excellent guide for how to do this in their docs now that walks through the whole flow and covers things like 3D-secure as well.
For a longer treatment on the topic specific to Django, see this article: How to Create a Subscription SaaS Application with Django and Stripe
Upvotes: 2
Reputation: 1146
You should take a look at the Billing Quickstart-documentation. It outlines step by step how to setup a subscription (or recurring payment). The gist of it is, you first create a product, then create a plan with that product, create a customer for whom you want to bill repeatedly, then attach that plan as a subscription to the customer.
Upvotes: 6
Reputation: 312
You need to create a plan, you specify the recurring payment price and duration and then enroll your stripe customer on the plan using subscriptions.
Upvotes: 2