Skip to content

Commit

Permalink
Added the transaction handler class (!experimental!) with retry feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 6, 2024
1 parent 64b4a74 commit eaf44ae
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
##### 2024-XX-YY

- Added support for passing `buttonText` and `buttonClass` variables to payment request's getHtmlSnippet() method
- Added transaction handler with retry payment support
- Changed minimum Vanilo requirement to v4.1

## 3.0.0
Expand Down
9 changes: 8 additions & 1 deletion src/NetopiaPaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Vanilo\Netopia\Concerns\HasFullNetopiaInteraction;
use Vanilo\Netopia\Factories\RequestFactory;
use Vanilo\Netopia\Factories\ResponseFactory;
use Vanilo\Netopia\Transaction\Handler;
use Vanilo\Payment\Contracts\Payment;
use Vanilo\Payment\Contracts\PaymentGateway;
use Vanilo\Payment\Contracts\PaymentRequest;
Expand All @@ -35,6 +36,8 @@ class NetopiaPaymentGateway implements PaymentGateway

private ?RequestFactory $requestFactory = null;

private ?Handler $transactionHandler = null;

public static function getName(): string
{
return 'Netopia';
Expand Down Expand Up @@ -68,7 +71,11 @@ public function processPaymentResponse(Request $request, array $options = []): P

public function transactionHandler(): ?TransactionHandler
{
return null;
if (null === $this->transactionHandler) {
$this->transactionHandler = new Handler($this);
}

return $this->transactionHandler;
}

public function isOffline(): bool
Expand Down
65 changes: 65 additions & 0 deletions src/Transaction/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

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

namespace Vanilo\Netopia\Transaction;

use Vanilo\Netopia\NetopiaPaymentGateway;
use Vanilo\Payment\Contracts\Payment;
use Vanilo\Payment\Contracts\PaymentRequest;
use Vanilo\Payment\Contracts\Transaction;
use Vanilo\Payment\Contracts\TransactionHandler;
use Vanilo\Payment\Contracts\TransactionNotCreated;
use Vanilo\Payment\Responses\NoTransaction;

class Handler implements TransactionHandler
{
public function __construct(
private NetopiaPaymentGateway $gateway,
) {
}

public function supportsRefunds(): bool
{
return false; // In, fact it does via the undocumented SOAP API
}

public function supportsRetry(): bool
{
return true;
}

public function allowsRefund(Payment $payment): bool
{
return false;
}

public function issueRefund(Payment $payment, float $amount, array $options = []): Transaction|TransactionNotCreated
{
return NoTransaction::create($payment, 'Feature not implemented');
}

public function canBeRetried(Payment $payment): bool
{
return $payment->getStatus()->isDeclined();
}

public function getRetryRequest(Payment $payment, array $options = []): PaymentRequest|TransactionNotCreated
{
if (!$this->canBeRetried($payment)) {
return NoTransaction::create($payment, __('The payment is not in a state that allows retrying'));
}

return $this->gateway->createPaymentRequest($payment, null, $options);
}
}
77 changes: 77 additions & 0 deletions tests/TransactionHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

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

namespace Vanilo\Netopia\Tests;

use Vanilo\Netopia\Messages\NetopiaPaymentRequest;
use Vanilo\Netopia\NetopiaPaymentGateway;
use Vanilo\Netopia\Tests\Dummies\Order;
use Vanilo\Netopia\Transaction\Handler;
use Vanilo\Payment\Contracts\TransactionNotCreated;
use Vanilo\Payment\Factories\PaymentFactory;
use Vanilo\Payment\Models\PaymentMethod;
use Vanilo\Payment\Models\PaymentStatus;
use Vanilo\Payment\PaymentGateways;

class TransactionHandlerTest extends TestCase
{
private PaymentMethod $method;

protected function setUp(): void
{
parent::setUp();

$this->method = PaymentMethod::create([
'gateway' => NetopiaPaymentGateway::DEFAULT_ID,
'name' => 'Netopia',
]);
}

/** @test */
public function it_returns_a_netopia_handler_instance()
{
$this->assertInstanceOf(Handler::class, PaymentGateways::make('netopia')->transactionHandler());
}

/** @test */
public function it_returns_no_transaction_when_the_payment_is_pending()
{
$order = Order::create(['currency' => 'RON', 'amount' => 315]);
$payment = PaymentFactory::createFromPayable($order, $this->method);

$this->assertInstanceOf(
TransactionNotCreated::class,
PaymentGateways::make('netopia')->transactionHandler()->getRetryRequest($payment),
);
}

/** @test */
public function it_returns_a_payment_request_when_the_payment_is_declined()
{
$order = Order::create(['currency' => 'RON', 'amount' => 180]);
$payment = PaymentFactory::createFromPayable($order, $this->method);
$payment->status = PaymentStatus::DECLINED;
$payment->save();

$this->assertInstanceOf(
NetopiaPaymentRequest::class,
PaymentGateways::make('netopia')->transactionHandler()->getRetryRequest($payment),
);
}

private function gateway(): NetopiaPaymentGateway
{
return PaymentGateways::make('netopia');
}
}

0 comments on commit eaf44ae

Please sign in to comment.