Home > Blockchain >  Everything is imported but Nest can't resolve dependencies of the stripePaymentService
Everything is imported but Nest can't resolve dependencies of the stripePaymentService

Time:02-03

I am integrating Stripe payment gateway through NestJS. I am getting the error which says "Nest can't resolve dependencies of the stripePaymentService". Although I am quite familiar with this error but somehow things are not working for me. I believe everything is imported where it should be.

This is complete error:

Error: Nest can't resolve dependencies of the stripePaymentService (?). Please make sure that the argument paymentIntentRepository at index [0] is available in the AppModule context.

Potential solutions:
- Is AppModule a valid NestJS module?
- If paymentIntentRepository is a provider, is it part of the current AppModule?
- If paymentIntentRepository is exported from a separate @Module, is that module imported within AppModule?

My stripePaymentModule is

/* eslint-disable prettier/prettier */
import { Module } from '@nestjs/common';
import { stripePaymentController } from './stripe-payment.controller';
import { stripePaymentService } from './stripe-payment.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { paymentIntent } from './entities/paymentIntent.entity';
import { ConfigModule } from '@nestjs/config';
@Module({
    imports:[
      ConfigModule.forRoot({
        isGlobal: true,
      }),
      TypeOrmModule.forFeature([paymentIntent])],
  controllers: [stripePaymentController],
  providers: [stripePaymentService],
  exports: [stripePaymentService],
})
export class stripePaymentModule {}

stripePaymentServices is

/* eslint-disable prettier/prettier */
import { Injectable, Logger } from '@nestjs/common';
import { InjectStripe } from 'nestjs-stripe';
import { ConfigService } from '@nestjs/config';
import Stripe from 'stripe';
import { paymentIntent } from './entities/paymentIntent.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
@Injectable()
export class stripePaymentService {
    stripe: Stripe;
  logger = new Logger(stripePaymentService.name);
    constructor( 
        @InjectRepository(paymentIntent)
        private readonly paymentIntent:Repository<paymentIntent>,
        
        )
         {const stripeApiKey = process.env.STRIPE_SECRET_KEY;
          this.stripe = new Stripe(stripeApiKey, {
            apiVersion: '2022-11-15',
          });}

        

    async payment_intent(data: any, user) {
        
        const intent = await this.stripe.paymentIntents.create({
            amount: data.amount,
            currency: data.currency,
            description: data.description,
            payment_method_types: ['card'],
        });
        return await this.paymentIntent.save({'userId':user.userId, 'intent_response':JSON.stringify(intent)})
        // return intent;
      }


}

And finally this is app.module

/* eslint-disable prettier/prettier */
import { Module } from '@nestjs/common';
import { UsersModule } from './users/users.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { User } from './users/entities/user.entity';
import { PassportModule } from '@nestjs/passport';
import { MailModule } from './mail/mail.module';
import { FlightBookingModule } from './flight-booking/flight-booking.module';
import { TravelBookings } from './flight-booking/entities/bookingToken.entity';
import { stripePaymentService } from './stripe/stripe-payment.service';
import { stripePaymentController } from './stripe/stripe-payment.controller';
import { StripeModule } from 'nestjs-stripe';
import { stripePaymentModule } from './stripe/stripe-payment.module';
import { paymentIntent } from './stripe/entities/paymentIntent.entity';


@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    StripeModule.forRoot({
      apiKey: process.env.STRIPE_API_KEY,
      apiVersion: '2022-11-15'
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('DB_HOST'),
        port:  configService.get<number>('DB_PORT'),
        username: configService.get('DB_USERNAME'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_NAME'),
        entities: [User, TravelBookings, paymentIntent],
        synchronize: true,
      }),
      inject: [ConfigService],
    }),
    PassportModule,
    UsersModule,
    AuthModule,
    MailModule,
    FlightBookingModule,
    stripePaymentModule
  ],
  controllers: [stripePaymentController],
  providers: [stripePaymentService],
})
export class AppModule {}

CodePudding user response:

The error is thrown from AppModule context. You wouldn't need to put stripePaymentService as a provider in AppModule.

If stripePaymentService is used by other modules, once you have export it, you just need to import the related module in a module that you want, and that would be enough.

So all you need to do is remove these lines from AppModule:

providers: [stripePaymentService]

You can remove this line as well:

controllers: [stripePaymentController]
  • Related