-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the transaction handler class (!experimental!) with retry feature
- Loading branch information
1 parent
64b4a74
commit eaf44ae
Showing
4 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |