diff --git a/src/transaction/dto/order-transaction.dto.ts b/src/transaction/dto/order-transaction.dto.ts index ff3d293..8dc3d30 100644 --- a/src/transaction/dto/order-transaction.dto.ts +++ b/src/transaction/dto/order-transaction.dto.ts @@ -9,4 +9,7 @@ export class OrderTransactionDto { @IsOptional() trx_id: string; + + @IsOptional() + bill_trx_id: string; } diff --git a/src/transaction/entities/check-bill-history.entity.ts b/src/transaction/entities/check-bill-history.entity.ts new file mode 100644 index 0000000..bb9799a --- /dev/null +++ b/src/transaction/entities/check-bill-history.entity.ts @@ -0,0 +1,42 @@ +import { Column, Entity, ManyToOne, OneToMany } from 'typeorm'; +import { BaseModel } from '../../config/basemodel.entity'; +import { statusTransaction, typeTransaction } from '../../helper/enum-list'; +import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity'; +import { UserDetail } from '../../users/entities/user_detail.entity'; +import { TransactionJournal } from './transaction-journal.entity'; + +@Entity() +export class CheckBillHistory extends BaseModel { + @Column() + trx_id: string; + + @Column() + partner_trx_id: string; + + @Column() + amount: number; + + @Column({ + type: 'uuid', + nullable: true, + }) + user: string; + + @Column({ + nullable: true, + }) + destination: string; + + @Column({ + nullable: true, + }) + request_json: string; + + @Column({ + nullable: true, + }) + callback_json: string; + + @Column() + product_code: string; +} diff --git a/src/transaction/ppob_callback.controller.ts b/src/transaction/ppob_callback.controller.ts index fb36659..d43199d 100644 --- a/src/transaction/ppob_callback.controller.ts +++ b/src/transaction/ppob_callback.controller.ts @@ -51,9 +51,9 @@ export class PpobCallbackController { async getMetro(@Req() request: FastifyRequest) { const response = request.query; - console.log(response, "INI DIA") + console.log(typeof response['message'], "INI DIA") - if (response['message'].include('CEK TAGIHAN')) { + if (response['message'].includes('CEK TAGIHAN')) { console.log("messagenya tuh",response['message']) } else { if (response['status'] != 20) { diff --git a/src/transaction/transaction.module.ts b/src/transaction/transaction.module.ts index 13bae9e..a384646 100644 --- a/src/transaction/transaction.module.ts +++ b/src/transaction/transaction.module.ts @@ -10,10 +10,16 @@ import { CoaService } from './coa.service'; import { ProductModule } from '../product/product.module'; import { UsersModule } from 'src/users/users.module'; import { ConfigurableModule } from '../configurable/configurable.module'; +import { CheckBillHistory } from './entities/check-bill-history.entity'; @Module({ imports: [ - TypeOrmModule.forFeature([COA, TransactionJournal, Transactions]), + TypeOrmModule.forFeature([ + COA, + TransactionJournal, + Transactions, + CheckBillHistory, + ]), ProductModule, ConfigurableModule, forwardRef(() => UsersModule), diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 5bd7db9..0fb35cf 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -27,6 +27,8 @@ import { DepositReturnDto } from './dto/deposit_return.dto'; import { UserDetail } from '../users/entities/user_detail.entity'; import { doTransaction } from '../helper/irs-api'; import { ProductHistoryPrice } from '../product/entities/product-history-price.entity'; +import axios from 'axios'; +import { CheckBillHistory } from './entities/check-bill-history.entity'; @Injectable() export class TransactionService { @@ -39,6 +41,8 @@ export class TransactionService { private transactionJournalRepository: Repository, @InjectRepository(COA) private coaRepository: Repository, + @InjectRepository(CheckBillHistory) + private checkBillHistoryRepository: Repository, private coaService: CoaService, private productService: ProductService, private productHistoryPriceService: ProductHistoryPriceService, @@ -477,6 +481,8 @@ export class TransactionService { `${coaType[coaType.SALES]}-SYSTEM`, ); + + if (coaAccount.amount < product_price.mark_up_price + product_price.price) { throw new HttpException( { @@ -814,21 +820,52 @@ export class TransactionService { }) .join(''); - let hitSupplier = await doTransaction( - 'CEK' + orderTransactionDto.productCode.slice(3), - orderTransactionDto.destination, - trxId, - supplier, - ); + let status; - // let hitSupplier = await doTransaction( - // 'CEKXL1', - // orderTransactionDto.destination, - // trxId, - // supplier, - // ); + try { + let hitSupplier = await doTransaction( + 'CEK' + orderTransactionDto.productCode.slice(3), + orderTransactionDto.destination, + trxId, + supplier, + ); + const parsingResponse = hitSupplier.split(' '); + hitSupplier = { + success: hitSupplier.include('diproses'), + msg: hitSupplier, + }; - return hitSupplier; + if (!hitSupplier.success) { + status = statusTransaction[statusTransaction.FAILED]; + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: hitSupplier.msg, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } else { + status = statusTransaction[statusTransaction.SUCCESS]; + + await this.checkBillHistoryRepository.insert({ + trx_id: trxId, + user: userData.id, + callback_json: JSON.stringify(hitSupplier), + destination: orderTransactionDto.destination, + product_code: orderTransactionDto.productCode, + partner_trx_id: orderTransactionDto.trx_id, + }); + } + } catch (e) { + throw e; + } + + return { + trx_id: trxId, + client_trx_id: orderTransactionDto.trx_id, + product: orderTransactionDto.productCode, + status: status, + }; } async createDepositReturn(currentUser, depositReturnDto: DepositReturnDto) { @@ -1060,6 +1097,20 @@ export class TransactionService { } catch (e) { throw e; } + + if (userData.partner) { + const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah gagal.`; + this.callbackToPartner( + userData.id, + message, + dataTransaction.partner_trx_id, + dataTransaction.amount, + product.code, + dataTransaction.destination, + '-', + 'gagal', + ); + } } async callbackOrderSuccess(supplier_trx_id: string, callback: any) { @@ -1131,6 +1182,36 @@ export class TransactionService { } catch (e) { throw e; } + + if (userData.partner) { + const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah berhasil.`; + this.callbackToPartner( + userData.id, + message, + dataTransaction.partner_trx_id, + dataTransaction.amount, + product.code, + dataTransaction.destination, + dataTransaction.seri_number, + 'berhasil', + ); + } + } + + async callbackToPartner( + partnerId: string, + message: string, + trxId: string, + harga: number, + productCode: string, + destination: string, + seriNumber: string, + status: string, + ) { + const partnerData = await this.userService.findPartner(partnerId); + const res = await axios.get( + `${partnerData.callback_url}?status=${status}&memberID=${partnerData.code}&trxid=${trxId}&harga=${harga}&product=${productCode}&dest=${destination}&seriNumber=${seriNumber}&message=${message}`, + ); } async withdrawBenefit(user) { diff --git a/src/users/entities/partner.entity.ts b/src/users/entities/partner.entity.ts index 6d53f95..57098af 100644 --- a/src/users/entities/partner.entity.ts +++ b/src/users/entities/partner.entity.ts @@ -27,4 +27,9 @@ export class Partner extends BaseModel { @Column({ default: true }) status: boolean; + + @Column({ + default: '', + }) + callback_url: string; } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index cc89d11..b1f8d81 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -20,12 +20,15 @@ import * as uuid from 'uuid'; import { UserDetail } from './entities/user_detail.entity'; import { COA } from '../transaction/entities/coa.entity'; import { mapSeries } from 'bluebird'; +import { Partner } from './entities/partner.entity'; @Injectable() export class UsersService { constructor( @InjectRepository(User) private usersRepository: Repository, + @InjectRepository(Partner) + private partnerRepository: Repository, @InjectRepository(UserDetail) private userDetailRepository: Repository, @Inject( @@ -676,4 +679,26 @@ export class UsersService { } } } + + async findPartner(partnerId: string) { + try { + return this.partnerRepository.findOneOrFail({ + where: { + id: partnerId, + }, + }); + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Partner not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } }