avatarNuno Bispo

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2090

Abstract

   </figure></iframe></div></div></figure><p id="7c84">In this view we have a check for a POST request or a GET request:</p><ul><li>The POST request is used after the user pressed the cancel button on the page to cancel the membership. For that we get the Stripe subscription and set the flag to cancel at the period end.</li><li>GET request we are just getting the information.</li></ul><p id="f91c">The corresponding html template to show this view is as follows:</p>
    <figure id="d67d">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/nunombispo/0818376f6c0972f75c3f89ff3e44cea0.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="fb47">And, of course, we need to add our new url to the ‘urls.py’ file:</p>
    <figure id="c940">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/nunombispo/56fa3593bc023ad051dec38a00542410.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h1 id="81a4">2) Get updates from Stripe</h1><p id="9196">We now have the capability of a user cancelling their subscription from our website, but what if there is a problem with the Stripe subscription?</p><p id="8194">For instance a credit card might expire and the subscription will not get renew. In those cases we want to update that info from our side and also suspend the membership for that user.</p><p id="6b8f">There are several ways of achieving this, for this tutorial I will create an update page that the Django superuser can access to perform the updates.</p><p id="7886">Let’s create view for the update page:</p>
    <figure id="5b5f">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/nunombispo/aa6db91e1bafd24194ec0384c5f2d8f9.js" allowfullscreen="" framebord

Options

er="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><p id="a85f">The decorator ‘user_passes_test’ allows us to define an inner function to perform the check if the logged user is a Django superuser. If that is the case then he can access the page.</p><p id="502e">The view handler gets all of our Customers from the database, their corresponding Stripe subscriptions and then according to the status, activate or deactivate the membership on our site. At the end we just return a simple response, so we don’t need an html template to render this view (perfectly fine for a backoffice page)</p><h1 id="b24e">3) Conclusion</h1><p id="99f4">On this last part of our tutorial, we performed the following:</p><ul><li>Created a page where our user can cancel their membership</li><li>Retrieve updates from Stripe for the subscriptions status</li></ul><p id="6198">This concludes our 3 part tutorial, hope that now with this basic knowledge you are prepared to expand and create your own subscription based Django website with Stripe.</p><p id="fc9e">You can check out my GitHub at <a href="https://github.com/nunombispo">https://github.com/nunombispo</a></p><p id="5fea">Or check my website at <a href="https://developer-service.io">https://developer-service.io</a></p><p id="254f">For low cost Nextcloud hosting, check out: <a href="https://cloudhomelab.com">https://cloudhomelab.com</a> (50% discount with promo code <b>50OFF</b>)</p><p id="e6c2">Until the next article…</p><p id="38dc">If you enjoyed reading this article and found it usefull, you can support me by <a href="https://blog.developer-service.io/membership">signing up for a Medium membership</a> (if you are not a member). It will only cost you $5 a month — this will give you access to all stories on Medium! (and I will receive a small commission)</p><p id="1391">Besides that, if you want to stay updated when I post a new story, you can <a href="https://blog.developer-service.io/subscribe">signup for my free newsletter</a>!</p></article></body>

Django and Stripe subscriptions — Part 3

Django + Stripe

In case you missed Part 2, you can find it here:

In the previous part of this tutorial series, we perform the following:

  • Stripe configuration, products and coupons
  • Views and html templates for checkout
  • Configured the static javascript files
  • Collected a test payment

On this Part 3 we will learn how to manage subscriptions and how to update our customer data in case something changes in Stripe.

1) Our settings page

If you remember from the previous articles, we had a settings page that only logged in members could access.

We will now expand that page so in case a user has a membership he can view the membership information and cancel if necessary.

Let’s start by changing our view to get the Stripe membership information:

In this view we have a check for a POST request or a GET request:

  • The POST request is used after the user pressed the cancel button on the page to cancel the membership. For that we get the Stripe subscription and set the flag to cancel at the period end.
  • GET request we are just getting the information.

The corresponding html template to show this view is as follows:

And, of course, we need to add our new url to the ‘urls.py’ file:

2) Get updates from Stripe

We now have the capability of a user cancelling their subscription from our website, but what if there is a problem with the Stripe subscription?

For instance a credit card might expire and the subscription will not get renew. In those cases we want to update that info from our side and also suspend the membership for that user.

There are several ways of achieving this, for this tutorial I will create an update page that the Django superuser can access to perform the updates.

Let’s create view for the update page:

The decorator ‘user_passes_test’ allows us to define an inner function to perform the check if the logged user is a Django superuser. If that is the case then he can access the page.

The view handler gets all of our Customers from the database, their corresponding Stripe subscriptions and then according to the status, activate or deactivate the membership on our site. At the end we just return a simple response, so we don’t need an html template to render this view (perfectly fine for a backoffice page)

3) Conclusion

On this last part of our tutorial, we performed the following:

  • Created a page where our user can cancel their membership
  • Retrieve updates from Stripe for the subscriptions status

This concludes our 3 part tutorial, hope that now with this basic knowledge you are prepared to expand and create your own subscription based Django website with Stripe.

You can check out my GitHub at https://github.com/nunombispo

Or check my website at https://developer-service.io

For low cost Nextcloud hosting, check out: https://cloudhomelab.com (50% discount with promo code 50OFF)

Until the next article…

If you enjoyed reading this article and found it usefull, you can support me by signing up for a Medium membership (if you are not a member). It will only cost you $5 a month — this will give you access to all stories on Medium! (and I will receive a small commission)

Besides that, if you want to stay updated when I post a new story, you can signup for my free newsletter!

Stripe
Django
Subscription
Python3
Programming
Recommended from ReadMedium