Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions src/Action/Admin/CompleteRefundPaymentAction.php

This file was deleted.

60 changes: 0 additions & 60 deletions src/Action/Admin/RefundUnitsAction.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;

/** @deprecated */
#[AutoconfigureTag(
name: 'payum.action',
attributes: [
Expand Down
1 change: 1 addition & 0 deletions src/Action/ConvertPaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

/** @deprecated */
#[AutoconfigureTag(
name: 'payum.action',
attributes: [
Expand Down
1 change: 1 addition & 0 deletions src/Action/NotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/** @deprecated */
#[AutoconfigureTag(
name: 'payum.action',
attributes: [
Expand Down
1 change: 1 addition & 0 deletions src/Action/StatusAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\HttpFoundation\RequestStack;

/** @deprecated */
#[AutoconfigureTag(
name: 'payum.action',
attributes: [
Expand Down
49 changes: 34 additions & 15 deletions src/Command/Handler/NotifyPaymentRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace PayPlug\SyliusPayPlugPlugin\Command\Handler;

use Payplug\Resource\Payment;
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactoryInterface;
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientInterface;
use PayPlug\SyliusPayPlugPlugin\Command\NotifyPaymentRequest;
use PayPlug\SyliusPayPlugPlugin\Handler\PaymentNotificationHandler;
use PayPlug\SyliusPayPlugPlugin\Handler\RefundNotificationHandler;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Bundle\PaymentBundle\Provider\PaymentRequestProviderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
Expand All @@ -21,34 +23,51 @@ class NotifyPaymentRequestHandler
public function __construct(
private PaymentRequestProviderInterface $paymentRequestProvider,
private StateMachineInterface $stateMachine,
private PayPlugApiClientFactoryInterface $apiClientFactory,
private PaymentNotificationHandler $paymentNotificationHandler,
private RefundNotificationHandler $refundNotificationHandler,
) {}

public function __invoke(NotifyPaymentRequest $notifyPaymentRequest): void
{
$paymentRequest = $this->paymentRequestProvider->provide($notifyPaymentRequest);
/** @var PaymentInterface $payment */
$payment = $paymentRequest->getPayment();
if ($payment->getState() !== PaymentInterface::STATE_COMPLETED) {
// If the payment is already completed, we do not need to notify again
$this->stateMachine->apply(
$paymentRequest,
PaymentRequestTransitions::GRAPH,
PaymentRequestTransitions::TRANSITION_COMPLETE,
);

return;
}

try {
// Payload contains what's send by payplug, no need to retrieve it from PayPlug
// @phpstan-ignore-next-line - cannot access offset content / http_request on mixed
$payplugPayment = Payment::fromAttributes(json_decode($paymentRequest->getPayload()['http_request']['content'] ?? '{}', true, 512, \JSON_THROW_ON_ERROR));
$payload = $paymentRequest->getPayload();
$content = $payload['http_request']['content'] ?? null; // @phpstan-ignore-line
if (!is_string($content) || '' === $content) {
throw new \LogicException('Invalid PayPlug notification payload.');
}

$method = $payment->getMethod();
if (null === $method) {
throw new \LogicException('Payment method is not set for the payment.');
}

$client = $this->apiClientFactory->createForPaymentMethod($method);
$resource = $client->treat($content);

if ($resource instanceof Payment && $payment->getState() === PaymentInterface::STATE_COMPLETED) {
// If the payment is already completed, we do not need to update it again
$this->stateMachine->apply(
$paymentRequest,
PaymentRequestTransitions::GRAPH,
PaymentRequestTransitions::TRANSITION_COMPLETE,
);

return;
}

$details = new \ArrayObject($payment->getDetails());
$this->paymentNotificationHandler->treat($payment, $payplugPayment, $details);
$this->paymentNotificationHandler->treat($payment, $resource, $details);
$this->refundNotificationHandler->treat($payment, $resource, $details);

$payment->setDetails($details->getArrayCopy());
$this->updatePaymentState($payment);
if ($resource instanceof Payment) {
$this->updatePaymentState($payment);
}

$this->stateMachine->apply(
$paymentRequest,
Expand Down
5 changes: 4 additions & 1 deletion src/Command/Handler/StatusPaymentRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public function __invoke(StatusPaymentRequest $statusPaymentRequest): void
$this->paymentNotificationHandler->treat($payment, $payplugPayment, $details);

$payment->setDetails($details->getArrayCopy());
$this->updatePaymentState($payment);
if ($payment->getState() !== PaymentInterface::STATE_COMPLETED) {
// If is already completed, do not try to update it again (updated by notification)
$this->updatePaymentState($payment);
}

// Mark the PaymentRequest as completed
$this->stateMachine->apply(
Expand Down
8 changes: 7 additions & 1 deletion src/Controller/IpnAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
use PayPlug\SyliusPayPlugPlugin\Handler\RefundNotificationHandler;
use PayPlug\SyliusPayPlugPlugin\Repository\PaymentRepositoryInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Model\GatewayConfigInterface;
use Psr\Log\LoggerInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Payment\Model\GatewayConfigInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Attribute\Route;
use Webmozart\Assert\Assert;

/** @deprecated */
#[AsController]
class IpnAction
{
Expand Down Expand Up @@ -60,6 +61,11 @@ public function __invoke(Request $request): JsonResponse
}

$payment = $this->paymentRepository->findOneByPayPlugPaymentId($details['id']);

if (null === $payment) {
return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
}

$paymentMethod = $payment->getMethod();

Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class);
Expand Down
10 changes: 7 additions & 3 deletions src/Controller/OneClickAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Payum\Core\ApiAwareInterface;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Model\GatewayConfigInterface;
use Sylius\Component\Payment\Model\GatewayConfigInterface;
use Payum\Core\Payum;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
Expand All @@ -23,7 +23,7 @@
use Symfony\Component\Routing\Attribute\Route;

/**
* TODO : !!! Check if this controller is still needed
* @deprecated - Used by payum
*/
#[AsController]
class OneClickAction extends AbstractController implements GatewayAwareInterface, ApiAwareInterface
Expand Down Expand Up @@ -51,9 +51,13 @@ public function __invoke(Request $request): Response

/** @var GatewayConfigInterface $paymentGateway */
$paymentGateway = $paymentMethod->getGatewayConfig();
$gatewayName = $paymentGateway->getGatewayName();
if (null === $gatewayName) {
throw new \InvalidArgumentException('Payment gateway name cannot be null');
}

$captureToken = $this->payum->getTokenFactory()->createCaptureToken(
$paymentGateway->getGatewayName(),
$gatewayName,
$payment,
'sylius_shop_order_thank_you',
[],
Expand Down
Loading
Loading