@@ -12,7 +12,7 @@ |
||
| 12 | 12 | const SEK = 'SEK'; |
| 13 | 13 | const USD = 'USD'; |
| 14 | 14 | |
| 15 | - static function isValid(string $currency){ |
|
| 15 | + static function isValid(string $currency) { |
|
| 16 | 16 | $currencies = self::getCurrencies(); |
| 17 | 17 | return in_array($currency, $currencies); |
| 18 | 18 | } |
@@ -2,7 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace BPCI\SumUp\Exception; |
| 4 | 4 | |
| 5 | -class FileNotFoundException extends \RuntimeException{ |
|
| 5 | +class FileNotFoundException extends \RuntimeException { |
|
| 6 | 6 | private $path; |
| 7 | 7 | public function __construct(string $message, int $code = 0, \Exception $previous = null, string $path = null) |
| 8 | 8 | { |
@@ -81,14 +81,14 @@ |
||
| 81 | 81 | */ |
| 82 | 82 | public static function loadContextFromFile(string $filePath): ContextInterface |
| 83 | 83 | { |
| 84 | - if(!file_exists($filePath)){ |
|
| 85 | - throw new Exception\FileNotFoundException('Context file not found: '.$filePath, 404, null, $filePath); |
|
| 84 | + if (!file_exists($filePath)) { |
|
| 85 | + throw new Exception\FileNotFoundException('Context file not found: ' . $filePath, 404, null, $filePath); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | $contents = file_get_contents($filePath); |
| 89 | 89 | $context_array = json_decode($contents, true); |
| 90 | 90 | |
| 91 | - if($context_array === null){ |
|
| 91 | + if ($context_array === null) { |
|
| 92 | 92 | throw new Exception\MalformedJsonException('JSON sintax error.'); |
| 93 | 93 | } |
| 94 | 94 | |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | |
| 26 | 26 | static function getEntrypoint(): string |
| 27 | 27 | { |
| 28 | - return self::ENTRYPOINT.self::VERSION.'/'; |
|
| 28 | + return self::ENTRYPOINT . self::VERSION . '/'; |
|
| 29 | 29 | } |
| 30 | 30 | } |
| 31 | 31 | |
@@ -7,29 +7,29 @@ |
||
| 7 | 7 | |
| 8 | 8 | class SumUp |
| 9 | 9 | { |
| 10 | - const VERSION = 'v0.1'; |
|
| 11 | - const ENTRYPOINT = 'https://api.sumup.com/'; |
|
| 10 | + const VERSION = 'v0.1'; |
|
| 11 | + const ENTRYPOINT = 'https://api.sumup.com/'; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 13 | + /** |
|
| 14 | 14 | * @param array $options |
| 15 | 15 | * @return ClientInterface |
| 16 | - */ |
|
| 16 | + */ |
|
| 17 | 17 | static function getClient(array $options = []): ClientInterface |
| 18 | - { |
|
| 18 | + { |
|
| 19 | 19 | $options = array_merge_recursive( |
| 20 | - [ |
|
| 21 | - 'base_uri' => self::getEntrypoint(), |
|
| 22 | - 'timeout' => 2 |
|
| 20 | + [ |
|
| 21 | + 'base_uri' => self::getEntrypoint(), |
|
| 22 | + 'timeout' => 2 |
|
| 23 | 23 | ], |
| 24 | 24 | $options |
| 25 | - ); |
|
| 25 | + ); |
|
| 26 | 26 | |
| 27 | 27 | return new Client($options); |
| 28 | - } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - static function getEntrypoint(): string |
|
| 31 | - { |
|
| 32 | - return self::ENTRYPOINT.self::VERSION.'/'; |
|
| 33 | - } |
|
| 30 | + static function getEntrypoint(): string |
|
| 31 | + { |
|
| 32 | + return self::ENTRYPOINT.self::VERSION.'/'; |
|
| 33 | + } |
|
| 34 | 34 | } |
| 35 | 35 | |
@@ -13,9 +13,9 @@ |
||
| 13 | 13 | |
| 14 | 14 | class NotAllowedProperty extends \RuntimeException |
| 15 | 15 | { |
| 16 | - public function __construct($object, $property, Throwable $previous = null) |
|
| 17 | - { |
|
| 18 | - $message = sprintf('Not allowed property %s into %s class!', $property,get_class($object)); |
|
| 19 | - parent::__construct($message, 100, $previous); |
|
| 20 | - } |
|
| 16 | + public function __construct($object, $property, Throwable $previous = null) |
|
| 17 | + { |
|
| 18 | + $message = sprintf('Not allowed property %s into %s class!', $property,get_class($object)); |
|
| 19 | + parent::__construct($message, 100, $previous); |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | { |
| 16 | 16 | public function __construct($object, $property, Throwable $previous = null) |
| 17 | 17 | { |
| 18 | - $message = sprintf('Not allowed property %s into %s class!', $property,get_class($object)); |
|
| 18 | + $message = sprintf('Not allowed property %s into %s class!', $property, get_class($object)); |
|
| 19 | 19 | parent::__construct($message, 100, $previous); |
| 20 | 20 | } |
| 21 | 21 | } |
@@ -7,8 +7,8 @@ discard block |
||
| 7 | 7 | class InvalidCheckoutException extends \RuntimeException |
| 8 | 8 | { |
| 9 | 9 | public function __construct(CheckoutInterface $checkout, $code = 10, Throwable $previous = null) |
| 10 | - { |
|
| 11 | - $message = sprintf(<<<MSG |
|
| 10 | + { |
|
| 11 | + $message = sprintf(<<<MSG |
|
| 12 | 12 | Something is wrong with this checkout: |
| 13 | 13 | checkout_reference: %s |
| 14 | 14 | amount: %s |
@@ -19,6 +19,6 @@ discard block |
||
| 19 | 19 | $checkout->getAmount(), |
| 20 | 20 | $checkout->getPayToEmail() |
| 21 | 21 | ); |
| 22 | - parent::__construct($message, $code, $previous); |
|
| 23 | - } |
|
| 22 | + parent::__construct($message, $code, $previous); |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -22,7 +22,7 @@ |
||
| 22 | 22 | * @param PaymentInstrumentInterface $card |
| 23 | 23 | * @return bool |
| 24 | 24 | */ |
| 25 | - public function disable(PaymentInstrumentInterface $card):? bool; |
|
| 25 | + public function disable(PaymentInstrumentInterface $card): ? bool; |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * @param CustomerInterface $customer |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | |
| 7 | 7 | interface CheckoutClientInterface extends SumUpClientInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 9 | + /** |
|
| 10 | 10 | * Create a checkout |
| 11 | 11 | * |
| 12 | 12 | * @param CheckoutInterface $checkout |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * @param CheckoutInterface $checkout |
| 29 | 29 | * @return string |
| 30 | - */ |
|
| 30 | + */ |
|
| 31 | 31 | static function getCompleteUrl(CheckoutInterface $checkout): string; |
| 32 | 32 | |
| 33 | 33 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * @return CheckoutInterface |
| 14 | 14 | * @see http://docs.sumup.com/rest-api/checkouts-api/#checkouts-create-checkout-post |
| 15 | 15 | */ |
| 16 | - public function create(CheckoutInterface $checkout):? CheckoutInterface; |
|
| 16 | + public function create(CheckoutInterface $checkout): ? CheckoutInterface; |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * Complete a checkout |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * @return CheckoutInterface |
| 23 | 23 | * @see http://docs.sumup.com/rest-api/checkouts-api/#checkouts-complete-checkout-put |
| 24 | 24 | */ |
| 25 | - public function complete(CheckoutInterface $checkout):? CheckoutInterface; |
|
| 25 | + public function complete(CheckoutInterface $checkout): ? CheckoutInterface; |
|
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * @param CheckoutInterface $checkout |
@@ -11,107 +11,107 @@ |
||
| 11 | 11 | const OAUTH_AUTHORIZATION = '/authorize'; |
| 12 | 12 | const OAUTH_TOKEN = '/token'; |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Generate an url to merchant authorization. |
|
| 16 | - * |
|
| 17 | - * @param ContextInterface $context |
|
| 18 | - * @return string |
|
| 19 | - */ |
|
| 14 | + /** |
|
| 15 | + * Generate an url to merchant authorization. |
|
| 16 | + * |
|
| 17 | + * @param ContextInterface $context |
|
| 18 | + * @return string |
|
| 19 | + */ |
|
| 20 | 20 | public static function getAuthorizationURL(ContextInterface $context) |
| 21 | - { |
|
| 22 | - $queryString = [ |
|
| 23 | - 'client_id' => $context->getClientId(), |
|
| 24 | - 'client_secret' => $context->getClientSecret(), |
|
| 25 | - 'redirect_uri' => $context->getRedirectUri(), |
|
| 26 | - 'response_type' => 'code', |
|
| 27 | - ]; |
|
| 28 | - |
|
| 29 | - return SumUp::ENTRYPOINT . self::OAUTH_AUTHORIZATION . '?' . http_build_query($queryString); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Request an acess token from sumup services. |
|
| 34 | - * |
|
| 21 | + { |
|
| 22 | + $queryString = [ |
|
| 23 | + 'client_id' => $context->getClientId(), |
|
| 24 | + 'client_secret' => $context->getClientSecret(), |
|
| 25 | + 'redirect_uri' => $context->getRedirectUri(), |
|
| 26 | + 'response_type' => 'code', |
|
| 27 | + ]; |
|
| 28 | + |
|
| 29 | + return SumUp::ENTRYPOINT . self::OAUTH_AUTHORIZATION . '?' . http_build_query($queryString); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Request an acess token from sumup services. |
|
| 34 | + * |
|
| 35 | 35 | * @param ContextInterface $context |
| 36 | 36 | * @param array|null $scopes |
| 37 | 37 | * @param array $options |
| 38 | 38 | * @return \BPCI\SumUp\OAuth\AccessToken |
| 39 | - */ |
|
| 39 | + */ |
|
| 40 | 40 | public static function getAccessToken( |
| 41 | 41 | ContextInterface $context, |
| 42 | 42 | array $scopes = null, |
| 43 | 43 | array $options = [] |
| 44 | 44 | ): AccessToken |
| 45 | - { |
|
| 46 | - $formParams = [ |
|
| 47 | - 'grant_type' => 'client_credentials', |
|
| 48 | - 'client_id' => $context->getClientId(), |
|
| 49 | - 'client_secret' => $context->getClientSecret(), |
|
| 50 | - ]; |
|
| 45 | + { |
|
| 46 | + $formParams = [ |
|
| 47 | + 'grant_type' => 'client_credentials', |
|
| 48 | + 'client_id' => $context->getClientId(), |
|
| 49 | + 'client_secret' => $context->getClientSecret(), |
|
| 50 | + ]; |
|
| 51 | 51 | |
| 52 | - if ($scopes !== null) { |
|
| 53 | - $formParams['scope'] = implode(',', $scopes); |
|
| 54 | - } |
|
| 52 | + if ($scopes !== null) { |
|
| 53 | + $formParams['scope'] = implode(',', $scopes); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | $client = SumUp::getClient($options); |
| 57 | 57 | |
| 58 | - $response = $client->request( |
|
| 59 | - 'POST', |
|
| 60 | - self::OAUTH_TOKEN, |
|
| 61 | - [ |
|
| 62 | - 'form_params' => $formParams, |
|
| 63 | - ]); |
|
| 64 | - |
|
| 65 | - $code = $response->getStatusCode(); |
|
| 66 | - if ($code !== 200) { |
|
| 67 | - $message = " Request code: $code \n Message: " . $response->getReasonPhrase(); |
|
| 68 | - throw new BadRequestException($message); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $body = json_decode($response->getBody()->getContents(), true); |
|
| 72 | - $token_params = [ |
|
| 73 | - $body['access_token'], |
|
| 74 | - $body['token_type'], |
|
| 75 | - $body['expires_in'], |
|
| 76 | - ]; |
|
| 77 | - |
|
| 78 | - if (isset($body['scope'])) { |
|
| 79 | - $token_params[] = $body['scope']; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - return new AccessToken(...$token_params); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * If available check token or generate a new token |
|
| 87 | - * |
|
| 88 | - * @param ContextInterface $context |
|
| 89 | - * @param AccessToken $token |
|
| 90 | - * @param array $scopes |
|
| 58 | + $response = $client->request( |
|
| 59 | + 'POST', |
|
| 60 | + self::OAUTH_TOKEN, |
|
| 61 | + [ |
|
| 62 | + 'form_params' => $formParams, |
|
| 63 | + ]); |
|
| 64 | + |
|
| 65 | + $code = $response->getStatusCode(); |
|
| 66 | + if ($code !== 200) { |
|
| 67 | + $message = " Request code: $code \n Message: " . $response->getReasonPhrase(); |
|
| 68 | + throw new BadRequestException($message); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $body = json_decode($response->getBody()->getContents(), true); |
|
| 72 | + $token_params = [ |
|
| 73 | + $body['access_token'], |
|
| 74 | + $body['token_type'], |
|
| 75 | + $body['expires_in'], |
|
| 76 | + ]; |
|
| 77 | + |
|
| 78 | + if (isset($body['scope'])) { |
|
| 79 | + $token_params[] = $body['scope']; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + return new AccessToken(...$token_params); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * If available check token or generate a new token |
|
| 87 | + * |
|
| 88 | + * @param ContextInterface $context |
|
| 89 | + * @param AccessToken $token |
|
| 90 | + * @param array $scopes |
|
| 91 | 91 | * @param array $options |
| 92 | 92 | * |
| 93 | 93 | * @return \BPCI\SumUp\OAuth\AccessToken |
| 94 | - */ |
|
| 94 | + */ |
|
| 95 | 95 | public static function getValidToken( |
| 96 | 96 | ContextInterface $context, |
| 97 | 97 | AccessToken $token = null, |
| 98 | 98 | array $scopes = null, |
| 99 | 99 | array $options = [] |
| 100 | 100 | ): AccessToken |
| 101 | - { |
|
| 102 | - if ($token === null || !$token->isValid()) { |
|
| 101 | + { |
|
| 102 | + if ($token === null || !$token->isValid()) { |
|
| 103 | 103 | $token = AuthenticationHelper::getAccessToken($context, $scopes, $options); |
| 104 | - } |
|
| 105 | - |
|
| 106 | - return $token; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public static function getOAuthHeader(AccessToken $token): array |
|
| 110 | - { |
|
| 111 | - return [ |
|
| 112 | - 'headers' => [ |
|
| 113 | - 'Authorization' => $token->getType() . ' ' . $token->getToken(), |
|
| 114 | - ], |
|
| 115 | - ]; |
|
| 116 | - } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return $token; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public static function getOAuthHeader(AccessToken $token): array |
|
| 110 | + { |
|
| 111 | + return [ |
|
| 112 | + 'headers' => [ |
|
| 113 | + 'Authorization' => $token->getType() . ' ' . $token->getToken(), |
|
| 114 | + ], |
|
| 115 | + ]; |
|
| 116 | + } |
|
| 117 | 117 | } |