Skip to content
Snippets Groups Projects
Commit 9d9c5de6 authored by Jonathan Sacksick's avatar Jonathan Sacksick
Browse files

Issue #2845321 followup by jsacksick: Improvement the log templates introduced...

Issue #2845321 followup by jsacksick: Improvement the log templates introduced and add special cases for manual payments.
parent 6f624491
No related branches found
No related tags found
6 merge requests!379Issue #3491248 Validation is breaking the amount number format decimal point,!375Issue #3413020 by czigor: Using a translatable string as a category for field...,!357Issue #2914933: Add service tags to order-related interfaces,!344Resolve #3107602 "Product attributes do not update visually when switching variations",!343Resolve #3107602 "Product attributes do not update visually when switching variations",!342Issue #3476581 by josephr5000: add OrderItemLabelEvent
......@@ -67,20 +67,24 @@ commerce_order_admin_comment:
payment_added:
category: commerce_payment
label: 'Payment added'
template: '<p>Payment added{% if method %} using {{ method }}{% endif %} with {{ gateway }}: {{ amount|commerce_price_format }}. State: {{ state }}.{% if remote_id %}<br />Transaction ID: {{ remote_id }}{% else %}ID: {{ id }}{% endif %}.</p>'
template: '<p>Payment added via <em>{{ gateway }}</em> for {{ amount|commerce_price_format }}{% if method %} using <em>{{ method }}</em>{% endif %}. State: {{ state }}.{% if remote_id %}<br /> Transaction ID: {{ remote_id }}.{% endif %}</p>'
payment_authorized:
category: commerce_payment
label: 'Payment authorized'
template: '<p>Payment authorized via {{ gateway }} for {{ amount|commerce_price_format }}{% if method %} using {{ method }}{% endif %}.{% if remote_id %}<br />Transaction ID: {{ remote_id }}{% endif %}.</p>'
template: '<p>Payment authorized via <em>{{ gateway }}</em> for {{ amount|commerce_price_format }}{% if method %} using <em>{{ method }}</em>{% endif %}.{% if remote_id %}<br /> Transaction ID: {{ remote_id }}.{% endif %}</p>'
payment_manual_received:
category: commerce_payment
label: 'Manual payment received'
template: '<p>Payment received via <em>{{ gateway }}</em> for {{ amount|commerce_price_format }}.</p>'
payment_completed:
category: commerce_payment
label: 'Payment authorized'
template: '<p>Payment captured via {{ gateway }} for {{ amount|commerce_price_format }}{% if method %} using {{ method }}{% endif %}.{% if remote_id %}<br />Transaction ID: {{ remote_id }}{% endif %}.</p>'
label: 'Payment captured'
template: '<p>Payment captured via <em>{{ gateway }}</em> for {{ amount|commerce_price_format }}{% if method %} using <em>{{ method }}</em>{% endif %}.{% if remote_id %}<br /> Transaction ID: {{ remote_id }}.{% endif %}</p>'
payment_updated:
category: commerce_payment
label: 'Payment updated'
template: '<p>Payment updated. Payment balance: {{ amount|commerce_price_format }}. State: {{ state }}.{% if remote_id %}<br />Transaction ID: {{ remote_id }}{% else %}ID: {{ id }}{% endif %}.</p>'
template: '<p>Payment updated. Payment balance: {{ amount|commerce_price_format }}. State: {{ state }}.{% if remote_id %}<br /> Transaction ID: {{ remote_id }}.{% endif %}</p>'
payment_deleted:
category: commerce_payment
label: 'Payment deleted'
template: '<p>Payment deleted: {{ amount|commerce_price_format }}.{% if method %} [{{ method }}].{% endif %}{% if remote_id %}<br />Transaction ID: {{ remote_id }}{% else %}ID: {{ id }}{% endif %}.</p>'
template: '<p>Payment deleted: {{ amount|commerce_price_format }}.{% if method %} [{{ method }}].{% endif %}{% if remote_id %}<br /> Transaction ID: {{ remote_id }}.{% endif %}</p>'
......@@ -5,6 +5,7 @@ namespace Drupal\commerce_log\EventSubscriber;
use Drupal\commerce_log\LogStorageInterface;
use Drupal\commerce_payment\Event\PaymentEvent;
use Drupal\commerce_payment\Event\PaymentEvents;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\ManualPaymentGatewayInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -53,14 +54,18 @@ class PaymentEventSubscriber implements EventSubscriberInterface {
return;
}
$gateway = $payment->getPaymentGateway();
$state = $payment->getState();
// Set template based on payment state.
$template_id = match ($state->getId()) {
'authorized' => 'payment_authorized',
'completed' => 'payment_completed',
default => 'payment_added',
};
if ($template_id === 'payment_completed' &&
($gateway && $gateway->getPlugin() instanceof ManualPaymentGatewayInterface)) {
$template_id = 'payment_manual_received';
}
$this->logStorage->generate($payment->getOrder(), $template_id, [
'id' => $payment->id(),
......@@ -68,7 +73,7 @@ class PaymentEventSubscriber implements EventSubscriberInterface {
'amount' => $payment->getAmount(),
'state' => $state->getLabel(),
'method' => $payment->getPaymentMethod()?->label(),
'gateway' => $payment->getPaymentGateway()?->label(),
'gateway' => $gateway?->label(),
])->save();
}
......@@ -92,6 +97,7 @@ class PaymentEventSubscriber implements EventSubscriberInterface {
// Gets original state if possible.
$original_state = isset($payment->original) ? $payment->original->getState()
->getId() : '';
$gateway = $payment->getPaymentGateway();
// For changed state to the authorized use another template.
if ($state->getId() === 'authorized' && $original_state !== 'authorized') {
......@@ -101,6 +107,10 @@ class PaymentEventSubscriber implements EventSubscriberInterface {
// For changed state to the completed use another template.
if ($state->getId() === 'completed' && $original_state !== 'completed') {
$template_id = 'payment_completed';
if ($gateway && $gateway->getPlugin() instanceof ManualPaymentGatewayInterface) {
$template_id = 'payment_manual_received';
}
}
$this->logStorage->generate($payment->getOrder(), $template_id, [
......@@ -109,7 +119,7 @@ class PaymentEventSubscriber implements EventSubscriberInterface {
'amount' => $payment->getBalance(),
'state' => $state->getLabel(),
'method' => $payment->getPaymentMethod()?->label(),
'gateway' => $payment->getPaymentGateway()?->label(),
'gateway' => $gateway?->label(),
])->save();
}
......
......@@ -154,7 +154,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[1];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText("Payment added using Visa ending in 1111 with Example: $39.99. State: New.Transaction ID: 123456.");
$this->assertText("Payment added via Example for $39.99 using Visa ending in 1111. State: New. Transaction ID: 123456.");
// Reload the payment.
$this->reloadEntity($payment);
......@@ -167,7 +167,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[2];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment authorized via Example for $39.99 using Visa ending in 1111.Transaction ID: 123456.');
$this->assertText('Payment authorized via Example for $39.99 using Visa ending in 1111. Transaction ID: 123456.');
// Reload the payment.
$this->reloadEntity($payment);
......@@ -180,7 +180,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[3];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment captured via Example for $39.99 using Visa ending in 1111.Transaction ID: 123456.');
$this->assertText('Payment captured via Example for $39.99 using Visa ending in 1111. Transaction ID: 123456.');
// Reload the payment.
$this->reloadEntity($payment);
......@@ -194,7 +194,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[4];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment updated. Payment balance: $29.99. State: Partially refunded.Transaction ID: 123456.');
$this->assertText('Payment updated. Payment balance: $29.99. State: Partially refunded. Transaction ID: 123456.');
// Reload the payment.
$this->reloadEntity($payment);
......@@ -206,7 +206,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[5];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment deleted: $29.99. [Visa ending in 1111].Transaction ID: 123456.');
$this->assertText('Payment deleted: $29.99. [Visa ending in 1111]. Transaction ID: 123456.');
}
/**
......@@ -234,7 +234,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[1];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment authorized via Example for $39.99 using Visa ending in 1111.Transaction ID: 123456.');
$this->assertText('Payment authorized via Example for $39.99 using Visa ending in 1111. Transaction ID: 123456.');
}
/**
......@@ -262,7 +262,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[1];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment captured via Example for $39.99 using Visa ending in 1111.Transaction ID: 123456.');
$this->assertText('Payment captured via Example for $39.99 using Visa ending in 1111. Transaction ID: 123456.');
}
/**
......@@ -288,7 +288,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[1];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment added with Example: $39.99. State: New.Transaction ID: 123456.');
$this->assertText('Payment added via Example for $39.99. State: New. Transaction ID: 123456.');
// Reload the payment.
$this->reloadEntity($payment);
......@@ -301,7 +301,7 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[2];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment captured via Example for $39.99.Transaction ID: 123456.');
$this->assertText('Payment captured via Example for $39.99. Transaction ID: 123456.');
// Reload the payment.
$this->reloadEntity($payment);
......@@ -313,7 +313,53 @@ class PaymentIntegrationTest extends OrderKernelTestBase {
$log = $logs[3];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment deleted: $39.99.Transaction ID: 123456.');
$this->assertText('Payment deleted: $39.99. Transaction ID: 123456.');
}
/**
* Tests manual payments.
*/
public function testManualPayments(): void {
$payment_gateway = PaymentGateway::create([
'id' => 'manual',
'label' => 'Manual',
'plugin' => 'manual',
]);
$payment_gateway->save();
// Create a dummy payment without payment method.
$payment = Payment::create([
'order_id' => $this->order->id(),
'payment_gateway' => 'manual',
'amount' => [
'number' => '39.99',
'currency_code' => 'USD',
],
'state' => 'pending',
'test' => TRUE,
]);
// Check that log was added on creation.
$payment->save();
$logs = $this->logStorage->loadMultipleByEntity($this->order);
$this->assertEquals(1, count($logs));
$log = $logs[1];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment added via Manual for $39.99. State: Pending.');
// Reload the payment.
$this->reloadEntity($payment);
// Check that log was added on update.
$payment->setState('completed');
$payment->save();
$logs = $this->logStorage->loadMultipleByEntity($this->order);
$this->assertEquals(2, count($logs));
$log = $logs[2];
$build = $this->logViewBuilder->view($log);
$this->render($build);
$this->assertText('Payment received via Manual for $39.99.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment