diff --git a/src/transaction/ppob_callback.controller.ts b/src/transaction/ppob_callback.controller.ts index 99f3cc7..caef626 100644 --- a/src/transaction/ppob_callback.controller.ts +++ b/src/transaction/ppob_callback.controller.ts @@ -3,7 +3,7 @@ import { Get, HttpException, HttpStatus, - Logger, + Logger, Post, Req, } from '@nestjs/common'; import { TransactionService } from './transaction.service'; @@ -131,4 +131,25 @@ export class PpobCallbackController { } } } + + @Public() + @Post('/hemat') + async getHemat(@Req() request: FastifyRequest) { + const response = request.body; + console.log('responsehemat', response); + console.log('responsehemat2', request.body); + if (response['msg'] != 'Transaksi Sukses') { + //TODO: UPDATE GAGAL + await this.transactionService.checkCallbackOrderFailed( + response['data']['idtransaction'], + response, + ); + } else { + //TODO: UPDATE BERHASIL + await this.transactionService.checkCallbackOrderSuccess( + response['data']['idtransaction'], + response, + ); + } + } } diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 668e42a..164a211 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -523,267 +523,267 @@ export class TransactionService { ); const statusProduct = await this.productHistoryStatusService.findOneActive( - product.id, + product.id, userData.partner == null ? null : userData.partner.id, ); if (statusProduct.status == 'NOT ACTIVE') { - throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: `Transaction Failed because product is not active`, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } else { - console.log('userdatapartnerid', userData.partner?.id); - - let product_price = await this.productHistoryPriceService.findOne( - product.id, - userData.partner?.id, - ); - - //GET COA - console.log('coamsk5', product_price.id); - - const coaAccount = await this.coaService.findByUser( - userData.id, - coaType.WALLET, - ); - - const coaInventory = await this.coaService.findByName( - `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, - ); - - const coaCostOfSales = await this.coaService.findByName( - `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, - ); - - const coaSales = await this.coaService.findByName( - `${coaType[coaType.SALES]}-SYSTEM`, - ); - - if (orderTransactionDto.bill_trx_id) { - try { - const billId = await this.checkBillHistoryRepository.findOneOrFail({ - where: { - trx_id: orderTransactionDto.bill_trx_id, - }, - }); - - product_price.price = billId.amount; - } catch (e) { - if (e instanceof EntityNotFoundError) { - throw new HttpException( - { - statusCode: HttpStatus.NOT_FOUND, - error: 'Bill not found', - }, - HttpStatus.NOT_FOUND, - ); - } else { - throw e; - } - } - } - - if (coaAccount.amount < product_price.mark_up_price + product_price.price) { throw new HttpException( { statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: `Transaction Failed because saldo not enough`, + error: `Transaction Failed because product is not active`, }, HttpStatus.INTERNAL_SERVER_ERROR, ); - } + } else { + console.log('userdatapartnerid', userData.partner?.id); - //HIT API SUPPLIER - const trxId = Array(6) - .fill(null) - .map(() => { - return Math.round(Math.random() * 16).toString(16); - }) - .join(''); + let product_price = await this.productHistoryPriceService.findOne( + product.id, + userData.partner?.id, + ); - let hitLoginHemat; + //GET COA + console.log('coamsk5', product_price.id); - if (supplier.code == 'Hemat') { - hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); - } + const coaAccount = await this.coaService.findByUser( + userData.id, + coaType.WALLET, + ); - let hitSupplier = supplier.code == 'Hemat' ? - await doTransaction( - orderTransactionDto.productCode, - orderTransactionDto.destination, - trxId, - supplier, - hitLoginHemat.data, - product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', - ) : await doTransaction( - orderTransactionDto.productCode, - orderTransactionDto.destination, - trxId, - supplier, - "", - product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', - ); + const coaInventory = await this.coaService.findByName( + `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, + ); - // let hitSupplier; + const coaCostOfSales = await this.coaService.findByName( + `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, + ); - console.log('iniresponsupplierharga', hitSupplier) - console.log('iniresponsupplier', hitSupplier.harga) - console.log(supplier.code, 'sdkfjsd'); - console.log(product_price, 'price'); + const coaSales = await this.coaService.findByName( + `${coaType[coaType.SALES]}-SYSTEM`, + ); + if (orderTransactionDto.bill_trx_id) { + try { + const billId = await this.checkBillHistoryRepository.findOneOrFail({ + where: { + trx_id: orderTransactionDto.bill_trx_id, + }, + }); - if (supplier.code == 'metro') { - const parsingResponse = hitSupplier.split(' '); - console.log('parsingResponse', parsingResponse) - const newHitSupplier = { - success: hitSupplier.includes('diproses'), - harga: parseInt( - parsingResponse[parsingResponse.length - 2].replace(/\./g, ' '), - ), - msg: hitSupplier, - }; - - hitSupplier = newHitSupplier; - - if (orderTransactionDto.bill_trx_id !== null) { - hitSupplier.harga = product_price.price; - } - } else if (supplier.code == 'Hemat') { - const newHitSupplier = { - success: hitSupplier.success, - harga: product_price.price, - msg: hitSupplier.msg, - }; - - hitSupplier = newHitSupplier; - - if (orderTransactionDto.bill_trx_id !== null) { - hitSupplier.harga = product_price.price; - } - } - - console.log(hitSupplier, 'newhitSupplier'); - - // const hitSupplier = { - // harga: 2000, - // success: true, - // msg: 'Berhasil', - // }; - - this.logger.log({ - responseAPISupplier: hitSupplier, - }); - - let costInventory = product_price.price; - - if (hitSupplier.harga != undefined) { - if (hitSupplier.harga != product_price.price) { - console.log('priceupdate1supplier', hitSupplier.harga); - console.log('priceupdate1normal', product_price.price); - product_price.endDate = new Date(); - costInventory = hitSupplier.harga; - - const listActivePrice = - await this.productHistoryPriceService.getAllActivePriceByProduct( - product.id, + product_price.price = billId.amount; + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Bill not found', + }, + HttpStatus.NOT_FOUND, ); - - await this.productHistoryPriceService.updateEndDate(product.id); - - listActivePrice.map(async (x) => { - const newProductPrice = new ProductHistoryPrice(); - - newProductPrice.id = uuid.v4(); - newProductPrice.type = x.type; - newProductPrice.price = hitSupplier.harga; - newProductPrice.mark_up_price = x.mark_up_price; - newProductPrice.startDate = new Date(); - newProductPrice.product = product; - newProductPrice.partner = x.partner; - await this.productHistoryPriceService.create(newProductPrice); - - product_price = newProductPrice; - }); - } - } - - try { - //TRANSACTION DATA - await this.connection.transaction(async (manager) => { - const transactionData = new Transactions(); - - transactionData.id = uuid.v4(); - transactionData.amount = - product_price.mark_up_price + product_price.price; - transactionData.user = userData.id; - transactionData.type = typeTransaction.ORDER; - transactionData.product_price = product_price; - transactionData.destination = orderTransactionDto.destination; - transactionData.partner_trx_id = orderTransactionDto.trx_id; - transactionData.supplier_trx_id = trxId; - transactionData.check_bill = orderTransactionDto.bill_trx_id; - transactionData.balance_remaining = - coaAccount.amount - product_price.mark_up_price - costInventory; - - if (!hitSupplier.success) { - transactionData.status = statusTransaction.FAILED; - status = statusTransaction[transactionData.status]; - await this.transactionRepository.insert(transactionData); - throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: hitSupplier.msg, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } else { - transactionData.status = statusTransaction.PENDING; - status = statusTransaction[transactionData.status]; + } else { + throw e; + } } + } - await manager.insert(Transactions, transactionData); + if (coaAccount.amount < product_price.mark_up_price + product_price.price) { + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because saldo not enough`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } - await this.accountingTransaction({ - createTransaction: false, - transactionalEntityManager: manager, - transaction: transactionData, - amount: transactionData.amount, - journals: [ - { - coa_id: coaInventory.id, - credit: costInventory, - }, - { - coa_id: coaCostOfSales.id, - debit: costInventory, - }, - { - coa_id: coaAccount.id, - debit: product_price.mark_up_price + costInventory, - }, - { - // eslint-disable-next-line camelcase - coa_id: coaSales.id, - credit: product_price.mark_up_price + costInventory, - }, - ], - }); + //HIT API SUPPLIER + const trxId = Array(6) + .fill(null) + .map(() => { + return Math.round(Math.random() * 16).toString(16); + }) + .join(''); + + let hitLoginHemat; + + if (supplier.code == 'Hemat') { + hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); + } + + let hitSupplier = supplier.code == 'Hemat' ? + await doTransaction( + orderTransactionDto.productCode, + orderTransactionDto.destination, + trxId, + supplier, + hitLoginHemat.data, + product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', + ) : await doTransaction( + orderTransactionDto.productCode, + orderTransactionDto.destination, + trxId, + supplier, + "", + product.type == 'prepaid' ? 'PURCHASE' : 'PAYMENT', + ); + + // let hitSupplier; + + console.log('iniresponsupplierharga', hitSupplier) + console.log('iniresponsupplier', hitSupplier.harga) + console.log(supplier.code, 'sdkfjsd'); + console.log(product_price, 'price'); + + + if (supplier.code == 'metro') { + const parsingResponse = hitSupplier.split(' '); + console.log('parsingResponse', parsingResponse) + const newHitSupplier = { + success: hitSupplier.includes('diproses'), + harga: parseInt( + parsingResponse[parsingResponse.length - 2].replace(/\./g, ' '), + ), + msg: hitSupplier, + }; + + hitSupplier = newHitSupplier; + + if (orderTransactionDto.bill_trx_id !== null) { + hitSupplier.harga = product_price.price; + } + } else if (supplier.code == 'Hemat') { + const newHitSupplier = { + success: hitSupplier.success, + harga: product_price.price, + msg: hitSupplier.msg, + }; + + hitSupplier = newHitSupplier; + + if (orderTransactionDto.bill_trx_id !== null) { + hitSupplier.harga = product_price.price; + } + } + + console.log(hitSupplier, 'newhitSupplier'); + + // const hitSupplier = { + // harga: 2000, + // success: true, + // msg: 'Berhasil', + // }; + + this.logger.log({ + responseAPISupplier: hitSupplier, }); - } catch (e) { - throw e; - } - return { - trx_id: trxId, - client_trx_id: orderTransactionDto.trx_id, - product: orderTransactionDto.productCode, - amount: product_price.mark_up_price + product_price.price, - status: status, - }; + let costInventory = product_price.price; + + if (hitSupplier.harga != undefined) { + if (hitSupplier.harga != product_price.price) { + console.log('priceupdate1supplier', hitSupplier.harga); + console.log('priceupdate1normal', product_price.price); + product_price.endDate = new Date(); + costInventory = hitSupplier.harga; + + const listActivePrice = + await this.productHistoryPriceService.getAllActivePriceByProduct( + product.id, + ); + + await this.productHistoryPriceService.updateEndDate(product.id); + + listActivePrice.map(async (x) => { + const newProductPrice = new ProductHistoryPrice(); + + newProductPrice.id = uuid.v4(); + newProductPrice.type = x.type; + newProductPrice.price = hitSupplier.harga; + newProductPrice.mark_up_price = x.mark_up_price; + newProductPrice.startDate = new Date(); + newProductPrice.product = product; + newProductPrice.partner = x.partner; + await this.productHistoryPriceService.create(newProductPrice); + + product_price = newProductPrice; + }); + } + } + + try { + //TRANSACTION DATA + await this.connection.transaction(async (manager) => { + const transactionData = new Transactions(); + + transactionData.id = uuid.v4(); + transactionData.amount = + product_price.mark_up_price + product_price.price; + transactionData.user = userData.id; + transactionData.type = typeTransaction.ORDER; + transactionData.product_price = product_price; + transactionData.destination = orderTransactionDto.destination; + transactionData.partner_trx_id = orderTransactionDto.trx_id; + transactionData.supplier_trx_id = trxId; + transactionData.check_bill = orderTransactionDto.bill_trx_id; + transactionData.balance_remaining = + coaAccount.amount - product_price.mark_up_price - costInventory; + + if (!hitSupplier.success) { + transactionData.status = statusTransaction.FAILED; + status = statusTransaction[transactionData.status]; + await this.transactionRepository.insert(transactionData); + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: hitSupplier.msg, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } else { + transactionData.status = statusTransaction.PENDING; + status = statusTransaction[transactionData.status]; + } + + await manager.insert(Transactions, transactionData); + + await this.accountingTransaction({ + createTransaction: false, + transactionalEntityManager: manager, + transaction: transactionData, + amount: transactionData.amount, + journals: [ + { + coa_id: coaInventory.id, + credit: costInventory, + }, + { + coa_id: coaCostOfSales.id, + debit: costInventory, + }, + { + coa_id: coaAccount.id, + debit: product_price.mark_up_price + costInventory, + }, + { + // eslint-disable-next-line camelcase + coa_id: coaSales.id, + credit: product_price.mark_up_price + costInventory, + }, + ], + }); + }); + } catch (e) { + throw e; + } + + return { + trx_id: trxId, + client_trx_id: orderTransactionDto.trx_id, + product: orderTransactionDto.productCode, + amount: product_price.mark_up_price + product_price.price, + status: status, + }; } } @@ -1011,7 +1011,7 @@ export class TransactionService { let hitLoginHemat; if (supplier.code == 'Hemat') { - hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); + hitLoginHemat = await doAuthorizeHemat(supplier.irs_user, supplier.irs_pass, supplier); } let hitSupplier = supplier.code == 'Hemat' ? await doTransaction( @@ -1257,19 +1257,19 @@ export class TransactionService { // } else { - const updateTransaction = await this.callbackOrderFailed( - supplier_trx_id, - callback, - ); + const updateTransaction = await this.callbackOrderFailed( + supplier_trx_id, + callback, + ); - throw new HttpException( - { - updateTransaction, - statusCode: HttpStatus.BAD_REQUEST, - error: 'updated transaction to failed', - }, - HttpStatus.BAD_REQUEST, - ); + throw new HttpException( + { + updateTransaction, + statusCode: HttpStatus.BAD_REQUEST, + error: 'updated transaction to failed', + }, + HttpStatus.BAD_REQUEST, + ); // } } @@ -1295,19 +1295,19 @@ export class TransactionService { // HttpStatus.BAD_REQUEST, // ); // } else { - const updateTransaction = await this.callbackOrderSuccess( - supplier_trx_id, - callback, - ); + const updateTransaction = await this.callbackOrderSuccess( + supplier_trx_id, + callback, + ); - throw new HttpException( - { - updateTransaction, - statusCode: HttpStatus.OK, - error: 'success', - }, - HttpStatus.OK, - ); + throw new HttpException( + { + updateTransaction, + statusCode: HttpStatus.OK, + error: 'success', + }, + HttpStatus.OK, + ); // } } @@ -1361,13 +1361,9 @@ export class TransactionService { product_price.product.id, ); - console.log('inipricetable', dataTransaction.product_price.price); - console.log('inipricecallback', callback['price']); - if (callback['message']) { if (callback['message'].includes('METRO')) { if (callback['price'] != dataTransaction.product_price.price) { - console.log('mskupdt', 'msk'); dataTransaction.product_price.price = parseInt(callback['price']); } } @@ -1463,37 +1459,6 @@ export class TransactionService { }, }); - dataTransaction.status = statusTransaction.SUCCESS; - - if (callback['sn']) { - dataTransaction.seri_number = callback['sn']; - console.log('msksn1', dataTransaction.seri_number); - } else { - const response = callback['message']; - const responseBaru = response.split(' '); - - dataTransaction.seri_number = - responseBaru[10].length > 1 ? responseBaru[10] : responseBaru[9]; - - if (dataTransaction.seri_number == 'SN:') { - dataTransaction.seri_number = responseBaru[11]; - } - } - - dataTransaction.callback_json = callback; - - if (callback['message']) { - if (callback['message'].includes('METRO')) { - if (callback['price'] != dataTransaction.product_price.price) { - dataProductHistoryPrice.price = parseInt(callback['price']); - } - } - } - - const userData = await this.userService.findExist(dataTransaction.user); - - let supervisorData = []; - const product_price = await this.productHistoryPriceService.findById( dataTransaction.product_price.id, ); @@ -1502,6 +1467,46 @@ export class TransactionService { product_price.product.id, ); + const supplier = await this.supplierService.findByCode( + product.supplier.code, + ); + + dataTransaction.status = statusTransaction.SUCCESS; + + if (supplier.code == 'Hemat') { + dataTransaction.seri_number = callback['data']['serial_number']; + dataProductHistoryPrice.price = parseInt(callback['data']['additional']['harga']); + } else { + if (callback['sn']) { + dataTransaction.seri_number = callback['sn']; + console.log('msksn1', dataTransaction.seri_number); + } else { + const response = callback['message']; + const responseBaru = response.split(' '); + + dataTransaction.seri_number = + responseBaru[10].length > 1 ? responseBaru[10] : responseBaru[9]; + + if (dataTransaction.seri_number == 'SN:') { + dataTransaction.seri_number = responseBaru[11]; + } + } + + dataTransaction.callback_json = callback; + + if (callback['message']) { + if (callback['message'].includes('METRO')) { + if (callback['price'] != dataTransaction.product_price.price) { + dataProductHistoryPrice.price = parseInt(callback['price']); + } + } + } + } + + const userData = await this.userService.findExist(dataTransaction.user); + + let supervisorData = []; + let profit = product_price.mark_up_price; //GET COA @@ -2121,7 +2126,7 @@ export class TransactionService { total_transaction: parseInt(data.total_transaction), total_modal: parseInt(data.total_modal), total_profit: parseInt(data.total_profit), - total_commission: parseInt(total_expense) - parseInt(data.total_profit), + total_commission: parseInt(data.total_profit) - parseInt(total_expense), }; }