-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPaymentEventSubscriber.php
More file actions
103 lines (92 loc) · 2.93 KB
/
PaymentEventSubscriber.php
File metadata and controls
103 lines (92 loc) · 2.93 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
<?php
namespace Drupal\commerce_log\EventSubscriber;
use Drupal\commerce_payment\Entity\PaymentInterface;
use Drupal\commerce_payment\Event\PaymentEvent;
use Drupal\commerce_payment\Event\PaymentEvents;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PaymentEventSubscriber implements EventSubscriberInterface {
/**
* The log storage.
*
* @var \Drupal\commerce_log\LogStorageInterface
*/
protected $logStorage;
/**
* Constructs a new PaymentEventSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->logStorage = $entity_type_manager->getStorage('commerce_log');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
PaymentEvents::PAYMENT_INSERT => ['onPaymentInsert', -100],
PaymentEvents::PAYMENT_UPDATE => ['onPaymentUpdate', -100],
PaymentEvents::PAYMENT_DELETE => ['onPaymentDelete', -100],
];
return $events;
}
/**
* Creates a log when a payment is inserted.
*
* @param \Drupal\commerce_payment\Event\PaymentEvent $event
* The payment event.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function onPaymentInsert(PaymentEvent $event) {
$this->logPayment($event->getPayment(), 'payment_insert');
}
/**
* Creates a log when a payment is updated.
*
* @param \Drupal\commerce_payment\Event\PaymentEvent $event
* The payment event.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function onPaymentUpdate(PaymentEvent $event) {
$payment = $event->getPayment();
$this->logPayment($payment, 'payment_update');
if ($refunded = $payment->getRefundedAmount()) {
$this->logStorage->generate($payment, 'refund', [
'refunded_amount' => $refunded,
])->save();
}
}
/**
* Creates a log when a payment is deleted.
*
* @param \Drupal\commerce_payment\Event\PaymentEvent $event
* The payment event.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function onPaymentDelete(PaymentEvent $event) {
$this->logPayment($event->getPayment(), 'payment_delete');
}
/**
* Creates a log when a payment is changed.
*
* @param \Drupal\commerce_payment\Entity\PaymentInterface $payment
* The payment.
* @param string $templateId
* The log template ID.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function logPayment(PaymentInterface $payment, $templateId) {
$this->logStorage->generate($payment, $templateId, [
'amount' => $payment->getAmount(),
])->save();
}
}