For now, I make One-time Payments with Checkout, passing "checkout session id" to client's side and get data from webhook after payment success.
Like in Stripe's docs:
stripe_sess = stripe.checkout.Session.create(
api_key=stripe_key,
customer=customer.id,
payment_method_types=['card'],
line_items=[{
'name': order.oid,
'amount': int(final_cost['total'] * 100),
'currency': 'usd',
'quantity': 1
}],
success_url=link,
cancel_url=c_url,
)
I tried to create it by dj-stripe way:
stripe_sess = Session.objects.create(
client_reference_id=order.oid,
customer=customer,
payment_method_types=['card'],
display_items=[{
'name': order.oid,
'amount': int(final_cost['total'] * 100),
'currency': 'usd',
'quantity': 1
}],
success_url=link,
cancel_url=c_url,
)
but got error due to empty (not unique) id field (on second session - first session creates with empty id)
How can I create Session with non-empty (autofilled?) id?
Hi, you should almost never directly call djstripe object.create (or objects.get_or_create), because those won't trigger object creation in stripe.
A few classes do have their own create classmethods eg Plan.create - note that that's not Plan.objects.create, but Session doesn't have that yet.
Currently your best bet is your existing first example, and the using Session.sync_from_stripe_data(stripe_sess) to sync that to the database.
@Guest007 in your code..
stripe_sess = stripe.checkout.Session.create(
api_key=stripe_key,
customer=customer.id,
...
)
That customer.id.. did you create a DJ-stripe Customer object?
If so, why? Wouldn't be better to use request.user? Just in case the payment fails or gets cancelled.
@Guest007 in your code..
stripe_sess = stripe.checkout.Session.create( api_key=stripe_key, customer=customer.id, ... )That customer.id.. did you create a DJ-stripe Customer object?
Yes. I do customer, created = Customer.get_or_create(subscriber=user) before shown code
that's what i mean.. if the payment fails, your customer object would be invalid.
that's what i mean.. if the payment fails, your customer object would be invalid.
Why? If now Customer for this User - it will be created. Once. In next try we got existed Customer, associated with giver User.
See https://github.com/dj-stripe/dj-stripe/issues/1228; docs updates are coming to make this more clear.
Most helpful comment
Hi, you should almost never directly call djstripe object.create (or objects.get_or_create), because those won't trigger object creation in stripe.
A few classes do have their own create classmethods eg
Plan.create- note that that's notPlan.objects.create, but Session doesn't have that yet.Currently your best bet is your existing first example, and the using
Session.sync_from_stripe_data(stripe_sess)to sync that to the database.