Skip to content

Commit

Permalink
Improve documentation (#3)
Browse files Browse the repository at this point in the history
* Update license and remove version in composer.json

* Update license in composer.json

* Improve documentation for API reference of the SDK

* Move documentation folder

* Add version in the composer.json because we use it

* Fix validation exception when to be thrown

* Improve documentation about exceptions

* Fix parsing errors

* Add custom service for building custom requests to the API

* Improve documentation

* Add guides to the documentation

* Simplify the usage of the Authorization service

* Improve documentation about the authorization service

* Improve documentation about Checkouts service

* Fix annotations

* Fix conventions about namespaces

* Improve documentation about Custom service

* Fix documentation styles

* Improve documentation about Customer service

* Improve documentation about Merchant service

* Improve documentation about Payouts service

* Improve documentation about Transaction service

* Improve documentation about SumUp service

* Improve documentation about exceptions handling and also the links between files

* Version bump

* Minor improvements after code review
  • Loading branch information
lyubomir-sumup authored Jan 29, 2019
1 parent 94beefb commit 44ecfab
Show file tree
Hide file tree
Showing 33 changed files with 1,120 additions and 213 deletions.
182 changes: 11 additions & 171 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,186 +24,26 @@ try {
'code' => 'YOUR-AUTHORIZATION-CODE'
]);
$checkoutService = $sumup->getCheckoutService();
$checkoutResponse = $checkoutService->create(/* pass here the required arguments */);
$checkoutResponse = $checkoutService->create($config);
// use the variable $checkoutResponse
} catch (\SumUp\Exceptions\SumUpAuthenticationException $e) {
echo 'Authentication error: ' . $e->getMessage();
} catch (\SumUp\Exceptions\SumUpResponseException $e) {
echo 'Response error: ' . $e->getMessage();
} catch(\SumUp\Exceptions\SumUpSDKException $e) {
echo 'SumUp SDK error: ' . $e->getMessage();
}
```

## Configurations

```php
$sumup = new \SumUp\SumUp([
// 'config-name': 'config-value'
]);
```

| Name | Description | Value | Default value | Required |
|--- |--- |--- |--- |--- |
|app_id | This is the client id that you receive after you [register](https://developer.sumup.com/docs/register-app) your application in SumUp | `string`| *No default value* | Yes |
|app_secret | This is the client secret that corresponds to the client id | `string` | *No default value* | Yes |
|grant_type | This indicates which authorization flow should be used to acquire OAuth token | One of: `authorization_code`, `client_credentials`, `password` | '`authorization_code`' | No |
|scopes | This is an array with all the [authorization scopes](https://developer.sumup.com/docs/authorization#authorization-scopes) that you need for your application | `array` with possible values: `payments`, `transactions.history`, `user.app-settings`, `user.profile_readonly`, `user.profile`, `user.subaccounts`, `user.payout-settings`, `balance`, `products` | `['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly']` | No |
|code | This is the code returned at the last step from [authorization code flow](https://developer.sumup.com/docs/authorization#authorization-flows) | `string` | `null` | Conditional (required only if `grant_type => 'authorization_code'`) |
|username | This is your SumUp's username if you want to use password authorization flow | `string` | `null` | Conditional (required only if `grant_type => 'password'`) |
|password | This is your SumUp's password if you want to use password authorization flow | `string` | `null` | Conditional (required only if `grant_type => 'password'`) |
|access_token | This is the value of a valid access token that is acquired through other methods. It is used if you don't want to request new access token | `string` | `null` | No |
|refresh_token | This is the refresh token through which can be requested new access token | `string` | `null` | No |
|use_guzzlehttp_over_curl | This is a configuration whether to use GuzzleHttp if both GuzzleHttp library and cURL module are installed. | `bool` | `false` | No |
|custom_headers | This sends custom headers with every http request to SumUp server | `array` with key-value pairs containing the header's name (as key) and the header's value (as value) | `[]` | No |

## Authorization

Every time you make an instance of the `\SumUp\SumUp` class you get a valid OAuth 2.0 access token. The access token is then passed automatically to every service call you make but of course you can override this (see examples below). In case you need the access token you can access it like this:

```php
$sumup = \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
// other configurations
]);
$accessToken = $sumup->getAccessToken();
// use the accessToken object as you need to
echo $accessToken->getValue() . ' ' . $accessToken->getRefreshToken();
```

This SDK supports 3 authorization flows which are described in [this guide](https://developer.sumup.com/docs/authorization).

### Authorization code flow

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'grant_type' => 'authorization_code',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'code' => 'YOUR-AUTHORIZATION-CODE'
]);
```

For more information about this flow read more in [this guide](https://developer.sumup.com/docs/authorization#authorization-code-flow).

### Client credentials flow

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'grant_type' => 'client_credentials',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly']
]);
```

For more information about this flow read more in [this guide](https://developer.sumup.com/docs/authorization#client-credentials-flow).

### Password flow

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'grant_type' => 'password',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'username' => 'YOUR-SUMUP-USERNAME',
'password' => 'YOUR-SUMUP-PASSWORD'
]);
```

