简体   繁体   中英

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

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

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

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

using the source 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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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