Skip to content

Commit

Permalink
Add support for the custom_info object as OrderRequest argument (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeSmedt authored Oct 25, 2023
1 parent 16a7cea commit b0ce0eb
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Api/Transactions/OrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,6 +131,11 @@ class OrderRequest extends RequestBody implements OrderRequestInterface
*/
private $recurringModel;

/**
* @var CustomInfo
*/
private $customInfo;

/**
* @param string $type
* @return OrderRequest
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand Down
104 changes: 104 additions & 0 deletions src/Api/Transactions/OrderRequest/Arguments/CustomInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php declare(strict_types=1);
/**
* Copyright © MultiSafepay, Inc. All rights reserved.
* See DISCLAIMER.md for disclaimer details.
*/

namespace MultiSafepay\Api\Transactions\OrderRequest\Arguments;

use MultiSafepay\Exception\InvalidArgumentException;
use MultiSafepay\Util\Version;

/**
* Class CustomInfo
* @package MultiSafepay\Api\Transactions\OrderRequest\Arguments
* phpcs:disable ObjectCalisthenics.Metrics.MethodPerClassLimit
* phpcs:disable ObjectCalisthenics.Files.ClassTraitAndInterfaceLength
*/
class CustomInfo
{
/**
* @var string
*/
private $custom1;

/**
* @var string
*/
private $custom2;

/**
* @var string
*/
private $custom3;


/**
* @param string $custom1
* @return CustomInfo
*/
public function addCustom1(string $custom1): CustomInfo
{
$this->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(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);
namespace MultiSafepay\Tests\Unit\Api\Transactions\OrderRequest\Arguments;

use MultiSafepay\Api\Transactions\OrderRequest\Arguments\CustomInfo;
use PHPUnit\Framework\TestCase;

/**
* Class DescriptionTest
* @package MultiSafepay\Tests\Unit\Api\Transactions\OrderRequest\Arguments
*/
class CustomInfoTest extends TestCase
{
/**
* Test case to guarantee that Custominfo properties is set properly
*/
public function testAddCustomInfo()
{
$customInfo = $this->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;
}
}
13 changes: 13 additions & 0 deletions tests/Unit/Api/Transactions/OrderRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
}
}

0 comments on commit b0ce0eb

Please sign in to comment.