### Reuse access token

In case you **already have a valid access token** you can **reuse it** like this:

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'access_token' => 'VALID-ACCESS-TOKEN'
]);
```

### Use refresh token
## API Reference

Here is how to get a **new access token from a refresh token**:
For a full list of classes, see the [API reference page](https://github.com/sumup/sumup-ecom-php-sdk/tree/master/docs).

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'refresh_token' => 'REFRESH-TOKEN'
]);
$sumup->refreshToken();
```

### Override access token

You can always **initialize** some **service** with a new **access token** like this:

```php
$checkoutService = $sumup->getCheckoutService('VALID-ACCESS-TOKEN');
```

## Services

To get a particular service you first need to create a `\SumUp\SumUp` object. Then from this object you can get any of the services we support in the SDK.

Here is an example how to get transactions history:

```php
try {
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'grant_type' => 'authorization_code',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'code' => 'YOUR-AUTHORIZATION-CODE'
]);
$transactionsService = $sumup->getTransactionService();
$filters = [
'limit' => 100,
'statuses' => ['SUCCESSFUL', 'REFUND']
];
$transactionsHistory = $transactionsService->getTransactionHistory($filters)->getBody();
// you can now iterate over the result in $transactionsHistory
} catch (\SumUp\Exceptions\SumUpSDKException $e) {
// handle exceptions
}
```

> All services' methods return response of type `\SumUp\HttpClients\Response` or throw an exception (view [exceptions handling](https://github.com/sumup/sumup-ecom-php-sdk#exceptions-handling)).
## Exceptions handling

Exceptions handling is an important part of our code. We pay attention to this detail and **we recommend to wrap every statement from this SDK with a `try {} catch() {}` clause**.

You should at least handle `\SumUp\Exceptions\SumUpSDKException` exception but if you want you can handle all sorts of exceptions.

```php
try {
$sumup = new \SumUp\SumUp(/* configuration */);
} catch (\SumUp\Exceptions\SumUpAuthenticationException $e) {
echo $e->getCode() . ': ' . $e->getMessage();
} catch (\SumUp\Exceptions\SumUpResponseException $e) {
echo $e->getCode() . ': ' . $e->getMessage();
} catch (\SumUp\Exceptions\SumUpSDKException $e) {
echo $e->getCode() . ': ' . $e->getMessage();
}
```
## FAQ

Here is a table with all the exceptions that are thrown from this SDK:

