如何使用stripe.js和python更新信用卡
我知道怎么用Python和Stripe更新信用卡信息。但是,建议不要让敏感信息经过我们的服务器。应该使用stripe.js来提交这些信息。我只见过用stripe.js创建信用卡的例子。那我该怎么用Python和stripe.js来更新信用卡呢?
请帮帮我。提前谢谢!
3 个回答
我觉得Stripe.js似乎不支持我们想要做的事情,不过Stripe的API好像可以。
不过,我写了一些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);
});
注意:这些字段(比如data-stripe="address_city",data-stripe="address_country"等)现在会被发送到你的表单处理地址,而如果只用Stripe.js的话,只有卡片的令牌会被发送。到这一步,你需要通过Stripe API来更新卡片信息,这可以不需要“新的信用卡令牌”,只需引用卡片ID即可。
你可以查看https://stripe.com/docs/api#update_card获取相关文档。
我刚刚在我的测试中成功运行了这个,得到了一个新的事件:
jdoe@example.com 更新了一张以4242结尾的Visa卡。 2015/12/15 13:47:43
假设你想要替换一个已经存在的Stripe客户的默认信用卡,而你已经知道了这个客户的ID。
你需要通过stripe.js获取一个卡片令牌,就像在创建新卡片时那样。
这个卡片令牌会提交到你的Python应用程序中,然后程序会找到对应的Stripe客户,并把新的卡片令牌绑定上去。
# get the stripe customer
cu = stripe.Customer.retrieve({CUSTOMER_ID})
# change the card property
cu.card = {CARD_TOKEN}
# save the customer
cu.save()
想了解更多信息,可以查看Stripe的文档:https://stripe.com/docs/api#update_customer
这个过程跟创建客户或者给客户的卡收费很像。你可以使用同一个Stripe按钮来提交到另一个文件。你可以添加一个隐藏的输入框,里面放入你想要替换的卡的客户ID。
<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>
在这个PYTHON文件页面上,你需要连接到Stripe,获取你的客户信息,然后使用Stripe提供的令牌添加一张新卡(Stripe称之为“sources”)。接着,你会得到新创建的卡的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
所有的操作都有详细的说明,可以在Stripe的文档中找到: https://stripe.com/docs/api#retrieve_customer & https://stripe.com/docs/api#create_card