mirror of
https://github.com/bitinflow/accounts.git
synced 2026-03-13 05:25:52 +00:00
Refactoring
This commit is contained in:
@@ -28,12 +28,15 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Bitinflow\\Accounts\\": "src/Accounts"
|
||||
"Bitinflow\\Accounts\\": "src/Accounts",
|
||||
"Bitinflow\\Payments\\": "src/Payments",
|
||||
"Bitinflow\\Support\\": "src/Support"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Bitinflow\\Accounts\\Tests\\": "tests/Accounts"
|
||||
"Bitinflow\\Accounts\\Tests\\": "tests/Accounts",
|
||||
"Bitinflow\\Payments\\Tests\\": "tests/Payments"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -20,4 +20,11 @@
|
||||
<directory suffix=".php">src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<php>
|
||||
<env name="BITINFLOW_ACCOUNTS_KEY" value="38"/>
|
||||
<env name="BITINFLOW_ACCOUNTS_SECRET" value="2goNRF8x37HPVZVaa28ySZGVXJuksvxnxM7TcGzM"/>
|
||||
<env name="BITINFLOW_PAYMENTS_BASE_URL" value="https://api.sandbox.pay.bitinflow.com/v1/"/>
|
||||
<env name="BITINFLOW_PAYMENTS_DASHBOARD_URL" value="https://sandbox.pay.bitinflow.com/v1/"/>
|
||||
<env name="CLIENT_ACCESS_TOKEN" value="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxNSIsImp0aSI6IjAxN2QxZDg0Y2MxNjAyZTYxOGZkNjYwZGViZWVjMDY4MTk2YmYzMDk1OGMzY2RiYzBjZmJkZWFjZjFhOTUxODQzZDU1YTk3OGY2YWIxY2YzIiwiaWF0IjoxNjY0NjIzMzA4LjQyODI4MSwibmJmIjoxNjY0NjIzMzA4LjQyODI4NCwiZXhwIjoxNjgwMzQ4MTA4LjQxNjc4MSwic3ViIjoiMzkiLCJzY29wZXMiOlsiYXBpIiwicmVhZF91c2VyIl0sImNsaWVudCI6eyJ0cnVzdGVkIjpmYWxzZX19.vxnzCaU4PpOrNVHa5AnGSS6gX_RCvxIERAnHFhjTrUzRafV9mr2Cvwd-BDVYoUr10wHvxa_TJSYfnAdDuhE-MEyDv13O3dL2XGTtJNa_Rg6L6CQ0JvC3wL-lWPvGPFax9pu-_lqbA3jm5B08hc3-7tq3f2nXcxjhtkqT6TTJv1-RCAppb2HCXiUDAqANzbhyInDjOH2WvFj1OGC_AI03J3W2KRWyeFLtUne8XKPFyr9XGcPuTrqogcuuXLeUt2kcf2bXbuIV1OlgIECrDiP1Ee0F2AzPs27ZVJ2z0R0JbT6AubKhGl5_Qi27cwjOH7hT2dmjcF1mLjzpN1uChLIdSnGSoStH8VzYHnHE2I8G-owW_aadG2UmGdnRY143q6g_28f3WIZNSucBSXkwFeS_t4fylsvpxhpjYJusf5qiEU_X3YbeawYMUCFUkSD2XTIypAqMJLNZQAeJ52eyL-9fln-Bv7n9v7K9G6ieR6Tm0tsJ1PRnaQi7rA1NTFwHoQmIOW9tfMycLzT7bgSoz3ra6Ez2J7ZNuWBZNKS0O-0YfSrAWyWK5U8YRfQuSVzP2VrIU63K6RGU2c284PfQGy11kgKUNQPykirb8p7MDQ8PwrxKaylBnD6hhDgjqEh2bfsr_43DfJA0R58L1HK3BmQnxgap0C77wK1e0yNlABpN28Q"/>
|
||||
</php>
|
||||
</phpunit>
|
||||
19
src/Accounts/ApiOperations/Validation.php
Normal file
19
src/Accounts/ApiOperations/Validation.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\ApiOperations;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresMissingParametersException;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
trait Validation
|
||||
{
|
||||
/**
|
||||
* @throws RequestRequiresMissingParametersException
|
||||
*/
|
||||
public function validateRequired(array $parameters, array $required)
|
||||
{
|
||||
if (!Arr::has($parameters, $required)) {
|
||||
throw RequestRequiresMissingParametersException::fromValidateRequired($parameters, $required);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use Bitinflow\Accounts\Exceptions\RequestRequiresClientIdException;
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresRedirectUriException;
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Bitinflow\Accounts\Traits;
|
||||
use Bitinflow\Support\Query;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
@@ -18,13 +19,10 @@ use Illuminate\Contracts\Auth\Authenticatable;
|
||||
*/
|
||||
class BitinflowAccounts
|
||||
{
|
||||
|
||||
use Traits\OauthTrait;
|
||||
use Traits\SshKeysTrait;
|
||||
use Traits\UsersTrait;
|
||||
|
||||
use Traits\HasBitinflowPaymentsWallet;
|
||||
|
||||
use ApiOperations\Delete;
|
||||
use ApiOperations\Get;
|
||||
use ApiOperations\Post;
|
||||
@@ -35,61 +33,51 @@ class BitinflowAccounts
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $cookie = 'bitinflow_token';
|
||||
public static string $cookie = 'bitinflow_token';
|
||||
|
||||
/**
|
||||
* Indicates if Bitinflow Accounts should ignore incoming CSRF tokens.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static $ignoreCsrfToken = false;
|
||||
public static bool $ignoreCsrfToken = false;
|
||||
|
||||
/**
|
||||
* Indicates if Bitinflow Accounts should unserializes cookies.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static $unserializesCookies = false;
|
||||
private static $baseUrl = 'https://accounts.bitinflow.com/api/';
|
||||
public static bool $unserializesCookies = false;
|
||||
|
||||
private static string $baseUrl = 'https://accounts.bitinflow.com/api/';
|
||||
|
||||
/**
|
||||
* Guzzle is used to make http requests.
|
||||
*
|
||||
* @var Client
|
||||
*/
|
||||
protected $client;
|
||||
protected Client $client;
|
||||
|
||||
/**
|
||||
* Paginator object.
|
||||
*
|
||||
* @var Paginator
|
||||
*/
|
||||
protected $paginator;
|
||||
protected Paginator $paginator;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts OAuth token.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $token = null;
|
||||
protected ?string $token = null;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts client id.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $clientId = null;
|
||||
protected ?string $clientId = null;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts client secret.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $clientSecret = null;
|
||||
protected ?string $clientSecret = null;
|
||||
|
||||
/**
|
||||
* bitinflow Accounts OAuth redirect url.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $redirectUri = null;
|
||||
protected ?string $redirectUri = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@@ -129,7 +117,7 @@ class BitinflowAccounts
|
||||
* @param string|null $cookie
|
||||
* @return string|static
|
||||
*/
|
||||
public static function cookie($cookie = null)
|
||||
public static function cookie(string $cookie = null)
|
||||
{
|
||||
if (is_null($cookie)) {
|
||||
return static::$cookie;
|
||||
@@ -268,7 +256,7 @@ class BitinflowAccounts
|
||||
* @return string|null
|
||||
* @throws RequestRequiresAuthenticationException
|
||||
*/
|
||||
public function getToken()
|
||||
public function getToken(): ?string
|
||||
{
|
||||
if (!$this->token) {
|
||||
throw new RequestRequiresAuthenticationException;
|
||||
@@ -323,7 +311,7 @@ class BitinflowAccounts
|
||||
* @param string $method HTTP method
|
||||
* @param string $path Query path
|
||||
* @param array $parameters Query parameters
|
||||
* @param Paginator $paginator Paginator object
|
||||
* @param Paginator|null $paginator Paginator object
|
||||
* @param mixed|null $jsonBody JSON data
|
||||
*
|
||||
* @return Result Result object
|
||||
@@ -332,13 +320,14 @@ class BitinflowAccounts
|
||||
*/
|
||||
public function query(string $method = 'GET', string $path = '', array $parameters = [], Paginator $paginator = null, $jsonBody = null): Result
|
||||
{
|
||||
/** @noinspection DuplicatedCode */
|
||||
if ($paginator !== null) {
|
||||
$parameters[$paginator->action] = $paginator->cursor();
|
||||
}
|
||||
try {
|
||||
$response = $this->client->request($method, $path, [
|
||||
'headers' => $this->buildHeaders($jsonBody ? true : false),
|
||||
'query' => $this->buildQuery($parameters),
|
||||
'headers' => $this->buildHeaders((bool)$jsonBody),
|
||||
'query' => Query::build($parameters),
|
||||
'json' => $jsonBody ?: null,
|
||||
]);
|
||||
$result = new Result($response, null, $paginator);
|
||||
@@ -401,26 +390,6 @@ class BitinflowAccounts
|
||||
$this->clientId = $clientId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build query with support for multiple smae first-dimension keys.
|
||||
*
|
||||
* @param array $query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function buildQuery(array $query): string
|
||||
{
|
||||
$parts = [];
|
||||
foreach ($query as $name => $value) {
|
||||
$value = (array)$value;
|
||||
array_walk_recursive($value, function ($value) use (&$parts, $name) {
|
||||
$parts[] = urlencode($name) . '=' . urlencode($value);
|
||||
});
|
||||
}
|
||||
|
||||
return implode('&', $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $parameters
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Accounts\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
class RequestRequiresMissingParametersException extends Exception
|
||||
{
|
||||
public function __construct($message = 'Request requires missing parameters', $code = 0, Exception $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
public static function fromValidateRequired(array $given, array $required): RequestRequiresMissingParametersException
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Request requires missing parameters. Required: %s. Given: %s',
|
||||
implode(', ', $required),
|
||||
implode(', ', $given)
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,17 @@ declare(strict_types=1);
|
||||
namespace Bitinflow\Accounts\Facades;
|
||||
|
||||
use Bitinflow\Accounts\BitinflowAccounts as BitinflowAccountsService;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
* @method static string|static cookie(string $cookie = null)
|
||||
* @method static Authenticatable actingAs($user, $scopes = [], $guard = 'api')
|
||||
* @method static static withClientId(string $clientId): self
|
||||
* @method static string getClientSecret(): string
|
||||
*/
|
||||
class BitinflowAccounts extends Facade
|
||||
{
|
||||
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return BitinflowAccountsService::class;
|
||||
|
||||
@@ -10,13 +10,13 @@ use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Helpers\JwtParser;
|
||||
use Bitinflow\Accounts\Contracts;
|
||||
use Bitinflow\Accounts\Repository;
|
||||
use Bitinflow\Payments\BitinflowPayments;
|
||||
use Illuminate\Auth\RequestGuard;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
@@ -41,6 +41,9 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
$this->app->singleton(BitinflowAccounts::class, function () {
|
||||
return new BitinflowAccounts;
|
||||
});
|
||||
$this->app->singleton(BitinflowPayments::class, function () {
|
||||
return new BitinflowPayments;
|
||||
});
|
||||
|
||||
$this->registerGuard();
|
||||
}
|
||||
@@ -85,6 +88,9 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return [BitinflowAccounts::class];
|
||||
return [
|
||||
BitinflowAccounts::class,
|
||||
BitinflowPayments::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,4 +262,9 @@ class Result
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
public function dump()
|
||||
{
|
||||
dump($this->data());
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Balance
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get balance from user.
|
||||
*
|
||||
* @return float|null
|
||||
*/
|
||||
public function get(): ?float
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->balance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deposit given amount from bank to account.
|
||||
*
|
||||
* @param float $amount
|
||||
* @param string $description
|
||||
* @return bool
|
||||
*/
|
||||
public function deposit(float $amount, string $decription): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('wallet/deposit', [
|
||||
'amount' => $amount,
|
||||
'decription' => $decription,
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge given amount from account.
|
||||
*
|
||||
* @param float $amount
|
||||
* @param string $decription
|
||||
* @return bool
|
||||
*/
|
||||
public function charge(float $amount, string $decription): bool
|
||||
{
|
||||
$order = $this->user->orders()->create([
|
||||
'name' => $decription,
|
||||
'description' => 'one-time payment',
|
||||
'amount' => 1,
|
||||
'price' => $amount,
|
||||
]);
|
||||
|
||||
return $this->user->orders()->checkout($order->id);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class CheckoutSessions
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function create(array $payload)
|
||||
{
|
||||
return $this->user->asPaymentsUser('POST', 'checkout-sessions', $payload);
|
||||
}
|
||||
|
||||
public function get(string $id)
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', sprintf('checkout-sessions/%s', $id));
|
||||
}
|
||||
|
||||
public function checkout(string $id)
|
||||
{
|
||||
return $this->user->asPaymentsUser('PUT', sprintf('checkout-sessions/%s/checkout', $id));
|
||||
}
|
||||
|
||||
public function revoke(string $id)
|
||||
{
|
||||
return $this->user->asPaymentsUser('PUT', sprintf('checkout-sessions/%s/revoke', $id));
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Orders
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get orders from user.
|
||||
*
|
||||
* @return object|null
|
||||
*/
|
||||
public function all(): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', 'orders');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @return object|null
|
||||
*/
|
||||
public function get(string $id): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', sprintf('orders/%s', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new order.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return object|false
|
||||
*/
|
||||
public function create(array $attributes = []): object|false
|
||||
{
|
||||
return $this->user->asPaymentsUser('POST', 'orders', $attributes)->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function checkout(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('orders/%s/checkout', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function revoke(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('orders/%s/revoke', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Subscriptions
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get subscriptions from user.
|
||||
*
|
||||
* @return object|null
|
||||
*/
|
||||
public function all(): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', 'subscriptions');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @return object|null
|
||||
*/
|
||||
public function get(string $id): ?object
|
||||
{
|
||||
return $this->user->asPaymentsUser('GET', sprintf('subscriptions/%s', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return object|false the subscription object
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function create(array $attributes): object|false
|
||||
{
|
||||
return $this->user->asPaymentsUser('POST', 'subscriptions', $attributes)->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name = 'default'): bool
|
||||
{
|
||||
$subscription = $this->get($name);
|
||||
|
||||
return $subscription && $subscription->status === 'settled' || $subscription && $subscription->resumeable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function checkout(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('subscriptions/%s/checkout', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function revoke(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('subscriptions/%s/revoke', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
public function resume(string $id): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('subscriptions/%s/resume', $id));
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Taxation
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get vat from user.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function get(): ?int
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->taxation->vat;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Accounts\Traits\BitinflowPaymentsWallet;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
class Wallets
|
||||
{
|
||||
public function __construct(protected User $user)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all wallets that belongs to the user.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function all(): ?array
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->wallets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has an active wallet.
|
||||
*
|
||||
* @return bool
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function has(): ?bool
|
||||
{
|
||||
return $this->user->getPaymentsUser()->data->has_wallet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default wallet to given wallet token.
|
||||
*
|
||||
* @param string $token default payment method token
|
||||
* @return bool
|
||||
*/
|
||||
public function setDefault(string $token): bool
|
||||
{
|
||||
$this->user->asPaymentsUser('PUT', sprintf('wallets/default', [
|
||||
'token' => $token
|
||||
]));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setupIntent()
|
||||
{
|
||||
return sprintf('%swallet/?continue_url=%s', config('bitinflow-accounts.payments.dashboard_url'), urlencode(url()->to($success_path)));
|
||||
}
|
||||
}
|
||||
19
src/Payments/Facades/BitinflowPayments.php
Normal file
19
src/Payments/Facades/BitinflowPayments.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Payments\Facades;
|
||||
|
||||
use Bitinflow\Payments\BitinflowPayments as BitinflowPaymentsService;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
class BitinflowPayments extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return BitinflowPaymentsService::class;
|
||||
}
|
||||
}
|
||||
10
src/Payments/Result.php
Normal file
10
src/Payments/Result.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Payments;
|
||||
|
||||
use Bitinflow\Accounts\Result as Base;
|
||||
|
||||
class Result extends Base
|
||||
{
|
||||
|
||||
}
|
||||
45
src/Payments/Traits/Balance.php
Normal file
45
src/Payments/Traits/Balance.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Payments\Traits;
|
||||
|
||||
use Bitinflow\Payments\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait Balance
|
||||
{
|
||||
/**
|
||||
* Get balance from user.
|
||||
*/
|
||||
public function getUser(): Result
|
||||
{
|
||||
return $this->query('GET', 'user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deposit given amount from bank to account.
|
||||
*/
|
||||
public function deposit(float $amount, string $description): Result
|
||||
{
|
||||
return $this->query('PUT', 'wallet/deposit', [], null, [
|
||||
'amount' => $amount,
|
||||
'description' => $description,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge given amount from account.
|
||||
*
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function charge(float $amount, string $description): bool
|
||||
{
|
||||
$result = $this->createOrder([
|
||||
'name' => $description,
|
||||
'description' => 'one-time payment',
|
||||
'amount' => 1,
|
||||
'price' => $amount,
|
||||
]);
|
||||
|
||||
return $this->checkoutOrder($result->data()->id);
|
||||
}
|
||||
}
|
||||
46
src/Payments/Traits/CheckoutSessions.php
Normal file
46
src/Payments/Traits/CheckoutSessions.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Payments\Traits;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresClientIdException;
|
||||
use Bitinflow\Payments\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait CheckoutSessions
|
||||
{
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function createCheckoutSession(array $parameters): Result
|
||||
{
|
||||
return $this->query('POST', 'checkout-sessions', [], null, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function getCheckoutSession(string $id): Result
|
||||
{
|
||||
return $this->query('GET', sprintf('checkout-sessions/%s', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function checkoutCheckoutSession(string $id): Result
|
||||
{
|
||||
return $this->query('PUT', sprintf('checkout-sessions/%s/checkout', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
*/
|
||||
public function revokeCheckoutSession(string $id): Result
|
||||
{
|
||||
return $this->query('PUT', sprintf('checkout-sessions/%s/revoke', $id));
|
||||
}
|
||||
}
|
||||
58
src/Payments/Traits/Orders.php
Normal file
58
src/Payments/Traits/Orders.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Payments\Traits;
|
||||
|
||||
use Bitinflow\Payments\Result;
|
||||
use Bitinflow\Payments\BitinflowPayments;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait Orders
|
||||
{
|
||||
/**
|
||||
* Get orders from user.
|
||||
*
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getOrders(): Result
|
||||
{
|
||||
return $this->query('GET', 'orders');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*/
|
||||
public function getOrder(string $id): Result
|
||||
{
|
||||
return $this->query('GET', sprintf('orders/%s', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new order.
|
||||
*
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function createOrder(array $parameters = []): Result
|
||||
{
|
||||
return $this->query('POST', 'orders', $parameters)->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkout given subscription.
|
||||
*
|
||||
* @param string $id
|
||||
*/
|
||||
public function checkoutOrder(string $id):Result
|
||||
{
|
||||
return $this->query('PUT', sprintf('orders/%s/checkout', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
*/
|
||||
public function revokeOrder(string $id):Result
|
||||
{
|
||||
return $this->query('PUT', sprintf('orders/%s/revoke', $id));
|
||||
}
|
||||
}
|
||||
82
src/Payments/Traits/Subscriptions.php
Normal file
82
src/Payments/Traits/Subscriptions.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Payments\Traits;
|
||||
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresClientIdException;
|
||||
use Bitinflow\Accounts\Exceptions\RequestRequiresMissingParametersException;
|
||||
use Bitinflow\Accounts\Helpers\Paginator;
|
||||
use Bitinflow\Payments\Result;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
trait Subscriptions
|
||||
{
|
||||
/**
|
||||
* Get subscriptions from user.
|
||||
*
|
||||
* @return object|null
|
||||
*/
|
||||
public function getSubscriptions(array $parameters = [], Paginator $paginator = null): Result
|
||||
{
|
||||
return $this->query('GET', 'subscriptions', $parameters, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
*/
|
||||
public function getSubscription(string $id): Result
|
||||
{
|
||||
return $this->query('GET', sprintf('subscriptions/%s', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new subscription.
|
||||
*
|
||||
* @param array $parameters array which requires following attributes:
|
||||
* name, description, period, price
|
||||
* and following attributes are optional:
|
||||
* vat, payload, ends_at, webhook_url, webhook_secret
|
||||
* @return object|false the subscription object
|
||||
* @throws GuzzleException
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws RequestRequiresMissingParametersException
|
||||
*/
|
||||
public function createSubscription(array $parameters): ?object
|
||||
{
|
||||
$this->validateRequired($parameters, ['name', 'description', 'period', 'price']);
|
||||
|
||||
return $this->query('POST', 'subscriptions', [], null, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Force given subscription to check out (trusted apps only).
|
||||
*
|
||||
* @param string $id
|
||||
* @return Result
|
||||
* @throws RequestRequiresClientIdException
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function checkoutSubscription(string $id): Result
|
||||
{
|
||||
return $this->query('PUT', sprintf('subscriptions/%s/checkout', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
*/
|
||||
public function revokeSubscription(string $id): Result
|
||||
{
|
||||
return $this->query('PUT', sprintf('subscriptions/%s/revoke', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume a running subscription.
|
||||
*
|
||||
* @param string $id
|
||||
*/
|
||||
public function resumeSubscription(string $id): Result
|
||||
{
|
||||
return $this->query('PUT', sprintf('subscriptions/%s/resume', $id));
|
||||
}
|
||||
}
|
||||
16
src/Payments/Traits/Taxation.php
Normal file
16
src/Payments/Traits/Taxation.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Payments\Traits;
|
||||
|
||||
use Bitinflow\Payments\Result;
|
||||
|
||||
trait Taxation
|
||||
{
|
||||
/**
|
||||
* Get vat from user.
|
||||
*/
|
||||
public function getTaxation(): Result
|
||||
{
|
||||
return $this->query('GET', 'taxation');
|
||||
}
|
||||
}
|
||||
34
src/Payments/Traits/Wallets.php
Normal file
34
src/Payments/Traits/Wallets.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Payments\Traits;
|
||||
|
||||
use Bitinflow\Payments\Result;
|
||||
use Bitinflow\Payments\BitinflowPayments;
|
||||
|
||||
trait Wallets
|
||||
{
|
||||
/**
|
||||
* Get all wallets that belong to the user.
|
||||
*/
|
||||
public function getWallets(): Result
|
||||
{
|
||||
return $this->query('GET', 'wallets');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default wallet to given wallet token.
|
||||
*
|
||||
* @param string $token default payment method token
|
||||
*/
|
||||
public function setDefaultWallet(string $token): Result
|
||||
{
|
||||
return $this->query('PUT', 'wallets/default', [], null, [
|
||||
'token' => $token
|
||||
]);
|
||||
}
|
||||
|
||||
public function getWalletSetupIntent(string $successUrl): string
|
||||
{
|
||||
return sprintf('%swallet/?continue_url=%s', config('bitinflow-accounts.payments.dashboard_url'), urlencode($successUrl));
|
||||
}
|
||||
}
|
||||
26
src/Support/Query.php
Normal file
26
src/Support/Query.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Bitinflow\Support;
|
||||
|
||||
class Query
|
||||
{
|
||||
/**
|
||||
* Build query with support for multiple same first-dimension keys.
|
||||
*
|
||||
* @param array $query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function build(array $query): string
|
||||
{
|
||||
$parts = [];
|
||||
foreach ($query as $name => $value) {
|
||||
$value = (array)$value;
|
||||
array_walk_recursive($value, function ($value) use (&$parts, $name) {
|
||||
$parts[] = urlencode($name) . '=' . urlencode($value);
|
||||
});
|
||||
}
|
||||
|
||||
return implode('&', $parts);
|
||||
}
|
||||
}
|
||||
26
tests/Payments/ApiOauthTest.php
Normal file
26
tests/Payments/ApiOauthTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Payments\Tests;
|
||||
|
||||
use Bitinflow\Accounts\Contracts\AppTokenRepository;
|
||||
use Bitinflow\Payments\Tests\TestCases\ApiTestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
class ApiOauthTest extends ApiTestCase
|
||||
{
|
||||
|
||||
public function testGetOauthToken(): void
|
||||
{
|
||||
$token = app(AppTokenRepository::class)->getAccessToken();
|
||||
|
||||
$this->getPaymentsClient()->withToken($this->getToken());
|
||||
$this->registerResult($result = $this->getPaymentsClient()->createSubscription([
|
||||
|
||||
]));
|
||||
$result->dump();
|
||||
}
|
||||
}
|
||||
80
tests/Payments/TestCases/ApiTestCase.php
Normal file
80
tests/Payments/TestCases/ApiTestCase.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Payments\Tests\TestCases;
|
||||
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Result;
|
||||
use Bitinflow\Payments\BitinflowPayments;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
abstract class ApiTestCase extends TestCase
|
||||
{
|
||||
protected static $rateLimitRemaining = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
if ($this->getAccountsBaseUrl()) {
|
||||
BitinflowAccounts::setBaseUrl($this->getAccountsBaseUrl());
|
||||
}
|
||||
if ($this->getPaymentsBaseUrl()) {
|
||||
BitinflowPayments::setBaseUrl($this->getPaymentsBaseUrl());
|
||||
}
|
||||
|
||||
if (!$this->getClientId()) {
|
||||
$this->markTestSkipped('No Client-ID given');
|
||||
}
|
||||
if (self::$rateLimitRemaining !== null && self::$rateLimitRemaining < 3) {
|
||||
$this->markTestSkipped('Rate Limit exceeded (' . self::$rateLimitRemaining . ')');
|
||||
}
|
||||
$this->getAccountsClient()->setClientId($this->getClientId());
|
||||
$this->getPaymentsClient()->setClientId($this->getClientId());
|
||||
}
|
||||
|
||||
protected function registerResult(Result $result): Result
|
||||
{
|
||||
self::$rateLimitRemaining = $result->rateLimit('remaining');
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function getAccountsBaseUrl()
|
||||
{
|
||||
return getenv('ACCOUNTS_BASE_URL');
|
||||
}
|
||||
|
||||
protected function getPaymentsBaseUrl()
|
||||
{
|
||||
return getenv('PAYMENTS_BASE_URL');
|
||||
}
|
||||
|
||||
protected function getClientId()
|
||||
{
|
||||
return getenv('CLIENT_ID');
|
||||
}
|
||||
|
||||
protected function getClientSecret()
|
||||
{
|
||||
return getenv('CLIENT_KEY');
|
||||
}
|
||||
|
||||
protected function getToken()
|
||||
{
|
||||
return getenv('CLIENT_ACCESS_TOKEN');
|
||||
}
|
||||
|
||||
public function getPaymentsClient(): BitinflowPayments
|
||||
{
|
||||
return app(BitinflowPayments::class);
|
||||
}
|
||||
|
||||
public function getAccountsClient(): BitinflowAccounts
|
||||
{
|
||||
return app(BitinflowAccounts::class);
|
||||
}
|
||||
}
|
||||
30
tests/Payments/TestCases/TestCase.php
Normal file
30
tests/Payments/TestCases/TestCase.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitinflow\Payments\Tests\TestCases;
|
||||
|
||||
use Bitinflow\Accounts\BitinflowAccounts;
|
||||
use Bitinflow\Accounts\Providers\BitinflowAccountsServiceProvider;
|
||||
use Orchestra\Testbench\TestCase as BaseTestCase;
|
||||
|
||||
/**
|
||||
* @author René Preuß <rene@preuss.io>
|
||||
*/
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return [
|
||||
BitinflowAccountsServiceProvider::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getPackageAliases($app)
|
||||
{
|
||||
return [
|
||||
'BitinflowAccounts' => BitinflowAccounts::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user