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.