Refactor PaymentDefaultProcessor to use MakePaymentToProcessorService for payment processing
This commit is contained in:
parent
507c774b23
commit
f0c95308b0
@ -1,46 +1,31 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { HttpService } from '@nestjs/axios';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { Payment } from '../entities/payment.entity';
|
import { Payment } from '../entities/payment.entity';
|
||||||
import { firstValueFrom } from 'rxjs';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
||||||
import { PaymentStatusEnum } from '../enumns/payment-status.enum';
|
import { PaymentStatusEnum } from '../enumns/payment-status.enum';
|
||||||
|
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PaymentDefaultProcessor {
|
export class PaymentDefaultProcessor {
|
||||||
private readonly logger = new Logger(PaymentDefaultProcessor.name);
|
private readonly logger = new Logger(PaymentDefaultProcessor.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly httpService: HttpService,
|
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
@InjectRepository(Payment) private readonly repository: Repository<Payment>,
|
@InjectRepository(Payment) private readonly repository: Repository<Payment>,
|
||||||
|
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(payment: Payment): Promise<boolean> {
|
async execute(payment: Payment): Promise<boolean> {
|
||||||
const url = this.configService.get('paymentProcessors.defaultUrl');
|
const url = this.configService.get('paymentProcessors.defaultUrl');
|
||||||
|
|
||||||
this.logger.log(
|
const responseExists = await this.makePaymentToProcessorService.execute(
|
||||||
`Processando Pagamento ${payment.id} com default processor`,
|
payment,
|
||||||
|
url,
|
||||||
);
|
);
|
||||||
|
|
||||||
const paymentData = {
|
if (responseExists) {
|
||||||
amount: payment.amount,
|
|
||||||
correlationId: payment.correlationId,
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await firstValueFrom(
|
|
||||||
this.httpService.post(`${url}/payments`, paymentData, {
|
|
||||||
timeout: 2000, // 30 segundos
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
this.logger.log(response.status === 200);
|
|
||||||
if (response.status === 200) {
|
|
||||||
this.logger.log(
|
|
||||||
`Payment ${payment.id} processed successfully via default processor`,
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.repository.update(payment.id, {
|
await this.repository.update(payment.id, {
|
||||||
...payment,
|
...payment,
|
||||||
paymentProcessor: ProcessorTypeEnum.DEFAULT,
|
paymentProcessor: ProcessorTypeEnum.DEFAULT,
|
||||||
@ -48,9 +33,7 @@ export class PaymentDefaultProcessor {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
this.logger.log(
|
|
||||||
`Payment ${payment.id} not processed successfully via default processor: ${response.data.message || 'Unknown error'}`,
|
|
||||||
);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user