ppob-backend/src/transaction/ppob_callback.controller.ts
2022-05-21 18:32:56 +07:00

113 lines
2.7 KiB
TypeScript

import { Controller, Get, HttpStatus, Logger, Req } from '@nestjs/common';
import { TransactionService } from './transaction.service';
import { FastifyRequest } from 'fastify';
import { Public } from '../auth/public.decorator';
@Controller({
path: 'ppob_callback',
version: '1',
})
export class PpobCallbackController {
private readonly logger = new Logger(PpobCallbackController.name);
constructor(private readonly transactionService: TransactionService) {}
@Public()
@Get()
async get(@Req() request: FastifyRequest) {
const response = request.query;
if (response['statuscode'] == 2) {
//TODO: UPDATE GAGAL
const updateTransaction =
await this.transactionService.callbackOrderFailed(
response['clientid'],
response,
);
return {
updateTransaction,
statusCode: HttpStatus.BAD_REQUEST,
message: 'failed to proccess',
};
}
//TODO: UPDATE BERHASIL
const updateTransaction =
await this.transactionService.callbackOrderSuccess(
response['clientid'],
response,
);
return {
updateTransaction,
statusCode: HttpStatus.OK,
message: 'success',
};
}
@Public()
@Get('/metro')
async getMetro(@Req() request: FastifyRequest) {
const response = request.query;
if (response['message'].toLowerCase().includes('cek tagihan')) {
if (response['status'] != 20) {
//TODO: UPDATE GAGAL
await this.transactionService.updateBill(
response['refid'],
null,
null,
false,
)
return {
statusCode: HttpStatus.OK,
message: 'success',
};
}
const splitMessage = response['message'].split('"');
//TODO: UPDATE BERHASIL
await this.transactionService.updateBill(
response['refid'],
Number(splitMessage[9].replace(/^\D+/g, '')),
Number(splitMessage[11].replace(/^\D+/g, '')),
true
);
//
} else {
if (response['status'].toString() != '20') {
//TODO: UPDATE GAGAL
const updateTransaction =
await this.transactionService.callbackOrderFailed(
response['refid'],
response,
);
return {
updateTransaction,
statusCode: HttpStatus.BAD_REQUEST,
message: 'failed to proccess',
};
}
//TODO: UPDATE BERHASIL
const updateTransaction =
await this.transactionService.callbackOrderSuccess(
response['refid'],
response,
);
}
this.logger.log({
requestQuery: request.query,
});
return {
statusCode: HttpStatus.OK,
message: 'success',
};
}
}