Skip to content

Commit

Permalink
CC-1768 : Add Open Banking
Browse files Browse the repository at this point in the history
  • Loading branch information
m-xhixha committed Nov 13, 2024
1 parent 011cc4e commit a78ba94
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Constants/IdStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class IdStrings
public const TWINT = 'twt';
public const WECHATPAY = 'wcp';

public const OPEN_BANKING = 'obp';

// Resources
public const BASKET = 'bsk';
public const CUSTOMER = 'cst';
Expand Down Expand Up @@ -93,5 +95,6 @@ class IdStrings
self::SOFORT,
self::TWINT,
self::WECHATPAY,
self::OPEN_BANKING,
];
}
41 changes: 41 additions & 0 deletions src/Resources/PaymentTypes/OpenBanking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace UnzerSDK\Resources\PaymentTypes;

use UnzerSDK\Traits\CanAuthorize;
use UnzerSDK\Traits\CanDirectCharge;

class OpenBanking extends BasePaymentType
{
use CanAuthorize;
use CanDirectCharge;

/** @var string|null $ibanCountry */
protected $ibanCountry;

public function __construct(string $ibanCountry = null)
{
$this->ibanCountry = $ibanCountry;
}

/**
* @return string|null
*/
public function getIbanCountry(): ?string
{
return $this->ibanCountry;
}

/**
* @param string|null $ibanCountry
*
* @return OpenBanking
*/
public function setIbanCountry(string $ibanCountry): OpenBanking
{
$this->ibanCountry = $ibanCountry;
return $this;
}


}
4 changes: 4 additions & 0 deletions src/Services/ResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use UnzerSDK\Resources\PaymentTypes\Invoice;
use UnzerSDK\Resources\PaymentTypes\InvoiceSecured;
use UnzerSDK\Resources\PaymentTypes\Klarna;
use UnzerSDK\Resources\PaymentTypes\OpenBanking;
use UnzerSDK\Resources\PaymentTypes\PaylaterDirectDebit;
use UnzerSDK\Resources\PaymentTypes\PaylaterInstallment;
use UnzerSDK\Resources\PaymentTypes\PaylaterInvoice;
Expand Down Expand Up @@ -945,6 +946,9 @@ public static function getTypeInstanceFromIdString($typeId): BasePaymentType
case IdStrings::WECHATPAY:
$paymentType = new Wechatpay();
break;
case IdStrings::OPEN_BANKING:
$paymentType = new OpenBanking();
break;
default:
throw new RuntimeException('Invalid payment type!');
break;
Expand Down
3 changes: 3 additions & 0 deletions test/Fixtures/jsonData/openBanking/createRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ibanCountry": "DE"
}
14 changes: 14 additions & 0 deletions test/Fixtures/jsonData/openBanking/fetchResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "s-obp-q0nucec6itwe",
"method": "openbanking-pis",
"recurring": false,
"geoLocation": {
"clientIp": "0:0:0:0:0:0:0:1",
"countryIsoA2": ""
},
"processing": {
"uniqueId": "31HA07BC8127DC45EE6946C3B070FF71",
"shortId": "5550.0369.6888",
"traceId": "24c0a2ecbe3d54a838c76444d4bdcd1f"
}
}
81 changes: 81 additions & 0 deletions test/integration/PaymentTypes/OpenBankingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpDocMissingThrowsInspection */
/**
* This class defines integration tests to verify interface and functionality of the payment method paypal.
*
* @link https://docs.unzer.com/
*
*/

namespace UnzerSDK\test\integration\PaymentTypes;

use UnzerSDK\Resources\PaymentTypes\BasePaymentType;
use UnzerSDK\Resources\PaymentTypes\OpenBanking;
use UnzerSDK\test\BaseIntegrationTest;

class OpenBankingTest extends BaseIntegrationTest
{
/**
* Verify OpenBanking payment type can be created and fetched.
*
* @test
*
* @return BasePaymentType
*/
public function openBankingShouldBeCreatableAndFetchable(): BasePaymentType
{
$openBanking = $this->unzer->createPaymentType(new OpenBanking('DE'));
$this->assertInstanceOf(OpenBanking::class, $openBanking);
$this->assertNotEmpty($openBanking->getId());

$fetchedOpenBanking = $this->unzer->fetchPaymentType($openBanking->getId());
$this->assertInstanceOf(OpenBanking::class, $fetchedOpenBanking);
$this->assertNotSame($openBanking, $fetchedOpenBanking);
$this->assertEquals($openBanking->expose(), $fetchedOpenBanking->expose());

return $fetchedOpenBanking;
}



/**
* Verify OpenBanking can authorize.
*
* @test
*
* @depends openBankingShouldBeCreatableAndFetchable
*
* @param OpenBanking $openBanking
*/
public function openBankingShouldBeAuthorizable(OpenBanking $openBanking): void
{
$authorization = $openBanking->authorize(100.0, 'EUR', self::RETURN_URL);
$this->assertNotNull($authorization);
$this->assertNotEmpty($authorization->getId());
$this->assertNotEmpty($authorization->getRedirectUrl());

$payment = $authorization->getPayment();
$this->assertNotNull($payment);
$this->assertTrue($payment->isPending());
}

/**
* Verify OpenBanking can charge.
*
* @test
*
* @depends openBankingShouldBeCreatableAndFetchable
*
* @param OpenBanking $openBanking
*/
public function openBankingShouldBeChargeable(OpenBanking $openBanking): void
{
$charge = $openBanking->charge(100.0, 'EUR', self::RETURN_URL);
$this->assertNotNull($charge);
$this->assertNotEmpty($charge->getId());
}


}
5 changes: 4 additions & 1 deletion test/integration/Resources/PaypageV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use UnzerSDK\Resources\PaymentTypes\Googlepay;
use UnzerSDK\Resources\PaymentTypes\Ideal;
use UnzerSDK\Resources\PaymentTypes\Klarna;
use UnzerSDK\Resources\PaymentTypes\OpenBanking;
use UnzerSDK\Resources\PaymentTypes\PaylaterDirectDebit;
use UnzerSDK\Resources\PaymentTypes\PaylaterInstallment;
use UnzerSDK\Resources\PaymentTypes\PaylaterInvoice;
Expand Down Expand Up @@ -295,7 +296,9 @@ public function paymentMethodsConfigsDataProvider()
->addMethodConfig(Bancontact::class, $enabledConfig)
->addMethodConfig(PostFinanceEfinance::class, $enabledConfig)
->addMethodConfig(PostFinanceCard::class, $enabledConfig)
->addMethodConfig(Twint::class, $enabledConfig);
->addMethodConfig(Twint::class, $enabledConfig)
->addMethodConfig(OpenBanking::class, $enabledConfig)
;

