简体   繁体   English

条纹python将现有卡/源附加到新客户

[英]Stripe python attach existing card/source to new customer

I have the following situation: 我有以下情况:

  • Users can book rooms. 用户可以预订房间。

  • Its possible that a guest books a room, without making an account. 访客可能没有预订就预订房间。 In this case I create a stripe charge without a customer 在这种情况下,我会在没有客户的情况下创建条纹收费

  • Its possible that a user, while he is in the booking process, decides to create an account. 用户在预订过程中可能决定创建一个帐户。 In this case I also create a stripe customer. 在这种情况下,我还创建了一个条纹客户。

What I need 我需要的

If a user decides to create an account: 如果用户决定创建一个帐户:

I want to take the credit card, which was entered by the user and used to create the charge and attach the credit card to the customer, so the user can see his credit card in his profile and select/use it for future bookings. 我要使用用户输入的信用卡,该信用卡用于创建费用并将信用卡附加到客户,因此用户可以在其个人资料中查看其信用卡,然后选择/使用该信用卡进行未来的预订。

Problem: 问题:

The stripe charge is created before the customer is created. 在创建客户之前创建条带状费用。 So I need to take the source and attach it to the customer. 因此,我需要拿货源并将其附加到客户身上。 (I already successfully can update the charge and add the customer.id, but the same process does not work for updating the customer.) (我已经可以成功更新费用并添加customer.id,但是相同的过程对更新客户无效。)

Updating the customer source gives me: 更新客户来源给我:

Cannot use stripe token more than once

What I tried so far: 到目前为止我尝试过的是:

getting the card_id from the charge 从收费中获取card_id

ch_ch = stripe.Charge.retrieve(new_booking.stripe_charge_id)

customer.sources.create(card=ch_ch.source.id)
customer.save()

using the source id 使用源ID

customer.sources.create(source=form.stripe_source_id.data)

different syntax 不同的语法

customer.source = form.stripe_source_id.data
customer.save()

Note: form.stripe_source_id.data contains a tok_1DEvMCGd8vfeewZVgrSRu4 , which is returned by stripe.js when creating a credit card element. 注意: form.stripe_source_id.data包含一个tok_1DEvMCGd8vfeewZVgrSRu4 ,它在创建信用卡元素时由stripe.js返回。 This is used to create the charge, which works perfect: 这用于创建费用,可以完美地发挥作用:

stripe_charge = stripe.Charge.create(
    amount=int(float(data_received['total_price']) * 100),
    currency="eur",
    source=form.stripe_source_id.data,
    description=None,
    #customer=user_id, # customer is anonymous
    capture=False, # if False the charge needs to be captured, otherwise it will expire in 7 days and is refunded
    #receipt_email='email für den typer, der die rechnung kriegt, funktioniert nur im livemode',
    metadata={
        'infos': 'process stripe payment anonymous charge'
    }
)

In the docs I red that if a customer source is attached/updated that a new source is created, but that means that its impossible to attach an EXISTING source to a customer? 我在文档中指出,如果附加/更新了客户资源,则将创建一个新资源,但这意味着不可能将现有资源附加到客户身上吗? That cant be true. 那不可能是真的。

A Token's or a Source's ID, as returned by Elements. 由Elements返回的令牌或源的ID。 Passing source will create a new source object, make it the new customer default source, and delete the old customer default if one exists. 传递源将创建一个新的源对象,使其成为新的客户默认源,如果存在则删除旧的客户默认值。

There is no way to add a card to a customer once the card token has already been used for a charge. 一旦卡令牌已用于收费,就无法将卡添加到客户。 You can either add the source to a customer and use that to make the charge, or you can ask the customer to re-enter their payment information when they make an account. 您可以将货源添加到客户并使用它来收取费用,也可以要求客户在开设帐户时重新输入其付款信息。

One of the reasons this is the case is to protect customer card data. 这种情况的原因之一是保护客户卡数据。 The restricted flow makes it easier for intermediate Stripe-powered platforms (such as Shopify) to prevent abuse by merchants saving card details against the customer's will. 受限制的流量使中间的基于Stripe的平台(例如Shopify)更容易防止商家滥用,从而违反了客户的意愿保存卡的详细信息。

If you are insistent on having the flow you described, here is a possible workaround: When someone enters card information, make a dummy customer and charge that. 如果您坚持要遵循所描述的流程,则可以采用以下解决方法:当有人输入卡信息时,请假顾客并向其收费。 Then if they make an account, associate their information with the customer. 然后,如果他们注册了帐户,则将其信息与客户相关联。 However, it would be extra work to manage and delete these extraneous dummy customers. 但是,管理和删除这些无关的虚拟客户将是额外的工作。

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

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