receive-Web-hook method is not working in nodeJS

Hi guys,

I’m a new Shopify developer and I wrote the below code and it’s working fine except web hooks verification middleware (

const webhook = receiveWebhook({ secret: config.shopifyAPI.secretKey });

)

I tried a lot of solutions but no result at all (web-hook middleware doesn’t pass)

require('isomorphic-fetch');
const Koa = require('koa');
const router = require('@koa/router')();
const { verifyRequest } = require('@shopify/koa-shopify-auth');
const { receiveWebhook } = require('@shopify/koa-shopify-webhooks');
const session = require('koa-session');
const bodyParser = require('koa-bodyparser');
const koaBody = require('koa-body');
const User = require('./routers/user.router');
const Store = require('./routers/store.router');

const config = require('./config');
const { createShopifyAuthentication } = require('./services/shopify.service');

/** DB section */
const mongoose = require('mongoose');
setTimeout(() => {
    mongoose.connect(config.databaseURL, { useNewUrlParser: true, useUnifiedTopology: true })
        .then(() => console.log('DB is connected'))
        .catch(err => console.log(err))
}, 500);

const app = new Koa();

/** APIs section */
router.post('/shopify/confirm-pricing', User.confirmPricing);
router.post('/shopify/setup-app', User.setupApp);
router.put('/shopify/user-update', User.userUpdate);
router.get('/shopify/user-plan', User.userPlan);
router.get('/shopify/get-shopify-data', User.userSiteId);
router.get('/shopify/add-to-cart-text', Store.getAddToCartButtonText);
router.get('/shopify/after-confirm-subscription', Store.afterConfirmSubscription);

/** Webhooks */
const webhook = receiveWebhook({ secret: config.shopifyAPI.secretKey });
router.post('/shopify/webhooks/fired', webhook, Store.webhookFired);
router.get('/shopify/webhooks/fired', webhook, Store.webhookFired);
router.get('/shopify/webhooks/update-subscription', webhook, Store.webhookFired);

/** Middlewares section */
app.use(bodyParser());
app.use(session({ secure: true, sameSite: 'none' }, app));
// app.use(async (ctx, next) => {
//     console.log('ctx', JSON.stringify(ctx));
//     !ctx.request.url.includes('/webhooks') ? await bodyParser()(ctx, next) : await koaBody()(ctx, next)
//     // await next();
// });

app.keys = [config.shopifyAPI.secretKey];
app.use(createShopifyAuthentication());

router.get("(.*)", verifyRequest({ authRoute: '/shopify/auth' }), async (ctx) => {
    const { status } = ctx.response;
    /** if app is already installed will send us these two extra parameters because verifyRequest will send status code 404 */
    if (status === 404) {
        console.log('Redirected');
        ctx.redirect(`${config.funnelllURL}/home`);
    }
});

app.use(router.allowedMethods());
app.use(router.routes());

app.listen(config.port, () => {
    console.log(`> Ready on port: ${config.port}`);
});

And also this web-hook (or any web-hook) is not received by my backend at all

Could you please help,
Thanks in advance

Try removing KOA-bodyparser. Our webhooks were failing in the same manner and removing the body parser fixed it (but broke all our API endpoints). We are looking for an alternative now. If you have found one let us know!