$withPaylaterConfig = (new PaymentMethodsConfigs())
->addMethodConfig(PaylaterInvoice::class, $paylaterConfig);
Expand Down
56 changes: 56 additions & 0 deletions test/unit/Resources/PaymentTypes/OpenBankingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace UnzerSDK\test\unit\Resources\PaymentTypes;

use UnzerSDK\Resources\PaymentTypes\Clicktopay;
use UnzerSDK\Resources\PaymentTypes\OpenBanking;
use UnzerSDK\test\BasePaymentTest;
use UnzerSDK\test\Fixtures\JsonProvider;

class OpenBankingTest extends BasePaymentTest
{
/**
* Verify the resource data is set properly.
*
* @test
*/
public function constructorShouldSetParameters(): void
{
$countryCode = 'DE';

$openBanking = new OpenBanking($countryCode);


$this->assertEquals($countryCode, $openBanking->getIbanCountry());
}

/**
* Test OpenBanking json serialization.
*
* @test
*/
public function jsonSerialization(): void
{
$openBankingObject = new OpenBanking("DE",);

$expectedJson = JsonProvider::getJsonFromFile('openBanking/createRequest.json');
$this->assertJsonStringEqualsJsonString($expectedJson, $openBankingObject->jsonSerialize());
}

/**
* Test OpenBanking json response handling.
*
* @test
*/
public function openBankingAuthorizationShouldBeMappedCorrectly(): void
{
$openBanking = new OpenBanking('DE');

$jsonResponse = JsonProvider::getJsonFromFile('openBanking/fetchResponse.json');

$jsonObject = json_decode($jsonResponse, false, 512, JSON_THROW_ON_ERROR);
$openBanking->handleResponse($jsonObject);

$this->assertEquals('s-obp-q0nucec6itwe', $openBanking->getId());
}
}
3 changes: 3 additions & 0 deletions test/unit/Services/ResourceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use UnzerSDK\Resources\PaymentTypes\Invoice;
use UnzerSDK\Resources\PaymentTypes\InvoiceSecured;
use UnzerSDK\Resources\PaymentTypes\Klarna;
use UnzerSDK\Resources\PaymentTypes\OpenBanking;
use UnzerSDK\Resources\PaymentTypes\PaylaterInvoice;
use UnzerSDK\Resources\PaymentTypes\Paypal;
use UnzerSDK\Resources\PaymentTypes\PIS;
Expand Down Expand Up @@ -1424,6 +1425,7 @@ public function fetchShouldCallFetchResourceDP(): array
'PaymentType HirePurchaseDirectDebit sandbox' => ['fetchPaymentType', ['s-hdd-12345678'], $getPaymentTypeCB(InstallmentSecured::class)],
'PaymentType InstallmentSecured sandbox' => ['fetchPaymentType', ['s-ins-12345678'], $getPaymentTypeCB(InstallmentSecured::class)],
'PaymentType Bancontact sandbox' => ['fetchPaymentType', ['s-bct-12345678'], $getPaymentTypeCB(Bancontact::class)],
'PaymentType OpenBanking sandbox' => ['fetchPaymentType', ['s-obp-12345678'], $getPaymentTypeCB(OpenBanking::class)],
'PaymentType Alipay production' => ['fetchPaymentType', ['p-ali-12345678'], $getPaymentTypeCB(Alipay::class)],
'PaymentType Bancontact production' => ['fetchPaymentType', ['p-bct-12345678'], $getPaymentTypeCB(Bancontact::class)],
'PaymentType Card production' => ['fetchPaymentType', ['p-crd-12345678'], $getPaymentTypeCB(Card::class)],
Expand All @@ -1446,6 +1448,7 @@ public function fetchShouldCallFetchResourceDP(): array
'PaymentType SepaDirectDebitSecured production' => ['fetchPaymentType', ['p-dds-12345678'], $getPaymentTypeCB(SepaDirectDebitSecured::class)],
'PaymentType Sofort production' => ['fetchPaymentType', ['p-sft-12345678'], $getPaymentTypeCB(Sofort::class)],
'PaymentType Wechatpay production' => ['fetchPaymentType', ['p-wcp-12345678'], $getPaymentTypeCB(Wechatpay::class)],
'PaymentType OpenBanking production' => ['fetchPaymentType', ['p-obp-12345678'], $getPaymentTypeCB(OpenBanking::class)],
];
}

Expand Down

0 comments on commit a78ba94

Please sign in to comment.