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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_payment\PaymentMethodPluginBase implements ContainerFactoryPluginInterface, PaymentMethodPluginInterface
- class \Drupal\uc_2checkout\Plugin\Ubercart\PaymentMethod\TwoCheckout implements OffsitePaymentMethodPluginInterface
- class \Drupal\uc_payment\PaymentMethodPluginBase implements ContainerFactoryPluginInterface, PaymentMethodPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TwoCheckout
File
- payment/
uc_2checkout/ src/ Plugin/ Ubercart/ PaymentMethod/ TwoCheckout.php, line 20
Namespace
Drupal\uc_2checkout\Plugin\Ubercart\PaymentMethodView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PaymentMethodPluginBase:: |
protected | property | The database service. | |
PaymentMethodPluginBase:: |
public | function |
Returns the payment method review details. Overrides PaymentMethodPluginInterface:: |
3 |
PaymentMethodPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Called when an order is being viewed by a customer. Overrides PaymentMethodPluginInterface:: |
2 |
PaymentMethodPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Called when an order is being deleted. Overrides PaymentMethodPluginInterface:: |
1 |
PaymentMethodPluginBase:: |
public | function |
Called when an order is being edited with this payment method. Overrides PaymentMethodPluginInterface:: |
3 |
PaymentMethodPluginBase:: |
public | function |
Called when an order is being submitted after being edited. Overrides PaymentMethodPluginInterface:: |
1 |
PaymentMethodPluginBase:: |
public | function |
Called when an order is being loaded with this payment method. Overrides PaymentMethodPluginInterface:: |
3 |
PaymentMethodPluginBase:: |
public | function |
Called when an order is being saved with this payment method. Overrides PaymentMethodPluginInterface:: |
3 |
PaymentMethodPluginBase:: |
public | function |
Called when an order is being submitted with this payment method. Overrides PaymentMethodPluginInterface:: |
4 |
PaymentMethodPluginBase:: |
public | function |
Called when an order is being viewed by an administrator. Overrides PaymentMethodPluginInterface:: |
6 |
PaymentMethodPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
PaymentMethodPluginBase:: |
public | function |
Constructs the PaymentMethodPluginBase object. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TwoCheckout:: |
public | function |
Form constructor. Overrides PaymentMethodPluginBase:: |
|
TwoCheckout:: |
public | function |
Form constructor. Overrides OffsitePaymentMethodPluginInterface:: |
|
TwoCheckout:: |
public | function |
Returns the form or render array to be displayed at checkout. Overrides PaymentMethodPluginBase:: |
|
TwoCheckout:: |
public | function |
Called when checkout is submitted with this payment method selected. Overrides PaymentMethodPluginBase:: |
|
TwoCheckout:: |
public | function |
Returns the payment method title to be used on the checkout review page. Overrides PaymentMethodPluginBase:: |
|
TwoCheckout:: |
public | function |
Gets default configuration for this plugin. Overrides PaymentMethodPluginBase:: |
|
TwoCheckout:: |
public | function |
Returns the payment method label with logo. Overrides PaymentMethodPluginBase:: |
|
TwoCheckout:: |
public | function |
Form submission handler. Overrides PaymentMethodPluginBase:: |