useVersion('v4'); if ($appEnvironment != 'production') { $client->useSandbox(); } $this->callbackUrl = route('pay.callback'); $this->redirectUrl = ['redirect_url' => route('pay.paymentStatus')]; $this->paymentTitle = "Pembayaran Ansuran"; $this->config = $billplzConfig; $this->client = $client; } public function getCollections() { return $this->client->collection()->all()->toArray(); } public function createBill(array $data) { $composed_data = [ $this->config['collection_id'], ...$data, $this->callbackUrl, $this->paymentTitle, $this->redirectUrl, ]; try{ $response = $this->client->bill()->create(...$composed_data)->toArray(); OnlineTransactionLoggerService::logs($response['id'], $composed_data, $response, TransactionOnlineStatus::BILL_CREATED); return $response; } catch(\Exception $e){ if($e->getCode() === 404){ $errorMessage = 'Please check your billplz payment gateway collection. Ensure that api_key, x_signature, and collection_id are correctly entered'; OnlineTransactionLoggerService::logs( null, $composed_data, $errorMessage, TransactionOnlineStatus::BILL_CREATED); throw new \ErrorException($errorMessage); } else { OnlineTransactionLoggerService::logs( null, $composed_data, $e->getMessage(), TransactionOnlineStatus::BILL_CREATED); }; throw $e; } } public function redirect($request) { try{ $response = $this->client->bill()->redirect($request); OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $response, TransactionOnlineStatus::UPDATE_TRANSACTION_REDIRECT); return $response; } catch(\Exception $e){ OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $e->getMessage(), TransactionOnlineStatus::UPDATE_TRANSACTION_REDIRECT); throw $e; } } public function webhook($request) { try { $response = $this->client->webhook($request); OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $response, TransactionOnlineStatus::UPDATE_TRANSACTION_CALLBACK); return $response; } catch (\Exception $e) { OnlineTransactionLoggerService::logs($request['billplz']['id'], $request, $e->getMessage(), TransactionOnlineStatus::UPDATE_TRANSACTION_CALLBACK); throw $e; } } public function getTransactions($date) { $arrahnTransactions = $this->getArrahnTransactions($date); $billplzID = collect($arrahnTransactions)->pluck('billplz_id')->unique()->values(); $billplzTransactions = collect($billplzID)->map(function ($billId) { return $this->client->transaction()->get($billId)->toArray(); }) ->map(function ($data) { $latestTransaction = reset($data['transactions']) == false ? [] : reset($data['transactions']); $billId = ['bill_id' => $data['bill_id']]; return array_merge($billId, $latestTransaction); }) ->keyBy('bill_id'); return $billplzTransactions; } public function getArrahnTransactions($date) { return Http::arrahn()->get('/api/transaction/online/query', ['date' => $date])->json(); } public function getBillplzTransactionReview($date) { $date = Carbon::now()->toDateString(); $arrahnTransactions = $this->getArrahnTransactions($date); // sort array by tarikh desc and masa key desc array_multisort(array_column($arrahnTransactions, 'tarikh'), SORT_DESC, array_column($arrahnTransactions, 'masa'), SORT_DESC, $arrahnTransactions); $arrahnTransactionsGroupedByBill = collect($arrahnTransactions)->groupBy('billplz_id'); $billplzID = $arrahnTransactionsGroupedByBill->keys(); $billplzTransactions = $this->getTransactions($date); $merged = $billplzID->map(function ($billId) use ($arrahnTransactionsGroupedByBill, $billplzTransactions) { return [ 'bill_id' => $billId, 'arrahn_transactions' => $arrahnTransactionsGroupedByBill->get($billId)->toArray(), 'billplz_latest_transaction' => $billplzTransactions->get($billId), ]; }); $summarizedBillplzTransactions = collect($merged)->map(function ($data) { $data['arrahn_transactions'] = collect($data['arrahn_transactions'])->map(function ($transaction) { return[ 'nokp' => $transaction['nokp'], 'norujukan' => $transaction['norujukan'], 'kodcaw' => $transaction['kodcaw'], 'total_payment' => $transaction['total_payment'], 'customer_name' => $transaction['customer_name'], 'status' => $transaction['status'], 'paid_at' => $transaction['paid_at'], 'tarikh_attempt_bayar' => $transaction['tarikh'], 'masa_attempt_bayar' => $transaction['masa'], 'portal' => $transaction['tagportal'] == 1 ? 'Arrahn Pay' : 'SPARE', ]; }); return $data; }); 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()); throw $th; } } }