简体   繁体   English

如何使用stripe.js和python更新信用卡

[英]How to update credit card using stripe.js and python

I know how to update credit card using python and stripe. 我知道如何使用python和stripe更新信用卡。 But, it is recommended that sensitive information can not go through our servers. 但是,建议敏感信息不能通过我们的服务器。 Use stripe.js for submitting it to stripe. 使用stripe.js将其提交到条带。 I have seen only examples of creating credit card using stripe.js. 我只看到了使用stripe.js创建信用卡的例子。 How can we update credit card using python and stripe.js 我们如何使用python和stripe.js更新信用卡

Please help. 请帮忙。 Thanks in advance. 提前致谢。

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. 您可以添加一个隐藏的输入,其中包含要替换的卡的customerID。

<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. 在此PYTHON FILE页面上,您将连接到条带,检索您的客户并使用发布的令牌条带添加新卡(或“源”是条带调用它)。 You would then get the newly created card's ID and update your customer's default card to this ID! 然后,您将获得新创建的卡的ID,并将您客户的默认卡更新为此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 所有文件都记录在条纹上: https//stripe.com/docs/api#retrieve_customer&https//stripe.com/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. 据推测,您希望替换现有条带客户的默认信用卡,该客户的ID是您所知道的。

You need to obtain a card token from stripe.js, just in the same way as when creating a new card. 您需要从stripe.js获取卡令牌,就像创建新卡时一样。

The card token is submitted to your python application which retrieves the stripe Customer and attaches the new card token. 卡令牌将提交给您的python应用程序,该应用程序检索条带Customer并附加新的卡令牌。

# 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 有关详细信息,请参阅Stripe文档: https//stripe.com/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 . 我不认为Stripe.js 支持我们要找这个做的,虽然看起来条纹API

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). 也就是说,这里是我编写的一些jQuery,可以很好地使用现有的Stripe.js表单,读取这些Stripe数据属性的值,并在提交时附加表单(这实际上是我现在看到的唯一解决方法)。

/*
 * 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 (ie 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. 注意:这些字段(即data-stripe =“address_city”,data-stripe =“address_country”等)现在将张贴到您的表单操作,而当仅使用Stripe.js时,只传递卡令牌。 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. 此时,您可以通过Stripe API更新卡详细信息,只需参考卡ID即可在没有 “新信用卡令牌”的情况下完成。

See https://stripe.com/docs/api#update_card for documentation there. 有关文档,请参阅https://stripe.com/docs/api#update_card

I just tested it successfully with mine and got a new event: 我刚刚与我的测试成功并获得了一个新的事件:

jdoe@example.com updated a Visa ending in 4242. 2015/12/15 13:47:43

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM