mirror of
https://github.com/bitinflow/accounts.git
synced 2026-03-19 08:25:52 +00:00
change namespace and cleanup code
This commit is contained in:
197
src/Accounts/Traits/HasBitinflowPaymentsWallet.php
Normal file
197
src/Accounts/Traits/HasBitinflowPaymentsWallet.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
|
||||
/**
|
||||
* @property string access_token todo: can we get this from HasBitinflowTokens ?
|
||||
* @property PendingRequest $paymentsGatewayUser
|
||||
*/
|
||||
trait HasBitinflowPaymentsWallet
|
||||
{
|
||||
protected $paymentsUser = null;
|
||||
|
||||
/**
|
||||
* Check if user has an active wallet.
|
||||
*
|
||||
* @return bool
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function hasWallet(): bool
|
||||
{
|
||||
return $this->getPaymentsUser()->data->has_wallet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user from payments gateway.
|
||||
*
|
||||
* @return object|null
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getPaymentsUser(): ?object
|
||||
{
|
||||
if (is_null($this->paymentsUser)) {
|
||||
$this->paymentsUser = $this->paymentsGatewayRequest('GET', 'user');
|
||||
}
|
||||
|
||||
return $this->paymentsUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new payment gateway request.
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
private function paymentsGatewayRequest(string $method, string $url, array $attributes = []): mixed
|
||||
{
|
||||
$client = new Client([
|
||||
'base_uri' => 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->access_token),
|
||||
],
|
||||
]);
|
||||
|
||||
return json_decode($response->getBody());
|
||||
}
|
||||
|
||||
public function getWalletSetupIntent(string $success_path = ''): string
|
||||
{
|
||||
return sprintf('%swallet?continue_url=%s', config('bitinflow-accounts.payments.dashboard_url'), url($success_path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get balance from user.
|
||||
*
|
||||
* @return float
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getBalance(): float
|
||||
{
|
||||
return $this->getPaymentsUser()->data->balance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get vat from user.
|
||||
*
|
||||
* @return int|null
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getVat(): ?int
|
||||
{
|
||||
return $this->getPaymentsUser()->data->taxation->vat;
|
||||
}
|
||||
|
||||
public function hasSubscribed($name = 'default'): bool
|
||||
{
|
||||
$subscription = $this->getSubscription($name);
|
||||
|
||||
return $subscription && $subscription->status === 'settled' || $subscription && $subscription->resumeable;
|
||||
}
|
||||
|
||||
public function getSubscription($name = 'default'): ?object
|
||||
{
|
||||
foreach ($this->getSubscriptions() as $subscription) {
|
||||
if (isset($subscription->payload->name) && $subscription->payload->name === $name) {
|
||||
return $subscription;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get vat from user.
|
||||
*
|
||||
* @return array|null
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getSubscriptions(): ?array
|
||||
{
|
||||
$subscriptions = $this->getPaymentsUser()->data->subscriptions;
|
||||
|
||||
foreach ($subscriptions as $key => $subscription) {
|
||||
if (!isset($subscription->payload->client_id) || $subscription->payload->client_id !== config('bitinflow-accounts.client_id')) {
|
||||
unset($subscriptions[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $subscriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new subscription.
|
||||
*
|
||||
* @param array $attributes array which requires following attributes:
|
||||
* name, description, period, price
|
||||
* and following attributes are optional:
|
||||
* vat, payload, ends_at, webhook_url, webhook_secret
|
||||
* @param array $payload optional data that is stored in the subscription
|
||||
* @param bool $checkout optional checkout it directly
|
||||
* @return object the subscription object
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function createSubscription(string $name, array $attributes, array $payload = [], bool $checkout = false): object
|
||||
{
|
||||
$client = [
|
||||
'name' => $name,
|
||||
'client_id' => config('bitinflow-accounts.client_id')
|
||||
];
|
||||
$defaults = ['period' => 'monthly'];
|
||||
$attributes = array_merge(array_merge($defaults, $attributes), [
|
||||
'payload' => array_merge($payload, $client),
|
||||
'checkout' => $checkout
|
||||
]);
|
||||
|
||||
return $this->paymentsGatewayRequest('POST', 'subscriptions', $attributes)->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function checkoutSubscription(string $id): void
|
||||
{
|
||||
$this->paymentsGatewayRequest('PUT', sprintf('subscriptions/%s/checkout', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a running subscription.
|
||||
*
|
||||
* @param $id
|
||||
* @return void
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function revokeSubscription($id): void
|
||||
{
|
||||
$this->paymentsGatewayRequest('PUT', sprintf('subscriptions/%s/revoke', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume a running subscription.
|
||||
*
|
||||
* @param $id
|
||||
* @return void
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function resumeSubscription($id): void
|
||||
{
|
||||
$this->paymentsGatewayRequest('PUT', sprintf('subscriptions/%s/resume', $id));
|
||||
}
|
||||
}
|
||||
51
src/Accounts/Traits/HasBitinflowTokens.php
Normal file
51
src/Accounts/Traits/HasBitinflowTokens.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use stdClass;
|
||||
|
||||
trait HasBitinflowTokens
|
||||
{
|
||||
/**
|
||||
* The current access token for the authentication user.
|
||||
*
|
||||
* @var stdClass
|
||||
*/
|
||||
protected $accessToken;
|
||||
|
||||
/**
|
||||
* Get the current access token being used by the user.
|
||||
*
|
||||
* @return stdClass|null
|
||||
*/
|
||||
public function bitinflowToken(): ?stdClass
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the current API token has a given scope.
|
||||
*
|
||||
* @param string $scope
|
||||
* @return bool
|
||||
*/
|
||||
public function bitinflowTokenCan(string $scope): bool
|
||||
{
|
||||
$scopes = $this->accessToken ? $this->accessToken->scopes : [];
|
||||
|
||||
return in_array('*', $scopes) || in_array($scope, $this->accessToken->scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current access token for the user.
|
||||
*
|
||||
* @param stdClass $accessToken
|
||||
* @return $this
|
||||
*/
|
||||
public function withBitinflowAccessToken(stdClass $accessToken): self
|
||||
{
|
||||
$this->accessToken = $accessToken;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
44
src/Accounts/Traits/OauthTrait.php
Normal file
44
src/Accounts/Traits/OauthTrait.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use Bitinflow\Accounts\Result;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
trait OauthTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* Retrieving a oauth token using a given grant type.
|
||||
*
|
||||
* @param string $grantType
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function retrievingToken(string $grantType, array $attributes): Result
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('POST', '/oauth/token', [
|
||||
'form_params' => $attributes + [
|
||||
'grant_type' => $grantType,
|
||||
'client_id' => $this->getClientId(),
|
||||
'client_secret' => $this->getClientSecret(),
|
||||
],
|
||||
]);
|
||||
|
||||
$result = new Result($response, null);
|
||||
} catch (RequestException $exception) {
|
||||
$result = new Result($exception->getResponse(), $exception);
|
||||
}
|
||||
|
||||
$result->bitinflow = $this;
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
56
src/Accounts/Traits/SshKeysTrait.php
Normal file
56
src/Accounts/Traits/SshKeysTrait.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use Bitinflow\Accounts\ApiOperations\Delete;
|
||||
use Bitinflow\Accounts\ApiOperations\Get;
|
||||
use Bitinflow\Accounts\ApiOperations\Post;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
trait SshKeysTrait
|
||||
{
|
||||
|
||||
use Get, Post, Delete;
|
||||
|
||||
/**
|
||||
* Get currently authed user with Bearer Token
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function getSshKeysByUserId(int $id): Result
|
||||
{
|
||||
return $this->get("users/$id/keys/json", [], null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates ssh key for the currently authed user
|
||||
*
|
||||
* @param string $publicKey
|
||||
* @param string|null $name
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function createSshKey(string $publicKey, string $name = null): Result
|
||||
{
|
||||
return $this->post('ssh-keys', [
|
||||
'public_key' => $publicKey,
|
||||
'name' => $name,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a given ssh key for the currently authed user
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function deleteSshKey(int $id): Result
|
||||
{
|
||||
return $this->delete("ssh-keys/$id", []);
|
||||
}
|
||||
}
|
||||
50
src/Accounts/Traits/UsersTrait.php
Normal file
50
src/Accounts/Traits/UsersTrait.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Accounts\Traits;
|
||||
|
||||
use Bitinflow\Accounts\ApiOperations\Get;
|
||||
use Bitinflow\Accounts\Result;
|
||||
|
||||
trait UsersTrait
|
||||
{
|
||||
|
||||
use Get;
|
||||
|
||||
/**
|
||||
* Get currently authed user with Bearer Token
|
||||
*
|
||||
* @return Result Result object
|
||||
*/
|
||||
public function getAuthedUser(): Result
|
||||
{
|
||||
return $this->get('users/me');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new user on behalf of the current user.
|
||||
*
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function createUser(array $parameters): Result
|
||||
{
|
||||
return $this->post('v2/users', $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given email exists.
|
||||
*
|
||||
* @param string $email
|
||||
*
|
||||
* @return Result
|
||||
*/
|
||||
public function isEmailExisting(string $email): Result
|
||||
{
|
||||
return $this->post('v2/users/check-email', [
|
||||
'email' => $email,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user