-
Notifications
You must be signed in to change notification settings - Fork 255
Add payment logging #610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add payment logging #610
Changes from all commits
ad8b4fd
1b6c883
d784d3b
b708481
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
<?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. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entity_type_manager) { | ||
$this->logStorage = $entity_type_manager->getStorage('commerce_log'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
$events = [ | ||
'commerce_payment.authorize.pre_transition' => ['onAuthorizeTransition', -100], | ||
'commerce_payment.void.pre_transition' => ['onVoidTransition', -100], | ||
'commerce_payment.expire.pre_transition' => ['onExpireTransition', -100], | ||
'commerce_payment.authorize_capture.pre_transition' => ['onAuthorizeCaptureTransition', -100], | ||
PaymentEvents::PAYMENT_AUTHORIZED => ['onAuthorize', -100], | ||
PaymentEvents::PAYMENT_VOIDED => ['onVoid', -100], | ||
PaymentEvents::PAYMENT_EXPIRED => ['onExpire', -100], | ||
PaymentEvents::PAYMENT_AUTHORIZED_CAPTURED => ['onAuthorizeCapture', -100], | ||
PaymentEvents::PAYMENT_PARTIALLY_CAPTURED => ['onPartialCapture', -100], | ||
PaymentEvents::PAYMENT_CAPTURED => ['onCapture', -100], | ||
PaymentEvents::PAYMENT_PARTIALLY_REFUNDED => ['onPartialRefund', -100], | ||
PaymentEvents::PAYMENT_REFUNDED => ['onRefund', -100], | ||
]; | ||
return $events; | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is authorized. | ||
* | ||
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event | ||
* The transition event. | ||
*/ | ||
public function onAuthorizeTransition(WorkflowTransitionEvent $event) { | ||
$this->dispatch(PaymentEvents::PAYMENT_AUTHORIZED, $event->getEntity()); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is voided. | ||
* | ||
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event | ||
* The transition event. | ||
*/ | ||
public function onVoidTransition(WorkflowTransitionEvent $event) { | ||
$this->dispatch(PaymentEvents::PAYMENT_VOIDED, $event->getEntity()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why no logging? |
||
} | ||
|
||
/** | ||
* Creates a log when a payment is expired. | ||
* | ||
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event | ||
* The transition event. | ||
*/ | ||
public function onExpireTransition(WorkflowTransitionEvent $event) { | ||
$this->dispatch(PaymentEvents::PAYMENT_EXPIRED, $event->getEntity()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And on through the rest of this subscriber... |
||
} | ||
|
||
/** | ||
* Creates a log when a payment is authorized and captured. | ||
* | ||
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event | ||
* The transition event. | ||
*/ | ||
public function onAuthorizeCaptureTransition(WorkflowTransitionEvent $event) { | ||
$this->dispatch(PaymentEvents::PAYMENT_AUTHORIZED_CAPTURED, $event->getEntity()); | ||
} | ||
|
||
/** | ||
* Dispatches a PaymentEvent for a payment. | ||
* | ||
* @param string $event_name | ||
* A name of the payment event to dispatch. | ||
* @param \Drupal\commerce_payment\Entity\PaymentInterface $payment | ||
* A payment to use for dispatching the event. | ||
*/ | ||
private function dispatch($event_name, PaymentInterface $payment) { | ||
/** @var \Drupal\commerce_payment\Event\PaymentEvent $event */ | ||
$event = new PaymentEvent($payment); | ||
/** @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher $event_dispatcher */ | ||
$event_dispatcher = \Drupal::service('event_dispatcher'); | ||
$event_dispatcher->dispatch($event_name, $event); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is authorized. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onAuthorize(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_authorized', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is voided. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onVoid(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_voided', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is expired. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onExpire(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_expired', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is authorized and captured. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onAuthorizeCapture(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_authorized_captured', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is partially captured. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onPartialCapture(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_partially_captured', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
'captured_amount' => $event->getAmount(), | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is fully captured. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onCapture(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_captured', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is partially refunded. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onPartialRefund(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_partially_refunded', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
'refunded_amount' => $event->getAmount(), | ||
])->save(); | ||
} | ||
|
||
/** | ||
* Creates a log when a payment is fully refunded. | ||
* | ||
* @param \Drupal\commerce_payment\Event\PaymentEvent $event | ||
* The payment event. | ||
*/ | ||
public function onRefund(PaymentEvent $event) { | ||
$payment = $event->getPayment(); | ||
$this->logStorage->generate($payment, 'payment_refunded', [ | ||
'payment_remote_id' => $payment->getRemoteId(), | ||
])->save(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
* bundle_plugin_type = "commerce_payment_type", | ||
* handlers = { | ||
* "access" = "Drupal\commerce_payment\PaymentAccessControlHandler", | ||
* "event" = "Drupal\commerce_payment\Event\PaymentEvent", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is necessary if we state_machine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is, because payments do not use transitions. |
||
* "list_builder" = "Drupal\commerce_payment\PaymentListBuilder", | ||
* "storage" = "Drupal\commerce_payment\PaymentStorage", | ||
* "form" = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Drupal\commerce_payment\Event; | ||
|
||
use Drupal\commerce_payment\Entity\PaymentInterface; | ||
use Drupal\commerce_price\Price; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Defines the payment event. | ||
* | ||
* @see \Drupal\commerce_payment\Event\PaymentEvents | ||
*/ | ||
class PaymentEvent extends Event { | ||
|
||
/** | ||
* The payment. | ||
* | ||
* @var \Drupal\commerce_payment\Entity\PaymentInterface | ||
*/ | ||
protected $payment; | ||
|
||
/** | ||
* The payment operation amount. | ||
* | ||
* @var \Drupal\commerce_price\Price | ||
*/ | ||
protected $amount; | ||
|
||
/** | ||
* Constructs a new PaymentEvent. | ||
* | ||
* @param \Drupal\commerce_payment\Entity\PaymentInterface $payment | ||
* The payment. | ||
*/ | ||
public function __construct(PaymentInterface $payment) { | ||
$this->payment = $payment; | ||
} | ||
|
||
/** | ||
* @param \Drupal\commerce_price\Price $amount | ||
* The payment operation amount. | ||
*/ | ||
public function setAmount(Price $amount) { | ||
$this->amount = $amount; | ||
} | ||
|
||
/** | ||
* Gets the payment operation amount. | ||
* | ||
* @return \Drupal\commerce_price\Price | ||
* The payment operation amount. | ||
*/ | ||
public function getAmount() { | ||
return $this->amount; | ||
} | ||
|
||
/** | ||
* Gets the payment. | ||
* | ||
* @return \Drupal\commerce_payment\Entity\PaymentInterface | ||
* The payment. | ||
*/ | ||
public function getPayment() { | ||
return $this->payment; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this log?