mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
e7c0e17ee4
# Conflicts: # app/Classes/Modules/Segments/ControllersLogic/UpdateConstantPostcodeLogic.php # app/Classes/ValueObjects/Constants/TransactionType.php # routes/api.php # routes/web.php
186 lines
8.4 KiB
PHP
186 lines
8.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Classes\Exceptions\ResourceNotFoundException;
|
|
use App\Classes\Modules\Billplzs\DataTransferObjects\BillplzXSignatureObject;
|
|
use App\Classes\Modules\Billplzs\Services\GetBillplzBill;
|
|
use App\Classes\Modules\Orders\Processors\UpdateDoFromVTPortalProcessor;
|
|
use App\Classes\Modules\Orders\Processors\UpdateDoFromYDPortalProcessor;
|
|
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
|
|
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\PaymentMethodType;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Models\Order;
|
|
use App\Models\Transaction;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class FixBillplzFailedCallbackPayment extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'billplz-failed-callback:fix';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Fix all failled callback from billplz';
|
|
|
|
protected $output = null;
|
|
|
|
protected $outputArray = [];
|
|
|
|
/** @var GetBillplzBill */
|
|
private $getBillplzBill;
|
|
|
|
/** @var FetchesTransaction */
|
|
private $fetchesTransaction;
|
|
|
|
/** @var UpdatesTransactionStatus */
|
|
private $updatesTransactionStatus;
|
|
|
|
/** @var UpdateDoFromVTPortalProcessor */
|
|
private $updateDoFromVTPortalProcessor;
|
|
|
|
/** @var UpdateDoFromYDPortalProcessor */
|
|
private $updateDoFromYDPortalProcessor ;
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(GetBillplzBill $getBillplzBill, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor, UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor)
|
|
{
|
|
parent::__construct();
|
|
$this->getBillplzBill = $getBillplzBill;
|
|
$this->fetchesTransaction = $fetchesTransaction;
|
|
$this->updatesTransactionStatus = $updatesTransactionStatus;
|
|
$this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor;
|
|
$this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor;
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
ini_set('memory_limit', '-1');
|
|
|
|
$this->info(Carbon::now() . ' : Fix failled callback from billplz cron started.');
|
|
$this->outputArray = [];
|
|
$start = new Carbon();
|
|
|
|
$approvalStatusArray = ApprovalStatus::APPROVAL_STATUS_ID;
|
|
|
|
$transactions = Transaction::where('type', TransactionType::PAYMENT)->where('payment_method', PaymentMethodType::PAYMENT_GATEWAY)->whereNotIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->get();
|
|
$totalTransactions = count($transactions);
|
|
$counter = 1;
|
|
$totalAmount = 0;
|
|
foreach ($transactions as $transaction){
|
|
$response = Http::withBasicAuth(config('billplz.api_key').':', '')->get(config('billplz.base_url').'/api/v3/bills/'.$transaction->payment_reference);
|
|
|
|
if($response->successful()){
|
|
$data = $response->json();
|
|
if($data['paid']){
|
|
$totalAmount += $transaction->amount;
|
|
$invoiceStatus = null;
|
|
|
|
switch($transaction->owner->status) {
|
|
case 3:
|
|
$invoiceStatus = 'Payment Completed';
|
|
break;
|
|
case 5:
|
|
$invoiceStatus = 'Dispute in progress';
|
|
break;
|
|
case 6:
|
|
$invoiceStatus = 'Cancelled Invoice';
|
|
break;
|
|
default:
|
|
$invoiceStatus = 'Pending Payment';
|
|
}
|
|
|
|
$order = $transaction->owner->owner->owner;
|
|
|
|
if (in_array($invoiceStatus , ['Pending Payment', 'Payment Completed'])) {
|
|
// code here
|
|
|
|
// $billPlz = $this->getBillplzBill->execute($billplzXSignatureObject->getBillPlzId());
|
|
|
|
// if(!$billPlz) throw new ResourceNotFoundException('Billplz bill not found.');
|
|
|
|
// $transaction = $this->fetchesTransaction->execute(['payment_reference' => $billplzXSignatureObject->getBillPlzId()]);
|
|
|
|
$invoice = $transaction->owner;
|
|
$packingList = $invoice->owner;
|
|
$order = $packingList->owner;
|
|
|
|
$status = ApprovalStatus::PENDING_VERIFICATION;
|
|
|
|
if($data['state'] === 'paid') {
|
|
$status = ApprovalStatus::APPROVED;
|
|
}
|
|
|
|
// if($data->state === 'due') {
|
|
// $status = $billplzXSignatureObject->getStatus() === 'failed' ? ApprovalStatus::REJECTED : ApprovalStatus::PENDING_VERIFICATION;
|
|
// }
|
|
|
|
// $token = Auth::fromUser(User::find(1));
|
|
// $request->headers->set('Authorization', 'Bearer '.$token);
|
|
|
|
$this->updatesTransactionStatus->execute($transaction, $status);
|
|
|
|
$totalPaidAmount = $invoice->transactions->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->sum('amount');
|
|
|
|
if(($invoice->amount - $totalPaidAmount) < 0.01) {
|
|
$this->updatesTransactionStatus->execute($invoice, ApprovalStatus::COMPLETED);
|
|
|
|
$packingList->status = ApprovalStatus::APPROVED;
|
|
$packingList->save();
|
|
|
|
if(app()->environment('production')){
|
|
$this->updateDoFromVTPortalProcessor->execute($packingList);
|
|
$this->updateDoFromYDPortalProcessor->execute($packingList);
|
|
}
|
|
|
|
$this->info('Updated invoice ' . $counter . ' of ' . $totalTransactions . '. Order: '. $order->reference . ' - Date: '.$transaction->owner->created_at->format('d-m-Y').' - Amount: '. $transaction->amount . '. Status: ' . $approvalStatusArray[$transaction->status] . '. Invoice Status: ' . $invoiceStatus);
|
|
} else {
|
|
$this->info('Failed to update invoice due to unsufficeint payment ' . $counter . ' of ' . $totalTransactions . '. Order: '. $order->reference . ' - Date: '.$transaction->owner->created_at->format('d-m-Y').' - Amount: '. $transaction->amount . '. Status: ' . $approvalStatusArray[$transaction->status] . '. Invoice Status: ' . $invoiceStatus);
|
|
}
|
|
|
|
|
|
} else {
|
|
if(!$order instanceof Order) {
|
|
$this->info("Error transaction id:" . $transaction->id . 'Type: ' . get_class($order));
|
|
} else {
|
|
$this->info($counter . ' of ' . $totalTransactions . '. Order: '. $order->reference . ' - Date: '.$transaction->owner->created_at->format('d-m-Y').' - Amount: '. $transaction->amount . '. Status: ' . $approvalStatusArray[$transaction->status] . '. Invoice Status: ' . $invoiceStatus);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
// $totalAmount += $transaction->amount;
|
|
// $this->appendToOutput($counter . ' of ' . $totalTransactions . '. Order: '. $transaction->owner->owner->owner->reference . ' - Date: '.$transaction->owner->created_at->format('d-m-Y').' - Amount: '. $transaction->amount);
|
|
}
|
|
}else{
|
|
$this->info("billplz error</br>");
|
|
}
|
|
$counter++;
|
|
}
|
|
|
|
$end = new Carbon();
|
|
$elapsedTime = $start->diff($end)->format('%H:%I:%S');
|
|
|
|
$this->info(Carbon::now() . ' : Done Billplz Failled Callback. ElapsedTime: ' . $elapsedTime . '. Total: ' . $totalAmount);
|
|
}
|
|
}
|