diff --git a/src/Accounts/Traits/HasBitinflowPaymentsWallet.php b/src/Accounts/Traits/HasBitinflowPaymentsWallet.php deleted file mode 100644 index 8c3a8d5..0000000 --- a/src/Accounts/Traits/HasBitinflowPaymentsWallet.php +++ /dev/null @@ -1,99 +0,0 @@ - config('bitinflow-accounts.payments.base_url'), - ]); - - $response = $client->request($method, $url, [ - RequestOptions::JSON => $attributes, - RequestOptions::HEADERS => [ - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', - 'Authorization' => sprintf('Bearer %s', $this->getAttribute(config('auth.providers.sso-users.access_token_field'))), - ], - ]); - - return json_decode($response->getBody()); - } - - /** - * Get user from payments gateway. - * - * @return object|null - * @throws GuzzleException - */ - public function getPaymentsUser(): ?object - { - if (is_null($this->paymentsUser)) { - $this->paymentsUser = $this->asPaymentsUser('GET', 'user'); - } - - return $this->paymentsUser; - } - - public function getBalanceAttribute(): Balance - { - return new Balance($this); - } - - public function getCheckoutSessionsAttribute(): CheckoutSessions - { - return new CheckoutSessions($this); - } - - public function getOrdersAttribute(): Orders - { - return new Orders($this); - } - - public function getSubscriptionsAttribute(): Subscriptions - { - return new Subscriptions($this); - } - - public function getTaxationAttribute(): Taxation - { - return new Taxation($this); - } - - public function getWalletsAttribute(): Wallets - { - return new Wallets($this); - } -} diff --git a/src/Payments/BitinflowPayments.php b/src/Payments/BitinflowPayments.php new file mode 100644 index 0000000..82a1353 --- /dev/null +++ b/src/Payments/BitinflowPayments.php @@ -0,0 +1,260 @@ +setClientId($clientId); + } + if ($clientSecret = config('bitinflow-accounts.client_secret')) { + $this->setClientSecret($clientSecret); + } + if ($redirectUri = config('bitinflow-accounts.payments.base_url')) { + self::setBaseUrl($redirectUri); + } + + $this->client = new Client([ + 'base_uri' => self::$baseUrl, + ]); + } + + /** + * @param string $baseUrl + * + * @internal only for internal and debug purposes. + */ + public static function setBaseUrl(string $baseUrl): void + { + self::$baseUrl = $baseUrl; + } + + /** + * Get OAuth token. + * + * @return string bitinflow Accounts token + * @return string|null + * @throws RequestRequiresAuthenticationException + */ + public function getToken(): ?string + { + if (!$this->token) { + throw new RequestRequiresAuthenticationException; + } + + return $this->token; + } + + /** + * Set OAuth token. + * + * @param string $token bitinflow Accounts OAuth token + * + * @return void + */ + public function setToken(string $token): void + { + $this->token = $token; + } + + /** + * Fluid OAuth token setter. + * + * @param string $token bitinflow Accounts OAuth token + * + * @return self + */ + public function withToken(string $token): self + { + $this->setToken($token); + + return $this; + } + + /** + * Build query & execute. + * + * @param string $method HTTP method + * @param string $path Query path + * @param array $parameters Query parameters + * @param Paginator|null $paginator Paginator object + * @param mixed|null $jsonBody JSON data + * + * @return Result Result object + * @throws GuzzleException + * @throws RequestRequiresClientIdException + */ + public function query(string $method = 'GET', string $path = '', array $parameters = [], Paginator $paginator = null, $jsonBody = null): Result + { + if ($paginator !== null) { + $parameters[$paginator->action] = $paginator->cursor(); + } + try { + $response = $this->client->request($method, $path, [ + 'headers' => $this->buildHeaders((bool)$jsonBody), + 'query' => Query::build($parameters), + 'json' => $jsonBody ?: null, + ]); + $result = new Result($response, null, $paginator); + } catch (RequestException $exception) { + $result = new Result($exception->getResponse(), $exception, $paginator); + } + $result->bitinflow = $this; + + return $result; + } + /** + * Build headers for request. + * + * @param bool $json Body is JSON + * + * @return array + * @throws RequestRequiresClientIdException + */ + private function buildHeaders(bool $json = false): array + { + $headers = [ + 'Client-ID' => $this->getClientId(), + 'Accept' => 'application/json', + ]; + if ($this->token) { + $headers['Authorization'] = 'Bearer ' . $this->token; + } + if ($json) { + $headers['Content-Type'] = 'application/json'; + } + + return $headers; + } + + /** + * Get client id. + * + * @return string + * @throws RequestRequiresClientIdException + */ + public function getClientId(): string + { + if (!$this->clientId) { + throw new RequestRequiresClientIdException; + } + + return $this->clientId; + } + + /** + * Set client id. + * + * @param string $clientId bitinflow Accounts client id + * + * @return void + */ + public function setClientId(string $clientId): void + { + $this->clientId = $clientId; + } + + /** + * Fluid client id setter. + * + * @param string $clientId bitinflow Accounts client id. + * + * @return self + */ + public function withClientId(string $clientId): self + { + $this->setClientId($clientId); + + return $this; + } + + /** + * Get client secret. + * + * @return string + * @throws RequestRequiresClientIdException + */ + public function getClientSecret(): string + { + if (!$this->clientSecret) { + throw new RequestRequiresClientIdException; + } + + return $this->clientSecret; + } + + /** + * Set client secret. + * + * @param string $clientSecret bitinflow Accounts client secret + * + * @return void + */ + public function setClientSecret(string $clientSecret): void + { + $this->clientSecret = $clientSecret; + } + + /** + * Fluid client secret setter. + * + * @param string $clientSecret bitinflow Accounts client secret + * + * @return self + */ + public function withClientSecret(string $clientSecret): self + { + $this->setClientSecret($clientSecret); + + return $this; + } + + public function getBaseUrl() + { + return self::$baseUrl; + } +} \ No newline at end of file