You are here

protected function PurchaseOrderGateway::authorizePayment in Commerce Purchase Order 8

Authorizes payment based on settings in the gateway configuration.

Parameters

\Drupal\commerce_payment\Entity\PaymentInterface $payment: The payment to authorize.

1 call to PurchaseOrderGateway::authorizePayment()
PurchaseOrderGateway::createPayment in src/Plugin/Commerce/PaymentGateway/PurchaseOrderGateway.php
Creates a payment.

File

src/Plugin/Commerce/PaymentGateway/PurchaseOrderGateway.php, line 239

Class

PurchaseOrderGateway
Provides the On-site payment gateway.

Namespace

Drupal\commerce_purchase_order\Plugin\Commerce\PaymentGateway

Code

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());
  }
}