From 78cf5d6831e7ecdd79a7b8dd66d5f534c54623a1 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Sat, 30 Mar 2024 22:51:38 +0200 Subject: [PATCH] Added the `getTransactionAmount()` method to the `PaymentResponse` interface --- Changelog.md | 2 ++ src/Payment/Changelog.md | 2 ++ src/Payment/Contracts/Payment.php | 3 --- src/Payment/Contracts/PaymentResponse.php | 3 +++ src/Payment/Models/PaymentHistory.php | 2 +- src/Payment/Processing/PaymentResponseHandler.php | 6 +++--- src/Payment/Responses/NullResponse.php | 5 +++++ src/Payment/Tests/Examples/SomePaymentResponse.php | 13 +++++++++---- src/Payment/Tests/GatewaysTest.php | 2 +- src/Payment/Tests/ResponseHandlerTest.php | 2 +- 10 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Changelog.md b/Changelog.md index a9c08bdd..7dd5ea05 100644 --- a/Changelog.md +++ b/Changelog.md @@ -106,6 +106,7 @@ - `hasRemoteId()` - `getRemoteId()` - `isOffline()` +- BC: Added the `getTransactionAmount()` method to the `PaymentResponse` interface - BC: Added the following methods to the `Payable` interface: - `getNumber()` - `getPayableRemoteId()` @@ -121,6 +122,7 @@ - `getBillingCountries()` - `getShippingCountries()` - Deprecated the `PaymentMethod::getConfiguration()` in favor of `configuration()` +- Deprecated the `PaymentResponse::getAmountPaid()` method in favor of `getTransactionAmount()` - Added the `Schematized` interface - Added the nette/schema package requirement (v1.2.5+) - Fixed possible null return type on Billpayer::getName() when is_organization is true but the company name is null diff --git a/src/Payment/Changelog.md b/src/Payment/Changelog.md index 0f5cd207..28c22d9e 100644 --- a/src/Payment/Changelog.md +++ b/src/Payment/Changelog.md @@ -18,8 +18,10 @@ - `hasRemoteId()` - `getRemoteId()` - `isOffline()` +- BC: Added the `getTransactionAmount()` method to the `PaymentResponse` interface - BC: Changed the `PaymentMethod` interface into Configurable - Deprecated the `PaymentMethod::getConfiguration()` in favor of `configuration()` +- Deprecated the `Payment::getAmountPaid()` method in favor of `getTransactionAmount()` ## 3.x Series diff --git a/src/Payment/Contracts/Payment.php b/src/Payment/Contracts/Payment.php index 70e2fd61..4afe9f60 100644 --- a/src/Payment/Contracts/Payment.php +++ b/src/Payment/Contracts/Payment.php @@ -24,9 +24,6 @@ public function getAmount(): float; public function getCurrency(): string; - // "Transaction amount" would've been a better name - // since the value returned here might represent - // an amount of a full/partial refund as well public function getAmountPaid(): float; public function getStatus(): PaymentStatus; diff --git a/src/Payment/Contracts/PaymentResponse.php b/src/Payment/Contracts/PaymentResponse.php index 061f4720..a6ae7b7c 100644 --- a/src/Payment/Contracts/PaymentResponse.php +++ b/src/Payment/Contracts/PaymentResponse.php @@ -24,8 +24,11 @@ public function getMessage(): ?string; public function getTransactionId(): ?string; + /** @deprecated use getTransactionAmount() instead */ public function getAmountPaid(): ?float; + public function getTransactionAmount(): float; + public function getPaymentId(): string; public function getStatus(): PaymentStatus; diff --git a/src/Payment/Models/PaymentHistory.php b/src/Payment/Models/PaymentHistory.php index 9f619e8e..9ecb6edf 100644 --- a/src/Payment/Models/PaymentHistory.php +++ b/src/Payment/Models/PaymentHistory.php @@ -60,7 +60,7 @@ public static function addPaymentResponse( 'old_status' => $oldStatus ?: $payment->getStatus()->value(), 'new_status' => $response->getStatus()->value(), 'message' => $response->getMessage(), - 'transaction_amount' => $response->getAmountPaid(), + 'transaction_amount' => $response->getTransactionAmount(), 'native_status' => $response->getNativeStatus()->value(), 'transaction_number' => $response->getTransactionId(), ]); diff --git a/src/Payment/Processing/PaymentResponseHandler.php b/src/Payment/Processing/PaymentResponseHandler.php index 4a8f9286..fdfdc9ae 100644 --- a/src/Payment/Processing/PaymentResponseHandler.php +++ b/src/Payment/Processing/PaymentResponseHandler.php @@ -53,9 +53,9 @@ public function writeResponseToHistory(): void public function updatePayment(): void { $this->payment->status = $this->newStatus; - $amountPaid = $this->response->getAmountPaid(); - if (null !== $amountPaid) { - $this->payment->amount_paid += $this->response->getAmountPaid(); + $transactionAmount = $this->response->getTransactionAmount(); + if (0.0 !== $transactionAmount) { + $this->payment->amount_paid += $transactionAmount; } $this->payment->status_message = $this->response->getMessage(); $this->payment->save(); diff --git a/src/Payment/Responses/NullResponse.php b/src/Payment/Responses/NullResponse.php index 64f12399..a5082202 100644 --- a/src/Payment/Responses/NullResponse.php +++ b/src/Payment/Responses/NullResponse.php @@ -41,6 +41,11 @@ public function getAmountPaid(): ?float return null; } + public function getTransactionAmount(): float + { + return 0; + } + public function getPaymentId(): string { return ''; diff --git a/src/Payment/Tests/Examples/SomePaymentResponse.php b/src/Payment/Tests/Examples/SomePaymentResponse.php index a0f2e757..5f8900db 100644 --- a/src/Payment/Tests/Examples/SomePaymentResponse.php +++ b/src/Payment/Tests/Examples/SomePaymentResponse.php @@ -26,7 +26,7 @@ class SomePaymentResponse implements PaymentResponse private string $transactionId; - private ?float $amountPaid; + private ?float $transactionAmount; private string $paymentId; @@ -38,7 +38,7 @@ public function __construct( string $message, bool $wasSuccessful, string $transactionId, - ?float $amountPaid, + ?float $transactionAmount, string $paymentId, SomeNativeStatus $nativeStatus, PaymentStatus $status @@ -47,7 +47,7 @@ public function __construct( $this->nativeStatus = $nativeStatus; $this->wasSuccessful = $wasSuccessful; $this->transactionId = $transactionId; - $this->amountPaid = $amountPaid; + $this->transactionAmount = $transactionAmount; $this->paymentId = $paymentId; $this->status = $status; } @@ -69,7 +69,12 @@ public function getTransactionId(): ?string public function getAmountPaid(): ?float { - return $this->amountPaid; + return $this->getTransactionAmount(); + } + + public function getTransactionAmount(): float + { + return (float) $this->transactionAmount; } public function getPaymentId(): string diff --git a/src/Payment/Tests/GatewaysTest.php b/src/Payment/Tests/GatewaysTest.php index 9a98ecf0..6d5e578b 100644 --- a/src/Payment/Tests/GatewaysTest.php +++ b/src/Payment/Tests/GatewaysTest.php @@ -64,7 +64,7 @@ public function non_illuminate_request_type_request_objects_can_be_accepted_by_p $this->assertInstanceOf(PaymentResponse::class, $result); $this->assertEquals(PaymentStatus::PAID(), $result->getStatus()); - $this->assertEquals(27.99, $result->getAmountPaid()); + $this->assertEquals(27.99, $result->getTransactionAmount()); $this->assertEquals('C9278', $result->getTransactionId()); } } diff --git a/src/Payment/Tests/ResponseHandlerTest.php b/src/Payment/Tests/ResponseHandlerTest.php index 0af23f9a..6f20ae1b 100644 --- a/src/Payment/Tests/ResponseHandlerTest.php +++ b/src/Payment/Tests/ResponseHandlerTest.php @@ -146,7 +146,7 @@ public function it_creates_payment_history_entries() $this->assertEquals(PaymentStatus::TIMEOUT, $entry->new_status->value()); $this->assertEquals(PaymentStatus::PENDING, $entry->old_status->value()); $this->assertEquals('Timed out like a lion', $entry->message); - $this->assertNull($entry->transaction_amount); + $this->assertEquals(0, $entry->transaction_amount); } /** @test */