diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c071825..48f3e8ef 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,18 @@ 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.2.0][1.0.2.0] + +### Added +* Made status information available in transactions (isSuccess, isError, isPending). + +### Fixed +* Http-Request: Remove payload from DELETE call. +* DebugLog: Remove payload output from GET and DELETE calls. + +### Changed +* Several code style issues. + ## [1.0.1.0][1.0.1.0] ### Added @@ -89,3 +101,4 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a [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 +[1.0.2.0]: https://github.com/heidelpay/heidelpayPHP/compare/1.0.1.0..1.0.2.0 diff --git a/src/Adapter/CurlAdapter.php b/src/Adapter/CurlAdapter.php index 2daf00c6..7ce2ff00 100755 --- a/src/Adapter/CurlAdapter.php +++ b/src/Adapter/CurlAdapter.php @@ -57,7 +57,7 @@ public function init($url, $payload = null, $httpMethod = HttpAdapterInterface:: $this->setOption(CURLOPT_SSL_VERIFYHOST, 2); $this->setOption(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 - if (HttpAdapterInterface::REQUEST_GET !== $httpMethod) { + if (in_array($httpMethod, [HttpAdapterInterface::REQUEST_POST, HttpAdapterInterface::REQUEST_PUT], true)) { $this->setOption(CURLOPT_POSTFIELDS, $payload); } } diff --git a/src/Heidelpay.php b/src/Heidelpay.php index 55ea9ee5..3edeaeba 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.1.0'; + const SDK_VERSION = '1.0.2.0'; /** @var string $key */ private $key; diff --git a/src/Resources/AbstractHeidelpayResource.php b/src/Resources/AbstractHeidelpayResource.php index 67487d5e..baaf96bd 100755 --- a/src/Resources/AbstractHeidelpayResource.php +++ b/src/Resources/AbstractHeidelpayResource.php @@ -165,7 +165,7 @@ private function updateValues($object, \stdClass $response) { foreach ($response as $key => $value) { $newValue = $value; - if (!\is_string($value) || $value === '') { + if ($value !== false && (!\is_string($value) || $value === '')) { $newValue = $value ?: null; } @@ -301,14 +301,14 @@ public function expose() if ($value === null) { unset($properties[$property]); - } else { - if ($value instanceof self) { - $newValue = $value->expose(); - } else { - $newValue = $value; - } - $properties[$property] = $newValue; + continue; + } + + $newValue = $value; + if ($value instanceof self) { + $newValue = $value->expose(); } + $properties[$property] = $newValue; } catch (\ReflectionException $e) { unset($properties[$property]); } diff --git a/src/Resources/Metadata.php b/src/Resources/Metadata.php index 0822bdb4..26d9bc0f 100755 --- a/src/Resources/Metadata.php +++ b/src/Resources/Metadata.php @@ -143,7 +143,7 @@ public function getMetadata($name) return $this->metadata[$name] ?? null; } - // + //> // diff --git a/src/Resources/Payment.php b/src/Resources/Payment.php index 4e4eeb14..f87151a7 100755 --- a/src/Resources/Payment.php +++ b/src/Resources/Payment.php @@ -44,18 +44,6 @@ class Payment extends AbstractHeidelpayResource use HasPaymentState; use HasOrderId; - /** - * @param null $parent - */ - public function __construct($parent = null) - { - $this->amount = new Amount(); - $this->metadata = new Metadata(); - - parent::__construct($parent); - } - - // /** @var string $redirectUrl */ private $redirectUrl; @@ -82,7 +70,17 @@ public function __construct($parent = null) /** @var Basket $basket */ private $basket; - // + + /** + * @param null $parent + */ + public function __construct($parent = null) + { + $this->amount = new Amount(); + $this->metadata = new Metadata(); + + parent::__construct($parent); + } // @@ -750,22 +748,16 @@ private function updateResponseResources($resources) } } - if (isset($resources->typeId) && !empty($resources->typeId)) { - if (!$this->paymentType instanceof BasePaymentType) { - $this->paymentType = $this->getHeidelpayObject()->fetchPaymentType($resources->typeId); - } + if (isset($resources->typeId) && !empty($resources->typeId) && !$this->paymentType instanceof BasePaymentType) { + $this->paymentType = $this->getHeidelpayObject()->fetchPaymentType($resources->typeId); } - if (isset($resources->metadataId) && !empty($resources->metadataId)) { - if ($this->metadata->getId() === null) { - $this->metadata = $this->getHeidelpayObject()->fetchMetadata($resources->metadataId); - } + if (isset($resources->metadataId) && !empty($resources->metadataId) && $this->metadata->getId() === null) { + $this->metadata = $this->getHeidelpayObject()->fetchMetadata($resources->metadataId); } - if (isset($resources->basketId) && !empty($resources->basketId)) { - if (!$this->basket instanceof Basket) { - $this->basket = $this->getHeidelpayObject()->fetchBasket($resources->basketId); - } + if (isset($resources->basketId) && !empty($resources->basketId) && !$this->basket instanceof Basket) { + $this->basket = $this->getHeidelpayObject()->fetchBasket($resources->basketId); } } diff --git a/src/Resources/TransactionTypes/AbstractTransactionType.php b/src/Resources/TransactionTypes/AbstractTransactionType.php index 7bfa6d70..79e5a81b 100755 --- a/src/Resources/TransactionTypes/AbstractTransactionType.php +++ b/src/Resources/TransactionTypes/AbstractTransactionType.php @@ -48,6 +48,15 @@ abstract class AbstractTransactionType extends AbstractHeidelpayResource /** @var string $shortId */ private $shortId; + /** @var bool $isError */ + private $isError = false; + + /** @var bool $isSuccess */ + private $isSuccess = false; + + /** @var bool $isPending */ + private $isPending = false; + // // @@ -162,6 +171,63 @@ protected function setShortId(string $shortId): AbstractTransactionType return $this; } + /** + * @return bool + */ + public function isError(): bool + { + return $this->isError; + } + + /** + * @param bool $isError + * + * @return AbstractTransactionType + */ + public function setIsError(bool $isError): AbstractTransactionType + { + $this->isError = $isError; + return $this; + } + + /** + * @return bool + */ + public function isSuccess(): bool + { + return $this->isSuccess; + } + + /** + * @param bool $isSuccess + * + * @return AbstractTransactionType + */ + public function setIsSuccess(bool $isSuccess): AbstractTransactionType + { + $this->isSuccess = $isSuccess; + return $this; + } + + /** + * @return bool + */ + public function isPending(): bool + { + return $this->isPending; + } + + /** + * @param bool $isPending + * + * @return AbstractTransactionType + */ + public function setIsPending(bool $isPending): AbstractTransactionType + { + $this->isPending = $isPending; + return $this; + } + // // diff --git a/src/Services/HttpService.php b/src/Services/HttpService.php index 343af7ae..25fca241 100755 --- a/src/Services/HttpService.php +++ b/src/Services/HttpService.php @@ -166,9 +166,14 @@ public function debugLog(AbstractHeidelpayResource $resource, $httpMethod, strin if ($heidelpayObj->isDebugMode()) { $debugHandler = $heidelpayObj->getDebugHandler(); if ($debugHandler instanceof DebugHandlerInterface) { - $resourceJson = $resource->jsonSerialize(); $debugHandler->log($httpMethod . ': ' . $url); - $debugHandler->log('Request: ' . $resourceJson); + if (in_array( + $httpMethod, + [HttpAdapterInterface::REQUEST_POST, HttpAdapterInterface::REQUEST_PUT], + true + )) { + $debugHandler->log('Request: ' . $resource->jsonSerialize()); + } $debugHandler->log('Response: ' . json_encode(json_decode($response))); } } diff --git a/test/integration/AuthorizationTest.php b/test/integration/AuthorizationTest.php index 9dd128c5..5bd602e6 100755 --- a/test/integration/AuthorizationTest.php +++ b/test/integration/AuthorizationTest.php @@ -26,6 +26,9 @@ namespace heidelpayPHP\test\integration; use heidelpayPHP\Exceptions\HeidelpayApiException; +use heidelpayPHP\Resources\AbstractHeidelpayResource; +use heidelpayPHP\Resources\PaymentTypes\BasePaymentType; +use heidelpayPHP\Resources\PaymentTypes\Paypal; use heidelpayPHP\Resources\TransactionTypes\Authorization; use heidelpayPHP\test\BasePaymentTest; use PHPUnit\Framework\ExpectationFailedException; @@ -140,4 +143,41 @@ public function authorizationCanBeFetched(Authorization $authorization) $fetchedAuthorization = $this->heidelpay->fetchAuthorization($authorization->getPaymentId()); $this->assertEquals($authorization->expose(), $fetchedAuthorization->expose()); } + + /** + * Verify authorization has the expected states. + * + * @test + * @dataProvider authorizeHasExpectedStatesDP + * + * @param BasePaymentType|AbstractHeidelpayResource $paymentType + * @param bool $isSuccess + * @param bool $isPending + * @param bool $isError + * + * @throws HeidelpayApiException + * @throws \PHPUnit\Framework\AssertionFailedError + * @throws \RuntimeException + */ + public function authorizeHasExpectedStates(BasePaymentType $paymentType, $isSuccess, $isPending, $isError) + { + $paymentType = $this->heidelpay->createPaymentType($paymentType); + $authorize = $this->heidelpay->authorize(100.0, 'EUR', $paymentType->getId(), self::RETURN_URL); + $this->assertEquals($isSuccess, $authorize->isSuccess()); + $this->assertEquals($isPending, $authorize->isPending()); + $this->assertEquals($isError, $authorize->isError()); + } + + /** + * @return array + * + * @throws \RuntimeException + */ + public function authorizeHasExpectedStatesDP(): array + { + return [ + 'card' => [$this->createCardObject(), true, false, false], + 'paypal' => [new Paypal(), false, true, false] + ]; + } } diff --git a/test/integration/CancelTest.php b/test/integration/CancelTest.php index 7f5bfed7..8fc95062 100755 --- a/test/integration/CancelTest.php +++ b/test/integration/CancelTest.php @@ -146,4 +146,22 @@ public function chargeCancellationsShouldBeFetchableViaPaymentObject() $this->assertNotNull($cancellation); $this->assertEquals($cancellation->expose(), $reversal->expose()); } + + /** + * Verify transaction status. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function cancelStatusIsSetCorrectly() + { + $charge = $this->createCharge(); + $reversal = $charge->cancel(); + + $this->assertTrue($reversal->isSuccess()); + $this->assertFalse($reversal->isPending()); + $this->assertFalse($reversal->isError()); + } } diff --git a/test/integration/ChargeTest.php b/test/integration/ChargeTest.php index fb8072e9..4df55069 100755 --- a/test/integration/ChargeTest.php +++ b/test/integration/ChargeTest.php @@ -76,4 +76,21 @@ public function chargeShouldWorkWithTypeObject() $this->assertNotEmpty($charge->getPayment()->getId()); $this->assertEquals(self::RETURN_URL, $charge->getReturnUrl()); } + + /** + * Verify transaction status. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function chargeStatusIsSetCorrectly() + { + $charge = $this->createCharge(); + + $this->assertTrue($charge->isSuccess()); + $this->assertFalse($charge->isPending()); + $this->assertFalse($charge->isError()); + } } diff --git a/test/integration/ShipmentTest.php b/test/integration/ShipmentTest.php index db34330a..dd4528a0 100755 --- a/test/integration/ShipmentTest.php +++ b/test/integration/ShipmentTest.php @@ -123,4 +123,30 @@ public function shipmentShouldBePossibleWithPaymentObject() $this->assertNotNull($shipment->getId()); $this->assertNotNull($shipment); } + + /** + * Verify transaction status. + * + * @test + * + * @throws HeidelpayApiException + * @throws \RuntimeException + */ + public function shippmentStatusIsSetCorrectly() + { + $invoiceGuaranteed = new InvoiceGuaranteed(); + $authorize = $this->heidelpay->authorize( + 100.0, + 'EUR', + $invoiceGuaranteed, + self::RETURN_URL, + $this->getMaximumCustomerInclShippingAddress()->setShippingAddress($this->getBillingAddress()) + ); + + $payment = $authorize->getPayment(); + $shipment = $this->heidelpay->ship($payment); + $this->assertTrue($shipment->isSuccess()); + $this->assertFalse($shipment->isPending()); + $this->assertFalse($shipment->isError()); + } } diff --git a/test/unit/DummyResource.php b/test/unit/DummyResource.php index f28ec6d9..d9251ecf 100755 --- a/test/unit/DummyResource.php +++ b/test/unit/DummyResource.php @@ -30,7 +30,7 @@ class DummyResource extends AbstractHeidelpayResource { public function jsonSerialize() { - return 'dummyResourceJsonSerialized'; + return '{"dummyResource": "JsonSerialized"}'; } public function getUri($appendId = true): string diff --git a/test/unit/Resources/TransactionTypes/AbstractTransactionTypeTest.php b/test/unit/Resources/TransactionTypes/AbstractTransactionTypeTest.php index 1a271b22..005609fb 100755 --- a/test/unit/Resources/TransactionTypes/AbstractTransactionTypeTest.php +++ b/test/unit/Resources/TransactionTypes/AbstractTransactionTypeTest.php @@ -54,6 +54,10 @@ public function theGettersAndSettersShouldWorkProperly() $this->assertNull($transactionType->getDate()); $this->assertNull($transactionType->getPaymentId()); + $this->assertFalse($transactionType->isError()); + $this->assertFalse($transactionType->isSuccess()); + $this->assertFalse($transactionType->isPending()); + $transactionType->setPayment($payment); $this->assertNull($transactionType->getRedirectUrl()); @@ -61,11 +65,15 @@ public function theGettersAndSettersShouldWorkProperly() $date = (new \DateTime('now'))->format('Y-m-d h:i:s'); $transactionType->setPayment($payment); $transactionType->setDate($date); + $transactionType->setIsError(true)->setIsPending(true)->setIsSuccess(true); $this->assertSame($payment, $transactionType->getPayment()); $this->assertEquals($date, $transactionType->getDate()); $this->assertNull($transactionType->getExternalId()); $this->assertEquals($payment->getId(), $transactionType->getPaymentId()); + $this->assertTrue($transactionType->isSuccess()); + $this->assertTrue($transactionType->isPending()); + $this->assertTrue($transactionType->isError()); } /** diff --git a/test/unit/Services/HttpServiceTest.php b/test/unit/Services/HttpServiceTest.php index cafc748a..63b542ba 100755 --- a/test/unit/Services/HttpServiceTest.php +++ b/test/unit/Services/HttpServiceTest.php @@ -25,6 +25,7 @@ namespace heidelpayPHP\test\unit; use heidelpayPHP\Adapter\CurlAdapter; +use heidelpayPHP\Adapter\HttpAdapterInterface; use heidelpayPHP\Exceptions\HeidelpayApiException; use heidelpayPHP\Heidelpay; use heidelpayPHP\Interfaces\DebugHandlerInterface; @@ -109,7 +110,7 @@ public function sendShouldInitAndSendRequest() $resource = (new DummyResource())->setParentResource(new Heidelpay('s-priv-MyTestKey')); $adapterMock->expects($this->once())->method('init')->with( 'https://api.heidelpay.com/v1/my/uri/123', - 'dummyResourceJsonSerialized', + '{"dummyResource": "JsonSerialized"}', 'GET' ); $adapterMock->expects($this->once())->method('setUserAgent')->with('HeidelpayPHP'); @@ -154,9 +155,11 @@ public function sendShouldLogDebugMessagesIfDebugModeAndHandlerAreSet() $httpServiceMock->method('getAdapter')->willReturn($adapterMock); $loggerMock = $this->getMockBuilder(DummyDebugHandler::class)->setMethods(['log'])->getMock(); - $loggerMock->expects($this->exactly(3))->method('log')->withConsecutive( + $loggerMock->expects($this->exactly(5))->method('log')->withConsecutive( ['GET: https://api.heidelpay.com/v1/my/uri/123'], - ['Request: dummyResourceJsonSerialized'], + ['Response: {"response":"myResponseString"}'], + ['POST: https://api.heidelpay.com/v1/my/uri/123'], + ['Request: {"dummyResource": "JsonSerialized"}'], ['Response: {"response":"myResponseString"}'] ); @@ -166,7 +169,9 @@ public function sendShouldLogDebugMessagesIfDebugModeAndHandlerAreSet() /** @var HttpService $httpServiceMock*/ $response = $httpServiceMock->send('/my/uri/123', $resource); + $this->assertEquals('{"response":"myResponseString"}', $response); + $response = $httpServiceMock->send('/my/uri/123', $resource, HttpAdapterInterface::REQUEST_POST); $this->assertEquals('{"response":"myResponseString"}', $response); }