diff --git a/src/transaction/entities/callback-partner.entity.ts b/src/transaction/entities/callback-partner.entity.ts new file mode 100644 index 0000000..f7c03b4 --- /dev/null +++ b/src/transaction/entities/callback-partner.entity.ts @@ -0,0 +1,18 @@ +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 CallbackPartner extends BaseModel { + @Column() + trx_id: string; + + @Column() + partner_trx_id: string; + + @Column() + url: string; +} diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index 0ebaf40..67ac85d 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -132,6 +132,31 @@ export class TransactionController { }; } + @Get('resend-partner/success/:code') + async resendSuccess(@Request() req, @Param('code') code: string) { + const data = await this.transactionService.resendOrderToPartner(code, true); + + return { + data, + statusCode: HttpStatus.OK, + message: 'success', + }; + } + + @Get('resend-partner/failed/:code') + async resendFailed(@Request() req, @Param('code') code: string) { + const data = await this.transactionService.resendOrderToPartner( + code, + false, + ); + + return { + data, + statusCode: HttpStatus.OK, + message: 'success', + }; + } + @Get('history') async getHistoryTransactionUser( @Query('page') page: number, diff --git a/src/transaction/transaction.module.ts b/src/transaction/transaction.module.ts index a384646..5e7c0ee 100644 --- a/src/transaction/transaction.module.ts +++ b/src/transaction/transaction.module.ts @@ -11,6 +11,7 @@ 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'; +import { CallbackPartner } from './entities/callback-partner.entity'; @Module({ imports: [ @@ -19,6 +20,7 @@ import { CheckBillHistory } from './entities/check-bill-history.entity'; TransactionJournal, Transactions, CheckBillHistory, + CallbackPartner, ]), ProductModule, ConfigurableModule, diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 2c3abb4..1ecff78 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -29,6 +29,7 @@ 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'; +import { CallbackPartner } from './entities/callback-partner.entity'; @Injectable() export class TransactionService { @@ -43,6 +44,8 @@ export class TransactionService { private coaRepository: Repository, @InjectRepository(CheckBillHistory) private checkBillHistoryRepository: Repository, + @InjectRepository(CallbackPartner) + private callbackPartnerRepository: Repository, private coaService: CoaService, private productService: ProductService, private productHistoryPriceService: ProductHistoryPriceService, @@ -1135,7 +1138,7 @@ export class TransactionService { if (userData.partner) { const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah gagal.`; this.callbackToPartner( - userData.id, + userData.partner.id, message, dataTransaction.partner_trx_id, dataTransaction.amount, @@ -1219,7 +1222,7 @@ export class TransactionService { if (userData.partner) { const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah berhasil.`; this.callbackToPartner( - userData.id, + userData.partner.id, message, dataTransaction.partner_trx_id, dataTransaction.amount, @@ -1231,6 +1234,53 @@ export class TransactionService { } } + async resendOrderToPartner(supplier_trx_id: string, status: boolean){ + const dataTransaction = await this.transactionRepository.findOne({ + where: { + supplier_trx_id: supplier_trx_id, + }, + relations: ['product_price'], + }); + + const userData = await this.userService.findExist(dataTransaction.user); + + const product_price = await this.productHistoryPriceService.findById( + dataTransaction.product_price.id, + ); + + const product = await this.productService.findOneById( + product_price.product.id, + ); + + if (status) { + const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah berhasil.`; + await this.callbackToPartner( + userData.partner.id, + message, + dataTransaction.partner_trx_id, + dataTransaction.amount, + product.code, + dataTransaction.destination, + dataTransaction.seri_number, + 'berhasil', + ); + } else { + const message = `Transaksi ${product.code} dengan tujuan ${dataTransaction.destination} telah gagal.`; + this.callbackToPartner( + userData.partner.id, + message, + dataTransaction.partner_trx_id, + dataTransaction.amount, + product.code, + dataTransaction.destination, + '-', + 'gagal', + ); + } + + + } + async callbackToPartner( partnerId: string, message: string, @@ -1242,9 +1292,14 @@ export class TransactionService { 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}`, - ); + const url = `${partnerData.callback_url}?status=${status}&memberID=${partnerData.code}&trxid=${trxId}&harga=${harga}&product=${productCode}&dest=${destination}&seriNumber=${seriNumber}&message=${message}`; + const result = await this.callbackPartnerRepository.insert({ + partner_trx_id: partnerId, + trx_id: trxId, + url: url, + }); + const res = await axios.get(url); + return res; } async withdrawBenefit(user) { diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 6382310..285c036 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -366,7 +366,7 @@ export class UsersService { where: { id: id, }, - relations: ['superior', 'roles'], + relations: ['superior', 'roles', 'partner'], }); } catch (e) { if (e instanceof EntityNotFoundError) {