Hey!
A month ago I developed a node and react Shopify app following the “Build a Shopify App with Node and React” tutorial. Since then they seem to have updated/changed the way to register and process webhooks.
The first time I had it the code was something like this:
/* Register webhook for cart update */
const registrationCartUpdate = await registerWebhook({
address: `${HOST}/webhooks/carts/update`,
topic: 'CARTS_UPDATE',
accessToken,
shop,
apiVersion: ApiVersion.October20
});
if (registrationCartUpdate.success) {
console.log('Successfully registered cart update webhook!');
} else {
console.log('Failed to register cart update webhook', registrationOrderPaid.result);
}
const webhook = receiveWebhook({secret: SHOPIFY_API_SECRET});
/*Receive webhook carts update*/
router.post('//webhooks/carts/update', webhook, (ctx) => {
console.log('received cart update webhook: ');
//Print all cart items
console.log(ctx.state.webhook.payload.line_items);
});
To something like this:
/* Register webhook for cart update*/
const registrationCartUpdate = await Shopify.Webhooks.Registry.register({
shop,
accessToken,
path: '/webhooks',
topic: 'CARTS_UPDATE',
webhookHandler: async (_topic, shop, body) => {
console.log('received cart update webhook: ');
//Print body
console.log(body);
},
});
if (registrationCartUpdate.success) {
console.log('Successfully registered cart update webhook!');
} else {
console.log('Failed to register cart update webhook', registrationOrderPaid.result);
}
router.post("/webhooks", async (ctx) => {
try {
//Process webhook
await Shopify.Webhooks.Registry.process(ctx.req, ctx.res);
console.log(`Webhook processed, returned status code 200`);
} catch (error) {
console.log(`Failed to process webhook: ${error}`);
}
});
My problem is that the body that I receive in the new way of doing it is a string (see console.log):
┃ received cart update webhook:
┃ {"id":"b28c0ef9ff6ce6b4b1261ab7f6f4ed76","token":"b28c0ef9ff6ce6b4b1261ab7f6f4ed76","line_items":[{"id":39266914205850,"properties":null,"quantity":1,"variant_id":39266914205850,"key":"39266914205850:2dae68072231626227fe28e192723de1","discounted_price":"25.00","discounts":[],"gift_card":false,"grams":0,"line_price":"25.00","original_line_price":"25.00","original_price":"25.00","price":"25.00","product_id":6541117784218,"sku":"","taxable":true,"title":"T-shirt","total_discount":"0.00","vendor":"A","discounted_price_set":{"shop_money":{"amount":"25.0","currency_code":"SEK"},"presentment_money":{"amount":"25.0","currency_code":"SEK"}},"line_price_set":{"shop_money":{"amount":"25.0","currency_code":"SEK"},"presentment_money":{"amount":"25.0","currency_code":"SEK"}},"original_line_price_set":{"shop_money":{"amount":"25.0","currency_code":"SEK"},"presentment_money":{"amount":"25.0","currency_code":"SEK"}},"price_set":{"shop_money":{"amount":"25.0","currency_code":"SEK"},"presentment_money":{"amount":"25.0","currency_code":"SEK"}},"total_discount_set":{"shop_money":{"amount":"0.0","currency_code":"SEK"},"presentment_money":{"amount":"0.0","currency_code":"SEK"}}},{"id":37335165829274,"properties":{},"quantity":1,"variant_id":37335165829274,"key":"37335165829274:677f0f916861c76dd6c1cf0be5719fd7","discounted_price":"0.00","discounts":[],"gift_card":false,"grams":0,"line_price":"0.00","original_line_price":"0.00","original_price":"0.00","price":"0.00","product_id":6010213040282,"sku":"","taxable":true,"title":"Movesgood T-shirt","total_discount":"0.00","vendor":"Test Store","discounted_price_set":{"shop_money":{"amount":"0.0","currency_code":"SEK"},"presentment_money":{"amount":"0.0","currency_code":"SEK"}},"line_price_set":{"shop_money":{"amount":"0.0","currency_code":"SEK"},"presentment_money":{"amount":"0.0","currency_code":"SEK"}},"original_line_price_set":{"shop_money":{"amount":"0.0","currency_code":"SEK"},"presentment_money":{"amount":"0.0","currency_code":"SEK"}},"price_set":{"shop_money":{"amount":"0.0","currency_code":"SEK"},"presentment_money":{"amount":"0.0","currency_code":"SEK"}},"total_discount_set":{"shop_money":{"amount":"0.0","currency_code":"SEK"},"presentment_money":{"amount":"0.0","currency_code":"SEK"}}}],"note":null,"updated_at":"2021-03-14T11:40:41.373Z","created_at":"2021-03-08T05:27:18.177Z"}
┃ Webhook processed, returned status code 200
So I can’t do body.line_items as then I get an undefined error.
The ctx variable seems to have changed also, as ctx.state.webhook.payload.line_items for example is also undefined.
How can I read the items in the cart now? (same problem applies when I try to do order/paid!)