Add payment logging to orders

Created on 20 January 2017, almost 8 years ago
Updated 22 March 2023, almost 2 years ago

No payment-related events are currently being logged.

Old PR: https://github.com/drupalcommerce/commerce/pull/797

Feature request
Status

Fixed

Version

2.0

Component

Log

Created by

🇫🇷France maciej.zgadzaj

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • @tbkot opened merge request.
  • 🇮🇱Israel jsacksick

    @tBKoT: For determining that a payment is authorized or completed, we need to follow the same logic as in the Payment::presave() method.

    For example, for determining that a payment is authorized, you need to do the following:

    $state = $payment->getState()->getId();
    $original_state = isset($payment->original) ? $payment->original->getState()->getId() : '';
    if ($state === 'authorization' && $original_state !== 'authorization') {
    

    For determining that a payment is completed, same principle:

    if ($state === 'completed' && $original_state !== 'completed') {
    

    So this means you should call a common method that is called from payment insert and payment update.

    Also, I'm thinking we should react to PaymentEvents::PAYMENT_DELETE instead of PaymentEvents::PAYMENT_PREDELETE since it is invoked AFTER the deletion.

  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work almost 2 years ago
  • 🇮🇱Israel jsacksick

    @tBKot: Looks good, we're close!

    Did some manual testing and ended up with the following crash on checkout completion:

    Error: Call to a member function label() on null in Drupal\commerce_log\EventSubscriber\PaymentEventSubscriber->onPaymentAdd()

    Not all payments have a payment method so:
    $payment->getPaymentMethod()->label() should be updated, we should ensure the payment method is set/exists before accessing it.

    Also, let's rename onPaymentAdd() to onPaymentInsert().

    And the payment event subscriber is missing an empty line at end of file.

    Would be great if we could expand the tests to ensure creating a payment doesn't crash when the payment method is empty so we're covered :).

  • 🇮🇱Israel jsacksick

    Also, we should be consistent with the variable names we use in templates (i.e we used "method" for "payment_authorized", "payment_completed" and "payment_updated", but we used payment_method_label for the "payment_deleted" template.

    Additionally, I see that we don't include the gateway in the payment deletion template, which... is probably fine though.

    Since the method can be NULL, I guess we need to adapt the templates to add conditions so "using" is only added when the method is known.

  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work almost 2 years ago
  • 🇮🇱Israel jsacksick

    There are confusing comments in logs:

    // Check that log was not added on creation.
     $payment->save();
     $logs = $this->logStorage->loadMultipleByEntity($this->order);
     $this->assertEquals(1, count($logs));

    We're literally checking that a log was created here. Same with the method comment.

  • Status changed to Needs review almost 2 years ago
  • 🇮🇱Israel jsacksick

    Decided to go ahead and commit this! We've been dragging this for too long :).

    Thanks to everyone involved!

  • Status changed to Fixed almost 2 years ago
  • 🇮🇱Israel jsacksick

    I'm wondering if we shouldn't revisit this already before it goes into the release as it doesn't really look good with manual payments;

    This is how it looks for "manual" payments:

    After checkout, when the order is placed:

    When "receiving" the payment:

    Perhaps we need specific templates for manual payments? Notice the extra docs and "capture" which isn't the right term as there isn't an actual capture occurring here...

    Also, wouldn't it make sense to log the order balance too? Perhaps on payment capture I guess?

    Should we open a followup issue instead? Thoughts?

  • Status changed to Needs review almost 2 years ago
  • 🇮🇱Israel jsacksick

    Made some changes to the patch.

    The "payment_added" template wasn't built the same as the others, the attached patch is making it more consistent with the others. Also added a "payment_manual_received" template.

  • 🇮🇱Israel jsacksick

    Minor tweaks, here's an example of the result:

    • jsacksick committed 9d9c5de6 on 8.x-2.x
      Issue #2845321 followup by jsacksick: Improvement the log templates...
  • 🇮🇱Israel jsacksick

    Decided to commit the followup changes as it improves the current templates. The rest can be implemented via followup issues.

  • Status changed to Fixed almost 2 years ago
    • jsacksick committed 10291406 on 3.0.x
      Issue #2845321 followup by jsacksick: Improvement the log templates...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024