From b0ce0ebefdc458db0f7d3fd85e70989845bb5b2d Mon Sep 17 00:00:00 2001 From: Daan De Smedt Date: Wed, 25 Oct 2023 15:35:53 +0200 Subject: [PATCH] Add support for the custom_info object as OrderRequest argument (#9) --- src/Api/Transactions/OrderRequest.php | 17 +++ .../OrderRequest/Arguments/CustomInfo.php | 104 ++++++++++++++++++ .../OrderRequest/Arguments/CustomInfoTest.php | 47 ++++++++ .../Api/Transactions/OrderRequestTest.php | 13 +++ 4 files changed, 181 insertions(+) create mode 100644 src/Api/Transactions/OrderRequest/Arguments/CustomInfo.php create mode 100644 tests/Unit/Api/Transactions/OrderRequest/Arguments/CustomInfoTest.php diff --git a/src/Api/Transactions/OrderRequest.php b/src/Api/Transactions/OrderRequest.php index 1595878..c3750b9 100644 --- a/src/Api/Transactions/OrderRequest.php +++ b/src/Api/Transactions/OrderRequest.php @@ -10,6 +10,7 @@ use MultiSafepay\Api\Gateways\Gateway; use MultiSafepay\Api\Transactions\OrderRequest\Arguments\CheckoutOptions; use MultiSafepay\Api\Transactions\OrderRequest\Arguments\CustomerDetails; +use MultiSafepay\Api\Transactions\OrderRequest\Arguments\CustomInfo; use MultiSafepay\Api\Transactions\OrderRequest\Arguments\Description; use MultiSafepay\Api\Transactions\OrderRequest\Arguments\GatewayInfoInterface; use MultiSafepay\Api\Transactions\OrderRequest\Arguments\GoogleAnalytics; @@ -130,6 +131,11 @@ class OrderRequest extends RequestBody implements OrderRequestInterface */ private $recurringModel; + /** + * @var CustomInfo + */ + private $customInfo; + /** * @param string $type * @return OrderRequest @@ -433,6 +439,16 @@ public function addDaysActive(int $days): OrderRequest return $this; } + /** + * @param CustomInfo $customInfo + * @return OrderRequest + */ + public function addCustomInfo(CustomInfo $customInfo): OrderRequest + { + $this->customInfo = $customInfo; + return $this; + } + /** * @return array * phpcs:disable ObjectCalisthenics.Files.FunctionLength @@ -459,6 +475,7 @@ public function getData(): array 'days_active' => $this->daysActive, 'seconds_active' => $this->secondsActive, 'plugin' => $this->pluginDetails ? $this->pluginDetails->getData() : null, + 'custom_info' => ($this->customInfo) ? $this->customInfo->getData() : null, ]; $data = $this->removeNullRecursive(array_merge($data, $this->data)); diff --git a/src/Api/Transactions/OrderRequest/Arguments/CustomInfo.php b/src/Api/Transactions/OrderRequest/Arguments/CustomInfo.php new file mode 100644 index 0000000..7787592 --- /dev/null +++ b/src/Api/Transactions/OrderRequest/Arguments/CustomInfo.php @@ -0,0 +1,104 @@ +custom1 = $custom1; + return $this; + } + + /** + * @return string|null + */ + public function getCustom1(): ?string + { + return $this->custom1; + } + + + /** + * @param string $custom2 + * @return CustomInfo + */ + public function addCustom2(string $custom2): CustomInfo + { + $this->custom2 = $custom2; + return $this; + } + + /** + * @return string|null + */ + public function getCustom2(): ?string + { + return $this->custom2; + } + + /** + * @param string $custom3 + * @return CustomInfo + */ + public function addCustom3(string $custom3): CustomInfo + { + $this->custom3 = $custom3; + return $this; + } + + + /** + * @return string|null + */ + public function getCustom3(): ?string + { + return $this->custom3; + } + + + /** + * @return string[] + */ + public function getData(): array + { + return [ + 'custom_1' => $this->getCustom1(), + 'custom_2' => $this->getCustom2(), + 'custom_3' => $this->getCustom3(), + ]; + } +} diff --git a/tests/Unit/Api/Transactions/OrderRequest/Arguments/CustomInfoTest.php b/tests/Unit/Api/Transactions/OrderRequest/Arguments/CustomInfoTest.php new file mode 100644 index 0000000..02e2003 --- /dev/null +++ b/tests/Unit/Api/Transactions/OrderRequest/Arguments/CustomInfoTest.php @@ -0,0 +1,47 @@ +getCustomInfo(); + $this->assertEquals('Multi', $customInfo->getCustom1()); + $this->assertEquals('Safe', $customInfo->getCustom2()); + $this->assertEquals('pay', $customInfo->getCustom3()); + } + + /** + * Test case to guarantee that Custominfo getData return data array properly + */ + public function testWorkingCustomInfo() + { + $customInfo = $this->getCustomInfo(); + $customInfoData = $customInfo->getData(); + $this->assertEquals('Multi', $customInfoData['custom_1']); + $this->assertEquals('Safe', $customInfoData['custom_2']); + $this->assertEquals('pay', $customInfoData['custom_3']); + } + + /** + * @return CustomInfo + */ + private function getCustomInfo(): CustomInfo + { + $customInfo = new CustomInfo(); + $customInfo->addCustom1('Multi'); + $customInfo->addCustom2('Safe'); + $customInfo->addCustom3('pay'); + return $customInfo; + } +} diff --git a/tests/Unit/Api/Transactions/OrderRequestTest.php b/tests/Unit/Api/Transactions/OrderRequestTest.php index 2c4563a..a42256c 100644 --- a/tests/Unit/Api/Transactions/OrderRequestTest.php +++ b/tests/Unit/Api/Transactions/OrderRequestTest.php @@ -169,4 +169,17 @@ public function testRequestOrderRequestWithoutPluginDetails() $data = $orderRequest->getData(); $this->assertArrayNotHasKey('plugin', $data); } + + /** + * Test if we can add a customer info object, only setting up the reference, and get the Order Request + */ + public function testRequestOrderRequestWithCustomInfo() + { + $orderRequest = $this->createIdealOrderRedirectRequestFixture(); + $customInfo = (new OrderRequest\Arguments\CustomInfo())->addCustom1('Multi')->addCustom2('Safe')->addCustom3('pay'); + $orderRequest->addCustomInfo($customInfo); + $data = $orderRequest->getData(); + $this->assertIsArray($data['custom_info']); + $this->assertEquals(3, count($data['custom_info'])); + } }