-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BP-3189-add-get-active-subscription-to-retrieve-all-buckaroo-subscrip…
…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
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |