Skip to content

Commit bb6bc01

Browse files
authored
PHPSDK-134: Fix errors when only reference is set, within the CustomerDetails object (#337)
1 parent bfe0ed0 commit bb6bc01

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

src/Api/Transactions/OrderRequest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,14 @@ public function getData(): array
473473
*/
474474
protected function validate(array $data): bool
475475
{
476-
if (!$data['plugin']) {
477-
throw new InvalidArgumentException('Required plugin details are missing');
478-
}
479-
480476
if ($this->strictMode) {
481477
(new TotalAmountValidator())->validate($data);
482478
}
483479

480+
if (!$data['plugin']) {
481+
throw new InvalidArgumentException('Required plugin details are missing');
482+
}
483+
484484
return true;
485485
}
486486
}

src/Api/Transactions/OrderRequest/Arguments/CustomerDetails.php

+26-12
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ public function getData(): array
5454
{
5555
$address = $this->getAddress();
5656
$data = [
57-
'firstname' => $this->getFirstName(),
58-
'lastname' => $this->getLastName(),
59-
'company_name' => $this->getCompanyName(),
60-
'address1' => $address->getStreetName(),
61-
'address2' => $address->getStreetNameAdditional(),
62-
'house_number' => $this->getHouseNumber(),
63-
'zip_code' => $address->getZipCode(),
64-
'city' => $address->getCity(),
65-
'state' => $address->getState(),
66-
'country' => $address->getCountry() ? $address->getCountry()->getCode() : null,
57+
'firstname' => $this->getFirstName() ?: null,
58+
'lastname' => $this->getLastName() ?: null,
59+
'company_name' => $this->getCompanyName() ?: null,
60+
'address1' => $address ? $address->getStreetName() : null,
61+
'address2' => $address ? $address->getStreetNameAdditional() : null,
62+
'house_number' => $address ? $this->getHouseNumber() : null,
63+
'zip_code' => $address ? $address->getZipCode() : null,
64+
'city' => $address ? $address->getCity() : null,
65+
'state' => $address ? $address->getState() : null,
66+
'country' => $this->getCountryCode() ? $this->getCountryCode() : null,
6767
'phone' => $this->getPhoneNumber() ? $this->getPhoneNumber()->get() : null,
6868
'email' => $this->getEmailAddress() ? $this->getEmailAddress()->get() : null,
6969
'ip_address' => $this->getIpAddress() ? $this->getIpAddress()->get() : null,
7070
'locale' => $this->getLocale(),
71-
'referrer' => $this->getReferrer(),
71+
'referrer' => $this->getReferrer() ?: null,
7272
'forwarded_ip' => $this->getForwardedIp() ? $this->getForwardedIp()->get() : null,
73-
'user_agent' => $this->getUserAgent(),
73+
'user_agent' => $this->getUserAgent() ?: null,
7474
'reference' => $this->reference,
7575
];
7676

@@ -187,6 +187,20 @@ private function getHouseNumber(): string
187187
return $houseNumber;
188188
}
189189

190+
/**
191+
* @return string|null
192+
*/
193+
private function getCountryCode(): ?string
194+
{
195+
$address = $this->getAddress();
196+
197+
if (!$address) {
198+
return null;
199+
}
200+
201+
return $address->getCountry() ? $address->getCountry()->getCode(): null;
202+
}
203+
190204
/**
191205
* @param string $reference
192206
* @return $this

src/ValueObject/Customer.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,23 @@ public function addPhoneNumberAsString(string $phoneNumber): Customer
155155
}
156156

157157
/**
158-
* @return IpAddress
158+
* @return IpAddress|null
159159
*/
160160
public function getIpAddress(): ?IpAddress
161161
{
162162
return $this->ipAddress;
163163
}
164164

165165
/**
166-
* @return EmailAddress
166+
* @return EmailAddress|null
167167
*/
168168
public function getEmailAddress(): ?EmailAddress
169169
{
170170
return $this->emailAddress;
171171
}
172172

173173
/**
174-
* @return Address
174+
* @return Address|null
175175
*/
176176
public function getAddress(): ?Address
177177
{
@@ -203,7 +203,7 @@ public function getCompanyName(): string
203203
}
204204

205205
/**
206-
* @return PhoneNumber
206+
* @return PhoneNumber|null
207207
*/
208208
public function getPhoneNumber(): ?PhoneNumber
209209
{

tests/Unit/Api/Transactions/OrderRequestTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,19 @@ public function testRequestOrderWithTerminalId()
142142
$this->assertArrayHasKey('terminal_id', $data['gateway_info']);
143143
$this->assertEquals('terminal-id', $data['gateway_info']['terminal_id']);
144144
}
145+
146+
/**
147+
* Test if we can add a customer object, only setting up the reference, and get the Order Request
148+
*/
149+
public function testCreateAndAddCustomerReference()
150+
{
151+
$orderRequest = $this->createIdealOrderRedirectRequestFixture();
152+
$customer = (new OrderRequest\Arguments\CustomerDetails())->addReference('customer-reference');
153+
$orderRequest->addCustomer($customer);
154+
$data = $orderRequest->getData();
155+
$this->assertIsArray($data['customer']);
156+
$this->assertEquals(2, count($data['customer']));
157+
$this->assertEquals('customer-reference', $data['customer']['reference']);
158+
$this->assertArrayNotHasKey('address1', $data['customer']);
159+
}
145160
}

0 commit comments

Comments
 (0)