Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BP-3189-add-get-active-subscription-to-retrieve-all-buckaroo-subscriptions #157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}