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{ return $this->client->bill()->create(...$composed_data)->toArray(); } catch(\Exception $e){ if($e->getCode() === 404){ throw new \ErrorException('Please check your billplz payment gateway collection. Ensure that api_key, x_signature, and collection_id are correctly entered'); }; } } public function redirect($request) { return $this->client->bill()->redirect($request); } public function webhook($request) { return $this->client->webhook($request); } 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) { $arrahnTransactions = $this->getArrahnTransactions($date); $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'], '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()); } } }