Skip to content

Commit

Permalink
Updates to get the test running
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym-stripe committed Sep 11, 2023
1 parent 3d07dff commit e28766a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 49 deletions.
36 changes: 17 additions & 19 deletions examples/webhook-signing/nestjs/app.controller.ts
Expand Up @@ -8,28 +8,26 @@ import {
RawBodyRequest,
Req,
Res,
} from '@nestjs/common'
import { Request,Response } from 'express'
import Stripe from 'stripe'
Injectable,
Inject,
} from '@nestjs/common';
import {Request, Response} from 'express';
import Stripe from 'stripe';
import {ConfigService} from '@nestjs/config';

@Controller()
export class AppController {
private readonly client: Stripe;
constructor(
private config: ConfigService
) {
this.client = new Stripe(
config.get('Stripe.secret_key'),
{
apiVersion: '2022-11-15',
typescript: true,
}
);
constructor(@Inject(ConfigService) private readonly config: ConfigService) {
this.client = new Stripe(this.config.get('Stripe.secret_key'), {
apiVersion: '2022-11-15',
typescript: true,
});
}

@Get('/')
async index(): Promise<string> {
return 'ok'
return 'ok';
}

@Post('/webhooks')
Expand All @@ -38,14 +36,14 @@ export class AppController {
@Req() req: RawBodyRequest<Request>,
@Res() res: Response
) {

let event: Stripe.Event;

try {
event = this.client.webhooks.constructEvent(
req.rawBody,
sig,
this.config.get('Stripe.webhook_secret'));
req.rawBody,
sig,
this.config.get('Stripe.webhook_secret')
);
} catch (err) {
// On error, log and return the error message
console.log(`❌ Error message: ${err.message}`);
Expand All @@ -69,6 +67,6 @@ export class AppController {
}

// Return a response to acknowledge receipt of the event
res.json({received: true});
res.status(200).json({received: true});
}
}
17 changes: 6 additions & 11 deletions examples/webhook-signing/nestjs/app.module.ts
@@ -1,21 +1,16 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from '@nestjs/config'
import { Config } from "./config";
import { AppController } from "./app.controller";

export const configuration = async (): Promise<Config> => {
const { config } = <{ config: Config }> await import(`${__dirname}/config`);
return config;
};
import {Module} from '@nestjs/common';
import {ConfigModule} from '@nestjs/config';
import {config} from './config';
import {AppController} from './app.controller';

@Module({
controllers: [AppController],
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [configuration],
load: [config],
envFilePath: `.env`,
})
}),
],
})
export class AppModule {}
21 changes: 9 additions & 12 deletions examples/webhook-signing/nestjs/config.ts
@@ -1,16 +1,13 @@
export const config: {
type Config = {
Stripe: {
publishable_key: string
secret_key: string
webhook_secret: string
}
} = {
secret_key: string;
webhook_secret: string;
};
};

export const config = (): Config => ({
Stripe: {
publishable_key: process.env.STRIPE_PUBLISHABLE_KEY || '',
secret_key: process.env.STRIPE_SECRET_KEY || '',
webhook_secret: process.env.STRIPE_WEBHOOK_SECRET || '',
}
}
export type Config = typeof config;


},
});
14 changes: 8 additions & 6 deletions examples/webhook-signing/nestjs/main.ts 100644 → 100755
@@ -1,16 +1,18 @@
#!/usr/bin/env -S npm run-script run

import { NestFactory } from "@nestjs/core";
import { INestApplication } from "@nestjs/common";
import { AppModule } from "./app.module";
import {NestFactory} from '@nestjs/core';
import {INestApplication} from '@nestjs/common';
import {AppModule} from './app.module';

async function bootstrap() {
const app = await NestFactory.create<INestApplication>(AppModule, { rawBody: true });
const app = await NestFactory.create<INestApplication>(AppModule, {
rawBody: true,
});
app.enableCors({
origin: "*",
origin: '*',
});

await app.listen(0);
console.log(`Webhook endpoint available at ${await app.getUrl()}/webhook`);
console.log(`Webhook endpoint available at ${await app.getUrl()}/webhooks`);
}
bootstrap();
4 changes: 3 additions & 1 deletion examples/webhook-signing/tsconfig.json
Expand Up @@ -10,6 +10,8 @@

/* Advanced Options */
/* Disallow inconsistently-cased references to the same file. */
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,

"experimentalDecorators": true,
}
}
2 changes: 2 additions & 0 deletions test/Integration.spec.ts
Expand Up @@ -107,4 +107,6 @@ describe('Integration test', function() {
});

it('Webhook sample deno', () => runWebhookTest('deno'));

it('Webhook sample nestjs', () => runWebhookTest('nestjs'));
});

0 comments on commit e28766a

Please sign in to comment.