Skip to content

Commit 4bfc75c

Browse files
authored
Merge pull request #102 from paytrail/network-token
adds new field to the getTokenResponse
2 parents c6bae34 + ac85d30 commit 4bfc75c

File tree

2 files changed

+239
-0
lines changed

2 files changed

+239
-0
lines changed

src/Model/Token/NetworkToken.php

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?php
2+
3+
/**
4+
* Class NetworkToken
5+
*/
6+
7+
namespace Paytrail\SDK\Model\Token;
8+
9+
use Paytrail\SDK\Exception\ValidationException;
10+
use Paytrail\SDK\Util\ObjectPropertyConverter;
11+
12+
/**
13+
* Class NetworkToken
14+
*
15+
* The NetworkToken class defiens additional details the network token.
16+
*
17+
* @package Paytrail\SDK\Model\Token
18+
*/
19+
class NetworkToken implements \JsonSerializable
20+
{
21+
use ObjectPropertyConverter;
22+
23+
/** @var string $type */
24+
protected $type;
25+
26+
/** @var string $partialPan */
27+
protected $partialPan;
28+
29+
/** @var string $expireYear */
30+
protected $expireYear;
31+
32+
/** @var string $expireMonth */
33+
protected $expireMonth;
34+
35+
/** @var string $imageUrl */
36+
protected $imageUrl;
37+
38+
/** @var string $paymentAccountReference */
39+
protected $paymentAccountReference;
40+
41+
/**
42+
* Validate email
43+
*
44+
* @throws ValidationException
45+
*/
46+
public function validate()
47+
{
48+
$props = $this->convertObjectVarsToSnake();
49+
50+
if (empty($props['type'])) {
51+
throw new ValidationException('Type is empty');
52+
}
53+
54+
if (empty($props['partial_pan'])) {
55+
throw new ValidationException('Partial pan is empty');
56+
}
57+
58+
if (empty($props['expire_year'])) {
59+
throw new ValidationException('Expire year is empty');
60+
}
61+
62+
if (empty($props['expire_month'])) {
63+
throw new ValidationException('Expire month is empty');
64+
}
65+
66+
return true;
67+
}
68+
69+
/**
70+
* @param string $type
71+
* @return NetworkToken
72+
*/
73+
public function setType(string $type): NetworkToken
74+
{
75+
$this->type = $type;
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* @return string
82+
*/
83+
public function getType(): string
84+
{
85+
return $this->type;
86+
}
87+
88+
/**
89+
* @param string $partialPan
90+
* @return NetworkToken
91+
*/
92+
public function setPartialPan(string $partialPan): NetworkToken
93+
{
94+
$this->partialPan = $partialPan;
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* @return string
101+
*/
102+
public function getPartialPan(): string
103+
{
104+
return $this->partialPan;
105+
}
106+
107+
/**
108+
* @param string $expireYear
109+
* @return NetworkToken
110+
*/
111+
public function setExpireYear(string $expireYear): NetworkToken
112+
{
113+
$this->expireYear = $expireYear;
114+
115+
return $this;
116+
}
117+
118+
/**
119+
* @return string
120+
*/
121+
public function getExpireYear(): string
122+
{
123+
return $this->expireYear;
124+
}
125+
126+
/**
127+
* @param string $expireMonth
128+
* @return NetworkToken
129+
*/
130+
public function setExpireMonth(string $expireMonth): NetworkToken
131+
{
132+
$this->expireMonth = $expireMonth;
133+
134+
return $this;
135+
}
136+
137+
/**
138+
* @return string
139+
*/
140+
public function getExpireMonth(): string
141+
{
142+
return $this->expireMonth;
143+
}
144+
145+
/**
146+
* @param string|null $imageUrl
147+
* @return NetworkToken
148+
*/
149+
public function setImageUrl(?string $imageUrl): NetworkToken
150+
{
151+
$this->imageUrl = $imageUrl;
152+
153+
return $this;
154+
}
155+
156+
/**
157+
* @return string
158+
*/
159+
public function getImageUrl(): ?string
160+
{
161+
return $this->imageUrl;
162+
}
163+
164+
/**
165+
* @param string|null $paymentAccountReference
166+
* @return NetworkToken
167+
*/
168+
public function setPaymentAccountReference(?string $paymentAccountReference): NetworkToken
169+
{
170+
$this->paymentAccountReference = $paymentAccountReference;
171+
172+
return $this;
173+
}
174+
175+
/**
176+
* @return string
177+
*/
178+
public function getPaymentAccountReference(): ?string
179+
{
180+
return $this->paymentAccountReference;
181+
}
182+
183+
/**
184+
* Implements the json serialize method and
185+
* return all object variables including
186+
* private/protected properties.
187+
*/
188+
public function jsonSerialize(): array
189+
{
190+
return array_filter($this->convertObjectVarsToSnake(), function ($item) {
191+
return $item !== null;
192+
});
193+
}
194+
195+
/**
196+
* @param \stdClass $networkToken
197+
* @return NetworkToken
198+
*/
199+
public function loadFromStdClass(\stdClass $networkToken): NetworkToken
200+
{
201+
$this->setType($networkToken->type);
202+
$this->setPartialPan($networkToken->partial_pan);
203+
$this->setExpireYear($networkToken->expire_year);
204+
$this->setExpireMonth($networkToken->expire_month);
205+
$this->setImageUrl($networkToken->image_url);
206+
$this->setPaymentAccountReference($networkToken->payment_account_reference ?? null);
207+
208+
209+
return $this;
210+
}
211+
}

src/Response/GetTokenResponse.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Paytrail\SDK\Interfaces\ResponseInterface;
1212
use Paytrail\SDK\Model\Token\Card;
1313
use Paytrail\SDK\Model\Token\Customer;
14+
use Paytrail\SDK\Model\Token\NetworkToken;
1415
use Paytrail\SDK\Util\ObjectPropertyConverter;
1516

1617
/**
@@ -31,6 +32,9 @@ class GetTokenResponse implements ResponseInterface, \JsonSerializable
3132
/** @var Customer $customer */
3233
protected $customer;
3334

35+
/** @var NetworkToken $networkToken */
36+
protected $networkToken;
37+
3438
/**
3539
* @param string $token
3640
* @return GetTokenResponse
@@ -88,6 +92,25 @@ public function getCustomer(): Customer
8892
return $this->customer;
8993
}
9094

95+
/**
96+
* @param NetworkToken|null $networkToken
97+
* @return GetTokenResponse
98+
*/
99+
public function setNetworkToken(?NetworkToken $networkToken): GetTokenResponse
100+
{
101+
$this->networkToken = $networkToken;
102+
103+
return $this;
104+
}
105+
106+
/**
107+
* @return NetworkToken
108+
*/
109+
public function getNetworkToken(): ?NetworkToken
110+
{
111+
return $this->networkToken;
112+
}
113+
91114
/**
92115
* Implements the json serialize method and
93116
* return all object variables including
@@ -112,6 +135,11 @@ public function loadFromStdClass(\stdClass $response): GetTokenResponse
112135
$customer->loadFromStdClass($response->customer);
113136
$this->setCustomer($customer);
114137

138+
if (isset($response->network_token)) {
139+
$networkToken = new NetworkToken();
140+
$networkToken->loadFromStdClass($response->network_token);
141+
$this->setNetworkToken($networkToken);
142+
}
115143
return $this;
116144
}
117145
}

0 commit comments

Comments
 (0)