You are here

class TwoCheckout in Ubercart 8.4

Defines the 2Checkout payment method.

Plugin annotation


@UbercartPaymentMethod(
  id = "2checkout",
  name = @Translation("2Checkout"),
  redirect = "\Drupal\uc_2checkout\Form\TwoCheckoutForm",
)

Hierarchy

Expanded class hierarchy of TwoCheckout

File

payment/uc_2checkout/src/Plugin/Ubercart/PaymentMethod/TwoCheckout.php, line 20

Namespace

Drupal\uc_2checkout\Plugin\Ubercart\PaymentMethod
View source
class TwoCheckout extends PaymentMethodPluginBase implements OffsitePaymentMethodPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function getDisplayLabel($label) {
    $build['#attached']['library'][] = 'uc_2checkout/2checkout.styles';
    $build['label'] = [
      '#plain_text' => $label,
      '#suffix' => '<br />',
    ];
    $build['image'] = [
      '#theme' => 'image',
      '#uri' => drupal_get_path('module', 'uc_2checkout') . '/images/2co_logo.jpg',
      '#alt' => $this
        ->t('2Checkout'),
      '#attributes' => [
        'class' => [
          'uc-2checkout-logo',
        ],
      ],
    ];
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'check' => FALSE,
      'checkout_type' => 'dynamic',
      'currency_code' => '',
      'demo' => TRUE,
      'language' => 'en',
      'notification_url' => '',
      'secret_word' => 'tango',
      'sid' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['sid'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Vendor account number'),
      '#description' => $this
        ->t('Your 2Checkout vendor account number.'),
      '#default_value' => $this->configuration['sid'],
      '#size' => 16,
    ];
    $form['secret_word'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Secret word for order verification'),
      '#description' => $this
        ->t('The secret word entered in your 2Checkout account Look and Feel settings.'),
      '#default_value' => $this->configuration['secret_word'],
      '#size' => 16,
    ];
    $form['demo'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable demo mode, allowing you to process fake orders for testing purposes.'),
      '#default_value' => $this->configuration['demo'],
    ];
    $form['language'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Language preference'),
      '#description' => $this
        ->t('Adjust language on 2Checkout pages.'),
      '#options' => [
        'en' => $this
          ->t('English'),
        'sp' => $this
          ->t('Spanish'),
      ],
      '#default_value' => $this->configuration['language'],
    ];
    $form['check'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow customers to choose to pay by credit card or online check.'),
      '#default_value' => $this->configuration['check'],
    ];
    $form['checkout_type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Checkout type'),
      '#options' => [
        'dynamic' => $this
          ->t('Dynamic checkout (user is redirected to 2CO)'),
        'direct' => $this
          ->t('Direct checkout (payment page opens in iframe popup)'),
      ],
      '#default_value' => $this->configuration['checkout_type'],
    ];
    $form['notification_url'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Instant notification settings URL'),
      '#description' => $this
        ->t('Pass this URL to the <a href=":help_url">instant notification settings</a> parameter in your 2Checkout account. This way, any refunds or failed fraud reviews will automatically cancel the Ubercart order.', [
        ':help_url' => Url::fromUri('https://www.2checkout.com/static/va/documentation/INS/index.html')
          ->toString(),
      ]),
      '#default_value' => Url::fromRoute('uc_2checkout.notification', [], [
        'absolute' => TRUE,
      ])
        ->toString(),
      '#attributes' => [
        'readonly' => 'readonly',
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['check'] = $form_state
      ->getValue('check');
    $this->configuration['checkout_type'] = $form_state
      ->getValue('checkout_type');
    $this->configuration['demo'] = $form_state
      ->getValue('demo');
    $this->configuration['language'] = $form_state
      ->getValue('language');
    $this->configuration['notification_url'] = $form_state
      ->getValue('notification_url');
    $this->configuration['secret_word'] = $form_state
      ->getValue('secret_word');
    $this->configuration['sid'] = $form_state
      ->getValue('sid');
  }

  /**
   * {@inheritdoc}
   */
  public function cartDetails(OrderInterface $order, array $form, FormStateInterface $form_state) {
    $build = [];
    $session = \Drupal::service('session');
    if ($this->configuration['check']) {
      $build['pay_method'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Select your payment type:'),
        '#default_value' => $session
          ->get('pay_method') == 'CK' ? 'CK' : 'CC',
        '#options' => [
          'CC' => $this
            ->t('Credit card'),
          'CK' => $this
            ->t('Online check'),
        ],
      ];
      $session
        ->remove('pay_method');
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function cartProcess(OrderInterface $order, array $form, FormStateInterface $form_state) {
    $session = \Drupal::service('session');
    if (NULL != $form_state
      ->getValue([
      'panes',
      'payment',
      'details',
      'pay_method',
    ])) {
      $session
        ->set('pay_method', $form_state
        ->getValue([
        'panes',
        'payment',
        'details',
        'pay_method',
      ]));
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function cartReviewTitle() {
    if ($this->configuration['check']) {
      return $this
        ->t('Credit card/eCheck');
    }
    else {
      return $this
        ->t('Credit card');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildRedirectForm(array $form, FormStateInterface $form_state, OrderInterface $order = NULL) {
    $address = $order
      ->getAddress('billing');
    if ($address
      ->getCountry()) {
      $country = \Drupal::service('country_manager')
        ->getCountry($address
        ->getCountry())
        ->getAlpha3();
    }
    else {
      $country = '';
    }
    $data = [
      'sid' => $this->configuration['sid'],
      'mode' => '2CO',
      'card_holder_name' => mb_substr($address
        ->getFirstName() . ' ' . $address
        ->getLastName(), 0, 128),
      'street_address' => mb_substr($address
        ->getStreet1(), 0, 64),
      'street_address2' => mb_substr($address
        ->getStreet2(), 0, 64),
      'city' => mb_substr($address
        ->getCity(), 0, 64),
      'state' => $address
        ->getZone(),
      'zip' => mb_substr($address
        ->getPostalCode(), 0, 16),
      'country' => $country,
      'email' => mb_substr($order
        ->getEmail(), 0, 64),
      'phone' => mb_substr($address
        ->getPhone(), 0, 16),
      'purchase_step' => 'payment-method',
      'demo' => $this->configuration['demo'] ? 'Y' : 'N',
      'lang' => $this->configuration['language'],
      'merchant_order_id' => $order
        ->id(),
      'pay_method' => 'CC',
      'x_receipt_link_url' => Url::fromRoute('uc_2checkout.complete', [
        'cart_id' => \Drupal::service('uc_cart.manager')
          ->get()
          ->getId(),
      ], [
        'absolute' => TRUE,
      ])
        ->toString(),
      'total' => uc_currency_format($order
        ->getTotal(), FALSE, FALSE, '.'),
      'currency_code' => $order
        ->getCurrency(),
      'cart_order_id' => $order
        ->id(),
    ];
    $i = 0;
    foreach ($order->products as $product) {
      $i++;
      $data['li_' . $i . '_type'] = 'product';
      $data['li_' . $i . '_name'] = $product->title->value;

      // @todo HTML escape and limit to 128 chars.
      $data['li_' . $i . '_quantity'] = $product->qty->value;
      $data['li_' . $i . '_product_id'] = $product->model->value;
      $data['li_' . $i . '_price'] = uc_currency_format($product->price->value, FALSE, FALSE, '.');
    }
    if ('direct' == $this->configuration['checkout_type']) {
      $form['#attached']['library'][] = 'uc_2checkout/2checkout.direct';
    }
    $host = $this->configuration['demo'] ? 'sandbox' : 'www';
    $form['#action'] = "https://{$host}.2checkout.com/checkout/purchase";
    foreach ($data as $name => $value) {
      $form[$name] = [
        '#type' => 'hidden',
        '#value' => $value,
      ];
    }
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit order'),
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::cartReview public function Returns the payment method review details. Overrides PaymentMethodPluginInterface::cartReview 3
PaymentMethodPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PaymentMethodPluginBase::customerView public function Called when an order is being viewed by a customer. Overrides PaymentMethodPluginInterface::customerView 2
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::orderEditDetails public function Called when an order is being edited with this payment method. Overrides PaymentMethodPluginInterface::orderEditDetails 3
PaymentMethodPluginBase::orderEditProcess public function Called when an order is being submitted after being edited. Overrides PaymentMethodPluginInterface::orderEditProcess 1
PaymentMethodPluginBase::orderLoad public function Called when an order is being loaded with this payment method. Overrides PaymentMethodPluginInterface::orderLoad 3
PaymentMethodPluginBase::orderSave public function Called when an order is being saved with this payment method. Overrides PaymentMethodPluginInterface::orderSave 3
PaymentMethodPluginBase::orderSubmit public function Called when an order is being submitted with this payment method. Overrides PaymentMethodPluginInterface::orderSubmit 4
PaymentMethodPluginBase::orderView public function Called when an order is being viewed by an administrator. Overrides PaymentMethodPluginInterface::orderView 6
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.
TwoCheckout::buildConfigurationForm public function Form constructor. Overrides PaymentMethodPluginBase::buildConfigurationForm
TwoCheckout::buildRedirectForm public function Form constructor. Overrides OffsitePaymentMethodPluginInterface::buildRedirectForm
TwoCheckout::cartDetails public function Returns the form or render array to be displayed at checkout. Overrides PaymentMethodPluginBase::cartDetails
TwoCheckout::cartProcess public function Called when checkout is submitted with this payment method selected. Overrides PaymentMethodPluginBase::cartProcess
TwoCheckout::cartReviewTitle public function Returns the payment method title to be used on the checkout review page. Overrides PaymentMethodPluginBase::cartReviewTitle
TwoCheckout::defaultConfiguration public function Gets default configuration for this plugin. Overrides PaymentMethodPluginBase::defaultConfiguration
TwoCheckout::getDisplayLabel public function Returns the payment method label with logo. Overrides PaymentMethodPluginBase::getDisplayLabel
TwoCheckout::submitConfigurationForm public function Form submission handler. Overrides PaymentMethodPluginBase::submitConfigurationForm