Writing a payment method controller

Last updated on
30 April 2025

A payment method controller (also known in the user interface as payment method type) is the logic for processing a payment using a particular payment service. Creating one involves declaring a class that extends from PaymentMethodController and exposing it through hook_payment_method_controller_info(). Example:


/**
 * Implements hook_payment_method_controller_info().
 */
function example_payment_method_controller_info() {
  return array('ExamplePaymentMethodController');
}

/**
 * An example payment method controller.
 */
class ExamplePaymentMethodController extends PaymentMethodController {

  function __construct() {
    $this->title = t('Example');
  }

  /**
   * Implements PaymentMethodController::validate().
   */
  function validate(Payment $payment, PaymentMethod $payment_method, $strict) {
    // Do not process payments of which the description is "foo".
    if ($payment->description == 'foo') {
      throw new PaymentValidationException(t('Example payment method cannot process "foo" payments.'));
    }
  }

  /**
   * Implements PaymentMethodController::execute().
   */
  function execute(Payment $payment) {
    if (ContactRemoteServerToVerifyPaymentAuthorization()) {
      $payment->setStatus(new PaymentStatusItem(PAYMENT_STATUS_SUCCESS));
    }
    else {
      $payment->setStatus(new PaymentStatusItem(PAYMENT_STATUS_FAILED));
    }
  }
}

To add a payment and payment method configuration form to your controller, set the PaymentMethodController::payment_configuration_form_elements_callback and PaymentMethodController::payment_method_configuration_form_elements_callback properties. See the documentation for those properties in the PaymentMethodController class declaration for information on how to implement the callbacks.

If your payment method type has extra configuration, you should use the entity insert/update/load/delete hooks to link the configuration (from a database table, for instance) to payment methods of your type.

Help improve this page

Page status: Not set

You can: