Rutger Cappendijk
Rutger Cappendijk

Reputation: 239

Integrating Mollie payments in NestJS backend

I am trying to integrate Mollie payments into my NestJS backend.

To make a connection with Mollie, I have to use their MollieClient function. However, when I try to use it in a service I get the error:

Nest can't resolve dependencies of the NewUserService (UserService, MailService, ConfigService, JwtService, ?). Please make sure that the argument Object at index [4] is available in the NewUserModule context.

I am pretty sure this means I have to add a Mollie module/service to the NewUserModule, but I think the package doesn't actually come with a module made for NestJS. So if I try to make a Mollie module/service or use the MollieClient in another service, it asks me to provide it whilst I don't have anything to provide.

I'm pretty new to NestJS and backend development in general, so am I mistaken? Or is there a module added in the installed package? If there isn't a module, should I make one? What exactly should be in such a module? Is there some sort of guide for it?

I realise this might be a rather vague series of questions, but I'm not very sure how to approach this.

Edit: Rewrote the question for clarification.

Thanks in advance!

Upvotes: 0

Views: 335

Answers (1)

hendra
hendra

Reputation: 2651

The message means, that Nest does not now how to resolve the 5th constructor argument of your NewUserService. I assume this is something like a MollieService?

You need to add MollieService as Provider to your NewUserModule:

@Module({
  imports: [...],
  controllers: [...],
  providers: [
    ...otherProviders,
    MollieService
  ]
})
export class NewUserModule {}

Or you can create a separate MollieModule and import it in NewUserModule:

@Module({
  providers: [ MollieService ],
  exports: [ MollieService ] // export MollieService, so that other modules can use it
})
export class MollieModule {}
@Module({
  imports: [MollieModule],
  controllers: [...],
  providers: [...] // no need to provide MollieService here, because it's imported from MollieModule
})
export class NewUserModule {}

Of course you must also implement the MollieService using their SDK.

A recommend to read the Documentation on Modules. They are a little hard to understand at a first sight, but a powerful concept!

EDIT: I would suggest to wrap the MollySDK in a service. This way you're not dealing with molly at various places in your app and prevent leaking it's api and types into your services.

@Injectable()
export class MollyService {
  private readonly mollyClient: WhateverType;

  constructor() {
    this.mollyClient = createMollieClient({ apiKey: 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM' });
  }

  createPayment() {
    this.mollieClient.payments.create({...});
  }
}

This service could be injected as usual.

However if you really want to use the molly-client directly, you have to use a custom provider

@Module({
  providers: [{
    provides: 'MOLLY_CLIENT',
    useFactory: () => createMollieClient({ apiKey: 'test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM' }) // you could also use useValue instead of useFactory
  }],
  exports: [ 'MOLLY_CLIENT' ]
})
export class MollieModule {}

Then inject it into NewUsersService like so:

constructor(@Inject('MOLLY_CLIENT')private readonly mollyClient: WhateverType) {}

Upvotes: 0

Related Questions