Refactor PaymentFallbackProcessor to utilize MakePaymentToProcessorService for payment execution
This commit is contained in:
parent
f0c95308b0
commit
d0d58e220d
@ -1,44 +1,31 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Payment } from '../entities/payment.entity';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
||||
import { PaymentStatusEnum } from '../enumns/payment-status.enum';
|
||||
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
||||
|
||||
@Injectable()
|
||||
export class PaymentFallbackProcessor {
|
||||
private readonly logger = new Logger(PaymentFallbackProcessor.name);
|
||||
|
||||
constructor(
|
||||
private readonly httpService: HttpService,
|
||||
private readonly configService: ConfigService,
|
||||
@InjectRepository(Payment) private readonly repository: Repository<Payment>,
|
||||
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
||||
) {}
|
||||
|
||||
async execute(payment: Payment): Promise<boolean> {
|
||||
const url = this.configService.get('paymentProcessors.fallbackUrl');
|
||||
|
||||
this.logger.log(`Processing payment ${payment.id} via fallback processor`);
|
||||
|
||||
const paymentData = {
|
||||
amount: payment.amount,
|
||||
correlationId: payment.correlationId,
|
||||
};
|
||||
|
||||
const response = await firstValueFrom(
|
||||
this.httpService.post(`${url}/payments`, paymentData, {
|
||||
timeout: 2000, // 30 segundos
|
||||
}),
|
||||
const responseExists = await this.makePaymentToProcessorService.execute(
|
||||
payment,
|
||||
url,
|
||||
);
|
||||
|
||||
if (response.status === 200 && response.data.success) {
|
||||
this.logger.log(
|
||||
`Payment ${payment.id} processed successfully via fallback processor`,
|
||||
);
|
||||
|
||||
if (responseExists) {
|
||||
await this.repository.update(payment.id, {
|
||||
...payment,
|
||||
paymentProcessor: ProcessorTypeEnum.FALLBACK,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user