Skip to content

Commit

Permalink
add method to get issuers for PayByBank (#152)
Browse files Browse the repository at this point in the history
+ add tests for PayByBank & iDeal
+ create trait for reusable
  • Loading branch information
vildanbina authored Nov 13, 2023
1 parent 96c957b commit 8885205
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 25 deletions.
16 changes: 16 additions & 0 deletions src/PaymentMethods/PaymentInitiation/PaymentInitiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace Buckaroo\PaymentMethods\PaymentInitiation;

use Buckaroo\Exceptions\BuckarooException;
use Buckaroo\Models\Model;
use Buckaroo\PaymentMethods\PayablePaymentMethod;
use Buckaroo\PaymentMethods\PaymentInitiation\Models\Pay;
use Buckaroo\Services\TraitHelpers\HasIssuers;
use Buckaroo\Transaction\Response\TransactionResponse;

class PaymentInitiation extends PayablePaymentMethod
{
use HasIssuers {
issuers as traitIssuers;
}
protected string $paymentName = 'PayByBank';
protected array $requiredConfigFields = ['currency', 'returnURL', 'returnURLCancel', 'pushURL'];

Expand All @@ -20,4 +25,15 @@ public function pay(?Model $model = null): TransactionResponse
{
return parent::pay($model ?? new Pay($this->payload));
}

/**
* @return array
* @throws BuckarooException
*/
public function issuers(): array
{
$this->serviceVersion = 1;

return $this->traitIssuers();
}
}
34 changes: 9 additions & 25 deletions src/PaymentMethods/iDeal/iDeal.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@

namespace Buckaroo\PaymentMethods\iDeal;

use Buckaroo\Exceptions\BuckarooException;
use Buckaroo\Models\Model;
use Buckaroo\PaymentMethods\iDeal\Models\Pay;
use Buckaroo\PaymentMethods\PayablePaymentMethod;
use Buckaroo\Transaction\Request\TransactionRequest;
use Buckaroo\Services\TraitHelpers\HasIssuers;
use Buckaroo\Transaction\Response\TransactionResponse;

class iDeal extends PayablePaymentMethod
{
use HasIssuers {
issuers as traitIssuers;
}

/**
* @var string
*/
Expand Down Expand Up @@ -72,33 +77,12 @@ public function instantRefund(?Model $model = null):TransactionResponse

/**
* @return array
* @throws \Buckaroo\Exceptions\BuckarooException
* @throws BuckarooException
*/
public function issuers(): array
{
$request = new TransactionRequest;

try
{
$response = $this->client->specification($request, 'ideal', 2);
} catch (BuckarooException $e)
{
return [];
}

$issuerList = [];
if (isset($response->data()['Actions']['0']['RequestParameters'][0]['ListItemDescriptions']))
{
$issuersData = $response->data()['Actions']['0']['RequestParameters'][0]['ListItemDescriptions'];
if (count($issuersData) > 0)
{
foreach ($issuersData as $issuer)
{
$issuerList[] = ['id' => $issuer['Value'], 'name' => $issuer['Description']];
}
}
}
$this->serviceVersion = 2;

return $issuerList;
return $this->traitIssuers();
}
}
59 changes: 59 additions & 0 deletions src/Services/TraitHelpers/HasIssuers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 [email protected] 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 [email protected] for more information.
*
* @copyright Copyright (c) Buckaroo B.V.
* @license https://tldrlegal.com/license/mit-license
*/

namespace Buckaroo\Services\TraitHelpers;

use Buckaroo\Exceptions\BuckarooException;
use Buckaroo\Transaction\Request\TransactionRequest;

trait HasIssuers
{
/**
* @return array
* @throws BuckarooException
*/
public function issuers(): array
{
$request = new TransactionRequest;

try
{
$response = $this->client->specification($request, $this->paymentName, $this->serviceVersion());
} catch (BuckarooException $e)
{
return [];
}

$issuerList = [];
if (isset($response->data()['Actions']['0']['RequestParameters'][0]['ListItemDescriptions']))
{
$issuersData = $response->data()['Actions']['0']['RequestParameters'][0]['ListItemDescriptions'];
if (count($issuersData) > 0)
{
foreach ($issuersData as $issuer)
{
$issuerList[] = ['id' => $issuer['Value'], 'name' => $issuer['Description']];
}
}
}

return $issuerList;
}
}
17 changes: 17 additions & 0 deletions tests/Buckaroo/Payments/IdealTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ protected function setUp(): void
];
}

/**
* @return void
* @test
*/
public function it_get_ideal_issuers()
{
$response = $this->buckaroo->method('ideal')->issuers();

$this->assertIsArray($response);
foreach ($response as $item)
{
$this->assertIsArray($item);
$this->assertArrayHasKey('id', $item);
$this->assertArrayHasKey('name', $item);
}
}

/**
* @return void
* @test
Expand Down
16 changes: 16 additions & 0 deletions tests/Buckaroo/Payments/PaymentInitiation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@

class PaymentInitiation extends BuckarooTestCase
{
/**
* @return void
* @test
*/
public function it_get_payment_initiation_issuers()
{
$response = $this->buckaroo->method('paybybank')->issuers();

$this->assertIsArray($response);
foreach ($response as $item)
{
$this->assertIsArray($item);
$this->assertArrayHasKey('id', $item);
$this->assertArrayHasKey('name', $item);
}
}

/**
* @test
Expand Down

0 comments on commit 8885205

Please sign in to comment.