Skip to content

Commit e737aa5

Browse files
authored
CC-1582/linkpay-update (#200)
* [CC-1582] Linkpay V2: - Add support to update linkpaypage. * [CC-1591] Prepare Release version 3.10.0
1 parent 82948e1 commit e737aa5

File tree

7 files changed

+200
-9
lines changed

7 files changed

+200
-9
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres
66
to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [3.10.0](https://github.com/unzerdev/php-sdk/compare/3.9.0..3.10.0)
9+
10+
### Added
11+
12+
* Add support for Linkpay v2.
13+
14+
### Changed
15+
16+
* Update paypage v2 styling parameter to match API changes.
17+
818
## [3.9.0](https://github.com/unzerdev/php-sdk/compare/3.8.0..3.9.0)
919

1020
### Changed

src/Resources/EmbeddedResources/Paypage/AmountSettings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AmountSettings extends AbstractUnzerResource
1313
* @param float|null $minimum
1414
* @param float|null $maximum
1515
*/
16-
public function __construct(?float $minimum, ?float $maximum)
16+
public function __construct(?float $minimum = null, ?float $maximum = null)
1717
{
1818
$this->minimum = $minimum;
1919
$this->maximum = $maximum;

src/Resources/EmbeddedResources/Paypage/Resources.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Resources extends AbstractUnzerResource
1616
* @param string|null $basketId
1717
* @param string|null $metadataId
1818
*/
19-
public function __construct(?string $customerId, ?string $basketId, ?string $metadataId)
19+
public function __construct(?string $customerId = null, ?string $basketId = null, ?string $metadataId = null)
2020
{
2121
$this->customerId = $customerId;
2222
$this->basketId = $basketId;

src/Resources/V2/Paypage.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ class Paypage extends AbstractUnzerResource
2121
{
2222
public const URI = '/merchant/paypage';
2323

24+
protected static $keyClassMap = [
25+
'urls' => Urls::class,
26+
'style' => Style::class,
27+
'resources' => Resources::class,
28+
'risk' => Risk::class,
29+
'paymentMethodsConfigs' => PaymentMethodsConfigs::class,
30+
'amountSettings' => AmountSettings::class
31+
];
32+
2433
/** @var string|null checkoutType
2534
* @see PaypageCheckoutTypes
2635
*/
@@ -31,8 +40,8 @@ class Paypage extends AbstractUnzerResource
3140
protected ?string $recurrenceType = null;
3241
protected ?string $shopName = null;
3342
protected ?string $type = null;
34-
protected ?float $amount;
35-
protected string $currency;
43+
protected ?float $amount = null;
44+
protected ?string $currency = null;
3645

3746
/** @var string $mode "charge" or "authorize" */
3847
protected string $mode;
@@ -63,7 +72,7 @@ class Paypage extends AbstractUnzerResource
6372
* @param $currency
6473
* @param $mode
6574
*/
66-
public function __construct(?float $amount, string $currency, string $mode = TransactionTypes::CHARGE)
75+
public function __construct(?float $amount = null, ?string $currency = null, ?string $mode = TransactionTypes::CHARGE)
6776
{
6877
$this->amount = $amount;
6978
$this->currency = $currency;
@@ -76,7 +85,7 @@ public function handleResponse(stdClass $response, string $method = HttpAdapterI
7685
$this->id = $response->paypageId;
7786
}
7887

79-
if (isset($response->payments) && !empty($response->payments)) {
88+
if ($this->keyValueEsists('payments', $response)) {
8089
$payments = [];
8190
foreach ($response->payments as $payment) {
8291
$newPayment = (new Payment());
@@ -87,14 +96,28 @@ public function handleResponse(stdClass $response, string $method = HttpAdapterI
8796
$this->payments = $payments;
8897
}
8998

99+
$expiresAtKey = 'expiresAt';
100+
if ($this->keyValueEsists($expiresAtKey, $response)) {
101+
$this->setExpiresAt(new DateTime($response->$expiresAtKey));
102+
unset($response->$expiresAtKey);
103+
}
104+
105+
// Instantiate embedded objects.
106+
foreach (self::$keyClassMap as $key => $class) {
107+
if ($this->keyValueEsists($key, $response) && $this->hasProperties($response->$key)) {
108+
$object = new $class();
109+
$this->$key = $object;
110+
}
111+
}
112+
90113
parent::handleResponse($response, $method);
91114
}
92115

93116

94117
/**
95118
* @return mixed
96119
*/
97-
public function getAmount(): float
120+
public function getAmount(): ?float
98121
{
99122
return $this->amount;
100123
}
@@ -134,7 +157,7 @@ public function getApiVersion(): string
134157
return 'v2';
135158
}
136159

137-
public function getCurrency(): string
160+
public function getCurrency(): ?string
138161
{
139162
return $this->currency;
140163
}
@@ -396,4 +419,20 @@ public function expose()
396419
}
397420
return $exposeArray;
398421
}
422+
423+
/**
424+
* @param string $key
425+
* @param stdClass $response
426+
* @return bool
427+
*/
428+
public function keyValueEsists(string $key, stdClass $response): bool
429+
{
430+
return isset($response->$key) && !empty($response->$key);
431+
}
432+
433+
protected function hasProperties(stdClass $object)
434+
{
435+
return count(get_object_vars($object)) > 0;
436+
}
437+
399438
}

src/Services/ResourceService.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,17 @@ public function deletePaypage(PaypageV2 $paypage): void
432432
$this->deleteResource($paypage);
433433
}
434434

435+
/** Delete Paypage V2
436+
* @throws UnzerApiException
437+
*/
438+
public function updatePaypage(PaypageV2 $paypage): PaypageV2
439+
{
440+
$paypage->setParentResource($this->unzer);
441+
$this->patchResource($paypage);
442+
443+
return $paypage;
444+
}
445+
435446
/**
436447
* @inheritDoc
437448
*/

src/Unzer.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Unzer implements
5757
public const BASE_URL = 'api.unzer.com';
5858
public const API_VERSION = 'v1';
5959
public const SDK_TYPE = 'UnzerPHP';
60-
public const SDK_VERSION = '3.9.0';
60+
public const SDK_VERSION = '3.10.0';
6161

6262
/** @var string $key */
6363
private $key;
@@ -974,6 +974,18 @@ public function deletePaypageById(string $paypageId): void
974974
$this->deletePaypage($paypage);
975975
}
976976

977+
/**
978+
* Update paypage v2 resource using patch method.
979+
*/
980+
public function patchPaypage(PaypageV2 $paypage): PaypageV2
981+
{
982+
if ($paypage->getId() === null) {
983+
throw new RuntimeException('Paypage ID is required for patch operation.');
984+
}
985+
986+
return $this->resourceService->updatePaypage($paypage);
987+
}
988+
977989
/**
978990
* Fetch list of associated payments for the given payment page. Use `\UnzerSDK\Resources\V2\Paypage::getPayments`
979991
* to get the list of payments.

test/integration/Resources/LinkpayV2Test.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,125 @@ public function createLinkpayWithSpecificFields()
9393
$this->assertEquals('linkpay', $paypage->getType());
9494
}
9595

96+
/**
97+
* @test
98+
* @group CC-1583
99+
*/
100+
public function updateLinkpayExpiryDate()
101+
{
102+
// init
103+
$unzer = $this->getUnzerObject();
104+
$paypage = new Paypage(null, 'EUR', 'charge');
105+
$paypage->setType('linkpay');
106+
$paypage->setAlias($this->generateRandomId());
107+
$interval = new \DateInterval("PT1H");
108+
$expiresAtInitial = new \DateTime('2024-09-25T16:39:01+00:00');
109+
$expiresUpdated = (new \DateTime('2024-09-25T16:39:01.1397+12:00'))->add($interval);
110+
111+
$paypage->setExpiresAt($expiresAtInitial);
112+
$paypage->setAmountSettings(new AmountSettings(10, 100));
113+
114+
// creation
115+
$unzer->createPaypage($paypage);
116+
$this->assertCreatedPaypage($paypage);
117+
$paypageId = $paypage->getId();
118+
119+
// update
120+
$updatePaypage = new Paypage();
121+
$updatePaypage
122+
->setId($paypageId)
123+
->setExpiresAt($expiresUpdated);
124+
$unzer->patchPaypage($updatePaypage);
125+
$this->assertNotEquals($expiresUpdated, $updatePaypage->getExpiresAt()); // update response contains no milliseconds.
126+
127+
$this->assertEquals('linkpay', $paypage->getType());
128+
$this->assertEquals($paypage->getCurrency(), $updatePaypage->getCurrency());
129+
$this->assertEquals($paypage->getAlias(), $updatePaypage->getAlias());
130+
$this->assertEquals($paypage->getAmountSettings(), $updatePaypage->getAmountSettings());
131+
$this->assertEquals($paypage->getResources(), $updatePaypage->getResources());
132+
}
133+
134+
/**
135+
* @test
136+
* @group CC-1583
137+
*/
138+
public function updateLinkpayAmount()
139+
{
140+
// init
141+
$unzer = $this->getUnzerObject();
142+
$paypage = new Paypage(99.99, 'EUR', 'charge');
143+
$paypage->setType('linkpay');
144+
$paypage->setAlias($this->generateRandomId());
145+
$expiresAtInitial = new \DateTime('2024-09-25T16:39:01+00:00');
146+
147+
$paypage->setExpiresAt($expiresAtInitial);
148+
149+
// creation
150+
$unzer->createPaypage($paypage);
151+
$this->assertCreatedPaypage($paypage);
152+
$paypageId = $paypage->getId();
153+
154+
// update
155+
$updatePaypage = new Paypage();
156+
$updatePaypage
157+
->setId($paypageId)
158+
->setAmount(66.66);
159+
160+
$unzer->patchPaypage($updatePaypage);
161+
162+
// updated value should be different
163+
$this->assertNotEquals($paypage->getAmount(), $updatePaypage->getAmount());
164+
165+
// initial values should match
166+
$this->assertEquals('linkpay', $paypage->getType());
167+
$this->assertEquals($paypage->getCurrency(), $updatePaypage->getCurrency());
168+
$this->assertEquals($paypage->getAlias(), $updatePaypage->getAlias());
169+
$this->assertEquals($paypage->getAmountSettings(), $updatePaypage->getAmountSettings());
170+
$this->assertEquals($paypage->getResources(), $updatePaypage->getResources());
171+
$this->assertEquals($paypage->getExpiresAt(), $updatePaypage->getExpiresAt());
172+
}
173+
174+
/**
175+
* @test
176+
* @group CC-1583
177+
*/
178+
public function updateLinkpayAmountSettings()
179+
{
180+
// init
181+
$unzer = $this->getUnzerObject();
182+
$paypage = new Paypage(null, 'EUR', 'charge');
183+
$paypage->setType('linkpay');
184+
$paypage->setAlias($this->generateRandomId());
185+
$expiresAtInitial = new \DateTime('2024-09-25T16:39:01+00:00');
186+
187+
$paypage->setExpiresAt($expiresAtInitial)
188+
->setAmountSettings(new AmountSettings(9.99, 99.99));
189+
190+
// creation
191+
$unzer->createPaypage($paypage);
192+
$this->assertCreatedPaypage($paypage);
193+
$paypageId = $paypage->getId();
194+
195+
// update
196+
$updatePaypage = new Paypage();
197+
$updatePaypage
198+
->setId($paypageId)
199+
->setAmountSettings(new AmountSettings(6.66, 66.66));
200+
201+
$unzer->patchPaypage($updatePaypage);
202+
203+
// updated value should be different
204+
$this->assertNotEquals($paypage->getAmountSettings(), $updatePaypage->getAmountSettings());
205+
206+
// initial values should match
207+
$this->assertEquals($paypage->getAlias(), $updatePaypage->getAlias());
208+
$this->assertEquals($paypage->getAmount(), $updatePaypage->getAmount());
209+
$this->assertEquals($paypage->getCurrency(), $updatePaypage->getCurrency());
210+
$this->assertEquals($paypage->getExpiresAt(), $updatePaypage->getExpiresAt());
211+
$this->assertEquals($paypage->getResources(), $updatePaypage->getResources());
212+
$this->assertEquals('linkpay', $paypage->getType());
213+
}
214+
96215
/**
97216
* @param Paypage $paypage
98217
* @return void

0 commit comments

Comments
 (0)