-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNotificationListener.php
More file actions
executable file
·222 lines (187 loc) · 8.25 KB
/
NotificationListener.php
File metadata and controls
executable file
·222 lines (187 loc) · 8.25 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace PayPlugModule\EventListener;
use Payplug\Resource\Refund;
use Payplug\Resource\Payment;
use PayPlugModule\Event\Notification\UnknownNotificationEvent;
use PayPlugModule\Event\Notification\PaymentNotificationEvent;
use PayPlugModule\Event\Notification\RefundNotificationEvent;
use PayPlugModule\Model\OrderPayPlugData;
use PayPlugModule\Model\OrderPayPlugDataQuery;
use PayPlugModule\Model\OrderPayPlugMultiPayment;
use PayPlugModule\Model\OrderPayPlugMultiPaymentQuery;
use PayPlugModule\Model\PayPlugCard;
use PayPlugModule\Model\PayPlugCardQuery;
use PayPlugModule\Model\PayPlugConfigValue;
use PayPlugModule\Model\PayPlugNotificationHistory;
use PayPlugModule\Model\PayPlugNotificationHistoryQuery;
use PayPlugModule\PayPlugModule;
use PayPlugModule\Service\OrderStatusService;
use Propel\Runtime\Collection\Collection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Log\Tlog;
use Thelia\Model\Order;
use Thelia\Model\OrderQuery;
use Thelia\Model\OrderStatusQuery;
class NotificationListener implements EventSubscriberInterface
{
/** @var OrderStatusService */
protected $orderStatusService;
/** @var EventDispatcherInterface */
protected $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher, OrderStatusService $orderStatusService)
{
$this->dispatcher = $dispatcher;
$this->orderStatusService = $orderStatusService;
}
public function handleUnknownNotification(UnknownNotificationEvent $event)
{
$resource = $event->getResource();
switch(true) {
case $resource instanceof Payment:
$paymentNotificationEvent = new PaymentNotificationEvent($resource);
$this->dispatcher->dispatch($paymentNotificationEvent, PaymentNotificationEvent::PAYMENT_NOTIFICATION_EVENT);
break;
case $resource instanceof Refund:
$refundNotificationEvent = new RefundNotificationEvent($resource);
$this->dispatcher->dispatch($refundNotificationEvent, RefundNotificationEvent::REFUND_NOTIFICATION_EVENT);
break;
}
}
public function handlePaymentNotification(PaymentNotificationEvent $event)
{
$transactionRef = $event->getResource()->id;
if (!$transactionRef) {
return null;
}
$order = OrderQuery::create()
->filterByPaymentModuleId(PayPlugModule::getModuleId())
->filterByTransactionRef($transactionRef)
->findOne();
if (null === $order) {
return;
}
$orderPayPlugData = OrderPayPlugDataQuery::create()
->findOneById($order->getId());
if (null === $orderPayPlugData) {
return;
}
$paymentResource = $event->getResource();
$orderStatusId = OrderStatusQuery::getCancelledStatus()->getId();
$orderPayPlugMultiPayment = OrderPayPlugMultiPaymentQuery::create()
->findByOrderId($order->getId());
// Multi payment is really different
if ($orderPayPlugMultiPayment->count() > 0) {
$this->handleMultiPaymentNotification($paymentResource, $order, $orderPayPlugData, $orderPayPlugMultiPayment);
return;
}
if ($orderPayPlugData->getNeedCapture()) {
// Handle differed payment
if ($paymentResource->is_paid) {
$orderPayPlugData->setCapturedAt((new \DateTime()))
->save();
// Don't update status on capture
$orderStatusId = null;
} elseif ($paymentResource->authorization->authorized_at) {
$orderStatusId = PayPlugModule::getConfigValue(PayPlugConfigValue::DIFFERED_PAYMENT_AUTHORIZED_CAPTURE_STATUS);
$orderPayPlugData->setCaptureExpireAt($paymentResource->authorization->expires_at)
->save();
}
} elseif ($paymentResource->is_paid) {
// Handle classic payment
$orderStatusId = OrderStatusQuery::getPaidStatus()->getId();
}
if (null !== $orderStatusId) {
$event = (new OrderEvent($order))
->setStatus($orderStatusId);
$this->dispatcher->dispatch($event, TheliaEvents::ORDER_UPDATE_STATUS);
}
if (null !== $paymentResource->card->id) {
$cardData = $paymentResource->card;
$cardExist = PayPlugCardQuery::create()
->filterByCustomerId($order->getCustomerId())
->filterByUuid($cardData->id)
->findOne();
if (null === $cardExist) {
(new PayPlugCard())
->setUuid($cardData->id)
->setCustomerId($order->getCustomerId())
->setBrand($cardData->brand)
->setLast4($cardData->last4)
->setExpireMonth($cardData->exp_month)
->setExpireYear($cardData->exp_year)
->save();
}
}
}
protected function handleMultiPaymentNotification($paymentResource, Order $order, OrderPayPlugData $orderPayPlugData, Collection $orderMultiPayments)
{
/** @var OrderPayPlugMultiPayment $orderMultiPayment */
foreach ($orderMultiPayments as $orderMultiPayment) {
$orderMultiPayment->setPaymentMethod($paymentResource->card->id);
if ($paymentResource->id === $orderMultiPayment->getPaymentId()) {
$orderStatusId = OrderStatusQuery::getCancelledStatus()->getId();
if ($paymentResource->is_paid) {
$orderMultiPayment->setPaidAt(new \DateTime());
$orderStatusId = OrderStatusQuery::getPaidStatus()->getId();
}
// Update order status only for first payment
if ($orderMultiPayment->getIsFirstPayment()) {
$event = (new OrderEvent($order))
->setStatus($orderStatusId);
$this->dispatcher->dispatch($event, TheliaEvents::ORDER_UPDATE_STATUS);
}
}
$orderMultiPayment->save();
}
}
public function handleRefundNotification(RefundNotificationEvent $event)
{
$transactionRef = $event->getResource()->payment_id;
if (!$transactionRef) {
return;
}
$id = $event->getResource()->id;
$order = OrderQuery::create()
->filterByTransactionRef($transactionRef)
->findOne();
$notificationHistory = PayPlugNotificationHistoryQuery::create()
->filterByUuid($id)
->findOne();
if (!!$notificationHistory) {
Tlog::getInstance()->addAlert("Notification '{$id}' already exist");
return;
}
$multiPayment = OrderPayPlugMultiPaymentQuery::create()
->findOneByPaymentId($transactionRef);
if (null !== $multiPayment) {
$multiPayment->setAmountRefunded((int)$multiPayment->getAmountRefunded() + $event->getResource()->amount)
->save();
$order = $multiPayment->getOrder();
}
if (null === $order) {
return;
}
(new PayPlugNotificationHistory())->setUuid($id)->setOrderId($order->getId())->save();
$orderPayPlugData = OrderPayPlugDataQuery::create()
->findOneById($order->getId());
$orderPayPlugData->setAmountRefunded((int)$orderPayPlugData->getAmountRefunded() + $event->getResource()->amount)
->save();
$event = (new OrderEvent($order))
->setStatus(OrderStatusQuery::getRefundedStatus()->getId());
$this->dispatcher->dispatch($event, TheliaEvents::ORDER_UPDATE_STATUS);
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
UnknownNotificationEvent::UNKNOWN_NOTIFICATION_EVENT => ['handleUnknownNotification', 128],
PaymentNotificationEvent::PAYMENT_NOTIFICATION_EVENT => ['handlePaymentNotification', 128],
RefundNotificationEvent::REFUND_NOTIFICATION_EVENT => ['handleRefundNotification', 128]
];
}
}