-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for redirect-based payments in API (#62)
- Loading branch information
Showing
20 changed files
with
467 additions
and
30 deletions.
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Api\Command; | ||
|
||
final class PayByRedirect | ||
{ | ||
public function __construct( | ||
public readonly int $paymentId, | ||
) { | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Api\Command; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Model\PaymentDetails; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
use Webmozart\Assert\Assert; | ||
|
||
#[AsMessageHandler] | ||
final class PayByRedirectHandler extends AbstractPayByHandler | ||
{ | ||
public function __invoke(PayByRedirect $command): PayResult | ||
{ | ||
$payment = $this->findOr404($command->paymentId); | ||
|
||
$this->createTransaction($payment); | ||
|
||
return $this->createResultFrom($payment); | ||
} | ||
|
||
private function createResultFrom(PaymentInterface $payment): PayResult | ||
{ | ||
$paymentDetails = PaymentDetails::fromArray($payment->getDetails()); | ||
|
||
Assert::notNull($paymentDetails->getStatus(), 'Payment status is required to create a result.'); | ||
Assert::notNull($paymentDetails->getPaymentUrl(), 'Payment URL is required to create a result.'); | ||
|
||
return new PayResult( | ||
$paymentDetails->getStatus(), | ||
$paymentDetails->getPaymentUrl(), | ||
); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Api\Command\Pay; | ||
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByRedirect; | ||
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\Exception\UnsupportedNextCommandFactory; | ||
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommandFactoryInterface; | ||
use Payum\Core\Model\GatewayConfigInterface; | ||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
|
||
final class PayByRedirectFactory implements NextCommandFactoryInterface | ||
{ | ||
public function __construct( | ||
private readonly CypherInterface $cypher, | ||
) { | ||
} | ||
|
||
public function create(Pay $command, PaymentInterface $payment): PayByRedirect | ||
{ | ||
if (!$this->supports($command, $payment)) { | ||
throw new UnsupportedNextCommandFactory('This factory does not support the given command.'); | ||
} | ||
|
||
/** @var int $paymentId */ | ||
$paymentId = $payment->getId(); | ||
|
||
return new PayByRedirect($paymentId); | ||
} | ||
|
||
public function supports(Pay $command, PaymentInterface $payment): bool | ||
{ | ||
if ($payment->getId() === null) { | ||
return false; | ||
} | ||
|
||
$gatewayConfig = $this->getGatewayConfig($payment); | ||
|
||
if (null === $gatewayConfig) { | ||
return false; | ||
} | ||
|
||
if ($gatewayConfig instanceof CryptedInterface) { | ||
$gatewayConfig->decrypt($this->cypher); | ||
} | ||
|
||
$config = $gatewayConfig->getConfig(); | ||
|
||
return isset($config['type']) && $config['type'] === 'redirect'; | ||
} | ||
|
||
private function getGatewayConfig(PaymentInterface $payment): ?GatewayConfigInterface | ||
{ | ||
/** @var PaymentMethodInterface|null $paymentMethod */ | ||
$paymentMethod = $payment->getMethod(); | ||
|
||
return $paymentMethod?->getGatewayConfig(); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...nses/shop/paying_for_orders_by_redirect/test_paying_with_redirect_based_payment_type.json
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,12 @@ | ||
{ | ||
"@context": { | ||
"@vocab": "http://localhost/api/v2/docs.jsonld#", | ||
"hydra": "http://www.w3.org/ns/hydra/core#", | ||
"status": "PayResult/status", | ||
"transactionPaymentUrl": "PayResult/transactionPaymentUrl" | ||
}, | ||
"@type": "PayResult", | ||
"@id": "/api/v2/.well-known/genid/@string@", | ||
"status": "pending", | ||
"transactionPaymentUrl": "@string@" | ||
} |
Oops, something went wrong.