Skip to content

Commit 44ecfab

Browse files
Improve documentation (#3)
* 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
1 parent 94beefb commit 44ecfab

33 files changed

+1120
-213
lines changed

README.md

Lines changed: 11 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -24,186 +24,26 @@ try {
2424
'code' => 'YOUR-AUTHORIZATION-CODE'
2525
]);
2626
$checkoutService = $sumup->getCheckoutService();
27-
$checkoutResponse = $checkoutService->create(/* pass here the required arguments */);
27+
$checkoutResponse = $checkoutService->create($config);
2828
// use the variable $checkoutResponse
29+
} catch (\SumUp\Exceptions\SumUpAuthenticationException $e) {
30+
echo 'Authentication error: ' . $e->getMessage();
31+
} catch (\SumUp\Exceptions\SumUpResponseException $e) {
32+
echo 'Response error: ' . $e->getMessage();
2933
} catch(\SumUp\Exceptions\SumUpSDKException $e) {
3034
echo 'SumUp SDK error: ' . $e->getMessage();
3135
}
3236
```
3337

34-
## Configurations
35-
36-
```php
37-
$sumup = new \SumUp\SumUp([
38-
// 'config-name': 'config-value'
39-
]);
40-
```
41-
42-
| Name | Description | Value | Default value | Required |
43-
|--- |--- |--- |--- |--- |
44-
|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 |
45-
|app_secret | This is the client secret that corresponds to the client id | `string` | *No default value* | Yes |
46-
|grant_type | This indicates which authorization flow should be used to acquire OAuth token | One of: `authorization_code`, `client_credentials`, `password` | '`authorization_code`' | No |
47-
|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 |
48-
|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'`) |
49-
|username | This is your SumUp's username if you want to use password authorization flow | `string` | `null` | Conditional (required only if `grant_type => 'password'`) |
50-
|password | This is your SumUp's password if you want to use password authorization flow | `string` | `null` | Conditional (required only if `grant_type => 'password'`) |
51-
|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 |
52-
|refresh_token | This is the refresh token through which can be requested new access token | `string` | `null` | No |
53-
|use_guzzlehttp_over_curl | This is a configuration whether to use GuzzleHttp if both GuzzleHttp library and cURL module are installed. | `bool` | `false` | No |
54-
|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 |
55-
56-
## Authorization
57-
58-
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:
59-
60-
```php
61-
$sumup = \SumUp\SumUp([
62-
'app_id' => 'YOUR-CLIENT-ID',
63-
'app_secret' => 'YOUR-CLIENT-SECRET',
64-
// other configurations
65-
]);
66-
$accessToken = $sumup->getAccessToken();
67-
// use the accessToken object as you need to
68-
echo $accessToken->getValue() . ' ' . $accessToken->getRefreshToken();
69-
```
70-
71-
This SDK supports 3 authorization flows which are described in [this guide](https://developer.sumup.com/docs/authorization).
72-
73-
### Authorization code flow
74-
75-
```php
76-
$sumup = new \SumUp\SumUp([
77-
'app_id' => 'YOUR-CLIENT-ID',
78-
'app_secret' => 'YOUR-CLIENT-SECRET',
79-
'grant_type' => 'authorization_code',
80-
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
81-
'code' => 'YOUR-AUTHORIZATION-CODE'
82-
]);
83-
```
84-
85-
For more information about this flow read more in [this guide](https://developer.sumup.com/docs/authorization#authorization-code-flow).
86-
87-
### Client credentials flow
88-
89-
```php
90-
$sumup = new \SumUp\SumUp([
91-
'app_id' => 'YOUR-CLIENT-ID',
92-
'app_secret' => 'YOUR-CLIENT-SECRET',
93-
'grant_type' => 'client_credentials',
94-
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly']
95-
]);
96-
```
97-
98-
For more information about this flow read more in [this guide](https://developer.sumup.com/docs/authorization#client-credentials-flow).
99-
100-
### Password flow
101-
102-
```php
103-
$sumup = new \SumUp\SumUp([
104-
'app_id' => 'YOUR-CLIENT-ID',
105-
'app_secret' => 'YOUR-CLIENT-SECRET',
106-
'grant_type' => 'password',
107-
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
108-
'username' => 'YOUR-SUMUP-USERNAME',
109-
'password' => 'YOUR-SUMUP-PASSWORD'
110-
]);
111-
```
112-
113-
### Reuse access token
114-
115-
In case you **already have a valid access token** you can **reuse it** like this:
116-
117-
```php
118-
$sumup = new \SumUp\SumUp([
119-
'app_id' => 'YOUR-CLIENT-ID',
120-
'app_secret' => 'YOUR-CLIENT-SECRET',
121-
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
122-
'access_token' => 'VALID-ACCESS-TOKEN'
123-
]);
124-
```
125-
126-
### Use refresh token
38+
## API Reference
12739

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

130-
```php
131-
$sumup = new \SumUp\SumUp([
132-
'app_id' => 'YOUR-CLIENT-ID',
133-
'app_secret' => 'YOUR-CLIENT-SECRET',
134-
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
135-
'refresh_token' => 'REFRESH-TOKEN'
136-
]);
137-
$sumup->refreshToken();
138-
```
139-
140-
### Override access token
141-
142-
You can always **initialize** some **service** with a new **access token** like this:
143-
144-
```php
145-
$checkoutService = $sumup->getCheckoutService('VALID-ACCESS-TOKEN');
146-
```
147-
148-
## Services
149-
150-
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.
151-
152-
Here is an example how to get transactions history:
153-
154-
```php
155-
try {
156-
$sumup = new \SumUp\SumUp([
157-
'app_id' => 'YOUR-CLIENT-ID',
158-
'app_secret' => 'YOUR-CLIENT-SECRET',
159-
'grant_type' => 'authorization_code',
160-
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
161-
'code' => 'YOUR-AUTHORIZATION-CODE'
162-
]);
163-
$transactionsService = $sumup->getTransactionService();
164-
$filters = [
165-
'limit' => 100,
166-
'statuses' => ['SUCCESSFUL', 'REFUND']
167-
];
168-
$transactionsHistory = $transactionsService->getTransactionHistory($filters)->getBody();
169-
// you can now iterate over the result in $transactionsHistory
170-
} catch (\SumUp\Exceptions\SumUpSDKException $e) {
171-
// handle exceptions
172-
}
173-
```
174-
175-
> 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)).
176-
177-
## Exceptions handling
178-
179-
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**.
180-
181-
You should at least handle `\SumUp\Exceptions\SumUpSDKException` exception but if you want you can handle all sorts of exceptions.
182-
183-
```php
184-
try {
185-
$sumup = new \SumUp\SumUp(/* configuration */);
186-
} catch (\SumUp\Exceptions\SumUpAuthenticationException $e) {
187-
echo $e->getCode() . ': ' . $e->getMessage();
188-
} catch (\SumUp\Exceptions\SumUpResponseException $e) {
189-
echo $e->getCode() . ': ' . $e->getMessage();
190-
} catch (\SumUp\Exceptions\SumUpSDKException $e) {
191-
echo $e->getCode() . ': ' . $e->getMessage();
192-
}
193-
```
42+
## FAQ
19443

195-
Here is a table with all the exceptions that are thrown from this SDK:
196-
197-
| Exception | Conditions |
198-
|--- |--- |
199-
|`\SumUp\Exceptions\SumUpAuthenticationException`| This exception is thrown when there is no access token or it is already expired. |
200-
|`\SumUp\Exceptions\SumUpConnectionException` | This exception is thrown when there is connectivity issues over the network. |
201-
|`\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`. |
202-
|`\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. |
203-
|`\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). |
204-
|`\SumUp\Exceptions\SumUpConfigurationException` | This exception is thrown when you provide a bad configuration for initialization of the `\SumUp\SumUp` object. |
205-
|`\SumUp\Exceptions\SumUpArgumentException` | This exception is thrown when you don't provide a mandatory argument to a function. |
206-
|`\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.|
44+
* [How to authorize?](https://github.com/sumup/sumup-ecom-php-sdk/blob/master/docs/HowToAuthorize.md)
45+
* [How to handle exceptions?](https://github.com/sumup/sumup-ecom-php-sdk/blob/master/docs/ExceptionsHandling.md)
46+
* [How to use my own HTTP client?](https://github.com/sumup/sumup-ecom-php-sdk/blob/master/docs/HowToOverrideHttpClient.md)
20747

20848
## Roadmap
20949

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "sumup/sumup-ecom-php-sdk",
33
"description": "SumUp eCom SDK for PHP",
44
"type": "library",
5-
"version": "1.0.0-rc",
5+
"license": "proprietary",
6+
"version": "1.0.0-rc.2",
67
"keywords": ["sumup", "sdk", "payment processing", "ecommerce", "payment", "checkout"],
78
"homepage": "https://developer.sumup.com",
89
"authors": [

docs/ExceptionsHandling.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Exceptions handling
2+
3+
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**.
4+
5+
You should at least handle `\SumUp\Exceptions\SumUpSDKException` exception but if you want you can handle all sorts of exceptions.
6+
7+
```php
8+
try {
9+
$sumup = new \SumUp\SumUp($config);
10+
} catch (\SumUp\Exceptions\SumUpAuthenticationException $e) {
11+
echo $e->getCode() . ': ' . $e->getMessage();
12+
} catch (\SumUp\Exceptions\SumUpResponseException $e) {
13+
echo $e->getCode() . ': ' . $e->getMessage();
14+
} catch (\SumUp\Exceptions\SumUpSDKException $e) {
15+
echo $e->getCode() . ': ' . $e->getMessage();
16+
}
17+
```
18+
19+
More information about the exceptions can be found in [the reference](https://github.com/sumup/sumup-ecom-php-sdk/tree/master/docs#exceptions).

docs/HowToAuthorize.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# How to authorize using the SDK
2+
3+
## Overview
4+
5+
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).
6+
7+
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).
8+
9+
## Authorization code flow
10+
11+
> **Note:** this is the flow we recommend.
12+
13+
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.
14+
15+
```php
16+
$sumup = new \SumUp\SumUp([
17+
'app_id' => 'YOUR-CLIENT-ID',
18+
'app_secret' => 'YOUR-CLIENT-SECRET',
19+
'grant_type' => 'authorization_code',
20+
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
21+
'code' => 'YOUR-AUTHORIZATION-CODE'
22+
]);
23+
$accessToken = $sumup->getAccessToken();
24+
$refreshToken = $accessToken->getRefreshToken();
25+
$value = $accessToken->getValue();
26+
```
27+
28+
> **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.
29+
30+
For more information about this flow read in [this guide](https://developer.sumup.com/docs/authorization#authorization-code-flow).
31+
32+
## Client credentials flow
33+
34+
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.
35+
36+
```php
37+
$sumup = new \SumUp\SumUp([
38+
'app_id' => 'YOUR-CLIENT-ID',
39+
'app_secret' => 'YOUR-CLIENT-SECRET',
40+
'grant_type' => 'client_credentials',
41+
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly']
42+
]);
43+
$accessToken = $sumup->getAccessToken();
44+
$value = $accessToken->getValue();
45+
```
46+
47+
For more information about this flow read in [this guide](https://developer.sumup.com/docs/authorization#client-credentials-flow).
48+
49+
## Password flow
50+
51+
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.
52+
53+
```php
54+
$sumup = new \SumUp\SumUp([
55+
'app_id' => 'YOUR-CLIENT-ID',
56+
'app_secret' => 'YOUR-CLIENT-SECRET',
57+
'grant_type' => 'password',
58+
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
59+
'username' => 'YOUR-SUMUP-USERNAME',
60+
'password' => 'YOUR-SUMUP-PASSWORD'
61+
]);
62+
$accessToken = $sumup->getAccessToken();
63+
$value = $accessToken->getValue();
64+
```
65+
66+
## How to get new access from a refresh token
67+
68+
Here is how to get a **new access token from a refresh token**:
69+
70+
```php
71+
$sumup = new \SumUp\SumUp([
72+
'app_id' => 'YOUR-CLIENT-ID',
73+
'app_secret' => 'YOUR-CLIENT-SECRET',
74+
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
75+
'refresh_token' => 'REFRESH-TOKEN'
76+
]);
77+
// you need to call the method `refreshToken()` to get a new access token
78+
$refreshedAccessToken = $sumup->refreshToken();
79+
$value = $refreshedAccessToken->getValue();
80+
```
81+
82+
> **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).
83+
84+
## How to reuse a valid access token
85+
86+
If you already have a valid access token you can reuse it like this:
87+
88+
```php
89+
$sumup = new \SumUp\SumUp([
90+
'app_id' => 'YOUR-CLIENT-ID',
91+
'app_secret' => 'YOUR-CLIENT-SECRET',
92+
'scope' => ['payments', 'transactions.history', 'user.app-settings', 'user.profile_readonly'],
93+
'access_token' => 'VALID-ACCESS-TOKEN'
94+
]);
95+
```
96+
97+
## Override access token for a service
98+
99+
You can always initialize a service with an access token that is different from the one you already have from your `SumUp\SumUp` instance.
100+
101+
```php
102+
$checkoutService = $sumup->getCheckoutService('ACCESS-TOKEN-INSTANCE');
103+
```

docs/HowToOverrideHttpClient.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# How to override HttpClient
2+
3+
## Overview
4+
5+
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.
6+
7+
## \SumUp\HttpClients\SumUpCUrlClient
8+
9+
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).
10+
11+
## \SumUp\HttpClients\SumUpGuzzleHttpClient
12+
13+
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.
14+
15+
> **Note:** This library is not required for using this SDK.
16+
17+
## Create your own HTTP client
18+
19+
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.
20+
21+
> **Note:** you also have to **handle** all the **responses** and all the **exceptions** that might occur.

0 commit comments

Comments
 (0)