You are here

class TestGateway in Ubercart 8.4

Defines the test gateway payment method.

This is a dummy payment gateway to use for testing or as an example. All payments using this test gateway will succeed, except when one of the following is TRUE:

  • Credit card number equals '0000000000000000'. (Note that ANY card number that fails the Luhn algorithm check performed by uc_credit will not even be submitted to this gateway).
  • CVV equals '000'.
  • Credit card is expired.
  • Payment amount equals 12.34 in store currency units.
  • Customer's billing first name equals 'Fictitious'.
  • Customer's billing telephone number equals '8675309'.

Plugin annotation


@UbercartPaymentMethod(
  id = "test_gateway",
  name = @Translation("Test gateway"),
)

Hierarchy

Expanded class hierarchy of TestGateway

File

payment/uc_credit/src/Plugin/Ubercart/PaymentMethod/TestGateway.php, line 29

Namespace

Drupal\uc_credit\Plugin\Ubercart\PaymentMethod
View source
class TestGateway extends CreditCardPaymentMethodBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'debug' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['debug'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Debug'),
      '#description' => $this
        ->t('Log debug payment information to dblog when card is "charged" by this gateway.'),
      '#default_value' => $this->configuration['debug'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['debug'] = $form_state
      ->getValue('debug');
    return parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function chargeCard(OrderInterface $order, $amount, $txn_type, $reference = NULL) {
    $user = \Drupal::currentUser();

    // cc_exp_month and cc_exp_year are also validated by
    // CreditCardPaymentMethodBase::validateExpirationDate().
    $month = $order->payment_details['cc_exp_month'];
    $year = $order->payment_details['cc_exp_year'];
    if ($year < 100) {
      $year = $year + 2000;
    }

    // Card is expired at 0:00 on the first day of the next month.
    $expiration_date = mktime(0, 0, 0, $month + 1, 1, $year);

    // Conditions for failure are described in file documentation block above.
    // All other transactions will succeed.
    if ($order->payment_details['cc_number'] == '0000000000000000') {
      $error = 'Invalid credit card number';
      $success = FALSE;
    }
    elseif (isset($order->payment_details['cc_cvv']) && $order->payment_details['cc_cvv'] == '000') {
      $error = 'Invalid CVV';
      $success = FALSE;
    }
    elseif ($expiration_date - REQUEST_TIME <= 0) {
      $error = 'Card is expired';
      $success = FALSE;
    }
    elseif ($amount == 12.34) {
      $error = 'Invalid payment amount';
      $success = FALSE;
    }
    elseif ($order->billing_first_name == 'Fictitious') {
      $error = 'Invalid customer name';
      $success = FALSE;
    }
    elseif ($order->billing_phone == '8675309') {
      $error = 'Obviously fake phone number';
      $success = FALSE;
    }
    else {
      $success = TRUE;
    }

    // The information for the payment is in the $order->payment_details array.
    if ($this->configuration['debug']) {
      \Drupal::logger('uc_credit')
        ->notice('Test gateway payment details @details.', [
        '@details' => print_r($order->payment_details, TRUE),
      ]);
    }
    if ($success) {
      $message = $this
        ->t('Credit card charged: @amount', [
        '@amount' => uc_currency_format($amount),
      ]);
      $comment = $this
        ->t('Card charged, resolution code: 0022548315');
    }
    else {
      $message = $this
        ->t('Credit card charge failed.');
      $comment = $this
        ->t('Card failed authorization, reason: @error', [
        '@error' => $error,
      ]);
    }
    uc_order_comment_save($order
      ->id(), $user
      ->id(), $message, 'admin');
    return [
      'success' => $success,
      'comment' => $comment,
      'message' => $message,
      'uid' => $user
        ->id(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CreditCardPaymentMethodBase::cartDetails public function Returns the form or render array to be displayed at checkout. Overrides PaymentMethodPluginBase::cartDetails
CreditCardPaymentMethodBase::cartProcess public function Called when checkout is submitted with this payment method selected. Overrides PaymentMethodPluginBase::cartProcess
CreditCardPaymentMethodBase::cartReview public function Returns the payment method review details. Overrides PaymentMethodPluginBase::cartReview
CreditCardPaymentMethodBase::cartReviewTitle public function Returns the payment method title to be used on the checkout review page. Overrides PaymentMethodPluginBase::cartReviewTitle
CreditCardPaymentMethodBase::customerView public function Called when an order is being viewed by a customer. Overrides PaymentMethodPluginBase::customerView
CreditCardPaymentMethodBase::displayCardNumber protected function Returns a credit card number with appropriate masking.
CreditCardPaymentMethodBase::getDisplayLabel public function Returns the payment method label with logo. Overrides PaymentMethodPluginBase::getDisplayLabel
CreditCardPaymentMethodBase::getEnabledFields public function Returns the set of fields which are used by this payment method.
CreditCardPaymentMethodBase::getEnabledTypes public function Returns the set of card types which are used by this payment method.
CreditCardPaymentMethodBase::getTransactionTypes public function Returns the set of transaction types allowed by this payment method. 1
CreditCardPaymentMethodBase::orderEditDetails public function Called when an order is being edited with this payment method. Overrides PaymentMethodPluginBase::orderEditDetails
CreditCardPaymentMethodBase::orderLoad public function Called when an order is being loaded with this payment method. Overrides PaymentMethodPluginBase::orderLoad
CreditCardPaymentMethodBase::orderSave public function Called when an order is being saved with this payment method. Overrides PaymentMethodPluginBase::orderSave
CreditCardPaymentMethodBase::orderSubmit public function Called when an order is being submitted with this payment method. Overrides PaymentMethodPluginBase::orderSubmit
CreditCardPaymentMethodBase::orderView public function Called when an order is being viewed by an administrator. Overrides PaymentMethodPluginBase::orderView
CreditCardPaymentMethodBase::processPayment public function Process a payment through the credit card gateway.
CreditCardPaymentMethodBase::validateCardNumber protected function Validates a credit card number during checkout.
CreditCardPaymentMethodBase::validateCvv protected function Validates a CVV number during checkout.
CreditCardPaymentMethodBase::validateExpirationDate protected function Validates an expiration date on a card.
CreditCardPaymentMethodBase::validateIssueNumber protected function Validates an issue number on a card.
CreditCardPaymentMethodBase::validateStartDate protected function Validates a start date on a card.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PaymentMethodPluginBase::$database protected property The database service.
PaymentMethodPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PaymentMethodPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PaymentMethodPluginBase::orderDelete public function Called when an order is being deleted. Overrides PaymentMethodPluginInterface::orderDelete 1
PaymentMethodPluginBase::orderEditProcess public function Called when an order is being submitted after being edited. Overrides PaymentMethodPluginInterface::orderEditProcess 1
PaymentMethodPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
PaymentMethodPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
PaymentMethodPluginBase::__construct public function Constructs the PaymentMethodPluginBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TestGateway::buildConfigurationForm public function Form constructor. Overrides CreditCardPaymentMethodBase::buildConfigurationForm
TestGateway::chargeCard protected function Called when a credit card should be processed. Overrides CreditCardPaymentMethodBase::chargeCard
TestGateway::defaultConfiguration public function Gets default configuration for this plugin. Overrides CreditCardPaymentMethodBase::defaultConfiguration
TestGateway::submitConfigurationForm public function Form submission handler. Overrides CreditCardPaymentMethodBase::submitConfigurationForm