class Onsite in Commerce Core 8.2
Provides the On-site payment gateway.
Plugin annotation
@CommercePaymentGateway(
id = "example_onsite",
label = "Example (On-site)",
display_label = "Example",
forms = {
"add-payment-method" = "Drupal\commerce_payment_example\PluginForm\Onsite\PaymentMethodAddForm",
"edit-payment-method" = "Drupal\commerce_payment\PluginForm\PaymentMethodEditForm",
},
payment_method_types = {"credit_card"},
credit_card_types = {
"amex", "dinersclub", "discover", "jcb", "maestro", "mastercard", "visa",
},
requires_billing_information = FALSE,
)
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\OnsitePaymentGatewayBase implements OnsitePaymentGatewayInterface
- class \Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway\Onsite implements OnsiteInterface
- class \Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayBase implements OnsitePaymentGatewayInterface
- 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 Onsite
1 file declares its use of Onsite
- TestOnsite.php in modules/
payment/ tests/ modules/ commerce_payment_test/ src/ Plugin/ Commerce/ PaymentGateway/ TestOnsite.php
File
- modules/
payment_example/ src/ Plugin/ Commerce/ PaymentGateway/ Onsite.php, line 36
Namespace
Drupal\commerce_payment_example\Plugin\Commerce\PaymentGatewayView source
class Onsite extends OnsitePaymentGatewayBase implements OnsiteInterface {
/**
* {@inheritdoc}
*/
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) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time, $minor_units_converter);
// You can create an instance of the SDK here and assign it to $this->api.
// Or inject Guzzle when there's no suitable SDK.
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'api_key' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// Example credential. Also needs matching schema in
// config/schema/$your_module.schema.yml.
$form['api_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('API key'),
'#default_value' => $this->configuration['api_key'],
'#required' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['api_key'] = $values['api_key'];
}
}
/**
* {@inheritdoc}
*/
public function createPayment(PaymentInterface $payment, $capture = TRUE) {
$this
->assertPaymentState($payment, [
'new',
]);
$payment_method = $payment
->getPaymentMethod();
$this
->assertPaymentMethod($payment_method);
// Add a built in test for testing decline exceptions.
// Note: Since requires_billing_information is FALSE, the payment method
// is not guaranteed to have a billing profile. Confirm tha
// $payment_method->getBillingProfile() is not NULL before trying to use it.
if ($billing_profile = $payment_method
->getBillingProfile()) {
/** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $billing_address */
$billing_address = $billing_profile
->get('address')
->first();
if ($billing_address
->getPostalCode() == '53140') {
throw new HardDeclineException('The payment was declined');
}
}
// 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
->setAvsResponseCode('A');
if (!$payment_method->card_type
->isEmpty()) {
$avs_response_code_label = $this
->buildAvsResponseCodeLabel('A', $payment_method->card_type->value);
$payment
->setAvsResponseCodeLabel($avs_response_code_label);
}
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function capturePayment(PaymentInterface $payment, Price $amount = NULL) {
$this
->assertPaymentState($payment, [
'authorization',
]);
// If not specified, capture the entire amount.
$amount = $amount ?: $payment
->getAmount();
// Perform the capture request here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
$remote_id = $payment
->getRemoteId();
$number = $amount
->getNumber();
$payment
->setState('completed');
$payment
->setAmount($amount);
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function voidPayment(PaymentInterface $payment) {
$this
->assertPaymentState($payment, [
'authorization',
]);
// Perform the void request here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
$remote_id = $payment
->getRemoteId();
$payment
->setState('authorization_voided');
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function refundPayment(PaymentInterface $payment, Price $amount = NULL) {
$this
->assertPaymentState($payment, [
'completed',
'partially_refunded',
]);
// If not specified, refund the entire amount.
$amount = $amount ?: $payment
->getAmount();
$this
->assertRefundAmount($payment, $amount);
// Perform the refund request here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
$remote_id = $payment
->getRemoteId();
$number = $amount
->getNumber();
$old_refunded_amount = $payment
->getRefundedAmount();
$new_refunded_amount = $old_refunded_amount
->add($amount);
if ($new_refunded_amount
->lessThan($payment
->getAmount())) {
$payment
->setState('partially_refunded');
}
else {
$payment
->setState('refunded');
}
$payment
->setRefundedAmount($new_refunded_amount);
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$required_keys = [
// The expected keys are payment gateway specific and usually match
// the PaymentMethodAddForm form elements. They are expected to be valid.
'type',
'number',
'expiration',
];
foreach ($required_keys as $required_key) {
if (empty($payment_details[$required_key])) {
throw new \InvalidArgumentException(sprintf('$payment_details must contain the %s key.', $required_key));
}
}
// Add a built in test for testing decline exceptions.
// Note: Since requires_billing_information is FALSE, the payment method
// is not guaranteed to have a billing profile. Confirm tha
// $payment_method->getBillingProfile() is not NULL before trying to use it.
if ($billing_profile = $payment_method
->getBillingProfile()) {
/** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $billing_address */
$billing_address = $billing_profile
->get('address')
->first();
if ($billing_address
->getPostalCode() == '53141') {
throw new HardDeclineException('The payment method was declined');
}
}
// If the remote API needs a remote customer to be created.
$owner = $payment_method
->getOwner();
if ($owner && $owner
->isAuthenticated()) {
$customer_id = $this
->getRemoteCustomerId($owner);
// If $customer_id is empty, create the customer remotely and then do
// $this->setRemoteCustomerId($owner, $customer_id);
// $owner->save();
}
// Perform the create request here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
// You might need to do different API requests based on whether the
// payment method is reusable: $payment_method->isReusable().
// Non-reusable payment methods usually have an expiration timestamp.
$payment_method->card_type = $payment_details['type'];
// Only the last 4 numbers are safe to store.
$payment_method->card_number = substr($payment_details['number'], -4);
$payment_method->card_exp_month = $payment_details['expiration']['month'];
$payment_method->card_exp_year = $payment_details['expiration']['year'];
$expires = CreditCard::calculateExpirationTimestamp($payment_details['expiration']['month'], $payment_details['expiration']['year']);
// The remote ID returned by the request.
$remote_id = '789';
$payment_method
->setRemoteId($remote_id);
$payment_method
->setExpiresTime($expires);
$payment_method
->save();
}
/**
* {@inheritdoc}
*/
public function deletePaymentMethod(PaymentMethodInterface $payment_method) {
// Delete the remote record here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
// Delete the local entity.
$payment_method
->delete();
}
/**
* {@inheritdoc}
*/
public function updatePaymentMethod(PaymentMethodInterface $payment_method) {
// Note: Since requires_billing_information is FALSE, the payment method
// is not guaranteed to have a billing profile. Confirm that
// $payment_method->getBillingProfile() is not NULL before trying to use it.
//
// Perform the update request here, throw an exception if it fails.
// See \Drupal\commerce_payment\Exception for the available exceptions.
}
/**
* {@inheritdoc}
*/
public function buildAvsResponseCodeLabel($avs_response_code, $card_type) {
if ($card_type == 'dinersclub' || $card_type == 'jcb') {
if ($avs_response_code == 'A') {
return $this
->t('Approved.');
}
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:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Onsite:: |
public | function |
Builds a label for the given AVS response code and card type. Overrides PaymentGatewayBase:: |
|
Onsite:: |
public | function |
Form constructor. Overrides OnsitePaymentGatewayBase:: |
|
Onsite:: |
public | function |
Captures the given authorized payment. Overrides SupportsAuthorizationsInterface:: |
|
Onsite:: |
public | function |
Creates a payment. Overrides SupportsStoredPaymentMethodsInterface:: |
|
Onsite:: |
public | function |
Creates a payment method with the given payment details. Overrides SupportsCreatingPaymentMethodsInterface:: |
|
Onsite:: |
public | function |
Gets default configuration for this plugin. Overrides PaymentGatewayBase:: |
|
Onsite:: |
public | function |
Deletes the given payment method. Overrides SupportsStoredPaymentMethodsInterface:: |
|
Onsite:: |
public | function |
Refunds the given payment. Overrides SupportsRefundsInterface:: |
|
Onsite:: |
public | function |
Form submission handler. Overrides PaymentGatewayBase:: |
|
Onsite:: |
public | function |
Updates the given payment method. Overrides SupportsUpdatingStoredPaymentMethodsInterface:: |
|
Onsite:: |
public | function |
Voids the given payment. Overrides SupportsVoidsInterface:: |
|
Onsite:: |
public | function |
Constructs a new PaymentGatewayBase object. Overrides PaymentGatewayBase:: |
|
OnsitePaymentGatewayBase:: |
protected | function |
Gets the default payment gateway forms. 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 static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
2 |
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:: |
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 | ||
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. |