'How do I get reliable notifications via webhooks from klarna?

I'm implementing Klarna as a payment option for a custom online shop using the klarna-payments-api.

Creating a session and an order in Klarnas system is working fine (only tested in the playground environment), but after the order is created i do need to get updates or notifications when the order is captured via Klarnas web interface. Therefore I have tried to set merchant_urls.push and merchant_urls.notification in the create order call to an url of a webhook that should handle any necessary updates.

The create order call and the merchant_urls field is described in Klarnas documentation.

The problem i'm facing is that the webhook isn't always called as can be seen in the protocols in the Klarna web interface.

The code of the webhook currently only sets a a DATETIME field in the database to now() and the actual update of the the data is handled by a cronjob with a delay of five minutes, because requesting the data instantly when the webhook is called only returns the old values and not the updated ones.

This is my first time working with Klarna, so if I'm doing something stupid or just missed some part in the api documentation please give me a pointer.

Is there a better way to get reliable updates from Klarna?

I could revert to polling, but I would prefer an approche using webhooks.

webhook code:

const app = express();
app.post('/webhook/klarna/:order_id',async function (req,res){
    try{
        const {order_id} = req.params;
        
        const q = `SELECT COUNT(*) AS cnt FROM payments.klarna_orders WHERE order_id = ?`;
        const [[{cnt}]] = await pool.execute(q,[order_id]);
        
        if(parseInt(cnt) === 0){
            res.sendStatus(404);
            return;
        }
        
        const update_q = `UPDATE payments.klarna_orders
                          SET last_notification = now()
                          WHERE order_id = ?`
        await pool.execute(update_q,[order_id]);
        
        await klarna_tools.post(`/ordermanagement/v1/orders/${order_id}/acknowledge`);
        
        res.sendStatus(200);
    }catch(e){
        res.sendStatus(500);
    }
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source