class Echeck in Commerce Authorize.Net 8
Provides the Authorize.net echeck payment gateway.
Plugin annotation
@CommercePaymentGateway(
  id = "authorizenet_echeck",
  label = "Authorize.net (Echeck)",
  display_label = "Authorize.net Echeck",
  forms = {
    "add-payment-method" = "Drupal\commerce_authnet\PluginForm\EcheckAddForm",
  },
  payment_type = "payment_manual",
  payment_method_types = {"authnet_echeck"},
  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_authnet\Plugin\Commerce\PaymentGateway\OnsiteBase implements OnsitePaymentGatewayInterface, SupportsAuthorizationsInterface- class \Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway\Echeck implements EcheckInterface
 
 
- class \Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway\OnsiteBase implements OnsitePaymentGatewayInterface, SupportsAuthorizationsInterface
 
- 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 Echeck
1 file declares its use of Echeck
1 string reference to 'Echeck'
- commerce_authnet_post_update_echeck in ./commerce_authnet.post_update.php 
- Separate echeck payment gateways from accept.js and update all affected payment_methods and payments.
File
- src/Plugin/ Commerce/ PaymentGateway/ Echeck.php, line 33 
Namespace
Drupal\commerce_authnet\Plugin\Commerce\PaymentGatewayView source
class Echeck extends OnsiteBase implements EcheckInterface {
  /**
   * {@inheritdoc}
   */
  public function createPayment(PaymentInterface $payment, $capture = TRUE) {
    $this
      ->assertPaymentState($payment, [
      'new',
    ]);
    $payment_method = $payment
      ->getPaymentMethod();
    $this
      ->assertPaymentMethod($payment_method);
    $order = $payment
      ->getOrder();
    // Transaction request.
    // eChecks have a pseudo "authorized" state, so just do AUTH_CAPTURE.
    $transaction_request = new TransactionRequest([
      'transactionType' => TransactionRequest::AUTH_CAPTURE,
      'amount' => $payment
        ->getAmount()
        ->getNumber(),
    ]);
    list($data_descriptor, $data_value) = explode('|', $payment_method
      ->getRemoteId());
    $payment_data = [
      'opaqueData' => [
        'dataDescriptor' => $data_descriptor,
        'dataValue' => $data_value,
      ],
    ];
    $transaction_request
      ->addData('payment', $payment_data);
    if ($billing_profile = $payment_method
      ->getBillingProfile()) {
      /** @var \Drupal\address\AddressInterface $address */
      $address = $billing_profile
        ->get('address')
        ->first();
      $bill_to = [
        // @todo how to allow customizing this.
        'firstName' => $address
          ->getGivenName(),
        'lastName' => $address
          ->getFamilyName(),
        'company' => $address
          ->getOrganization(),
        'address' => substr($address
          ->getAddressLine1() . ' ' . $address
          ->getAddressLine2(), 0, 60),
        'country' => $address
          ->getCountryCode(),
        'city' => $address
          ->getLocality(),
        'state' => $address
          ->getAdministrativeArea(),
        'zip' => $address
          ->getPostalCode(),
      ];
      $transaction_request
        ->addDataType(new BillTo(array_filter($bill_to)));
    }
    $profiles = $order
      ->collectProfiles();
    if (isset($profiles['shipping']) && !$profiles['shipping']
      ->get('address')
      ->isEmpty()) {
      /** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $shipping_address */
      $shipping_address = $profiles['shipping']
        ->get('address')
        ->first();
      $ship_data = [
        // @todo how to allow customizing this.
        'firstName' => $shipping_address
          ->getGivenName(),
        'lastName' => $shipping_address
          ->getFamilyName(),
        'address' => substr($shipping_address
          ->getAddressLine1() . ' ' . $shipping_address
          ->getAddressLine2(), 0, 60),
        'country' => $shipping_address
          ->getCountryCode(),
        'company' => $shipping_address
          ->getOrganization(),
        'city' => $shipping_address
          ->getLocality(),
        'state' => $shipping_address
          ->getAdministrativeArea(),
        'zip' => $shipping_address
          ->getPostalCode(),
      ];
      $transaction_request
        ->addDataType(new ShipTo(array_filter($ship_data)));
    }
    // Adding order information to the transaction.
    $transaction_request
      ->addOrder(new OrderDataType([
      'invoiceNumber' => $order
        ->getOrderNumber() ?: $order
        ->id(),
    ]));
    $transaction_request
      ->addData('customerIP', $order
      ->getIpAddress());
    // Adding line items.
    $line_items = $this
      ->getLineItems($order);
    foreach ($line_items as $line_item) {
      $transaction_request
        ->addLineItem($line_item);
    }
    // Adding tax information to the transaction.
    $transaction_request
      ->addData('tax', $this
      ->getTax($order)
      ->toArray());
    $transaction_request
      ->addData('shipping', $this
      ->getShipping($order)
      ->toArray());
    $request = new CreateTransactionRequest($this->authnetConfiguration, $this->httpClient);
    $request
      ->setTransactionRequest($transaction_request);
    $response = $request
      ->execute();
    if ($response
      ->getResultCode() != 'Ok') {
      $this
        ->logResponse($response);
      $message = $response
        ->getMessages()[0];
      switch ($message
        ->getCode()) {
        case 'E00040':
          $payment_method
            ->delete();
          throw new PaymentGatewayException('The provided payment method is no longer valid');
        default:
          throw new PaymentGatewayException($message
            ->getText());
      }
    }
    if (!empty($response
      ->getErrors())) {
      $message = $response
        ->getErrors()[0];
      throw new HardDeclineException($message
        ->getText());
    }
    // Mark the payment as pending as we await for transaction details from
    // Authorize.net.
    $payment
      ->setState('pending');
    $payment
      ->setRemoteId($response->transactionResponse->transId);
    $payment
      ->save();
  }
  /**
   * {@inheritdoc}
   */
  public function capturePayment(PaymentInterface $payment, Price $amount = NULL) {
    // If not specified, capture the entire amount.
    $amount = $amount ?: $payment
      ->getAmount();
    $this
      ->assertPaymentState($payment, [
      'pending',
    ]);
    $payment
      ->setState('completed');
    $payment
      ->setAmount($amount);
    $payment
      ->save();
  }
  /**
   * {@inheritdoc}
   */
  public function voidPayment(PaymentInterface $payment) {
    $this
      ->assertPaymentState($payment, [
      'pending',
    ]);
    $payment
      ->setState('voided');
    $payment
      ->save();
  }
  /**
   * {@inheritdoc}
   *
   * @todo Needs kernel test
   */
  public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
    $required_keys = [
      'data_descriptor',
      'data_value',
    ];
    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));
      }
    }
    // Reusing echecks is not supported at the moment.
    // @see https://community.developer.authorize.net/t5/Integration-and-Testing/Accept-JS-and-ACH/td-p/55874
    $payment_method
      ->setReusable(FALSE);
    $payment_method
      ->setRemoteId($payment_details['data_descriptor'] . '|' . $payment_details['data_value']);
    // OpaqueData expire after 15min. We reduce that time by 5s to account for
    // the time it took to do the server request after the JS tokenization.
    $expires = $this->time
      ->getRequestTime() + 15 * 60 - 5;
    $payment_method
      ->setExpiresTime($expires);
    $payment_method
      ->save();
  }
  /**
   * {@inheritdoc}
   */
  public function getSettledTransactions($from_date, $to_date) {
    $request = new GetSettledBatchListRequest($this->authnetConfiguration, $this->httpClient, FALSE, $from_date, $to_date);
    $batch_response = $request
      ->execute();
    $batch_ids = [];
    if ($batch_response
      ->getResultCode() === 'Ok') {
      if (is_object($batch_response
        ->contents()->batchList->batch)) {
        if ($batch_response
          ->contents()->batchList->batch->paymentMethod === 'eCheck') {
          if ($batch_response
            ->contents()->batchList->batch->settlementState === 'settledSuccessfully') {
            $batch_ids[] = $batch_response
              ->contents()->batchList->batch->batchId;
          }
        }
      }
      else {
        foreach ($batch_response
          ->contents()->batchList->batch as $batch) {
          if ($batch->paymentMethod === 'eCheck') {
            if ($batch->settlementState === 'settledSuccessfully') {
              $batch_ids[] = $batch->batchId;
            }
          }
        }
      }
    }
    $remote_ids = [];
    foreach ($batch_ids as $batch_id) {
      $request = new GetTransactionListRequest($this->authnetConfiguration, $this->httpClient, $batch_id);
      $transaction_list_response = $request
        ->execute();
      if ($transaction_list_response
        ->contents()->totalNumInResultSet == 1) {
        $remote_ids[] = $transaction_list_response
          ->contents()->transactions->transaction->transId;
      }
      else {
        foreach ($transaction_list_response
          ->contents()->transactions->transaction as $transaction) {
          $remote_ids[] = $transaction->transId;
        }
      }
    }
    $payments = [];
    if ($remote_ids) {
      $payment_storage = $this->entityTypeManager
        ->getStorage('commerce_payment');
      $payment_ids = $payment_storage
        ->getQuery()
        ->condition('state', 'pending')
        ->condition('remote_id', $remote_ids)
        ->execute();
      if ($payment_ids) {
        $payments = $payment_storage
          ->loadMultiple($payment_ids);
      }
    }
    return $payments;
  }
}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. | |
| Echeck:: | public | function | Captures the given authorized payment. Overrides OnsiteBase:: | |
| Echeck:: | public | function | Creates a payment. Overrides SupportsStoredPaymentMethodsInterface:: | |
| Echeck:: | public | function | @todo Needs kernel test Overrides SupportsCreatingPaymentMethodsInterface:: | |
| Echeck:: | public | function | Get settled transactions from authorize.net. Overrides EcheckInterface:: | |
| Echeck:: | public | function | Voids the given payment. Overrides OnsiteBase:: | |
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| OnsiteBase:: | protected | property | The adjustment transformer. | |
| OnsiteBase:: | protected | property | The Authorize.net API configuration. | |
| OnsiteBase:: | protected | property | The HTTP client. | |
| OnsiteBase:: | protected | property | The logger. | |
| OnsiteBase:: | protected | property | The messenger service. Overrides MessengerTrait:: | |
| OnsiteBase:: | protected | property | The private temp store factory. | |
| OnsiteBase:: | public | function | Form constructor. Overrides OnsitePaymentGatewayBase:: | 1 | 
| OnsiteBase:: | public static | function | Creates an instance of the plugin. Overrides PaymentGatewayBase:: | |
| OnsiteBase:: | public | function | Gets default configuration for this plugin. Overrides PaymentGatewayBase:: | 1 | 
| OnsiteBase:: | public | function | @todo Needs kernel test Overrides SupportsStoredPaymentMethodsInterface:: | |
| OnsiteBase:: | protected | function | Formats an API response as a string. | |
| OnsiteBase:: | protected | function | Gets the line items from order. | 1 | 
| OnsiteBase:: | public | function | Returns the customer identifier from a payment method's remote id. | |
| OnsiteBase:: | public | function | Returns the payment method remote identifier ensuring customer identifier is removed. | |
| OnsiteBase:: | protected | function | Gets the shipping from order. | |
| OnsiteBase:: | protected | function | Gets the tax from order. | |
| OnsiteBase:: | protected | function | Writes an API response to the log for debugging. | |
| OnsiteBase:: | public | function | Form submission handler. Overrides PaymentGatewayBase:: | 1 | 
| OnsiteBase:: | public | function | Form validation handler. Overrides PaymentGatewayBase:: | |
| OnsiteBase:: | 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 a label for the given AVS response code and card type. Overrides PaymentGatewayInterface:: | 2 | 
| 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:: | 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 | 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. | 
