#Vinz

1 messages · Page 1 of 1 (latest)

eager urchinBOT
silver summit
abstract pumice
#

I want to create a purchase in my db each month i receive a webhook

#

so if i have a session first time OK what about next months

silver summit
abstract pumice
#

yep but no invoice is created first time session ?

#

I don't want to create double purchases in my db first time

silver summit
#

For each purchase there'll be an invoice created, so you can refer just to invoice's line items

abstract pumice
#

well there is a misunderstanding

    if event_type == 'checkout.session.completed':
        I deal with line_items

During the process, a webhook is triggered with 'invoice.paid'

    if event_type == 'invoice.paid':
        I deal with line_items

So during the process I give the user twice as many purchase as he sould get

silver summit
#

So you remove the first deal with line_items of the event checkout.session.completed. You keep just invoice.paidin order to process the line_items

abstract pumice
#

that's what I have done first but in invoice paid, the user is not found : triggering is too quick for my local DB

#

further question : might be a question of event order

#

where can i find the order of the events triggered

silver summit
silver summit
abstract pumice
#

Well @silver summit when I pass that webhook view :

    elif event_type == 'invoice.paid':
        invoice = event['data']['object']
        customer_id = invoice['customer']
        try:
            company = Company.objects.get(stripe_customer_id=customer_id)
        except Company.DoesNotExist:
            return JsonResponse({'status': 'error'}, status=404)
        _create_purchases(invoice, company)

the try fails

#

but when all fire of the webhook pass the Company.objects.get(stripe_customer_id=customer_id) works

silver summit
#

Thanks for sharing these details. This is a concurrency issue, you need to save that event in a temporary stack and retry consuming it once the "Company" object is created. Or you implement a method "getOrCreate" on your "Company" objects and you replace the create with createOrSave.