I am working to connect Shopify webhook to nestjs. Although I made the app and made the connection to Shopify and installed the app.
Meanwhile, i have created the endpoint in my nestjs application, but it is not reflecting anything from Shopify
here are the files below
shopify-orders.service.ts
import "@babel/polyfill";
import { Injectable, BadRequestException, ServiceUnavailableException } from '@nestjs/common';
import "isomorphic-fetch";
import dotenv from "dotenv";
import Koa from 'koa';
const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth');
import { default as Shopify, ApiVersion } from '@shopify/shopify-api';
const { verifyRequest} = require('@shopify/koa-shopify-auth');
// const { default: Shopify, ApiVersion } = require('@shopify/shopify-api');
const Router = require('koa-router');
import {receiveWebhook, registerWebhook} from '@shopify/koa-shopify-webhooks';
const session = require('koa-session');
const app = new Koa();
const router = new Router();
@Injectable()
export class ShopifyOrdersService {
constructor() {}
public ShopifyApiCalls = () => {
const app = new Koa();
const router = new Router();
const constport = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const { SHOPIFY_API_SECRET, SHOPIFY_API_KEY, SCOPES } = process.env;
app.use(
createShopifyAuth({
apiKey: SHOPIFY_API_KEY,
secret: SHOPIFY_API_SECRET,
scopes : [SCOPES],
async afterAuth(ctx) {
const { shop, accessToken} = ctx.state.shopify;
const handleWebhookRequest = async (topic: string, shop: string, webhookRequestBody: string) => {
// this handler is triggered when a webhook is sent by the Shopify platform to your application
}
const registration = await Shopify.Webhooks.Registry.register({
shop,
accessToken,
path: '/webhooks/orders',
topic: 'ORDERS_CREATE',
webhookHandler: handleWebhookRequest,
})
if (registration.success) {
console.log('Successfully registered webhook!');
} else {
console.log('Failed to register webhook', registration.result);
}
ctx.redirect(`/?shop=${shop}`);
}
})
)
app.use(
receiveWebhook({
path:'/webhooks/orders',
secret: SHOPIFY_API_SECRET,
onReceived(ctx) {
console.log('received webhook: ',ctx.state.webhook);
},
}),
);
app.use(verifyRequest());
app.use(ctx => {
});
}
}
shopify-orders.controller.ts
import "@babel/polyfill";
import { BadRequestException,
Body,
Controller,
Get,
Headers,
HttpCode,
HttpStatus,
Post,
Query, } from '@nestjs/common';
import { ShopifyOrdersService } from './shopify-orders.service';
@Controller('webhooks')
export class ShopifyOrdersController {
public constructor(
private readonly shopifyOrdersService: ShopifyOrdersService
) {}
('orders')
getHello(): any {
return this.shopifyOrdersService.ShopifyApiCalls();
}
}
shopify-orders.module.ts
import { Module } from '@nestjs/common';
import { ShopifyOrdersController } from './shopify-orders.controller';
import { ShopifyOrdersService } from './shopify-orders.service';
import { ApiCallsModule } from '../api-calls/api-calls.module'
@Module({
imports: [
ApiCallsModule
],
controllers: [ShopifyOrdersController],
providers: [ShopifyOrdersService]
})
export class ShopifyOrdersModule {}
i dont know what i am missing, thanks