diff --git a/composer.json b/composer.json index 7e30bcd0..fc972a9c 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "buckaroo/sdk", "description": "Buckaroo payment SDK", "license": "MIT", - "version": "1.9.0", + "version": "1.9.1", "type": "library", "require": { "php": ">=7.4|^8.0", diff --git a/example/additional_services/getActiveSubscriptions.php b/example/additional_services/getActiveSubscriptions.php new file mode 100644 index 00000000..e7d2d8e6 --- /dev/null +++ b/example/additional_services/getActiveSubscriptions.php @@ -0,0 +1,17 @@ +getActiveSubscriptions() as $subscription) { + $availableCurrencies = implode(",", $subscription['currencies']); + echo "serviceCode: " . $subscription['serviceCode'] . "\n"; + echo "- available currencies: " . $availableCurrencies . "\n"; + } +} catch (\Throwable $th) { + var_dump($th); +} diff --git a/example/transactions/externalPayment.php b/example/transactions/externalPayment.php new file mode 100644 index 00000000..85a881fa --- /dev/null +++ b/example/transactions/externalPayment.php @@ -0,0 +1,19 @@ +method('externalPayment')->pay([ + 'invoice' => uniqid(), + 'amountDebit' => 10.10, + 'channel' => 'BackOffice' +]); + +$buckaroo->method('externalPayment')->refund([ + 'amountCredit' => 10, + 'invoice' => 'testinvoice 123', + 'originalTransactionKey' => $response->getTransactionKey(), +]); \ No newline at end of file diff --git a/src/BuckarooClient.php b/src/BuckarooClient.php index ad682cc4..e86c2577 100644 --- a/src/BuckarooClient.php +++ b/src/BuckarooClient.php @@ -27,6 +27,7 @@ use Buckaroo\Handlers\Logging\Observer as LoggingObserver; use Buckaroo\PaymentMethods\BatchTransactions; use Buckaroo\PaymentMethods\PaymentFacade; +use Buckaroo\Services\ActiveSubscriptions; use Buckaroo\Services\TransactionService; use Buckaroo\Transaction\Client; @@ -73,6 +74,12 @@ public function method(string $method = null): PaymentFacade return new PaymentFacade($this->client, $method); } + + public function getActiveSubscriptions(): array + { + return (new ActiveSubscriptions($this->client))->get(); + } + /** * @param array $transactions * @return BatchTransactions diff --git a/src/PaymentMethods/ExternalPayment/ExternalPayment.php b/src/PaymentMethods/ExternalPayment/ExternalPayment.php new file mode 100644 index 00000000..f10e6d07 --- /dev/null +++ b/src/PaymentMethods/ExternalPayment/ExternalPayment.php @@ -0,0 +1,30 @@ + ['payperemail'], PaymentInitiation::class => ['paymentinitiation','paybybank'], EPS::class => ['eps'], + ExternalPayment::class => ['externalpayment'], Emandates::class => ['emandates'], Sofort::class => ['sofort', 'sofortueberweisung'], Tinka::class => ['tinka'], diff --git a/src/Services/ActiveSubscriptions.php b/src/Services/ActiveSubscriptions.php new file mode 100644 index 00000000..e5fb4f84 --- /dev/null +++ b/src/Services/ActiveSubscriptions.php @@ -0,0 +1,120 @@ +client = $client; + } + + public function get(): array + { + try { + $xmlString = $this->client + ->dataRequest($this->buildTransaction()) + ->getServiceParameters()[self::SERVICE_PARAM_KEY] ?? null; + + if (!is_string($xmlString)) { + return []; + } + + $xml = new SimpleXMLElement($xmlString, LIBXML_NOCDATA); + + return $this->format( + $xml->xpath('/ArrayOfServiceCurrencies/ServiceCurrencies') + ); + } catch (Exception $e) { + return []; + } + } + + private function buildTransaction(): TransactionRequest + { + $transaction = new TransactionRequest(); + + $transaction + ->getServices() + ->pushServiceList( + new ServiceList( + self::SERVICE_CODE_AND_ACTION, + self::VERSION_ZERO, + self::SERVICE_CODE_AND_ACTION + ) + ); + return $transaction; + } + + private function format($data): array + { + $decoded = json_decode(json_encode($data), true); + if (!is_array($decoded)) { + return []; + } + + $formated = []; + foreach ($decoded as $subscription) { + $formatedSubscription = []; + foreach ($subscription as $key => $subscriptionData) { + $camelKey = lcfirst($key); + $formatedSubscription[$camelKey] = $this->formatValue($camelKey, $subscriptionData); + } + $formated[] = $formatedSubscription; + } + return $formated; + } + + /** + * Format value for currency + * + * @param string $key + * @param string|array $value + * + * @return string|array + */ + private function formatValue($key, $value) + { + if ($key === 'currencies') { + $value = $value["string"]; + if (is_string($value)) { + $value = [$value]; + } + } + + return $value; + } +} diff --git a/src/Transaction/Client.php b/src/Transaction/Client.php index b3116fac..a1c4d878 100644 --- a/src/Transaction/Client.php +++ b/src/Transaction/Client.php @@ -31,7 +31,7 @@ use Buckaroo\Services\TransactionHeaders\DefaultHeader; use Buckaroo\Services\TransactionHeaders\HmacHeader; use Buckaroo\Services\TransactionHeaders\SoftwareHeader; -use Buckaroo\Transaction\Request\HttpClient\HttpClientGuzzle; +use Buckaroo\Transaction\Request\HttpClient\HttpClientFactory; use Buckaroo\Transaction\Request\HttpClient\HttpClientInterface; use Buckaroo\Transaction\Request\Request; use Buckaroo\Transaction\Response\Response; @@ -43,7 +43,7 @@ class Client private const METHOD_POST = 'POST'; /** - * @var HttpClientInterface|HttpClientGuzzle + * @var HttpClientInterface */ protected HttpClientInterface $httpClient; /** @@ -61,7 +61,7 @@ class Client public function __construct(?Config $config) { $this->config = $config; - $this->httpClient = new HttpClientGuzzle($config->getLogger()); + $this->httpClient = HttpClientFactory::createClient($config->getLogger()); } /** diff --git a/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php new file mode 100644 index 00000000..acb11dc5 --- /dev/null +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php @@ -0,0 +1,70 @@ +logger = $logger; + + $this->httpClient = new Client([ + 'timeout' => self::TIMEOUT, + 'connect_timeout' => self::CONNECT_TIMEOUT, + ]); + } + + /** + * @param string $url + * @param array $headers + * @param string $method + * @param string|null $data + * @return array|mixed + * @throws TransferException + * @throws BuckarooException|GuzzleException + */ + + public function call(string $url, array $headers, string $method, string $data = null) + { + $headers = $this->convertHeadersFormat($headers); + + $request = $this->httpClient->createRequest($method, $url, [ + 'headers' => $headers, + 'body' => $data, + ]); + + try + { + $response = $this->httpClient->send($request); + + $result = (string) $response->getBody(); + + $this->logger->info('RESPONSE HEADERS: ' . json_encode($response->getHeaders())); + $this->logger->info('RESPONSE BODY: ' . $response->getBody()); + } catch (RequestException $e) { + throw new TransferException($this->logger, "Transfer failed", 0, $e); + } + + $result = $this->getDecodedResult($response, $result); + + return [ + $response, + $result, + ]; + } +} diff --git a/src/Transaction/Request/HttpClient/HttpClientGuzzle.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php similarity index 67% rename from src/Transaction/Request/HttpClient/HttpClientGuzzle.php rename to src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php index d15a2fd7..015b4e09 100644 --- a/src/Transaction/Request/HttpClient/HttpClientGuzzle.php +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php @@ -1,27 +1,8 @@ logger->info('RESPONSE HEADERS: ' . json_encode($response->getHeaders())); $this->logger->info('RESPONSE BODY: ' . $response->getBody()); - } catch (GuzzleException $e) - { + } catch (GuzzleException $e) { throw new TransferException($this->logger, "Transfer failed", 0, $e); } diff --git a/src/Transaction/Request/HttpClient/HttpClientFactory.php b/src/Transaction/Request/HttpClient/HttpClientFactory.php new file mode 100644 index 00000000..1c7a1220 --- /dev/null +++ b/src/Transaction/Request/HttpClient/HttpClientFactory.php @@ -0,0 +1,23 @@ +buckaroo->getActiveSubscriptions(); + $this->assertIsArray($response); + $this->assertArrayHasKey('serviceCode', $response[0]); + $this->assertArrayHasKey('currencies', $response[0]); + } +} diff --git a/tests/Buckaroo/Payments/ExternalPaymentTest.php b/tests/Buckaroo/Payments/ExternalPaymentTest.php new file mode 100644 index 00000000..5b0338c3 --- /dev/null +++ b/tests/Buckaroo/Payments/ExternalPaymentTest.php @@ -0,0 +1,56 @@ +buckaroo->method('externalPayment')->pay([ + 'invoice' => uniqid(), + 'amountDebit' => 11.10, + 'channel' => 'BACKOFFICE' + ]); + + $this->assertTrue($response->isSuccess()); + } + + /** + * @test + */ + public function it_creates_a_external_refund() + { + $response = $this->buckaroo->method('externalPayment')->refund([ + 'amountCredit' => 10, + 'invoice' => 'testinvoice 123', + 'description' => 'refund', + 'originalTransactionKey' => '2D04704995B74D679AACC59F87XXXXXX', + ]); + + $this->assertTrue($response->isFailed()); + } +}