Extracted from #2828525: Implement manual payment gateways.

PaymentMethod has the following base field definition:

    $fields['expires'] = BaseFieldDefinition::create('timestamp')
      ->setLabel(t('Expires'))
      ->setDescription(t('The time when the payment method expires. 0 for never.'))

But PaymentMethodStorage::loadReusable() always does:

  ->condition('expires', $this->time->getRequestTime(), '>')

making it impossible to load non-expiring payment methods.

commerce-payment-method.html.twig also assumes that the expires timestamp is never empty.

CommentFileSizeAuthor
#3 2882136-3.patch3.65 KBbojanz

Comments

bojanz created an issue. See original summary.

bojanz’s picture

Here are the changes that @vasike made in the original issue:

diff --git a/modules/payment/commerce_payment.module b/modules/payment/commerce_payment.module
index e1db90fe3..09c5dc0f6 100755
--- a/modules/payment/commerce_payment.module
+++ b/modules/payment/commerce_payment.module
@@ -93,6 +93,8 @@ function template_preprocess_commerce_payment_method(array &$variables) {
       '#markup' => $payment_method->label(),
     ],
   ];
+  $expires = $payment_method->getExpiresTime();
+  $variables['payment_method_expires'] = $expires ? date('n/Y', $expires) : t('Never');
   foreach (Element::children($variables['elements']) as $key) {
     $variables['payment_method'][$key] = $variables['elements'][$key];
   }

diff --git a/modules/payment/templates/commerce-payment-method.html.twig b/modules/payment/templates/commerce-payment-method.html.twig
index 43283beba..d165b4945 100644
--- a/modules/payment/templates/commerce-payment-method.html.twig
+++ b/modules/payment/templates/commerce-payment-method.html.twig
@@ -24,7 +24,7 @@
     {{ payment_method.label }}
   </div>
   <div class="field field--name-expires">
-    {{ 'Expires'|t }} {{ payment_method_entity.expiresTime|format_date('custom', 'n/Y') }}
+    {{ 'Expires'|t }} {{ payment_method_expires }}
   </div>
   {{ payment_method.billing_profile }}
 </article>

diff --git a/modules/payment/src/PaymentMethodStorage.php b/modules/payment/src/PaymentMethodStorage.php
index 76b6fc4c7..ec300d5eb 100644
--- a/modules/payment/src/PaymentMethodStorage.php
+++ b/modules/payment/src/PaymentMethodStorage.php
@@ -82,8 +82,11 @@ public function loadReusable(UserInterface $account, PaymentGatewayInterface $pa
     $query = $this->getQuery()
       ->condition('uid', $account->id())
       ->condition('payment_gateway', $payment_gateway->id())
-      ->condition('reusable', TRUE)
+      ->condition('reusable', TRUE);
+    $group = $query->orConditionGroup()
       ->condition('expires', $this->time->getRequestTime(), '>')
+      ->condition('expires', 0);
+    $query->condition($group)
       ->sort('created', 'DESC');
     $result = $query->execute();
     if (empty($result)) {

diff --git a/modules/payment_example/src/Plugin/Commerce/PaymentGateway/Onsite.php b/modules/payment_example/src/Plugin/Commerce/PaymentGateway/Onsite.php
index 45fe77bd9..df241fd4e 100644
--- a/modules/payment_example/src/Plugin/Commerce/PaymentGateway/Onsite.php
+++ b/modules/payment_example/src/Plugin/Commerce/PaymentGateway/Onsite.php
@@ -92,7 +92,7 @@ public function createPayment(PaymentInterface $payment, $capture = TRUE) {
     if (empty($payment_method)) {
       throw new \InvalidArgumentException('The provided payment has no payment method referenced.');
     }
-    if (\Drupal::time()->getRequestTime() >= $payment_method->getExpiresTime()) {
+    if ($payment_method->isExpired()) {
       throw new HardDeclineException('The provided payment method has expired');
     }
 
bojanz’s picture

Status: Active » Needs review
StatusFileSize
new3.65 KB

Here's a rerolled patch, let's see what the existing tests say.
We need to add test coverage, for sure.

bojanz credited facine.

bojanz’s picture

Adding facine, who started attacking this problem in another issue.

bojanz credited vasike.

bojanz’s picture

  • bojanz committed 878b37c on 8.x-2.x authored by vasike
    Issue #2882136 by bojanz, facine, vasike: Improve support for non-...
bojanz’s picture

Status: Needs review » Fixed

Tweaked the patch so that we don't show the "Expires" line for non-expiring methods, looks more natural than "Expires Never". Expanded the kernel tests.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.