class StoredOffsiteRedirect in Commerce Core 8.2
Provides an example offsite payment gateway with stored payment methods.
Plugin annotation
@CommercePaymentGateway(
id = "example_stored_offsite_redirect",
label = "Example (Off-site redirect with stored payment methods)",
display_label = "Example Stored Offsite",
forms = {
"offsite-payment" = "Drupal\commerce_payment_example\PluginForm\OffsiteRedirect\PaymentOffsiteForm",
},
payment_method_types = {"credit_card"},
credit_card_types = {
"amex", "dinersclub", "discover", "jcb", "maestro", "mastercard", "visa",
},
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\PaymentGatewayBase implements PaymentGatewayInterface, ContainerFactoryPluginInterface uses PluginWithFormsTrait
- class \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayBase implements OffsitePaymentGatewayInterface
- class \Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway\OffsiteRedirect
- class \Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway\StoredOffsiteRedirect implements SupportsStoredPaymentMethodsInterface
- class \Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway\OffsiteRedirect
- class \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayBase implements OffsitePaymentGatewayInterface
- class \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\PaymentGatewayBase implements PaymentGatewayInterface, ContainerFactoryPluginInterface uses PluginWithFormsTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of StoredOffsiteRedirect
File
- modules/
payment_example/ src/ Plugin/ Commerce/ PaymentGateway/ StoredOffsiteRedirect.php, line 36
Namespace
Drupal\commerce_payment_example\Plugin\Commerce\PaymentGatewayView source
class StoredOffsiteRedirect extends OffsiteRedirect implements SupportsStoredPaymentMethodsInterface {
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new PaymentGatewayBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\commerce_payment\PaymentTypeManager $payment_type_manager
* The payment type manager.
* @param \Drupal\commerce_payment\PaymentMethodTypeManager $payment_method_type_manager
* The payment method type manager.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
* @param \Drupal\commerce_price\MinorUnitsConverterInterface $minor_units_converter
* The minor units converter.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PaymentTypeManager $payment_type_manager, PaymentMethodTypeManager $payment_method_type_manager, TimeInterface $time, MinorUnitsConverterInterface $minor_units_converter, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time, $minor_units_converter);
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('plugin.manager.commerce_payment_type'), $container
->get('plugin.manager.commerce_payment_method_type'), $container
->get('datetime.time'), $container
->get('commerce_price.minor_units_converter'), $container
->get('messenger'));
}
/**
* {@inheritdoc}
*/
public function createPaymentMethod(PaymentMethodInterface $payment_method, Request $request) {
// Off-site gateways often can created stored customer profiles or payment
// references from a processed transaction. A payment gateway would perform
// an API action to generate the payment method reference on the gateway
// here and then save it as a payment method.
$payment_method->card_type = 'visa';
$payment_method->card_number = '1111';
$payment_method->card_exp_month = '12';
$payment_method->card_exp_year = '26';
$expires = CreditCard::calculateExpirationTimestamp($payment_method->card_exp_month->value, $payment_method->card_exp_year->value);
$payment_method
->setExpiresTime($expires);
$payment_method
->setRemoteId('1234');
$payment_method
->save();
}
/**
* {@inheritdoc}
*/
public function deletePaymentMethod(PaymentMethodInterface $payment_method) {
$payment_method
->delete();
}
/**
* {@inheritdoc}
*/
public function createPayment(PaymentInterface $payment, $capture = TRUE) {
$this
->assertPaymentState($payment, [
'new',
]);
$payment_method = $payment
->getPaymentMethod();
$this
->assertPaymentMethod($payment_method);
// Perform the create payment request here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
// Remember to take into account $capture when performing the request.
$amount = $payment
->getAmount();
$payment_method_token = $payment_method
->getRemoteId();
// The remote ID returned by the request.
$remote_id = '123456';
$next_state = $capture ? 'completed' : 'authorization';
$payment
->setState($next_state);
$payment
->setRemoteId($remote_id);
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function onReturn(OrderInterface $order, Request $request) {
// This off-site payment gateway example creates a payment method as a
// part of processing the payment data returned from the gateway.
$payment_method_storage = $this->entityTypeManager
->getStorage('commerce_payment_method');
assert($payment_method_storage instanceof PaymentMethodStorageInterface);
$payment_method = $payment_method_storage
->createForCustomer('credit_card', $this->parentEntity
->id(), $order
->getCustomerId(), $order
->getBillingProfile());
// The payment method is created first so that it can be attached to the
// generated payment transaction.
$this
->createPaymentMethod($payment_method, $request);
$payment_storage = $this->entityTypeManager
->getStorage('commerce_payment');
$payment = $payment_storage
->create([
'state' => 'completed',
'amount' => $order
->getBalance(),
'payment_gateway' => $this->parentEntity
->id(),
'order_id' => $order
->id(),
'remote_id' => $request->query
->get('txn_id'),
'remote_state' => $request->query
->get('payment_status'),
'payment_method' => $payment_method,
'avs_response_code' => 'Z',
]);
if (!$payment_method->card_type
->isEmpty()) {
$avs_response_code_label = $this
->buildAvsResponseCodeLabel('Z', $payment_method->card_type->value);
$payment
->setAvsResponseCodeLabel($avs_response_code_label);
}
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function buildAvsResponseCodeLabel($avs_response_code, $card_type) {
if ($card_type == 'dinersclub' || $card_type == 'jcb') {
if ($avs_response_code == 'Z') {
return $this
->t('Zip code.');
}
return NULL;
}
return parent::buildAvsResponseCodeLabel($avs_response_code, $card_type);
}
}
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. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OffsitePaymentGatewayBase:: |
public | function |
Gets the URL to the "notify" page. Overrides OffsitePaymentGatewayInterface:: |
|
OffsitePaymentGatewayBase:: |
public | function |
Processes the "cancel" request. Overrides OffsitePaymentGatewayInterface:: |
|
OffsitePaymentGatewayBase:: |
public | function |
Processes the notification request. Overrides SupportsNotificationsInterface:: |
|
OffsiteRedirect:: |
public | function |
Form constructor. Overrides PaymentGatewayBase:: |
|
OffsiteRedirect:: |
public | function |
Gets default configuration for this plugin. Overrides PaymentGatewayBase:: |
|
OffsiteRedirect:: |
public | function |
Form submission handler. Overrides PaymentGatewayBase:: |
|
PaymentGatewayBase:: |
protected | property | The ID of the parent config entity. | |
PaymentGatewayBase:: |
protected | property | The entity type manager. | |
PaymentGatewayBase:: |
protected | property | The minor units converter. | |
PaymentGatewayBase:: |
protected | property | The parent config entity. | |
PaymentGatewayBase:: |
protected | property | The payment method types handled by the gateway. | |
PaymentGatewayBase:: |
protected | property | The payment type used by the gateway. | |
PaymentGatewayBase:: |
protected | property | The time. | |
PaymentGatewayBase:: |
protected | function | Asserts that the payment method is neither empty nor expired. | |
PaymentGatewayBase:: |
protected | function | Asserts that the payment state matches one of the allowed states. | |
PaymentGatewayBase:: |
protected | function | Asserts that the refund amount is valid. | |
PaymentGatewayBase:: |
public | function |
Builds the available operations for the given payment. Overrides PaymentGatewayInterface:: |
1 |
PaymentGatewayBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
PaymentGatewayBase:: |
public | function | ||
PaymentGatewayBase:: |
public | function | ||
PaymentGatewayBase:: |
public | function | ||
PaymentGatewayBase:: |
public | function |
Gets whether the payment gateway collects billing information. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets the credit card types handled by the gateway. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
protected | function | Gets the default payment gateway forms. | 1 |
PaymentGatewayBase:: |
public | function |
Gets the default payment method type. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets the payment gateway display label. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets the JS library ID. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets the payment gateway label. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets the mode in which the payment gateway is operating. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets the payment method types handled by the payment gateway. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Gets the payment type used by the payment gateway. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
protected | function | Gets the remote customer ID for the given user. | |
PaymentGatewayBase:: |
public | function |
Gets the supported modes. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PaymentGatewayBase:: |
protected | function | Sets the remote customer ID for the given user. | |
PaymentGatewayBase:: |
public | function |
Converts the given amount to its minor units. Overrides PaymentGatewayInterface:: |
|
PaymentGatewayBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
PaymentGatewayBase:: |
public | function |
Overrides DependencySerializationTrait:: |
|
PaymentGatewayBase:: |
public | function |
Overrides DependencySerializationTrait:: |
|
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. | |
PluginWithFormsTrait:: |
public | function | ||
PluginWithFormsTrait:: |
public | function | ||
StoredOffsiteRedirect:: |
protected | property |
The messenger. Overrides MessengerTrait:: |
|
StoredOffsiteRedirect:: |
public | function |
Builds a label for the given AVS response code and card type. Overrides PaymentGatewayBase:: |
|
StoredOffsiteRedirect:: |
public static | function |
Creates an instance of the plugin. Overrides PaymentGatewayBase:: |
|
StoredOffsiteRedirect:: |
public | function |
Creates a payment. Overrides SupportsStoredPaymentMethodsInterface:: |
|
StoredOffsiteRedirect:: |
public | function | ||
StoredOffsiteRedirect:: |
public | function |
Deletes the given payment method. Overrides SupportsStoredPaymentMethodsInterface:: |
|
StoredOffsiteRedirect:: |
public | function |
Processes the "return" request. Overrides OffsiteRedirect:: |
|
StoredOffsiteRedirect:: |
public | function |
Constructs a new PaymentGatewayBase object. Overrides PaymentGatewayBase:: |
|
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. |