@@ -22,71 +22,71 @@ discard block |
||
| 22 | 22 | request as protected traitRequest; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - protected $context; |
|
| 26 | - protected $lastResponse; |
|
| 25 | + protected $context; |
|
| 26 | + protected $lastResponse; |
|
| 27 | 27 | protected $options; |
| 28 | 28 | protected $currentToken; |
| 29 | 29 | |
| 30 | 30 | function __construct(ContextInterface $context, ?array $options = []) |
| 31 | - { |
|
| 32 | - $this->context = $context; |
|
| 31 | + { |
|
| 32 | + $this->context = $context; |
|
| 33 | 33 | $this->options = $options; |
| 34 | - } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @inheritDoc |
|
| 38 | - * @throws BadResponseException |
|
| 39 | - * @throws ConnectException |
|
| 40 | - */ |
|
| 36 | + /** |
|
| 37 | + * @inheritDoc |
|
| 38 | + * @throws BadResponseException |
|
| 39 | + * @throws ConnectException |
|
| 40 | + */ |
|
| 41 | 41 | function create(CheckoutInterface $checkout, AccessToken $accessToken = null):? CheckoutInterface |
| 42 | - { |
|
| 42 | + { |
|
| 43 | 43 | return $this->request('post', $checkout) ? $checkout : null; |
| 44 | - } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @inheritDoc |
|
| 48 | - * @throws BadResponseException |
|
| 49 | - * @throws ConnectException |
|
| 50 | - */ |
|
| 46 | + /** |
|
| 47 | + * @inheritDoc |
|
| 48 | + * @throws BadResponseException |
|
| 49 | + * @throws ConnectException |
|
| 50 | + */ |
|
| 51 | 51 | function complete(CheckoutInterface $checkout, AccessToken $accessToken = null):? CheckoutInterface |
| 52 | - { |
|
| 52 | + { |
|
| 53 | 53 | return $this->request('put', $checkout) ? $checkout : null; |
| 54 | - } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @param string $action |
|
| 58 | - * @param CheckoutInterface $checkout |
|
| 56 | + /** |
|
| 57 | + * @param string $action |
|
| 58 | + * @param CheckoutInterface $checkout |
|
| 59 | 59 | * @param string|null $endpoint |
| 60 | - * @return bool|null |
|
| 61 | - */ |
|
| 60 | + * @return bool|null |
|
| 61 | + */ |
|
| 62 | 62 | function request(string $action, $checkout, string $endpoint = null):? bool |
| 63 | - { |
|
| 63 | + { |
|
| 64 | 64 | if (!$checkout->isValid()) { |
| 65 | 65 | throw new InvalidCheckoutException($checkout); |
| 66 | - } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | 68 | return $this->traitRequest($action, $checkout); |
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Generate a body to create a new checkout |
|
| 73 | - * |
|
| 74 | - * @param CheckoutInterface $checkout |
|
| 75 | - * @return array |
|
| 76 | - */ |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Generate a body to create a new checkout |
|
| 73 | + * |
|
| 74 | + * @param CheckoutInterface $checkout |
|
| 75 | + * @return array |
|
| 76 | + */ |
|
| 77 | 77 | protected static function getCheckoutBody(CheckoutInterface $checkout): array |
| 78 | - { |
|
| 79 | - return [ |
|
| 80 | - "checkout_reference" => $checkout->getReference(), |
|
| 81 | - "amount" => $checkout->getAmount(), |
|
| 82 | - "currency" => $checkout->getCurrency(), |
|
| 83 | - "fee_amount" => $checkout->getFeeAmount(), |
|
| 84 | - "pay_to_email" => $checkout->getPayToEmail(), |
|
| 85 | - "pay_from_email" => $checkout->getPayFromEmail(), |
|
| 86 | - "description" => $checkout->getDescription(), |
|
| 87 | - "return_url" => $checkout->getRedirectUrl(), |
|
| 88 | - ]; |
|
| 89 | - } |
|
| 78 | + { |
|
| 79 | + return [ |
|
| 80 | + "checkout_reference" => $checkout->getReference(), |
|
| 81 | + "amount" => $checkout->getAmount(), |
|
| 82 | + "currency" => $checkout->getCurrency(), |
|
| 83 | + "fee_amount" => $checkout->getFeeAmount(), |
|
| 84 | + "pay_to_email" => $checkout->getPayToEmail(), |
|
| 85 | + "pay_from_email" => $checkout->getPayFromEmail(), |
|
| 86 | + "description" => $checkout->getDescription(), |
|
| 87 | + "return_url" => $checkout->getRedirectUrl(), |
|
| 88 | + ]; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | 91 | /** |
| 92 | 92 | * @param CheckoutInterface $checkout |
@@ -94,74 +94,74 @@ discard block |
||
| 94 | 94 | * @return array |
| 95 | 95 | */ |
| 96 | 96 | static function getBody($checkout, string $type = null): array |
| 97 | - { |
|
| 98 | - $defaultBody = self::getCheckoutBody($checkout); |
|
| 99 | - switch ($checkout->getType()) { |
|
| 100 | - case 'card': |
|
| 101 | - $completeBody = self::getCardBody($checkout); |
|
| 102 | - break; |
|
| 103 | - |
|
| 104 | - case 'boleto': |
|
| 105 | - $completeBody = self::getBoletoBody($checkout); |
|
| 106 | - break; |
|
| 107 | - |
|
| 108 | - default: |
|
| 109 | - $completeBody = []; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return array_merge($defaultBody, $completeBody); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - private static function getCardBody(CheckoutInterface $checkout): array |
|
| 116 | - { |
|
| 117 | - return [ |
|
| 118 | - 'payment_type' => $checkout->getType(), |
|
| 119 | - 'token' => $checkout->getCard()->getToken(), |
|
| 120 | - 'customer_id' => $checkout->getCustomer()->getCustomerId() |
|
| 121 | - ]; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - private static function getBoletoBody(CheckoutInterface $checkout): array |
|
| 125 | - { |
|
| 126 | - $customer = $checkout->getCustomer(); |
|
| 127 | - return [ |
|
| 128 | - 'payment_type' => $checkout->getType(), |
|
| 129 | - 'description' => $checkout->getDescription(), |
|
| 130 | - 'boleto_details' => [ |
|
| 97 | + { |
|
| 98 | + $defaultBody = self::getCheckoutBody($checkout); |
|
| 99 | + switch ($checkout->getType()) { |
|
| 100 | + case 'card': |
|
| 101 | + $completeBody = self::getCardBody($checkout); |
|
| 102 | + break; |
|
| 103 | + |
|
| 104 | + case 'boleto': |
|
| 105 | + $completeBody = self::getBoletoBody($checkout); |
|
| 106 | + break; |
|
| 107 | + |
|
| 108 | + default: |
|
| 109 | + $completeBody = []; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return array_merge($defaultBody, $completeBody); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + private static function getCardBody(CheckoutInterface $checkout): array |
|
| 116 | + { |
|
| 117 | + return [ |
|
| 118 | + 'payment_type' => $checkout->getType(), |
|
| 119 | + 'token' => $checkout->getCard()->getToken(), |
|
| 120 | + 'customer_id' => $checkout->getCustomer()->getCustomerId() |
|
| 121 | + ]; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + private static function getBoletoBody(CheckoutInterface $checkout): array |
|
| 125 | + { |
|
| 126 | + $customer = $checkout->getCustomer(); |
|
| 127 | + return [ |
|
| 128 | + 'payment_type' => $checkout->getType(), |
|
| 129 | + 'description' => $checkout->getDescription(), |
|
| 130 | + 'boleto_details' => [ |
|
| 131 | 131 | // 'cpf_cnpj' => $customer->getCpfCnpj(), |
| 132 | - 'payer_name' => $customer->getName(), |
|
| 133 | - 'payer_address' => $customer->getAddress(), |
|
| 134 | - 'payer_city' => $customer->getAddress()->getCity(), |
|
| 135 | - 'payer_state_code' => $customer->getAddress()->getState(), |
|
| 136 | - 'payer_post_code' => $customer->getAddress()->getPostalCode() |
|
| 137 | - ], |
|
| 138 | - ]; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @inheritDoc |
|
| 143 | - */ |
|
| 132 | + 'payer_name' => $customer->getName(), |
|
| 133 | + 'payer_address' => $customer->getAddress(), |
|
| 134 | + 'payer_city' => $customer->getAddress()->getCity(), |
|
| 135 | + 'payer_state_code' => $customer->getAddress()->getState(), |
|
| 136 | + 'payer_post_code' => $customer->getAddress()->getPostalCode() |
|
| 137 | + ], |
|
| 138 | + ]; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @inheritDoc |
|
| 143 | + */ |
|
| 144 | 144 | static function getScopes(): array |
| 145 | - { |
|
| 146 | - return [ |
|
| 147 | - 'payments', |
|
| 148 | - 'boleto' |
|
| 149 | - ]; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - function getLastResponse(): ResponseInterface |
|
| 153 | - { |
|
| 154 | - return $this->lastResponse; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * return the context used to created the client. |
|
| 159 | - * @return ContextInterface |
|
| 160 | - */ |
|
| 161 | - function getContext(): ContextInterface |
|
| 162 | - { |
|
| 163 | - return $this->context; |
|
| 164 | - } |
|
| 145 | + { |
|
| 146 | + return [ |
|
| 147 | + 'payments', |
|
| 148 | + 'boleto' |
|
| 149 | + ]; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + function getLastResponse(): ResponseInterface |
|
| 153 | + { |
|
| 154 | + return $this->lastResponse; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * return the context used to created the client. |
|
| 159 | + * @return ContextInterface |
|
| 160 | + */ |
|
| 161 | + function getContext(): ContextInterface |
|
| 162 | + { |
|
| 163 | + return $this->context; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | 166 | function getOptions(): array |
| 167 | 167 | { |
@@ -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 | |
@@ -11,48 +11,48 @@ |
||
| 11 | 11 | |
| 12 | 12 | trait PropertyHandler |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @param array $data |
|
| 16 | - */ |
|
| 14 | + /** |
|
| 15 | + * @param array $data |
|
| 16 | + */ |
|
| 17 | 17 | function fillProperties(array $data): void |
| 18 | - { |
|
| 19 | - foreach ($data as $p => $v) |
|
| 20 | - { |
|
| 21 | - $this->fillProperty($p, $v); |
|
| 22 | - } |
|
| 23 | - } |
|
| 18 | + { |
|
| 19 | + foreach ($data as $p => $v) |
|
| 20 | + { |
|
| 21 | + $this->fillProperty($p, $v); |
|
| 22 | + } |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param string $property |
|
| 27 | - * @param mixed $value |
|
| 28 | - */ |
|
| 25 | + /** |
|
| 26 | + * @param string $property |
|
| 27 | + * @param mixed $value |
|
| 28 | + */ |
|
| 29 | 29 | function fillProperty(string $property, $value): void |
| 30 | - { |
|
| 31 | - $property = lcfirst(str_replace('_', '', ucwords($property, '_'))); |
|
| 32 | - if(property_exists(__CLASS__, $property)) |
|
| 33 | - { |
|
| 34 | - $method = sprintf('set%s', ucfirst($property)); |
|
| 35 | - $this->{$method}($value); |
|
| 36 | - } |
|
| 37 | - } |
|
| 30 | + { |
|
| 31 | + $property = lcfirst(str_replace('_', '', ucwords($property, '_'))); |
|
| 32 | + if(property_exists(__CLASS__, $property)) |
|
| 33 | + { |
|
| 34 | + $method = sprintf('set%s', ucfirst($property)); |
|
| 35 | + $this->{$method}($value); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return array |
|
| 41 | - */ |
|
| 42 | - function getPropertyArray(): array |
|
| 43 | - { |
|
| 44 | - $reflection = new \ReflectionClass(__CLASS__); |
|
| 45 | - $properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED); |
|
| 46 | - $data = []; |
|
| 47 | - foreach($properties as $property){ |
|
| 48 | - $prop_name = $property->getName(); |
|
| 49 | - $method = sprintf('get%s', ucfirst($prop_name)); |
|
| 50 | - $form_name = strtolower(preg_replace('/[A-Z]/', '_$0', $prop_name)); |
|
| 51 | - if($reflection->hasMethod($method)){ |
|
| 52 | - $data[$form_name] = $this->{$method}(); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - return $data; |
|
| 56 | - } |
|
| 39 | + /** |
|
| 40 | + * @return array |
|
| 41 | + */ |
|
| 42 | + function getPropertyArray(): array |
|
| 43 | + { |
|
| 44 | + $reflection = new \ReflectionClass(__CLASS__); |
|
| 45 | + $properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED); |
|
| 46 | + $data = []; |
|
| 47 | + foreach($properties as $property){ |
|
| 48 | + $prop_name = $property->getName(); |
|
| 49 | + $method = sprintf('get%s', ucfirst($prop_name)); |
|
| 50 | + $form_name = strtolower(preg_replace('/[A-Z]/', '_$0', $prop_name)); |
|
| 51 | + if($reflection->hasMethod($method)){ |
|
| 52 | + $data[$form_name] = $this->{$method}(); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + return $data; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |