Tevin Joseph K O
Tevin Joseph K O

Reputation: 2654

How to update credit card using stripe.js and python

I know how to update credit card using python and stripe. But, it is recommended that sensitive information can not go through our servers. Use stripe.js for submitting it to stripe. I have seen only examples of creating credit card using stripe.js. How can we update credit card using python and stripe.js

Please help. Thanks in advance.

Upvotes: 2

Views: 7062

Answers (3)

Matt Borja
Matt Borja

Reputation: 1577

I don't think Stripe.js supports what we're looking to do with this, though it appears Stripe API does.

That said, here is some jQuery I cooked up that will play nicely with an existing Stripe.js form, reading the values of these Stripe data attributes and appending the form on submit (which is really the only workaround I see right now).

/*
 * Finds elements in form by data-KEY attributes where KEY in fields array
 */
var includeStripeData = function(form, fields)
{
    for (var i in fields)
    {
        // Skip if <input name="k" /> already exists (won't overwrite)
        var k = fields[i];
        if (form.find('input[name=' + k + ']').length)
        {
            console.log('Field exists: input[name=' + k + ']');
            continue;
        }

        // Skip if <input data-stripe-k="..." /> is not found
        var field = form.find('input[data-stripe=' + k + ']');
        if (!field)
        {
            console.log('Stripe field not found: input[data-stripe=' + k + ']');
            continue;
        }

        // Append to form
        form.append($('<input type="hidden" />').attr('name', k).val(field.val()));
    }
}

$('.update-card').submit(function() {
    var stripeFields = [
        'name',
        'address_line1',
        'address_line2',
        'address_city',
        'address_state',
        'address_zip',
        'address_country'
    ];

    includeStripeData($(this), stripeFields);
});

Note: These fields (i.e. data-stripe="address_city", data-stripe="address_country", etc) will now be POSTed to your form action, whereas when using Stripe.js exclusively, only the card token is passed. At this point, it is up to you to update card details via Stripe API which can be done without a "new credit card token" by simply referencing the card ID.

See https://stripe.com/docs/api#update_card for documentation there.

I just tested it successfully with mine and got a new event:

[email protected] updated a Visa ending in 4242. 2015/12/15 13:47:43

Upvotes: 0

Alexander Poon
Alexander Poon

Reputation: 81

This would be done similar to creating a customer or charging a client card. You can use the same stripe button to post to another file. You can add a hidden input which contains the customerID of the card you want to replace.

<form action='PYTHON FILE' method='POST'>
    <input type='hidden' name='cuid' value='<?php echo $cuid?>'/>
    <script
        src='https://checkout.stripe.com/checkout.js' class='stripe-button'
        data-key='YOUR TOKEN'
        data-panel-label='Change Card'
        data-label='Change Card'
        data-name='Change Card'
        data-description='Change your card'
        data-billing-address='false'>
    </script>
</form>

On this PYTHON FILE page, you would then connect to stripe, retrieve your customer and add a new card (or "sources" is what stripe calls it) using the token stripe posted. You would then get the newly created card's ID and update your customer's default card to this ID!

import stripe
stripe.api_key ="YOUR KEY" #ENTER YOUR KEY HERE
token = request.POST['stripeToken']
try:
    cuid = request.POST['cuid'];

    #GET CUSTOMER ON FILE
    customer = stripe.Customer.retrieve(cuid)

    #CREATE NEW CARD THAT WAS JUST INPUTTED USING THE TOKEN
    card = customer.sources.create(source=token)

    #GET NEW CARD ID
    newcardID = card.id

    #SET CUSTOMER'S NEW CARD ID TO TO DEFAULT
    customer.default_source = newcardID

    #SAVE NEW CARD
    customer.save()

except stripe.error.CardError, e:
# The card has been declined
pass

All documented on stripe here: https://stripe.com/docs/api#retrieve_customer & https://stripe.com/docs/api#create_card

Upvotes: 8

brew
brew

Reputation: 492

Presumably, you want to replace the default credit card for an existing stripe customer who's ID is known to you.

You need to obtain a card token from stripe.js, just in the same way as when creating a new card.

The card token is submitted to your python application which retrieves the stripe Customer and attaches the new card token.

# get the stripe customer
cu = stripe.Customer.retrieve({CUSTOMER_ID})
# change the card property
cu.card = {CARD_TOKEN}
# save the customer
cu.save()

See the Stripe documentation for more information: https://stripe.com/docs/api#update_customer

Upvotes: 0

Related Questions