add bitinflow payments subscriptions

This commit is contained in:
2022-05-08 22:53:02 +02:00
parent b5b4f9cf5e
commit 032c771e49
14 changed files with 274 additions and 403 deletions

61
AUTH.md Normal file
View File

@@ -0,0 +1,61 @@
# Implementing Auth
This method should typically be called in the `boot` method of your `AuthServiceProvider` class:
```php
use GhostZero\BitinflowAccounts\BitinflowAccounts;
use GhostZero\BitinflowAccounts\Providers\BitinflowAccountsSsoUserProvider;
use Illuminate\Http\Request;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
Auth::provider('sso-users', function ($app, array $config) {
return new BitinflowAccountsSsoUserProvider(
$app->make(BitinflowAccounts::class),
$app->make(Request::class),
$config['model'],
$config['fields'] ?? [],
$config['assess_token_field'] ?? null
);
});
}
```
reference the guard in the `guards` configuration of your `auth.php` configuration file:
```php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'bitinflow-accounts',
'provider' => 'sso-users',
],
],
```
reference the provider in the `providers` configuration of your `auth.php` configuration file:
```php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'sso-users' => [
'driver' => 'sso-users',
'model' => App\Models\User::class,
'fields' => ['first_name', 'last_name', 'email'],
'assess_token_field' => 'sso_access_token',
],
],
```

View File

@@ -56,7 +56,7 @@ protected $listen = [
Copy configuration to config folder: Copy configuration to config folder:
``` ```
$ php artisan vendor:publish --provider="GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider" $ bitinflow-accounts
``` ```
Add environmental variables to your `.env` Add environmental variables to your `.env`
@@ -149,22 +149,6 @@ BitinflowAccounts::withClientId('abc123')->withToken('abcdef123456')->getAuthedU
## Documentation ## Documentation
### Charges
```php
public function createCharge(array $parameters)
public function getCharge(string $id)
public function updateCharge(string $id, array $parameters)
public function captureCharge(string $id, array $parameters = array ())
```
### Documents
```php
public function createDocument(array $parameters)
public function createDocumentDownloadUrl(string $identifier, CarbonInterface $expiresAt = NULL)
```
### Oauth ### Oauth
```php ```php

View File

@@ -1,8 +0,0 @@
<?php
return [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI'),
'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URI'),
];

View File

@@ -0,0 +1,12 @@
<?php
return [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
'redirect_url' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI'),
'base_url' => env('BITINFLOW_ACCOUNTS_BASE_URL'),
'payments' => [
'base_url' => env('BITINFLOW_PAYMENTS_BASE_URL', 'https://api.pay.bitinflow.com/v1/'),
'dashboard_url' => env('BITINFLOW_PAYMENTS_DASHBOARD_URL', 'https://pay.bitinflow.com/v1/'),
]
];

View File

