class PurchaseOrderGateway in Commerce Purchase Order 8
Provides the On-site payment gateway.
Plugin annotation
@CommercePaymentGateway(
id = "purchase_order_gateway",
label = "Purchase Orders",
display_label = "Purchase Orders",
forms = {
"receive-payment" =
"Drupal\commerce_payment\PluginForm\PaymentReceiveForm",
"add-payment-method" =
"Drupal\commerce_purchase_order\PluginForm\PurchaseOrder\PaymentMethodAddForm",
},
payment_method_types = {"purchase_order"},
payment_type = "payment_purchase_order"
)
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_purchase_order\Plugin\Commerce\PaymentGateway\PurchaseOrderGateway implements HasPaymentInstructionsInterface, OnsitePaymentGatewayInterface, SupportsRefundsInterface, SupportsVoidsInterface, ContainerFactoryPluginInterface
- 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 PurchaseOrderGateway
File
- src/
Plugin/ Commerce/ PaymentGateway/ PurchaseOrderGateway.php, line 34
Namespace
Drupal\commerce_purchase_order\Plugin\Commerce\PaymentGatewayView source
class PurchaseOrderGateway extends PaymentGatewayBase implements OnsitePaymentGatewayInterface, HasPaymentInstructionsInterface, SupportsVoidsInterface, SupportsRefundsInterface, ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public function getCreditCardTypes() {
return [];
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'instructions' => [
'value' => '',
'format' => 'plain_text',
],
'limit_open' => 1.0,
'user_approval' => TRUE,
'payment_method_types' => [
'purchase_order',
],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['limit_open'] = [
'#type' => 'number',
'#title' => $this
->t('Limit maximum open purchase orders'),
'#default_value' => $this->configuration['limit_open'],
'#description' => $this
->t('During the authorization state at checkout, the transaction is denied if the number of unpaid payments exceeds this number.'),
'#min' => 1,
'#step' => 1.0,
'#size' => 3,
];
$form['user_approval'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Purchase order users require approval in the user account settings.'),
'#default_value' => $this->configuration['user_approval'],
];
$form['instructions'] = [
'#type' => 'text_format',
'#title' => $this
->t('Payment instructions'),
'#description' => $this
->t('Shown the end of checkout, after the customer has placed their order.'),
'#default_value' => $this->configuration['instructions']['value'],
'#format' => $this->configuration['instructions']['format'],
];
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['instructions'] = $values['instructions'];
$this->configuration['limit_open'] = (int) $values['limit_open'];
$this->configuration['user_approval'] = (bool) $values['user_approval'];
}
}
/**
* {@inheritdoc}
*/
public function buildPaymentOperations(PaymentInterface $payment) {
$payment_state = $payment
->getState()->value;
$operations = [];
$operations['receive'] = [
'title' => $this
->t('Receive'),
'page_title' => $this
->t('Receive payment'),
'plugin_form' => 'receive-payment',
'weight' => -99,
'access' => $payment_state == 'completed',
];
$operations['void'] = [
'title' => $this
->t('Void'),
'page_title' => $this
->t('Void payment'),
'plugin_form' => 'void-payment',
'access' => $payment_state == 'completed',
'weight' => 90,
];
$operations['refund'] = [
'title' => $this
->t('Refund'),
'page_title' => $this
->t('Refund payment'),
'plugin_form' => 'refund-payment',
'access' => in_array($payment_state, [
'completed',
'partially_refunded',
]),
];
return $operations;
}
/**
* {@inheritdoc}
*/
public function buildPaymentInstructions(PaymentInterface $payment) {
$instructions = [];
if (!empty($this->configuration['instructions']['value'])) {
$instructions = [
'#type' => 'processed_text',
'#text' => $this->configuration['instructions']['value'],
'#format' => $this->configuration['instructions']['format'],
];
}
return $instructions;
}
/**
* {@inheritdoc}
*/
public function createPayment(PaymentInterface $payment, $capture = TRUE) {
$this
->assertPaymentState($payment, [
'new',
]);
$this
->authorizePayment($payment);
$this
->assertAuthorized($payment);
$payment_method = $payment
->getPaymentMethod();
$this
->assertPaymentMethod($payment_method);
$payment
->setState('completed');
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function receivePayment(PaymentInterface $payment, Price $amount = NULL) {
$this
->assertPaymentState($payment, [
'completed',
]);
// If not specified, use the entire amount.
$amount = $amount ?: $payment
->getAmount();
$payment->state = 'paid';
$payment
->setAmount($amount);
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function voidPayment(PaymentInterface $payment) {
$this
->assertPaymentState($payment, [
'completed',
]);
$payment->state = 'voided';
$payment
->save();
}
/**
* {@inheritdoc}
*/
public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$required_keys = [
'number',
];
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));
}
}
// Not re-usable because we will store the PO number in the method.
$payment_method
->setReusable(FALSE);
$payment_method->po_number = $payment_details['number'];
$payment_method
->setExpiresTime(strtotime("+60 day"));
$payment_method
->save();
}
/**
* {@inheritdoc}
*/
public function deletePaymentMethod(PaymentMethodInterface $payment_method) {
// There is no remote system. These are only stored locally.
$payment_method
->delete();
}
/**
* {@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);
$old_refunded_amount = $payment
->getRefundedAmount();
$new_refunded_amount = $old_refunded_amount
->add($amount);
if ($new_refunded_amount
->lessThan($payment
->getAmount())) {
$payment->state = 'partially_refunded';
}
else {
$payment->state = 'refunded';
}
$payment
->setRefundedAmount($new_refunded_amount);
$payment
->save();
}
/**
* Authorizes payment based on settings in the gateway configuration.
*
* @param \Drupal\commerce_payment\Entity\PaymentInterface $payment
* The payment to authorize.
*/
protected function authorizePayment(PaymentInterface $payment) {
$customer = $payment
->getOrder()
->getCustomer();
if ($this->configuration['user_approval']) {
$user_approved = $customer
->hasField('field_purchase_orders_authorized') && !$customer
->get('field_purchase_orders_authorized')
->isEmpty() && $customer
->get('field_purchase_orders_authorized')
->first()->value;
}
else {
// There is no user approval.
$user_approved = TRUE;
}
if (!$user_approved) {
$this
->messenger()
->addWarning($this
->t('Please contact us about using purchase orders for checkout.'));
}
$user_po_methods = $this->entityTypeManager
->getStorage('commerce_payment_method')
->loadByProperties([
'uid' => $customer
->id(),
'type' => 'purchase_order',
]);
if (!empty($user_po_methods)) {
$user_po_method_ids = [];
foreach ($user_po_methods as $method) {
$user_po_method_ids[] = $method
->id();
}
$payment_query = $this->entityTypeManager
->getStorage('commerce_payment')
->getQuery();
$payment_query
->condition('payment_method', $user_po_method_ids, 'IN')
->condition('state', 'completed')
->count();
$open_po_count = $payment_query
->execute();
}
else {
$open_po_count = 0;
}
if ($user_approved && $open_po_count < $this->configuration['limit_open']) {
$payment
->setState('authorized');
$payment
->setAuthorizedTime($this->time
->getRequestTime());
}
}
/**
* Asserts that the payment successfully authorized.
*
* @param \Drupal\commerce_payment\Entity\PaymentInterface $payment
* The payment.
*
* @throws \Drupal\commerce_payment\Exception\HardDeclineException
* Thrown when the payment method did not authorize.
*/
protected function assertAuthorized(PaymentInterface $payment) {
if ($payment
->getState()->value != 'authorized') {
throw new HardDeclineException('The purchase order failed to authorized. Please contact a site administrator.');
}
}
}
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. | |
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 a label for the given AVS response code and card type. Overrides PaymentGatewayInterface:: |
2 |
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:: |
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 |
Constructs a new PaymentGatewayBase object. Overrides PluginBase:: |
3 |
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 | ||
PurchaseOrderGateway:: |
protected | function | Asserts that the payment successfully authorized. | |
PurchaseOrderGateway:: |
protected | function | Authorizes payment based on settings in the gateway configuration. | |
PurchaseOrderGateway:: |
public | function |
Form constructor. Overrides PaymentGatewayBase:: |
|
PurchaseOrderGateway:: |
public | function |
Builds the payment instructions. Overrides HasPaymentInstructionsInterface:: |
|
PurchaseOrderGateway:: |
public | function |
Builds the available operations for the given payment. Overrides PaymentGatewayBase:: |
|
PurchaseOrderGateway:: |
public | function |
Creates a payment. Overrides SupportsStoredPaymentMethodsInterface:: |
|
PurchaseOrderGateway:: |
public | function |
Creates a payment method with the given payment details. Overrides SupportsCreatingPaymentMethodsInterface:: |
|
PurchaseOrderGateway:: |
public | function |
Gets default configuration for this plugin. Overrides PaymentGatewayBase:: |
|
PurchaseOrderGateway:: |
public | function |
Deletes the given payment method. Overrides SupportsStoredPaymentMethodsInterface:: |
|
PurchaseOrderGateway:: |
public | function |
Gets the credit card types handled by the gateway. Overrides PaymentGatewayBase:: |
|
PurchaseOrderGateway:: |
public | function | ||
PurchaseOrderGateway:: |
public | function |
Refunds the given payment. Overrides SupportsRefundsInterface:: |
|
PurchaseOrderGateway:: |
public | function |
Form submission handler. Overrides PaymentGatewayBase:: |
|
PurchaseOrderGateway:: |
public | function |
Voids the given payment. Overrides SupportsVoidsInterface:: |
|
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. |