From e2c98fe16fe70fa1a3585b1024c7ffe8ab270eae Mon Sep 17 00:00:00 2001 From: Ivascu Madalin Date: Tue, 28 Nov 2023 09:44:05 +0200 Subject: [PATCH 1/6] BP-3189 Add "GetActiveSubscription" to retrieve all Buckaroo subscriptions --- .../getActiveSubscriptions.php | 17 +++ src/BuckarooClient.php | 7 ++ src/Services/ActiveSubscriptions.php | 101 ++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 example/additional_services/getActiveSubscriptions.php create mode 100644 src/Services/ActiveSubscriptions.php diff --git a/example/additional_services/getActiveSubscriptions.php b/example/additional_services/getActiveSubscriptions.php new file mode 100644 index 00000000..7c587440 --- /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/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/Services/ActiveSubscriptions.php b/src/Services/ActiveSubscriptions.php new file mode 100644 index 00000000..fe8b696d --- /dev/null +++ b/src/Services/ActiveSubscriptions.php @@ -0,0 +1,101 @@ +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) { + $formatedSubscription[lcfirst($key)] = $subscriptionData; + } + $formated[] = $formatedSubscription; + } + return $formated; + } +} From b1947753619671a945d39e8812d56469fbc16111 Mon Sep 17 00:00:00 2001 From: Ivascu Madalin Date: Tue, 28 Nov 2023 10:51:01 +0200 Subject: [PATCH 2/6] add tests --- tests/Buckaroo/GetActiveSubscription.php | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/Buckaroo/GetActiveSubscription.php diff --git a/tests/Buckaroo/GetActiveSubscription.php b/tests/Buckaroo/GetActiveSubscription.php new file mode 100644 index 00000000..278e03ba --- /dev/null +++ b/tests/Buckaroo/GetActiveSubscription.php @@ -0,0 +1,38 @@ +buckaroo->getActiveSubscriptions(); + $this->assertIsArray($response); + $this->assertArrayHasKey('serviceCode',$response[0]); + $this->assertArrayHasKey('currencies',$response[0]); + } +} From b5373e16e4fa4f2d5bce848b3111b7789e0bbf3b Mon Sep 17 00:00:00 2001 From: Ivascu Madalin Date: Tue, 28 Nov 2023 11:09:57 +0200 Subject: [PATCH 3/6] format currency --- src/Services/ActiveSubscriptions.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Services/ActiveSubscriptions.php b/src/Services/ActiveSubscriptions.php index fe8b696d..f4d6b8fe 100644 --- a/src/Services/ActiveSubscriptions.php +++ b/src/Services/ActiveSubscriptions.php @@ -92,10 +92,31 @@ private function format($data): array foreach ($decoded as $subscription) { $formatedSubscription = []; foreach ($subscription as $key => $subscriptionData) { - $formatedSubscription[lcfirst($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; + } } From e267bb87085a3e932572d99cd7a79c38a8c2f53e Mon Sep 17 00:00:00 2001 From: Ivascu Madalin Date: Tue, 28 Nov 2023 11:27:01 +0200 Subject: [PATCH 4/6] fix formatting --- example/additional_services/getActiveSubscriptions.php | 2 +- tests/Buckaroo/GetActiveSubscription.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/example/additional_services/getActiveSubscriptions.php b/example/additional_services/getActiveSubscriptions.php index 7c587440..e7d2d8e6 100644 --- a/example/additional_services/getActiveSubscriptions.php +++ b/example/additional_services/getActiveSubscriptions.php @@ -9,7 +9,7 @@ try { foreach ($buckaroo->getActiveSubscriptions() as $subscription) { $availableCurrencies = implode(",", $subscription['currencies']); - echo "serviceCode: " . $subscription['serviceCode'] . "\n";; + echo "serviceCode: " . $subscription['serviceCode'] . "\n"; echo "- available currencies: " . $availableCurrencies . "\n"; } } catch (\Throwable $th) { diff --git a/tests/Buckaroo/GetActiveSubscription.php b/tests/Buckaroo/GetActiveSubscription.php index 278e03ba..cc83d082 100644 --- a/tests/Buckaroo/GetActiveSubscription.php +++ b/tests/Buckaroo/GetActiveSubscription.php @@ -32,7 +32,7 @@ public function it_gets_the_active_subscriptions() { $response = $this->buckaroo->getActiveSubscriptions(); $this->assertIsArray($response); - $this->assertArrayHasKey('serviceCode',$response[0]); - $this->assertArrayHasKey('currencies',$response[0]); + $this->assertArrayHasKey('serviceCode', $response[0]); + $this->assertArrayHasKey('currencies', $response[0]); } } From e1b0b893e2756fd0f671c1f0c7039051a2ce21ed Mon Sep 17 00:00:00 2001 From: Ivascu Madalin Date: Tue, 28 Nov 2023 11:32:04 +0200 Subject: [PATCH 5/6] fix x --- src/Services/ActiveSubscriptions.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Services/ActiveSubscriptions.php b/src/Services/ActiveSubscriptions.php index f4d6b8fe..0807568b 100644 --- a/src/Services/ActiveSubscriptions.php +++ b/src/Services/ActiveSubscriptions.php @@ -88,7 +88,6 @@ private function format($data): array } $formated = []; - foreach ($decoded as $subscription) { $formatedSubscription = []; foreach ($subscription as $key => $subscriptionData) { From 7e65e5707656b73e9056c9e9afc08f477df11746 Mon Sep 17 00:00:00 2001 From: Ivascu Madalin Date: Tue, 28 Nov 2023 11:33:59 +0200 Subject: [PATCH 6/6] fix pipe error --- src/Services/ActiveSubscriptions.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Services/ActiveSubscriptions.php b/src/Services/ActiveSubscriptions.php index 0807568b..e5fb4f84 100644 --- a/src/Services/ActiveSubscriptions.php +++ b/src/Services/ActiveSubscriptions.php @@ -45,7 +45,6 @@ public function __construct(Client $client) public function get(): array { try { - $xmlString = $this->client ->dataRequest($this->buildTransaction()) ->getServiceParameters()[self::SERVICE_PARAM_KEY] ?? null;