Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recurring payments using Stripe and Django

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.

like image 288
aalberti333 Avatar asked Jun 26 '18 19:06

aalberti333


People also ask

Can I take recurring payments with Stripe?

Stripe Billing is the fastest way for your business to bill customers with subscriptions or invoices. Capture more revenue, support new products or business models, and accept recurring payments globally.


2 Answers

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.

like image 111
korben Avatar answered Sep 22 '22 02:09

korben


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.

like image 40
umgelurgel Avatar answered Sep 22 '22 02:09

umgelurgel