How to manage webhooks from Shopify in Laravel

0

Good, I have an application that basically receives webhooks of events from a Shopify store, and stores them in the database, the problem is that many times the events arrive simultaneously at the application and not in the correct order, it is Say, when you make a purchase come the webhooks of:

order / create, order / update and order / paid at the same time, then what I do in the webhook of the create comes to be:

$customer = $this->createOrUpdateCustomerWithJson($json);
$this->createOrder($json, $customer);

And in the update:

$order = $this->getOrderByShopifyId($json['id']);
if($order) {
    $this->updateOrder($order, $json);
} else {
    $this->newOrder($json, $customer);
}

Basically the create saves the order and the update if there is an update and if it does not exist it creates it. The paid does the same, if it exists it is marked as paid and if not, it creates it.

The problem is that when the update webhook asks if the order exists, in the DB it is not yet, but then after it has consulted that, if the webhook of the create, inserts the record, when the update tries to create the record, crashing because there is already a record with that primary key.

At first I think of a way to solve it, which would be to convert the process into a serial process, storing the webhooks that are arriving in a queue, and then processing the queue, but this could slow down the application and would not be serious. a desired solution.

Any solution to process webhooks in parallel without them failing between them?

    
asked by Victor Company 30.01.2018 в 14:30
source

0 answers