Skip to content

Commit

Permalink
Extended the experimental transaction handling classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 4, 2024
1 parent 24ce558 commit 6cc94d2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Payment/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
##### 2024-XX-YY

- Changed the offline payment gateway's icon from a circle to a plug+x
- [EXPERIMENTAL] Added the supportsRetry, allowsRefund, canBeRetried and getRetryRequest methods to the TransactionHandler interface
- [EXPERIMENTAL] Added the `NoTransaction` DTO

## 4.0.0
##### 2024-04-25
Expand Down
9 changes: 9 additions & 0 deletions src/Payment/Contracts/TransactionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
*/
interface TransactionHandler
{
/** @todo Move this to the Gateway in v5 along with all future supports<Feature> methods */
public function supportsRefunds(): bool;

public function supportsRetry(): bool;

public function allowsRefund(Payment $payment): bool;

public function issueRefund(Payment $payment, float $amount, array $options = []): Transaction|TransactionNotCreated;

public function canBeRetried(Payment $payment): bool;

public function getRetryRequest(Payment $payment, array $options = []): PaymentRequest|TransactionNotCreated;
}
80 changes: 80 additions & 0 deletions src/Payment/Responses/NoTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/**
* Contains the NoTransaction class.
*
* @copyright Copyright (c) 2024 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2024-06-04
*
*/

namespace Vanilo\Payment\Responses;

use Vanilo\Payment\Contracts\Payment;
use Vanilo\Payment\Contracts\TransactionNotCreated;
use Vanilo\Payment\Models\PaymentProxy;

final class NoTransaction implements TransactionNotCreated
{
private readonly string $paymentId;
private ?Payment $payment = null;

private ?string $reason = null;

private bool $shouldBeRetried = false;

public mixed $details = null;

public function __construct(
Payment|string $payment,
) {
if ($payment instanceof Payment) {
$this->paymentId = $payment->getPaymentId();
$this->payment = $payment;
} else {
$this->paymentId = $payment;
}
}

public static function create(Payment|string $payment, ?string $reason, bool $shouldBeRetried = false): self
{
$instance = new self($payment);
$instance->reason = $reason;
$instance->shouldBeRetried = $shouldBeRetried;

return $instance;
}

public function getPaymentId(): string
{
return $this->paymentId;
}

public function getPayment(): Payment
{
if (null === $this->payment) {
$this->payment = PaymentProxy::find($this->paymentId);
}

return $this->payment;
}

public function reason(): ?string
{
return $this->reason;
}

public function shouldBeRetried(): bool
{
return $this->shouldBeRetried;
}

public function getDetails()
{
return $this->details;
}
}

0 comments on commit 6cc94d2

Please sign in to comment.