From 713922d2ee55c7df3b552e48af445df45211ecaa Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Sun, 16 Jun 2024 14:04:16 +0800 Subject: [PATCH] Data patch meant to fix a group payment with group id 609 and transaction id 16803 that got stuck after making paying --- .../Processors/CallbackBillplzProcessor.php | 24 ++- ...ePaymentTransactionOneTimeFixProcessor.php | 188 ++++++++++++++++++ ...imeTransactionFixBillplzFailedCallback.php | 11 +- 3 files changed, 215 insertions(+), 8 deletions(-) create mode 100644 app/Classes/Modules/Transactions/Processors/CreatePaymentTransactionOneTimeFixProcessor.php diff --git a/app/Classes/Modules/Billplzs/Processors/CallbackBillplzProcessor.php b/app/Classes/Modules/Billplzs/Processors/CallbackBillplzProcessor.php index b5909cf1..aca0da1c 100644 --- a/app/Classes/Modules/Billplzs/Processors/CallbackBillplzProcessor.php +++ b/app/Classes/Modules/Billplzs/Processors/CallbackBillplzProcessor.php @@ -8,6 +8,7 @@ use App\Models\Group; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\PaymentMethodType; use App\Classes\Modules\Transactions\Processors\CreatePaymentTransactionProcessor; +use App\Classes\Modules\Transactions\Processors\CreatePaymentTransactionOneTimeFixProcessor; use App\Classes\Modules\Transactions\Processors\ReleaseGoodsToCustomerProcessor; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; use App\Classes\Modules\Wallets\Services\UpdatesWalletBalance; @@ -32,6 +33,9 @@ class CallbackBillplzProcessor /** @var CreatePaymentTransactionProcessor */ private $createPaymentTransactionProcessor; + /** @var CreatePaymentTransactionOneTimeFixProcessor */ + private $createPaymentTransactionOneTimeFixProcessor; + /** @var ReleaseGoodsToCustomerProcessor */ private $releaseGoodsToCustomerProcessor; @@ -44,7 +48,7 @@ class CallbackBillplzProcessor * @param CreatePaymentTransactionProcessor $createPaymentTransactionProcessor * @param ReleaseGoodsToCustomerProcessor $releaseGoodsToCustomerProcessor */ - public function __construct(UpdatesTransactionStatus $updatesTransactionStatus, UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor, UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor, UpdatesWalletBalance $updatesWalletBalance, CreatePaymentTransactionProcessor $createPaymentTransactionProcessor, ReleaseGoodsToCustomerProcessor $releaseGoodsToCustomerProcessor) + public function __construct(UpdatesTransactionStatus $updatesTransactionStatus, UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor, UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor, UpdatesWalletBalance $updatesWalletBalance, CreatePaymentTransactionProcessor $createPaymentTransactionProcessor, ReleaseGoodsToCustomerProcessor $releaseGoodsToCustomerProcessor, CreatePaymentTransactionOneTimeFixProcessor $createPaymentTransactionOneTimeFixProcessor) { $this->updatesTransactionStatus = $updatesTransactionStatus; $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor; @@ -52,6 +56,7 @@ class CallbackBillplzProcessor $this->updatesWalletBalance = $updatesWalletBalance; $this->createPaymentTransactionProcessor = $createPaymentTransactionProcessor; $this->releaseGoodsToCustomerProcessor = $releaseGoodsToCustomerProcessor; + $this->createPaymentTransactionOneTimeFixProcessor = $createPaymentTransactionOneTimeFixProcessor; } @@ -81,8 +86,16 @@ class CallbackBillplzProcessor foreach ($group->groupTransactions as $groupTransaction) { $invoice = $groupTransaction->transaction; if($invoice->status !== ApprovalStatus::COMPLETED){ - $paymentTransaction = $this->createPaymentTransactionProcessor->execute($invoice, PaymentMethodType::WALLET, null, false); - + if($group->id === 609){ + Log::info(json_encode($group->id)); + $paymentTransaction = $this->createPaymentTransactionOneTimeFixProcessor->execute($invoice, PaymentMethodType::WALLET, null, $transaction->created_at, false); + $paymentTransaction->created_at = $transaction->created_at; + $paymentTransaction->updated_at = $transaction->created_at; + $paymentTransaction->save(); + } + else{ + $paymentTransaction = $this->createPaymentTransactionProcessor->execute($invoice, PaymentMethodType::WALLET, null, false); + } if($paymentTransaction && $paymentTransaction->status == ApprovalStatus::APPROVED){ $pL = $invoice->owner; $this->releaseGoodsToCustomerProcessor->execute($pL, $invoice); @@ -123,6 +136,11 @@ class CallbackBillplzProcessor else{ Log::channel('storage_invoices')->info('Total amount from current transaction: '.$totalAmountToBePaid); //cief todo: to be removed Log::channel('storage_invoices')->info('Total amount from paid transaction: '.$transaction->amount); //cief todo: to be removed + + if($transaction->id === 16803){ + Log::channel('storage_invoices')->info('One time data patch for transaction: '.$transaction->id); + return true; + } return false; } } diff --git a/app/Classes/Modules/Transactions/Processors/CreatePaymentTransactionOneTimeFixProcessor.php b/app/Classes/Modules/Transactions/Processors/CreatePaymentTransactionOneTimeFixProcessor.php new file mode 100644 index 00000000..3d648e76 --- /dev/null +++ b/app/Classes/Modules/Transactions/Processors/CreatePaymentTransactionOneTimeFixProcessor.php @@ -0,0 +1,188 @@ +generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsPaymentTransaction = $createsPaymentTransaction; + $this->createsBillplzBill = $createsBillplzBill; + $this->createsTransactionableTransaction = $createsTransactionableTransaction; + $this->updatesWalletBalance = $updatesWalletBalance; + $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor; + $this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor; + $this->updatesTransactionStatus = $updatesTransactionStatus; + } + + /** + * @throws MalformedRequestException + */ + public function execute(Transaction $invoice, $payment_method, $bank_code, $date, $run = true) + { + $amount = $invoice->amount; + Log::info($invoice->owner); + $company_module = $invoice->owner->owner->companyModule()->first(); + $approvalStatus = ApprovalStatus::PENDING_SUBMISSION; + $billNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); + $payment_reference = null; + + if ($payment_method == PaymentMethodType::PAYMENT_GATEWAY) { + + // create billplz transaction + $payment_method = PaymentMethodType::PAYMENT_GATEWAY; + $billPlzBill = $this->createsBillplzBill->execute( + $company_module->name, + (app()->environment(['production'])) ? $company_module->employees()->first()->email : 'uldvstar@gmail.com', + 'This payment is for the invoice number . ' . $billNumber, + $amount, + $billNumber, + $bank_code, + true + ); + + $payment_reference = $billPlzBill->id; + } + else if ($payment_method === PaymentMethodType::WALLET) { + /** @var Wallet $wallet */ + $wallet = $company_module->wallets()->first(); + + + // if((float) number_format(($wallet->amount - $amount),2) < 0){ + // throw new MalformedRequestException('Insufficient wallet balance. Please Top up your wallet.'); + // } + + $walletPaymentBillNumber = $this->generatesTransactionBillNumber->execute('PYMT-'); + + $transaction_object = new TransactionObject($walletPaymentBillNumber, TransactionType::PAYMENT, 1, $company_module->id, 1, PaymentMethodType::WALLET, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], ''); + $transaction = $this->createsTransactionableTransaction->execute($wallet, $transaction_object); + $transaction->created_at = $date; + $transaction->updated_at = $date; + $transaction->save(); + + $payment_reference = $walletPaymentBillNumber; + + $this->updatesWalletBalance->execute($wallet, ($amount * -1)); + + if($run) + { + $packingList = $invoice->owner; + $order = $packingList->owner; + $packingList->status = ApprovalStatus::APPROVED; + $packingList->save(); + + if(app()->environment('production')){ + $this->updateDoFromVTPortalProcessor->execute($packingList); + $this->updateDoFromYDPortalProcessor->execute($packingList); + } + } + + // later use this variabke to create a approved payment transaction + $approvalStatus = ApprovalStatus::APPROVED; + + // update invoice to completed + if($run){ + $this->updatesTransactionStatus->execute($invoice, ApprovalStatus::COMPLETED); + } + } + else { + $payment_method = PaymentMethodType::CASH; + } + + $object = new TransactionObject( + $billNumber, + TransactionType::PAYMENT, + $company_module->id, + 1, + 1, + $payment_method, + $amount, + $amount, + 1, + 1, + 0, + 0, + 0, + null, + $approvalStatus, + null, + $payment_reference + ); + + $payment_transaction = $this->createsPaymentTransaction->execute($invoice, $object); + + return $payment_transaction; + } +} diff --git a/app/Console/Commands/OneTimeTransactionFixBillplzFailedCallback.php b/app/Console/Commands/OneTimeTransactionFixBillplzFailedCallback.php index f015e41b..9d4c648d 100644 --- a/app/Console/Commands/OneTimeTransactionFixBillplzFailedCallback.php +++ b/app/Console/Commands/OneTimeTransactionFixBillplzFailedCallback.php @@ -55,11 +55,12 @@ class OneTimeTransactionFixBillplzFailedCallback extends Command $this->outputArray = []; $start = new Carbon(); - //This transaction, 15205 has approve payment but not its owner, shipping invoice - $transaction = Transaction::whereIn('id', [15205])->first(); - $this->info(Carbon::now() . ' : One time fix failled callback from billplz for transaction with id 15205 cron started.'); + //Transaction fix with this one time fix command: 15205, 16803 + //This transaction, 16803 has approve payment but not its owner, shipping invoice + $transaction = Transaction::whereIn('id', [16803])->first(); + $this->info(Carbon::now() . ' : One time fix failled callback from billplz for transaction with id 16803 cron started.'); - if($transaction && $transaction->id == 15205){ + if($transaction && $transaction->id == 16803){ $status = ApprovalStatus::APPROVED; $this->callbackBillplzProcessor->execute($transaction, $status); @@ -68,6 +69,6 @@ class OneTimeTransactionFixBillplzFailedCallback extends Command $end = new Carbon(); $elapsedTime = $start->diff($end)->format('%H:%I:%S'); - $this->info(Carbon::now() . ' : One time fix failled callback from billplz for transaction with id 15205 cron ended. ElapsedTime: ' . $elapsedTime); + $this->info(Carbon::now() . ' : One time fix failled callback from billplz for transaction with id 16803 cron ended. ElapsedTime: ' . $elapsedTime); } }