2 Commits
0.0.1 ... 0.1.0

Author SHA1 Message Date
René Preuß
a1ef9075e8 Update to Laravel 6 2019-10-16 16:09:43 +02:00
René Preuß
228c845bb5 Improve api operations
Change directory structure
Add bitinflow-accounts socialite provider
Improve docs generation
2019-09-22 21:53:49 +02:00
32 changed files with 847 additions and 315 deletions

View File

@@ -9,9 +9,10 @@ PHP bitinflow Accounts API Client for Laravel 5+
## Table of contents
1. [Installation](#installation)
2. [Configuration](#configuration)
3. [Examples](#examples)
4. [Documentation](#documentation)
2. [Event Listener](#event-listener)
3. [Configuration](#configuration)
4. [Examples](#examples)
5. [Documentation](#documentation)
6. [Development](#Development)
## Installation
@@ -28,6 +29,28 @@ Add Service Provider to your `app.php` configuration file:
GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
```
## Event Listener
- Add `SocialiteProviders\Manager\SocialiteWasCalled` event to your `listen[]` array in `app/Providers/EventServiceProvider`.
- Add your listeners (i.e. the ones from the providers) to the `SocialiteProviders\Manager\SocialiteWasCalled[]` that you just created.
- The listener that you add for this provider is `'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',`.
- Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
```
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// add your listeners (aka providers) here
'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',
],
];
```
## Configuration
Copy configuration to config folder:
@@ -44,6 +67,18 @@ BITINFLOW_ACCOUNTS_SECRET=
BITINFLOW_ACCOUNTS_REDIRECT_URI=http://localhost
```
You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command `artisan config:cache`) all config is still available.
**Add to `config/services.php`:**
```php
'bitinflow-accounts' => [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
'redirect' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI')
],
```
## Examples
#### Basic
@@ -114,10 +149,20 @@ BitinflowAccounts::withClientId('abc123')->withToken('abcdef123456')->getAuthedU
## Documentation
### Users
### Charges
```php
public function getAuthedUser()
public function createCharge(array $parameters)
public function getCharge(string $id)
public function updateCharge(string $id, array $parameters)
public function captureCharge(string $id, array $parameters = [])
```
### CheckoutSessions
```php
public function getCheckoutSession(string $id)
public function createCheckoutSession(array $parameters)
```
### SshKeys
@@ -128,7 +173,13 @@ public function createSshKey(string $publicKey, string $name = NULL)
public function deleteSshKey(int $id)
```
[**OAuth Scopes Enums**](https://git.preuss.io/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
### Users
```php
public function getAuthedUser()
```
[**OAuth Scopes Enums**](https://github.com/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
## Development

View File

@@ -28,6 +28,28 @@ Add Service Provider to your `app.php` configuration file:
GhostZero\BitinflowAccounts\Providers\BitinflowAccountsServiceProvider::class,
```
## Event Listener
- Add `SocialiteProviders\Manager\SocialiteWasCalled` event to your `listen[]` array in `app/Providers/EventServiceProvider`.
- Add your listeners (i.e. the ones from the providers) to the `SocialiteProviders\Manager\SocialiteWasCalled[]` that you just created.
- The listener that you add for this provider is `'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',`.
- Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
```
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// add your listeners (aka providers) here
'GhostZero\\BitinflowAccounts\\Socialite\\BitinflowExtendSocialite@handle',
],
];
```
## Configuration
Copy configuration to config folder:
@@ -44,6 +66,18 @@ BITINFLOW_ACCOUNTS_SECRET=
BITINFLOW_ACCOUNTS_REDIRECT_URI=http://localhost
```
You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command `artisan config:cache`) all config is still available.
**Add to `config/services.php`:**
```php
'bitinflow-accounts' => [
'client_id' => env('BITINFLOW_ACCOUNTS_KEY'),
'client_secret' => env('BITINFLOW_ACCOUNTS_SECRET'),
'redirect' => env('BITINFLOW_ACCOUNTS_REDIRECT_URI')
],
```
## Examples
#### Basic
@@ -116,7 +150,7 @@ BitinflowAccounts::withClientId('abc123')->withToken('abcdef123456')->getAuthedU
<!-- GENERATED-DOCS -->
[**OAuth Scopes Enums**](https://git.preuss.io/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
[**OAuth Scopes Enums**](https://github.com/ghostzero/bitinflow-accounts/blob/master/src/Enums/Scope.php)
## Development

View File

@@ -10,23 +10,25 @@
],
"require": {
"php": ">=7.2",
"illuminate/support": "^5.5",
"illuminate/console": "^5.5",
"guzzlehttp/guzzle": "^6.3"
"ext-json": "*",
"illuminate/support": "^6.0",
"illuminate/console": "^6.0",
"guzzlehttp/guzzle": "^6.3",
"socialiteproviders/manager": "^3.4"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"orchestra/testbench": "~3.8.0",
"orchestra/testbench": "~4.0",
"codedungeon/phpunit-result-printer": "^0.26.2"
},
"autoload": {
"psr-4": {
"GhostZero\\BitinflowAccounts\\": "src"
"GhostZero\\BitinflowAccounts\\": "src/GhostZero/BitinflowAccounts"
}
},
"autoload-dev": {
"psr-4": {
"GhostZero\\BitinflowAccounts\\Tests\\": "tests"
"GhostZero\\BitinflowAccounts\\Tests\\": "tests/GhostZero/BitinflowAccounts"
}
},
"scripts": {

739
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -76,6 +76,8 @@ $markdown = collect(class_uses(BitinflowAccounts::class))
return $markdown;
})->join(PHP_EOL . PHP_EOL);
$markdown = str_replace("array (\n)", '[]', $markdown);
$content = file_get_contents(__DIR__ . '/../README.stub');
$content = str_replace('<!-- GENERATED-DOCS -->', $markdown, $content);

View File

@@ -1,22 +0,0 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Enums;
/**
* @author René Preuß <rene@preuss.io>
*/
class Scope
{
/*
* v0 API
*/
// Deprecated scope.
const API = 'api';
// Read nonpublic user information, including email address.
const READ_USER = 'read_user';
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Delete
{
abstract public function delete(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Get
{
abstract public function get(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Post
{
abstract public function post(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -6,7 +6,11 @@ namespace GhostZero\BitinflowAccounts\ApiOperations;
use GhostZero\BitinflowAccounts\Helpers\Paginator;
/**
* @author René Preuß <rene@preuss.io>
*/
trait Put
{
abstract public function put(string $path = '', array $parameters = [], Paginator $paginator = null);
}

View File

@@ -17,8 +17,10 @@ use GuzzleHttp\Exception\RequestException;
class BitinflowAccounts
{
use Traits\UsersTrait;
use Traits\ChargesTrait;
use Traits\CheckoutSessionsTrait;
use Traits\SshKeysTrait;
use Traits\UsersTrait;
const BASE_URI = 'https://accounts.bitinflow.com/api/';
const OAUTH_BASE_URI = 'https://accounts.bitinflow.com/api/';

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace GhostZero\BitinflowAccounts\Enums;
/**
* @author René Preuß <rene@preuss.io>
*/
class Scope
{
/*
* v0 API
*/
// Deprecated scope.
const API = 'api';
// Read nonpublic user information, including email address.
const READ_USER = 'read_user';
/*
* v1 API
*/
// Read authorized user´s email address.
const USERS_READ_EMAIL = 'users:read:email';
// Manage a authorized user object.
const USERS_EDIT = 'users:edit';
// Read authorized user´s transactions.
const TRANSACTIONS_READ = 'transactions:read';
// Create a new charge for the authorized user.
const CHARGES_CREATE = 'charges:create';
}

View File

@@ -0,0 +1,24 @@
<?php
namespace GhostZero\BitinflowAccounts\Socialite;
use SocialiteProviders\Manager\SocialiteWasCalled;
/**
* @author René Preuß <rene@preuss.io>
*/
class BitinflowExtendSocialite
{
/**
* Register the provider.
*
* @param SocialiteWasCalled $socialiteWasCalled
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite(
'bitinflow-accounts', __NAMESPACE__ . '\Provider'
);
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace GhostZero\BitinflowAccounts\Socialite;
use GhostZero\BitinflowAccounts\Enums\Scope;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\ProviderInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
/**
* @author René Preuß <rene@preuss.io>
*/
class Provider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'BITINFLOW_ACCOUNTS';
/**
* {@inheritdoc}
*/
protected $scopes = [Scope::READ_USER];
/**
* {@inherticdoc}.
*/
protected $scopeSeparator = ' ';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://accounts.bitinflow.com/oauth/authorize', $state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://accounts.bitinflow.com/oauth/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://accounts.bitinflow.com/api/user', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody()->getContents(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['name'],
'name' => $user['name'],
'email' => Arr::get($user, 'email'),
'avatar' => $user['avatar'],
]);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
}

View File

@@ -0,0 +1,69 @@
<?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

@@ -0,0 +1,42 @@
<?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 CheckoutSessionsTrait
{
use Get, Post;
/**
* Get a Session object
*
* @param string $id
*
* @return Result Result object
*/
public function getCheckoutSession(string $id): Result
{
return $this->get("checkout/sessions/$id");
}
/**
* Create a Session object
*
* @param array $parameters
*
* @return Result
*/
public function createCheckoutSession(array $parameters): Result
{
return $this->post('payments/sessions', $parameters);
}
}

View File

@@ -39,7 +39,7 @@ trait SshKeysTrait
return $this->post('ssh-keys', [
'public_key' => $publicKey,
'name' => $name,
], null);
]);
}
/**
@@ -51,6 +51,6 @@ trait SshKeysTrait
*/
public function deleteSshKey(int $id): Result
{
return $this->delete("ssh-keys/$id", [], null);
return $this->delete("ssh-keys/$id", []);
}
}