1

I have followed the documentation at https://docs.drupalcommerce.org/commerce2/developer-guide/payments/create-payment-gateway/getting-started#step-3-create-a-payment-gateway-plugin.

My CommercePaymentGateway shows up and is selectable when I get to the checkout order information step (checkout/2/order_information). However, I would like to add some fields to this to capture extra information.

From what I have seen it looks like I need add-payment-method for the form key in the annotation and I need to extend the PaymentMethodAddForm class to use the buildConfigurationForm method.

I can't get custom form elements to show on the order information screen when using my custom CommercePaymentGateway.

If I move to OnsitePaymentGatewayBase (as suggested by @zaporylie) my form element does show. However, I don't want all of the other fields(CC info and billing info) that come with it.

These are the files I have.

ACHPayment.php

<?php

namespace Drupal\ach_payment\Plugin\Commerce\PaymentGateway;

use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\PaymentGatewayBase;

/**
 * @CommercePaymentGateway(
 *   id = "ach_payment",
 *   label = @Translation("ACH"),
 *   display_label = @Translation("ACH"),
 *   forms = {
 *     "add-payment-method" = "Drupal\ach_payment\PluginForm\ACHPaymentMethodAddForm",
 *   },
 * )
 */
class ACHPayment extends PaymentGatewayBase {
}

ACHPaymentMethodAddForm.php

<?php

namespace Drupal\ach_payment\PluginForm;

use Drupal\commerce_payment\PluginForm\PaymentMethodAddForm;
use Drupal\Core\Form\FormStateInterface;

class ACHPaymentMethodAddForm extends PaymentMethodAddForm {

  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    $form['test_ach'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Test ACH'),
    ];

    return $form;
  }

  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    // TODO: Implement submitConfigurationForm() method.
  }

}

closed as unclear what you're asking by kiamlaluno Aug 27 at 10:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Are you trying to create OnSite or OffSite payment gateway? You should extend respective base class and only OnSite allows for stored payment methods as of now - ticket to change improve it is here: drupal.org/project/commerce/issues/2838380 – zaporylie Aug 21 at 15:58
  • Onsite expects to store things like CC information. I don't need to process a CC. I need to have a gateway that "allows" to pay by check. So I've created an ACH. The idea is, during the order_information page the user will say how they want to be contacted (phone or email). They will submit their order it will be authorized but not captured. – FatGuyLaughing Aug 22 at 13:44
  • It should extend either OnsitePaymentGatewayBase or OffsitePaymentGatewayBase. – Jdrupal Aug 23 at 15:18
  • Sorry @Jdrupal I couldn't update the issue quick enough :) If you look at the edit section in the new body text of the ticket you'll see the issue I have with that. – FatGuyLaughing Aug 23 at 15:24
  • Does it work if you extend OnsitePaymentGatewayBase? – Jdrupal Aug 23 at 15:28
0

The problem is that you don’t extend the OnsitePaymentGatewayBase or the OffsitePaymentGatewayBase class.

Make your class extends one of those classes ( in your case the OnsitePaymentGatewayBase class). Then you can remove the unnecessary fields using hook_form_alter.

Should be something like this:

function MY_MODULE_form_alter(array $form, $form_state, $form_id) {
  if ($form_id == 'some_form_id') {
    $form['cc_field']['#access'] = FALSE;
  }
}

You can find the form ID by printing the $form_id variable on the page that renders the form.

Not the answer you're looking for? Browse other questions tagged or ask your own question.