client = new GuzzleClient([ 'headers' => [ 'Authorization' => 'Bearer ' . config('laravel-payku.public_token'), 'User-Agent' => 'sebacarrasco93/laravel-payku' ], 'base_uri' => $this->apiRoute() . '/' ]); $this->hasValidConfig(); } public function apiRoute() { if (config('laravel-payku.base_url')) { return config('laravel-payku.base_url'); } if (config('laravel-payku.environment') == 'production') { return self::URL_API_PROD; } if (config('app.env') == 'production') { return self::URL_API_PROD; } return self::URL_API_DEV; } public function findEnvKeys() { $found = []; foreach ($this->minimumApiKeys as $key) { $found[$key] = config('laravel-payku')[$key]; } return array_filter($found); } public function hasValidConfig() { $count = count($this->minimumApiKeys); if (count($this->findEnvKeys()) == $count) { return true; } throw new MissingEnvKeys(); } public function postApi(string $transaction_id, string $subject, int $amount_clp, string $email) { $body = $this->client->request('POST', 'transaction', [ 'json' => $this->prepareOrder($transaction_id, $subject, $amount_clp, $email), ])->getBody(); return json_decode($body, true); } public function getApi(PaykuTransaction $transaction) { $body = $this->client->request('GET', 'transaction/' . $transaction->id)->getBody(); return json_decode($body, true); } public function handleAPIResponse($response) { if (! in_array($response['status'], $this->allowedTransactionsStatuses)) { throw new InvalidResponseStatus($response); } $this->hasValidResponse = true; // $this->status = $response['status']; foreach ($response as $key => $value) { $this->$key = $value; } } public function saveAPIResponse($response, $transaction_id = null) { $response = collect($response); $this->handleAPIResponse($response); // dd($response); if ($transaction_id) { // Creating... $response['order'] = $transaction_id; } $firstResponse = $response->except('payment', 'gateway_response')->toArray(); if ($firstResponse['status'] === 'failed') { throw new CantCreateTransactionId($response); } $transaction = PaykuTransaction::updateOrCreate(['id' => $response['id']], $firstResponse); if (isset($response['payment'])) { $payment = collect($response['payment']); if ($payment->count()) { $transaction->payment()->create($payment->toArray()); } } } public function create(string $transaction_id, string $subject, int $amount_clp, string $email) { $response = $this->postApi($transaction_id, $subject, $amount_clp, $email); $database = $this->saveAPIResponse($response, $transaction_id); return redirect()->away($response['url']); } public function return($order) { $found = PaykuTransaction::whereOrder($order)->firstOrFail(); $response = $this->getApi($found); $this->saveAPIResponse($response); return redirect()->route('payku.notify', $order); } public function notify($order) { return PaykuTransaction::whereOrder($order) ->firstOrFail(); } public function findById($id) { return PaykuTransaction::whereId($id) ->firstOrFail(); } public function hasStatus(string $id, string $status) { return $this->findById($id)->status === $status; } public function hasStatusSuccess(string $id) { return $this->hasStatus($id, 'success'); } public function hasStatusPending(string $id) { return $this->hasStatus($id, 'pending'); } }