| Exception | Conditions |
|--- |--- |
|`\SumUp\Exceptions\SumUpAuthenticationException`| This exception is thrown when there is no access token or it is already expired. |
|`\SumUp\Exceptions\SumUpConnectionException` | This exception is thrown when there is connectivity issues over the network. |
|`\SumUp\Exceptions\SumUpResponseException` | This exception is thrown when there are some [4xx http errors](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors) such as `404 Not Found`. |
|`\SumUp\Exceptions\SumUpValidationException` | This exception is thrown when there is one or more wrong data send to the server. In the message there is information about which field(s) is incorrect. |
|`\SumUp\Exceptions\SumUpServerException` | This exception is thrown when there are http errors of [5xx](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_errors). |
|`\SumUp\Exceptions\SumUpConfigurationException` | This exception is thrown when you provide a bad configuration for initialization of the `\SumUp\SumUp` object. |
|`\SumUp\Exceptions\SumUpArgumentException` | This exception is thrown when you don't provide a mandatory argument to a function. |
|`\SumUp\Exceptions\SumUpSDKException` | This is the main exception which others inherit from. So this is the last exception to handle in your code if you want to catch many exceptions.|
* [How to authorize?](https://github.com/sumup/sumup-ecom-php-sdk/blob/master/docs/HowToAuthorize.md)
* [How to handle exceptions?](https://github.com/sumup/sumup-ecom-php-sdk/blob/master/docs/ExceptionsHandling.md)
* [How to use my own HTTP client?](https://github.com/sumup/sumup-ecom-php-sdk/blob/master/docs/HowToOverrideHttpClient.md)

## Roadmap

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "sumup/sumup-ecom-php-sdk",
"description": "SumUp eCom SDK for PHP",
"type": "library",
"version": "1.0.0-rc",
"license": "proprietary",
"version": "1.0.0-rc.2",
"keywords": ["sumup", "sdk", "payment processing", "ecommerce", "payment", "checkout"],
"homepage": "https://developer.sumup.com",
"authors": [
Expand Down
19 changes: 19 additions & 0 deletions docs/ExceptionsHandling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Exceptions handling

Exceptions handling is an important part of our code. We pay attention to this detail and **we recommend to wrap every statement from this SDK with a `try {} catch() {}` clause**.

You should at least handle `\SumUp\Exceptions\SumUpSDKException` exception but if you want you can handle all sorts of exceptions.

```php
try {
$sumup = new \SumUp\SumUp($config);
} catch (\SumUp\Exceptions\SumUpAuthenticationException $e) {
echo $e->getCode() . ': ' . $e->getMessage();
} catch (\SumUp\Exceptions\SumUpResponseException $e) {
echo $e->getCode() . ': ' . $e->getMessage();
} catch (\SumUp\Exceptions\SumUpSDKException $e) {
echo $e->getCode() . ': ' . $e->getMessage();
}
```

More information about the exceptions can be found in [the reference](https://github.com/sumup/sumup-ecom-php-sdk/tree/master/docs#exceptions).
103 changes: 103 additions & 0 deletions docs/HowToAuthorize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# How to authorize using the SDK

## Overview

This guide will help you to authorize and get an OAuth 2.0 token using the SDK. If you want to know what happens behind the scenes you can visit this [authorization guide](https://developer.sumup.com/docs/authorization).

Every time you make an instance of the `\SumUp\SumUp` class you get a valid OAuth 2.0 access token. The access token is then passed to every service call you make (but of course you can [override](https://github.com/sumup/sumup-ecom-php-sdk/blob/master/docs/HowToOverrideHttpClient.md) this).

## Authorization code flow

> **Note:** this is the flow we recommend.
First you need to send the merchant to pass through the [authorization flow](https://developer.sumup.com/docs/authorization#1-your-application-requests-authorization) so you can get a `code` and after that you can continue with the following example code.

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'grant_type' => 'authorization_code',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'code' => 'YOUR-AUTHORIZATION-CODE'
]);
$accessToken = $sumup->getAccessToken();
$refreshToken = $accessToken->getRefreshToken();
$value = $accessToken->getValue();
```

> **Note:** once you get a refresh token you can store it in a database and then use it to get new access tokens for the same merchant.
For more information about this flow read in [this guide](https://developer.sumup.com/docs/authorization#authorization-code-flow).

## Client credentials flow

If you want to use just the `client_id` and the `client_secret` you can use following snippet of code but keep in mind that not all endpoints can be requested with access token from this flow.

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'grant_type' => 'client_credentials',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly']
]);
$accessToken = $sumup->getAccessToken();
$value = $accessToken->getValue();
```

For more information about this flow read in [this guide](https://developer.sumup.com/docs/authorization#client-credentials-flow).

## Password flow

Here is an example how you can use the `password` flow. But also keep in mind that not all endpoints can be requested with access token from this flow.

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'grant_type' => 'password',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'username' => 'YOUR-SUMUP-USERNAME',
'password' => 'YOUR-SUMUP-PASSWORD'
]);
$accessToken = $sumup->getAccessToken();
$value = $accessToken->getValue();
```

## How to get new access from a refresh token

Here is how to get a **new access token from a refresh token**:

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'refresh_token' => 'REFRESH-TOKEN'
]);
// you need to call the method `refreshToken()` to get a new access token
$refreshedAccessToken = $sumup->refreshToken();
$value = $refreshedAccessToken->getValue();
```

> **Note:** keep in mind that the refresh token can also expire although it has long life span. For more information you can read [here](https://developer.sumup.com/docs/authorization#6-the-authorization-server-returns-an-access-token).
## How to reuse a valid access token

If you already have a valid access token you can reuse it like this:

```php
$sumup = new \SumUp\SumUp([
'app_id' => 'YOUR-CLIENT-ID',
'app_secret' => 'YOUR-CLIENT-SECRET',
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
'access_token' => 'VALID-ACCESS-TOKEN'
]);
```

## Override access token for a service

You can always initialize a service with an access token that is different from the one you already have from your `SumUp\SumUp` instance.

```php
$checkoutService = $sumup->getCheckoutService('ACCESS-TOKEN-INSTANCE');
```
21 changes: 21 additions & 0 deletions docs/HowToOverrideHttpClient.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# How to override HttpClient

## Overview

We provide two clients for dealing with HTTP communication: `\SumUp\HttpClients\SumUpCUrlClient` and `\SumUp\HttpClients\SumUpGuzzleHttpClient`. We also give the option to use your own HTTP client if you want to.

## \SumUp\HttpClients\SumUpCUrlClient

The `SumUpCUrlClient` client provides functionality for creating HTTP requests and getting responses using the PHP module [cURL](http://php.net/manual/en/book.curl.php).

## \SumUp\HttpClients\SumUpGuzzleHttpClient

The `SumUpGuzzleHttpClient` client provides functionality for creating HTTP requests and getting responses using the open-source library [Guzzle](https://packagist.org/packages/guzzlehttp/guzzle). We support **version 6.x** of the library.

> **Note:** This library is not required for using this SDK.
## Create your own HTTP client

If you have another way of HTTP communication you can make a class that implements the interface `\SumUp\HttpClients\SumUpHttpClientInterface`. After that you can pass an instance of that class to the constructor of `\SumUp\SumUp` as second parameter. Then the SDK would use this client for every request to the SumUp's servers.

> **Note:** you also have to **handle** all the **responses** and all the **exceptions** that might occur.
Loading

0 comments on commit 44ecfab

Please sign in to comment.