最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to update credit card using stripe.js and python - Stack Overflow

programmeradmin0浏览0评论

I know how to update credit card using python and stripe. But, it is remended 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.

I know how to update credit card using python and stripe. But, it is remended 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.

Share asked Apr 10, 2014 at 5:13 Tevin Joseph K OTevin Joseph K O 2,65423 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

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./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./docs/api#retrieve_customer & https://stripe./docs/api#create_card

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./docs/api#update_customer

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./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

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>