diff --git a/example/additional_services/getActiveSubscriptions.php b/example/additional_services/getActiveSubscriptions.php new file mode 100644 index 0000000..e7d2d8e --- /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 ad682cc..e86c257 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 0000000..e5fb4f8 --- /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/tests/Buckaroo/GetActiveSubscription.php b/tests/Buckaroo/GetActiveSubscription.php new file mode 100644 index 0000000..cc83d08 --- /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]); + } +}