Home > Blockchain >  Cannot Inject Stripe Client in NestJS (https://www.npmjs.com/package/@golevelup/nestjs-stripe)
Cannot Inject Stripe Client in NestJS (https://www.npmjs.com/package/@golevelup/nestjs-stripe)

Time:12-17

I'm trying to use the GoLevelUp stripe package to integrate stripe with my NestJs project. I'm able to import the package in my global app module, but I am unable to actually inject a working client into the designated controller.

(https://github.com/golevelup/nestjs/tree/master/packages/stripe) (https://www.npmjs.com/package/@golevelup/nestjs-stripe) (https://www.npmjs.com/package/@nestjs/core)

redacted.controller.ts:

import { InjectStripeClient } from '@golevelup/nestjs-stripe';
import Stripe from 'stripe';

@Controller('[REDACTED]')
export class MyAwesomeController {
  constructor(
    @InjectStripeClient() stripeClient: Stripe, //this line crashes
    private readonly myService: MyCoolService
  ) { }
}

app.module.ts:

import { StripeModule } from '@golevelup/nestjs-stripe';
import { OtherModules } from 'where/ever';

@Module({
  imports: [
    ModuleA,
    ModuleB,
    ...,

    StripeModule.forRoot(StripeModule,
      {
        apiKey: 'PC_LOAD_KEY',
        webhookConfig: {
          // stripeWebhookSecret: 'PC_LOAD_SECRET'

          //TODO -- same deal w/ config
          stripeWebhookSecret: 'SHHHH',
          
        },
        
      }),


    MoreModulues,
    ...,
  ],
  controllers: [AppController, ControllerB, ControllerC, ...],
  providers: [AppService, ServiceB, ServiceC, ...]
}

The above code fails to build with the following error:

Nest can't resolve dependencies of the [REDACTED]Controller (?, [REDACTED]Service). Please make sure that the argument Symbol(STRIPE_CLIENT_TOKEN) at index [0] is available in the [REDACTED]Module context.

Potential solutions:
- If Symbol(STRIPE_CLIENT_TOKEN) is a provider, is it part of the current [REDACTED]Module?
- If Symbol(STRIPE_CLIENT_TOKEN) is exported from a separate @Module, is that module imported within [REDACTED]Module?
  @Module({
    imports: [ /* the Module containing Symbol(STRIPE_CLIENT_TOKEN) */ ]

I need to be able to actually instantiate a stripe client so I can make calls to the stripe api. Without that, the package is unusable. I'm a little out of my depth in understanding how to configure this so it doesn't crash.

I've tried hunting around in the golevelup package for the offending "STRIPE_CLIENT_TOKEN".

It lives here: https://github.com/golevelup/nestjs/tree/master/packages/stripe/src/stripe.constants.ts https://github.com/golevelup/nestjs/tree/master/packages/stripe/src/stripe.decorators.ts https://github.com/golevelup/nestjs/tree/master/packages/stripe/src/stripe.module.ts

I've also looked at official stripe package for clues on how to configure the missing tokens. At this point I'm in the weeds and could do with a pointer or working example. Otherwise, I will just implement it myself using Stripe™ primitives.

CodePudding user response:

It looks like with the way the StripeModule is written you need to use StripeModule.forRoot() in the [REDACTED]Module. The StripeModule isn't global, so it only exposes the stripe client (via its decorator) to the module that configured nd imported the module.

  • Related