diff --git a/app/Console/Commands/updateUnsuccess.php b/app/Console/Commands/updateUnsuccess.php index 3b7b791..52ec140 100644 --- a/app/Console/Commands/updateUnsuccess.php +++ b/app/Console/Commands/updateUnsuccess.php @@ -2,22 +2,11 @@ namespace App\Console\Commands; -use Illuminate\Http\Request; +use App\Services\BillplzService; use Illuminate\Support\Facades\Http; -use Session; -use Illuminate\Support\Facades\Auth; - -use Billplz\Client; -use Http\Client\Common\HttpMethodsClient; -use Http\Adapter\Guzzle6\Client as GuzzleHttpClient; -use Http\Message\MessageFactory\GuzzleMessageFactory; -use Money\Money; - -use Carbon\Carbon; - use Illuminate\Console\Command; -class updateUnsuccess extends Command +class UpdateUnsuccess extends Command { /** * The name and signature of the console command. @@ -33,61 +22,39 @@ class updateUnsuccess extends Command */ protected $description = 'Command description'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - if(config('app.env') != 'production'){ - $this->api_key = 'abecfd0c-98d8-4635-abc2-2fcba8c60c11'; - $this->XSignature_Key = 'S-GWr7z03XjAhqPsFfu_H5nQ'; - $this->collection_id = 'z0r4kjhi'; - }else{ - $this->api_key = 'f3e82fe1-a422-4660-97f0-65cc59ff6c47'; - $this->XSignature_Key = 'S-alZUbzmD2_TF3vmrRqgehQ'; - } - } - /** * Execute the console command. * * @return int */ - public function handle() + public function handle(BillplzService $billplzService) { - $http = new HttpMethodsClient( - new GuzzleHttpClient(), - new GuzzleMessageFactory() - ); - - $billplz = new Client($http, $this->api_key); - if(config('app.env') != 'production'){ - $billplz->useSandbox(); - } - $billplz->useVersion('v4'); - - $transaction = $billplz->bill(); - - $list_transaction = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/transaction/online/unsuccessful')->json(); - - if($list_transaction){ - foreach($list_transaction as $index=>$list){ - $show_transaction = $transaction->transaction($list['billplz_id']); - $data = $show_transaction->toArray(); - - if(!empty($data['transactions'])){ - if($data['transactions'][0]['status'] == 'completed'){ - Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/transaction/online/unsuccesfull/update',[ - 'billplz_id' => $data['bill_id'], - 'status' => 'PAID', - 'paid_at' => $data['transactions'][0]['completed_at'] - ]); - } - } - } + + $getFailedOnlineTransaction = fn() => Http::arrahn()->get('/api/transaction/online/unsuccessful')->json(); + + $FailedBillplzID = array_map(fn($x) => $x['billplz_id'], $getFailedOnlineTransaction()); + + if(sizeof($FailedBillplzID) < 0){ + return; + } + + $billplzUpdatedTransactionStatus = array_map(fn($x) => $billplzService->getTransactionByBillId($x), $FailedBillplzID); + + $getCompletedBillplzTransaction = function() use($billplzUpdatedTransactionStatus){ + + $dataNotNull = array_filter($billplzUpdatedTransactionStatus, fn($x) => $x !== null); + + $dataHaveTransaction = array_filter($dataNotNull, fn($x) => sizeof($x['transactions']) > 0); + + return array_filter($dataHaveTransaction, fn($x) => $x['transactions'][0]['status'] == 'completed'); + }; + + foreach($getCompletedBillplzTransaction() as $transaction){ + Http::arrahn()->put('/api/transaction/online/unsuccesfull/update',[ + 'billplz_id' => $transaction['bill_id'], + 'status' => 'PAID', + 'paid_at' => $transaction['transactions'][0]['completed_at'] + ]); } } } diff --git a/app/Services/BillplzService.php b/app/Services/BillplzService.php index 283c45b..1447b6a 100644 --- a/app/Services/BillplzService.php +++ b/app/Services/BillplzService.php @@ -119,4 +119,14 @@ class BillplzService return $summarizedBillplzTransactions; } + + public function getTransactionByBillId($billId) + { + try { + return $this->client->bill()->transaction($billId)->toArray(); + } catch (\Throwable $th) { + logger()->error('Error when querying billplz id : ' . $billId); + logger()->error($th->getMessage()); + } + } } diff --git a/resources/views/includes/home.blade.php b/resources/views/includes/home.blade.php index a21ef07..ef08b1f 100644 --- a/resources/views/includes/home.blade.php +++ b/resources/views/includes/home.blade.php @@ -83,7 +83,7 @@ @endif
- @if (request()->has('filter') && request()->get('filter') == 'pembiayaan-akan-dilelong') + @if (request()->get('filter') == 'pembiayaan-akan-dilelong') @@ -100,7 +100,7 @@
- @if (request()->has('filter') && request()->get('filter') == 'pembiayaan-akan-dilelong') + @if (request()->get('filter') == 'pembiayaan-akan-dilelong') @include('includes.partials.table-pembiayaan-akan-dilelong') @else @include('includes.partials.table-pembiayaan')