Skip to content

Commit

Permalink
BP-3189-add-get-active-subscription-to-retrieve-all-buckaroo-subscrip…
Browse files Browse the repository at this point in the history
…tions (#157)

* BP-3189 Add "GetActiveSubscription" to retrieve all Buckaroo subscriptions

* add tests

* format currency

* fix formatting

* fix x

* fix pipe error

---------

Co-authored-by: Ivascu Madalin <madalin.ivascu@arnia.ro>
  • Loading branch information
harli91 and Ivascu Madalin authored Nov 28, 2023
1 parent 3d3c2c7 commit f8271d3
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
17 changes: 17 additions & 0 deletions example/additional_services/getActiveSubscriptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once __DIR__ . '/../bootstrap.php';

use Buckaroo\BuckarooClient;

$buckaroo = new BuckarooClient($_ENV['BPE_WEBSITE_KEY'], $_ENV['BPE_SECRET_KEY']);

try {
foreach ($buckaroo->getActiveSubscriptions() as $subscription) {
$availableCurrencies = implode(",", $subscription['currencies']);
echo "serviceCode: " . $subscription['serviceCode'] . "\n";
echo "- available currencies: " . $availableCurrencies . "\n";
}
} catch (\Throwable $th) {
var_dump($th);
}
7 changes: 7 additions & 0 deletions src/BuckarooClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
120 changes: 120 additions & 0 deletions src/Services/ActiveSubscriptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* It is available through the world-wide-web at this URL:
* https://tldrlegal.com/license/mit-license
* If you are unable to obtain it through the world-wide-web, please send an email
* to support@buckaroo.nl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please contact support@buckaroo.nl for more information.
*
* @copyright Copyright (c) Buckaroo B.V.
* @license https://tldrlegal.com/license/mit-license
*/

namespace Buckaroo\Services;

use Exception;
use SimpleXMLElement;
use Buckaroo\Models\ServiceList;
use Buckaroo\Transaction\Client;
use Buckaroo\Transaction\Request\TransactionRequest;

class ActiveSubscriptions
{

private const SERVICE_CODE_AND_ACTION = 'GetActiveSubscriptions';

private const VERSION_ZERO = 0;

private const SERVICE_PARAM_KEY = 'activesubscriptions';

private Client $client;

public function __construct(Client $client)
{
$this->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;
}
}
38 changes: 38 additions & 0 deletions tests/Buckaroo/GetActiveSubscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* It is available through the world-wide-web at this URL:
* https://tldrlegal.com/license/mit-license
* If you are unable to obtain it through the world-wide-web, please send an email
* to support@buckaroo.nl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please contact support@buckaroo.nl for more information.
*
* @copyright Copyright (c) Buckaroo B.V.
* @license https://tldrlegal.com/license/mit-license
*/

namespace Tests\Buckaroo;

use Tests\Buckaroo\BuckarooTestCase;

class GetActiveSubscription extends BuckarooTestCase
{
/**
* @return void
* @test
*/
public function it_gets_the_active_subscriptions()
{
$response = $this->buckaroo->getActiveSubscriptions();
$this->assertIsArray($response);
$this->assertArrayHasKey('serviceCode', $response[0]);
$this->assertArrayHasKey('currencies', $response[0]);
}
}

0 comments on commit f8271d3

Please sign in to comment.