This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Update user credit card details using Stripe + Django


So you are making an internet app/web site that accepts funds utilizing the highly effective Django internet framework and the world’s main cost’s processor stripe, nevertheless there comes a time when your person leads to one of many following conditions :

  • Card expired
  • Change who’s paying
  • Acquired a number of playing cards and wanna change?

Both approach the power to alter your cost particulars inside a web site/app is a necessity and on this tutorial you may study precisely the right way to do it.



The entrance finish


index.html

<kind methodology="put up" id="payment-form">
            {% csrf_token %}
            <div class="col-md-4 form-row">
                <label for="card-element">Credit score or Debit Card</label>

                <div id="card-element">

                </div>

                <div id="alert alert-warning card-errors" function="alert"></div>
                <br>
                <br>
                <button class="btn btn-outline-dark" kind="submit" class="btn pay-btn col-sm-2">Replace</button>
            </div>
        </kind>
Enter fullscreen mode

Exit fullscreen mode


type.css

.StripeElement {
            box-sizing: border-box;

            peak: 40px;

            padding: 10px 12px;

            border: 1px stable clear;
            border-radius: 4px;
            background-color: white;

            box-shadow: 0 1px 3px 0 #e6ebf1;
            -webkit-transition: box-shadow 150ms ease;
            transition: box-shadow 150ms ease;
            }

            .StripeElement--focus {
            box-shadow: 0 1px 3px 0 #cfd7df;
            }

            .StripeElement--invalid {
            border-color: #fa755a;
            }

            .StripeElement--webkit-autofill {
            background-color: #fefde5 !necessary;
            }
Enter fullscreen mode

Exit fullscreen mode


script.js

var stripe = Stripe("PUBLIC_STRIPE_TOKEN");
        // Create an occasion of Components.
        var parts = stripe.parts();

        // Customized styling might be handed to choices when creating an Ingredient.
        // (Word that this demo makes use of a wider set of kinds than the information under.)
        var type = {
        base: {
            shade: '#32325d',
            fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
            fontSmoothing: 'antialiased',
            fontSize: '16px',
            '::placeholder': {
            shade: '#aab7c4'
            }
        },
        invalid: {
            shade: '#fa755a',
            iconColor: '#fa755a'
        }
        };

        // Create an occasion of the cardboard Ingredient.
        var card = parts.create('card', {type: type});

        // Add an occasion of the cardboard Ingredient into the `card-element` <div>.
        card.mount('#card-element');

        // Deal with real-time validation errors from the cardboard Ingredient.
        card.addEventListener('change', perform(occasion) {
        var displayError = doc.getElementById('card-errors');
        if (occasion.error) {
            displayError.textContent = occasion.error.message;
        } else {
            displayError.textContent = '';
        }
        });

        // Deal with kind submission.
        var kind = doc.getElementById('payment-form');
        kind.addEventListener('submit', perform(occasion) {
        occasion.preventDefault();

        stripe.createToken(card).then(perform(end result) {
            if (end result.error) {
            // Inform the person if there was an error.
            var errorElement = doc.getElementById('card-errors');
            errorElement.textContent = end result.error.message;
            } else {
            // Ship the token to your server.
            stripeTokenHandler(end result.token);
            }
        });
        });

        // Submit the shape with the token ID.
        perform stripeTokenHandler(token) {
        // Insert the token ID into the shape so it will get submitted to the server
        var kind = doc.getElementById('payment-form');
        var hiddenInput = doc.createElement('enter');
        hiddenInput.setAttribute('kind', 'hidden');
        hiddenInput.setAttribute('identify', 'stripeToken');
        hiddenInput.setAttribute('worth', token.id);
        kind.appendChild(hiddenInput);

        // Submit the shape
        kind.submit();
        }
Enter fullscreen mode

Exit fullscreen mode



Lastly…The backend


So the logic right here is that we wish to seize the stripe token that’s despatched again to us after the person submits his/her card data so now the ball is within the server facet’s court docket so we have to seize that token after which utilizing that create the supply for the stripe buyer and at last extract the cardboard ID from the card_data object and at last go that card ID to the modify_source() methodology as proven under and voila! You’re accomplished!

views.py

from django.shortcuts import render 
import stripe 

stripe.api_key = 'YOUR_STRIPE_SECRET_KEY'

card_data = stripe.Buyer.create_source(
      user_setting.stripe_customer_id,
      supply=request.POST['stripeToken']
)

stripe.Buyer.modify_source(
      user_setting.stripe_customer_id,
      card_data['id']
)

return redirect('some-view-name')
Enter fullscreen mode

Exit fullscreen mode

Simply add the next code to the Django view you might be eager to set because the cost particulars replace web page.

You probably have any questions go forward and remark down under 🙂

Hope you discovered this tutorial useful!

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?