@@ -18,13 +18,12 @@ use GuzzleHttp\Exception\RequestException;
class BitinflowAccounts class BitinflowAccounts
{ {
use Traits\ChargesTrait;
use Traits\DocumentsTrait;
use Traits\OauthTrait; use Traits\OauthTrait;
use Traits\PaymentIntentsTrait;
use Traits\SshKeysTrait; use Traits\SshKeysTrait;
use Traits\UsersTrait; use Traits\UsersTrait;
use Traits\HasBitinflowPaymentsWallet;
use ApiOperations\Delete; use ApiOperations\Delete;
use ApiOperations\Get; use ApiOperations\Get;
use ApiOperations\Post; use ApiOperations\Post;

View File

@@ -1,17 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Enums;
/**
* @author René Preuß <rene@preuss.io>
*/
class DocumentType
{
// Read authorized user´s email address.
public const TYPE_PDF_INVOICE = 'pdf.invoice';
// Manage a authorized user object.
public const TYPE_PDF_ORDER = 'pdf.order';
}

View File

@@ -22,7 +22,7 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
public function boot() public function boot()
{ {
$this->publishes([ $this->publishes([
dirname(__DIR__) . '/../../../config/bitinflow-accounts-api.php' => config_path('bitinflow-accounts-api.php'), dirname(__DIR__, 4) . '/config/bitinflow-accounts.php' => config_path('bitinflow-accounts.php'),
], 'config'); ], 'config');
} }
@@ -32,9 +32,7 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
$this->mergeConfigFrom( $this->mergeConfigFrom(dirname(__DIR__, 4) . '/config/bitinflow-accounts.php', 'bitinflow-accounts');
dirname(__DIR__) . '/../../../config/bitinflow-accounts-api.php', 'bitinflow-accounts-api'
);
$this->app->singleton(BitinflowAccounts::class, function () { $this->app->singleton(BitinflowAccounts::class, function () {
return new BitinflowAccounts; return new BitinflowAccounts;
}); });

View File

@@ -1,69 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Traits;
use GhostZero\BitinflowAccounts\ApiOperations\Get;
use GhostZero\BitinflowAccounts\ApiOperations\Post;
use GhostZero\BitinflowAccounts\ApiOperations\Put;
use GhostZero\BitinflowAccounts\Result;
/**
* @author René Preuß <rene@preuss.io>
*/
trait ChargesTrait
{
use Get, Post, Put;
/**
* Create a Charge object
*
* @param array $parameters
*
* @return Result Result object
*/
public function createCharge(array $parameters): Result
{
return $this->post('charges', $parameters);
}
/**
* Get a Charge object
*
* @param string $id
*
* @return Result Result object
*/
public function getCharge(string $id): Result
{
return $this->get("charges/$id");
}
/**
* Update a Charge object
*
* @param string $id
* @param array $parameters
*
* @return Result Result object
*/
public function updateCharge(string $id, array $parameters): Result
{
return $this->put("charges/$id", $parameters);
}
/**
* Capture a Charge object
*
* @param string $id
* @param array $parameters
*
* @return Result Result object
*/
public function captureCharge(string $id, array $parameters = []): Result
{
return $this->post("charges/$id/capture", $parameters);
}
}

View File

@@ -1,48 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Traits;
use Carbon\CarbonInterface;
use GhostZero\BitinflowAccounts\ApiOperations\Get;
use GhostZero\BitinflowAccounts\ApiOperations\Post;
use GhostZero\BitinflowAccounts\Result;
/**
* @author René Preuß <rene@preuss.io>
*/
trait DocumentsTrait
{
use Get, Post;
/**
* Create a Documents object
*
* @param array $parameters
*
* @return Result
*/
public function createDocument(array $parameters): Result
{
return $this->post('documents', $parameters);
}
/**
* Create a Documents download url
*
* @param string $identifier
* @param CarbonInterface|null $expiresAt
*
* @return Result
*/
public function createDocumentDownloadUrl(string $identifier, ?CarbonInterface $expiresAt = null): Result
{
return $this->post("documents/$identifier/download-url", [
'expires_at' => $expiresAt
? $expiresAt->toDateTimeString()
: now()->addHour()->toDateTimeString(),
]);
}
}

View File

@@ -0,0 +1,196 @@
<?php
namespace GhostZero\BitinflowAccounts\Traits;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Log;
use RuntimeException;
/**
* @property string access_token todo: can we get this from HasBitinflowTokens ?
* @property PendingRequest $paymentsGatewayUser
*/
trait HasBitinflowPaymentsWallet
{
protected ?object $paymentsUser = null;
/**
* 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());
}
/**
* 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;
}
/**
* Check if user has an active wallet.
*
* @return bool
* @throws GuzzleException
*/
public function hasWallet(): bool
{
return $this->getPaymentsUser()->data->has_wallet;
}
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;
}
/**
* 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;
}
public function getSubscription($name = 'default'): ?object
{
foreach ($this->getSubscriptions() as $subscription) {
if (isset($subscription->payload->name) && $subscription->payload->name === $name) {
return $subscription;
}
}
return null;
}
public function hasSubscribed($name = 'default'): bool
{
$subscription = $this->getSubscription($name);
return $subscription && $subscription->status === 'settled' || $subscription && $subscription->resumeable;
}
/**
* 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));
}
}

View File

@@ -1,42 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Traits;
use GhostZero\BitinflowAccounts\ApiOperations\Get;
use GhostZero\BitinflowAccounts\ApiOperations\Post;
use GhostZero\BitinflowAccounts\Result;
/**
* @author René Preuß <rene@preuss.io>
*/
trait PaymentIntentsTrait
{
use Get, Post;
/**
* Get a Payment Intent object
*
* @param string $id
*
* @return Result Result object
*/
public function getPaymentIntent(string $id): Result
{
return $this->get("payment-intents/$id");
}
/**
* Create a Payment Intent object
*
* @param array $parameters
*
* @return Result
*/
public function createPaymentIntent(array $parameters): Result
{
return $this->post('payment-intents', $parameters);
}
}

View File

@@ -1,62 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Tests;
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
/**
* @author René Preuß <rene@preuss.io>
*/
class ApiChargesTest extends ApiTestCase
{
public function testCaptureWithoutCapture(): void
{
$this->getClient()->withToken($this->getToken());
$result = $this->getClient()->createCharge([
'amount' => 2000,
'currency' => 'usd',
'source' => 'tok_visa',
'description' => 'Charge for jenny.rosen@example.com',
]);
$this->registerResult($result);
$this->assertTrue($result->success());
$this->assertArrayHasKey('id', $result->data());
$this->assertEquals(2000, $result->data()->amount);
$this->assertTrue($result->data()->captured);
}
public function testChargeWithCapture(): void
{
$this->getClient()->withToken($this->getToken());
$result = $this->getClient()->createCharge([
'amount' => 2000,
'currency' => 'usd',
'source' => 'tok_visa',
'description' => 'Charge for jenny.rosen@example.com',
'capture' => false, // default is true for instant capture
'metadata' => [
'foo' => 'bar',
],
'receipt_email' => 'rene+unittest@bitinflow.com',
]);
$this->registerResult($result);
$this->assertTrue($result->success());
$this->assertArrayHasKey('id', $result->data());
$this->assertEquals(2000, $result->data()->amount);
$this->assertFalse($result->data()->captured);
$charge = $result->data();
$result = $this->getClient()->captureCharge($charge->id);
$this->registerResult($result);
$this->assertTrue($result->success());
$this->assertArrayHasKey('id', $result->data());
$this->assertEquals(2000, $result->data()->amount);
$this->assertTrue($result->data()->captured);
}
}

View File

@@ -1,87 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Tests;
use GhostZero\BitinflowAccounts\Enums\DocumentType;
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
/**
* @author René Preuß <rene@preuss.io>
*/
class ApiDocumentsTest extends ApiTestCase
{
public function testCreateDocument(): void
{
$this->getClient()->withToken($this->getToken());
$result = $this->getClient()->createDocument([
'branding' => [
'primary_color' => '#8284df',
'watermark_url' => 'https://fbs.streamkit.gg/img/pdf/wm.png',
'logo_url' => 'https://fbs.streamkit.gg/img/pdf/logo_dark_small.png',
],
'locale' => 'de',
'type' => DocumentType::TYPE_PDF_INVOICE,
'data' => $this->createDummyInvoiceData(),
'receipt_email' => 'rene+unittest@bitinflow.com',
]);
$this->registerResult($result);
$this->assertTrue($result->success());
$this->assertArrayHasKey('id', $result->data());
$this->assertArrayHasKey('download_url', $result->data());
$this->assertEquals(
'rene+unittest@bitinflow.com',
$result->data()->receipt_email
);
}
public function testGenerateDocumentStoragePath(): void
{
$this->getClient()->withToken($this->getToken());
$expiresAt = now()->addHours(2);
$result = $this->getClient()->createDocumentDownloadUrl('1', $expiresAt);
$this->registerResult($result);
$this->assertTrue($result->success());
$this->assertArrayHasKey('download_url', $result->data());
$this->assertEquals(
$expiresAt->toDateTimeString(),
$result->data()->expires_at
);
}
private function createDummyInvoiceData(): array
{
return [
'id' => 'FBS-IN-1337',
'customer' => [
'name' => 'GhostZero',
'email' => 'rene@preuss.io',
'address' => [
'Example Street 123',
'50733 Cologne',
'GERMANY',
],
],
'line_items' => [
[
'name' => 'T-shirt',
'description' => 'Comfortable cotton t-shirt',
'unit' => 'T-shirt', // optional unit name
'amount' => 1500,
'currency' => 'usd',
'quantity' => 2,
],
],
'legal_notice' => 'According to the German §19 UStG no sales tax is calculated. However, the product is a digital good delivered via Internet we generally offer no refunds. The delivery date corresponds to the invoice date.',
'already_paid' => true,
'created_at' => now()->format('d.m.Y'),
];
}
}

View File

@@ -1,46 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Tests;
use GhostZero\BitinflowAccounts\Tests\TestCases\ApiTestCase;
/**
* @author René Preuß <rene@preuss.io>
*/
class ApiPaymentIntentsTest extends ApiTestCase
{
private $paymentIntent;
public function testCreatePaymentIntent(): void
{
$this->getClient()->withToken($this->getToken());
$result = $this->getClient()->createPaymentIntent([
'payment_method_types' => ['card'],
'amount' => 1000,
'currency' => 'usd',
'application_fee_amount' => 123,
]);
$this->registerResult($result);
$this->assertTrue($result->success());
$this->assertArrayHasKey('id', $result->data());
$this->assertArrayHasKey('redirect_url', $result->data());
$this->assertEquals(1000, $result->data()->amount);
// use this payment intent for our next tests
$this->paymentIntent = $result->data();
}
public function testGetPaymentIntent(): void
{
$this->getClient()->withToken($this->getToken());
$result = $this->getClient()->getPaymentIntent($this->paymentIntent->id);
$this->registerResult($result);
$this->assertTrue($result->success());
$this->assertArrayHasKey('id', $result->data());
$this->assertEquals(1000, $result->data()->amount);
}
}