PaymentOptionsBuilder::buildOptions should dispatch an event to customize payment options

Created on 3 February 2020, almost 5 years ago
Updated 7 August 2023, over 1 year ago

This event would allow altering the labels displayed for payment gateways. For instance, customizing the display label.

Payment options are currently built in an unmodifiable way

    // 1) Add options to reuse stored payment methods for known customers.  
          $options[$payment_method_id] = new PaymentOption([
            'id' => $payment_method_id,
            'label' => $payment_method->label(),
            'payment_gateway_id' => $payment_gateway->id(),
            'payment_method_id' => $payment_method_id,
          ]);

    // 2) Add the order's payment method if it was not included above.
        $options[$order_payment_method_id] = new PaymentOption([
          'id' => $order_payment_method_id,
          'label' => $order_payment_method->label(),
          'payment_gateway_id' => $order_payment_method->getPaymentGatewayId(),
          'payment_method_id' => $order_payment_method_id,
        ]);

     // 3) Add options to create new stored payment methods of supported types.
        $option_label = $payment_method_type->getCreateLabel();
        // If there is more than one option for this payment method type,
        // append the payment gateway label to avoid duplicate option labels.
        if ($payment_method_type_counts[$payment_method_type_id] > 1) {
          $option_label = $this->t('@payment_method_label (@payment_gateway_label)', [
            '@payment_method_label' => $payment_method_type->getCreateLabel(),
            '@payment_gateway_label' => $payment_gateway_plugin->getDisplayLabel(),
          ]);
        }
        $options[$option_id] = new PaymentOption([
          'id' => $option_id,
          'label' => $option_label,
          'payment_gateway_id' => $payment_gateway->id(),
          'payment_method_type_id' => $payment_method_type_id,
        ]);

    // 4) Add options for the remaining gateways (off-site, manual, etc).
    foreach ($other_payment_gateways as $payment_gateway) {
      $payment_gateway_id = $payment_gateway->id();
      $options[$payment_gateway_id] = new PaymentOption([
        'id' => $payment_gateway_id,
        'label' => $payment_gateway->getPlugin()->getDisplayLabel(),
        'payment_gateway_id' => $payment_gateway_id,
      ]);
    }

This can lead to needing to override the PaymentInformation pane or verbose form alters.

A main example is the "@payment_method_label (@payment_gateway_label)" display and modifying this behavior.

✨ Feature request
Status

Active

Version

2.0

Component

Payment

Created by

πŸ‡ΊπŸ‡ΈUnited States mglaman WI, USA

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.

  • πŸ‡¦πŸ‡ΉAustria mvonfrie

    Totally agree. I'm currently stuck in (3) always returning the $payment_method_type->createLabel() if there is only one option for a payment method type (in my case of paypal_checkout => "PayPal") and the customer requiring to customize the label (as his customers otherwise won't understand that they have to select that option to pay by credit card or EPS (Austrian online banking) which they can select on the next checkout step.

    At the moment I don't have time to implement the event but will start with a small fix of changing the option label for (3) to use the payment gateway's display label. This way it gets customizable via configuration and if users don't have more than one gateway configured per payment method type this is an improvement already. Furthermore it will be in line with how the label is generated for (4).

  • πŸ‡¦πŸ‡ΉAustria mvonfrie

    Just removed the leading dot of the issue title.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update over 1 year ago
    782 pass, 1 fail
  • @mvonfrie opened merge request.
Production build 0.71.5 2024