Skip to content

Commit 8e27f35

Browse files
PHPSDK-192 Add isWallet method to PaymentMethod (#403)
1 parent 3f014a3 commit 8e27f35

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

src/Api/PaymentMethods/PaymentMethod.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class PaymentMethod
2929
public const TOKENIZATION_KEY = 'tokenization';
3030
public const TOKENIZATION_MODELS_KEY = 'models';
3131
public const IS_ENABLED_KEY = 'is_enabled';
32+
public const IS_WALLET_KEY = 'is_wallet';
3233
public const SHOPPING_CART_REQUIRED_KEY = 'shopping_cart_required';
3334
public const PREFERRED_COUNTRIES_KEY = 'preferred_countries';
3435
public const ALLOWED_COUNTRIES_KEY = 'allowed_countries';
@@ -120,6 +121,11 @@ class PaymentMethod
120121
*/
121122
private $manualCapture;
122123

124+
/**
125+
* @var bool
126+
*/
127+
private $isWallet;
128+
123129
/**
124130
* Transaction constructor.
125131
* @param array $data
@@ -142,6 +148,7 @@ public function __construct(array $data)
142148
$this->iconUrls = $data[self::ICON_URLS_KEY] ?? '';
143149
$this->requiredCustomerData = $data[self::REQUIRED_CUSTOMER_DATA_KEY] ?? null;
144150
$this->manualCapture = $data[self::MANUAL_CAPTURE_KEY] ?? [];
151+
$this->isWallet = (bool)($data[self::IS_WALLET_KEY] ?? false);
145152
}
146153

147154
/**
@@ -374,6 +381,14 @@ public function isCoupon()
374381
return $this->getType() === self::COUPON_TYPE;
375382
}
376383

384+
/**
385+
* @return bool
386+
*/
387+
public function isWallet(): bool
388+
{
389+
return $this->isWallet;
390+
}
391+
377392
/**
378393
* Return an array with the payment method object information
379394
*
@@ -393,6 +408,7 @@ public function getData(): array
393408
self::ALLOWED_CURRENCIES_KEY => $this->allowedCurrencies,
394409
self::ALLOWED_COUNTRIES_KEY => $this->allowedCountries,
395410
self::APPS_KEY => $this->apps,
411+
self::IS_WALLET_KEY => $this->isWallet,
396412
self::TOKENIZATION_KEY => [
397413
self::IS_ENABLED_KEY => $this->tokenization[self::IS_ENABLED_KEY] ?? false,
398414
self::TOKENIZATION_MODELS_KEY => [

tests/Unit/Api/PaymentMethods/PaymentMethodTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php declare(strict_types=1);
22
namespace MultiSafepay\Tests\Unit\Api\PaymentMethods;
33

4+
use Exception;
45
use MultiSafepay\Api\PaymentMethodManager;
56
use MultiSafepay\Api\PaymentMethods\PaymentMethod;
67
use MultiSafepay\Exception\InvalidDataInitializationException;
78
use MultiSafepay\Tests\Integration\MockClient;
89
use PHPUnit\Framework\TestCase;
10+
use Psr\Http\Client\ClientExceptionInterface;
911

1012
/**
1113
* Class PaymentMethodTest
@@ -23,9 +25,11 @@ class PaymentMethodTest extends TestCase
2325
*/
2426
private $wrongData;
2527

26-
2728
/**
2829
* Test normal initialization
30+
*
31+
* @throws Exception
32+
* @throws ClientExceptionInterface
2933
*/
3034
public function testNormalInitialization()
3135
{
@@ -50,6 +54,9 @@ public function testNormalInitialization()
5054

5155
/**
5256
* Test wrong initialization
57+
*
58+
* @throws Exception
59+
* @throws ClientExceptionInterface
5360
*/
5461
public function testIncompleteData()
5562
{
@@ -151,6 +158,65 @@ public function testGetDataIncludesManualCaptureWhenEnabled()
151158
$this->assertTrue($result[PaymentMethod::MANUAL_CAPTURE_KEY][PaymentMethod::IS_ENABLED_KEY]);
152159
$this->assertTrue($result[PaymentMethod::MANUAL_CAPTURE_KEY][PaymentMethod::SUPPORTED_KEY]);
153160
}
161+
162+
/**
163+
* Test if the wallet payment method is detected from the payload
164+
*
165+
* @throws InvalidDataInitializationException
166+
*/
167+
public function testIsWalletReturnsTrueWhenPayloadMarksWallet()
168+
{
169+
$data = $this->getValidData();
170+
$data[PaymentMethod::IS_WALLET_KEY] = true;
171+
172+
$paymentMethod = new PaymentMethod($data);
173+
174+
$this->assertTrue($paymentMethod->isWallet());
175+
}
176+
177+
/**
178+
* Test if a non-wallet payment method is detected from the payload
179+
*
180+
* @throws InvalidDataInitializationException
181+
*/
182+
public function testIsWalletReturnsFalseWhenPayloadDoesNotMarkWallet()
183+
{
184+
$paymentMethod = new PaymentMethod($this->getValidData());
185+
186+
$this->assertFalse($paymentMethod->isWallet());
187+
}
188+
189+
/**
190+
* Test if wallet defaults to false when payload omits is_wallet
191+
*
192+
* @throws InvalidDataInitializationException
193+
*/
194+
public function testIsWalletReturnsFalseWhenIsWalletKeyIsMissing()
195+
{
196+
$data = $this->getValidData();
197+
unset($data[PaymentMethod::IS_WALLET_KEY]);
198+
199+
$paymentMethod = new PaymentMethod($data);
200+
201+
$this->assertFalse($paymentMethod->isWallet());
202+
}
203+
204+
/**
205+
* Test if getData includes wallet flag
206+
*
207+
* @throws InvalidDataInitializationException
208+
*/
209+
public function testGetDataIncludesIsWallet()
210+
{
211+
$data = $this->getValidData();
212+
$data[PaymentMethod::IS_WALLET_KEY] = true;
213+
214+
$paymentMethod = new PaymentMethod($data);
215+
$result = $paymentMethod->getData();
216+
217+
$this->assertArrayHasKey(PaymentMethod::IS_WALLET_KEY, $result);
218+
$this->assertTrue($result[PaymentMethod::IS_WALLET_KEY]);
219+
}
154220

155221
/**
156222
* @return array
@@ -205,6 +271,7 @@ private function getValidData(): array
205271
PaymentMethod::ICON_URLS_VECTOR_KEY => 'https://example.com/visa.svg',
206272
],
207273
PaymentMethod::ALLOWED_CURRENCIES_KEY => ['EUR'],
274+
PaymentMethod::IS_WALLET_KEY => false,
208275
];
209276
}
210277

0 commit comments

Comments
 (0)