-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathIpnAction.php
More file actions
112 lines (90 loc) · 4.36 KB
/
IpnAction.php
File metadata and controls
112 lines (90 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace PayPlug\SyliusPayPlugPlugin\Controller;
use Doctrine\ORM\EntityManagerInterface;
use LogicException;
use Payplug\Exception\PayplugException;
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientFactoryInterface;
use PayPlug\SyliusPayPlugPlugin\ApiClient\PayPlugApiClientInterface;
use PayPlug\SyliusPayPlugPlugin\Gateway\ApplePayGatewayFactory;
use PayPlug\SyliusPayPlugPlugin\Gateway\BancontactGatewayFactory;
use PayPlug\SyliusPayPlugPlugin\Gateway\OneyGatewayFactory;
use PayPlug\SyliusPayPlugPlugin\Gateway\PayPlugGatewayFactory;
use PayPlug\SyliusPayPlugPlugin\Handler\PaymentNotificationHandler;
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 Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;
class IpnAction
{
private LoggerInterface $logger;
private PaymentNotificationHandler $paymentNotificationHandler;
private RefundNotificationHandler $refundNotificationHandler;
private PayPlugApiClientFactoryInterface $apiClientFactory;
private PayPlugApiClientInterface $payPlugApiClient;
private PaymentRepositoryInterface $paymentRepository;
private EntityManagerInterface $entityManager;
public function __construct(
LoggerInterface $logger,
PaymentNotificationHandler $paymentNotificationHandler,
RefundNotificationHandler $refundNotificationHandler,
PayPlugApiClientFactoryInterface $apiClientFactory,
PaymentRepositoryInterface $paymentRepository,
EntityManagerInterface $entityManager
) {
$this->logger = $logger;
$this->paymentNotificationHandler = $paymentNotificationHandler;
$this->refundNotificationHandler = $refundNotificationHandler;
$this->apiClientFactory = $apiClientFactory;
$this->paymentRepository = $paymentRepository;
$this->entityManager = $entityManager;
}
public function __invoke(Request $request): JsonResponse
{
$input = $request->getContent();
if (!is_string($input)) {
throw new LogicException('Input must be of type string.');
}
$content = json_decode($input, true);
$details = ArrayObject::ensureArrayObject($content);
// if we are too fast canceling a payment before we got an answer from PayPlug gateway
if (null === $details['id']) {
return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
}
$payment = $this->paymentRepository->findOneByPayPlugPaymentId($details['id']);
$paymentMethod = $payment->getMethod();
Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class);
$gateway = $paymentMethod->getGatewayConfig();
Assert::isInstanceOf($gateway, GatewayConfigInterface::class);
$gatewayConfig = $gateway->getConfig();
if (
!$paymentMethod->getGatewayConfig() instanceof GatewayConfigInterface ||
!\in_array($factoryName = $paymentMethod->getGatewayConfig()->getFactoryName(), [
PayPlugGatewayFactory::FACTORY_NAME,
OneyGatewayFactory::FACTORY_NAME,
BancontactGatewayFactory::FACTORY_NAME,
ApplePayGatewayFactory::FACTORY_NAME,
], true)
) {
return new JsonResponse(null, Response::HTTP_UNAUTHORIZED);
}
$this->payPlugApiClient = $this->apiClientFactory->create($factoryName, $gatewayConfig['secretKey']);
$this->payPlugApiClient->initialise($gatewayConfig['secretKey']);
try {
$resource = $this->payPlugApiClient->treat($input);
$this->paymentNotificationHandler->treat($payment, $resource, $details);
$this->refundNotificationHandler->treat($payment, $resource, $details);
$this->entityManager->flush();
} catch (PayplugException $exception) {
$details['status'] = PayPlugApiClientInterface::FAILED;
$this->logger->error('[PayPlug] Notify action', ['error' => $exception->getMessage()]);
}
return new JsonResponse();
}
}