feat: add sync midtrans transaction
parent
3938504fd3
commit
acf8861823
|
@ -1,12 +1,12 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { EventBus } from '@nestjs/cqrs';
|
|
||||||
import { mappingMidtransTransaction } from '../../domain/usecases/helpers/mapping-transaction.helper';
|
import { mappingMidtransTransaction } from '../../domain/usecases/helpers/mapping-transaction.helper';
|
||||||
import { Snap } from 'midtrans-client';
|
import { Snap } from 'midtrans-client';
|
||||||
import { MidtransStatus } from '../../domain/entities/midtrans-callback.event';
|
import { MidtransStatus } from '../../domain/entities/midtrans-callback.event';
|
||||||
|
import { TransactionReadService } from 'src/modules/transaction/transaction/data/services/transaction-read.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MidtransService {
|
export class MidtransService {
|
||||||
constructor(private eventBus: EventBus) {}
|
constructor(private transaction: TransactionReadService) {}
|
||||||
|
|
||||||
get midtransInstance() {
|
get midtransInstance() {
|
||||||
return new Snap({
|
return new Snap({
|
||||||
|
@ -20,6 +20,25 @@ export class MidtransService {
|
||||||
return await this.midtransInstance.transaction.status(orderId);
|
return await this.midtransInstance.transaction.status(orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async syncPendingStatus(): Promise<any[]> {
|
||||||
|
const pendingIds = await this.transaction.getPendingOrderId();
|
||||||
|
const responses = [];
|
||||||
|
|
||||||
|
for (const id of pendingIds) {
|
||||||
|
let status;
|
||||||
|
try {
|
||||||
|
status = await this.getStatus(id);
|
||||||
|
} catch (error) {
|
||||||
|
status = {
|
||||||
|
order_id: id,
|
||||||
|
transaction_status: 'cancel',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
responses.push(status);
|
||||||
|
}
|
||||||
|
return responses;
|
||||||
|
}
|
||||||
|
|
||||||
async changeStatus(orderId: string, action: MidtransStatus): Promise<any> {
|
async changeStatus(orderId: string, action: MidtransStatus): Promise<any> {
|
||||||
return await this.midtransInstance.transaction[action](orderId);
|
return await this.midtransInstance.transaction[action](orderId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,27 @@ export class MidtransController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('sync')
|
||||||
|
async syncStatus() {
|
||||||
|
try {
|
||||||
|
const results = await this.dataService.syncPendingStatus();
|
||||||
|
|
||||||
|
for (const data of results) {
|
||||||
|
this.eventBus.publishAll([
|
||||||
|
new MidtransCallbackEvent({
|
||||||
|
id: data.order_id,
|
||||||
|
data: data,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'Berhasil update status transaksi';
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error.message);
|
||||||
|
throw new Error('Gagal update status transaksi');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Get(':id/change-status')
|
@Get(':id/change-status')
|
||||||
@ApiQuery({ name: 'status', enum: MidtransStatus })
|
@ApiQuery({ name: 'status', enum: MidtransStatus })
|
||||||
async cancel(
|
async cancel(
|
||||||
|
|
|
@ -3,12 +3,20 @@ import { CqrsModule } from '@nestjs/cqrs';
|
||||||
import { MidtransController } from './infrastructure/midtrans.controller';
|
import { MidtransController } from './infrastructure/midtrans.controller';
|
||||||
import { MidtransService } from './data/services/midtrans.service';
|
import { MidtransService } from './data/services/midtrans.service';
|
||||||
import { Global, Module } from '@nestjs/common';
|
import { Global, Module } from '@nestjs/common';
|
||||||
|
import { TransactionReadService } from 'src/modules/transaction/transaction/data/services/transaction-read.service';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
||||||
|
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule.forRoot(), CqrsModule],
|
imports: [
|
||||||
|
ConfigModule.forRoot(),
|
||||||
|
CqrsModule,
|
||||||
|
TypeOrmModule.forFeature([TransactionModel], CONNECTION_NAME.DEFAULT),
|
||||||
|
],
|
||||||
controllers: [MidtransController],
|
controllers: [MidtransController],
|
||||||
providers: [MidtransService],
|
providers: [MidtransService, TransactionReadService],
|
||||||
exports: [MidtransService],
|
exports: [MidtransService],
|
||||||
})
|
})
|
||||||
export class MidtransModule {}
|
export class MidtransModule {}
|
||||||
|
|
|
@ -2,9 +2,13 @@ import { Injectable } from '@nestjs/common';
|
||||||
import { TransactionEntity } from '../../domain/entities/transaction.entity';
|
import { TransactionEntity } from '../../domain/entities/transaction.entity';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { TransactionModel } from '../models/transaction.model';
|
import { TransactionModel } from '../models/transaction.model';
|
||||||
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
import {
|
||||||
|
CONNECTION_NAME,
|
||||||
|
STATUS,
|
||||||
|
} from 'src/core/strings/constants/base.constants';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
|
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
|
||||||
|
import { TransactionPaymentType } from '../../constants';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
||||||
|
@ -14,4 +18,16 @@ export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
||||||
) {
|
) {
|
||||||
super(repo);
|
super(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getPendingOrderId() {
|
||||||
|
const transactions = await this.repo.find({
|
||||||
|
where: {
|
||||||
|
status: STATUS.PENDING,
|
||||||
|
payment_type: TransactionPaymentType.MIDTRANS,
|
||||||
|
},
|
||||||
|
select: ['id'],
|
||||||
|
});
|
||||||
|
|
||||||
|
return transactions.map(({ id }) => id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,7 @@ import { PaymentMethodDataService } from '../payment-method/data/services/paymen
|
||||||
import { PaymentMethodModel } from '../payment-method/data/models/payment-method.model';
|
import { PaymentMethodModel } from '../payment-method/data/models/payment-method.model';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
exports: [TransactionReadService],
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot(),
|
ConfigModule.forRoot(),
|
||||||
TypeOrmModule.forFeature(
|
TypeOrmModule.forFeature(
|
||||||
|
|
Loading…
Reference in New Issue