You are here

public function Echeck::getSettledTransactions in Commerce Authorize.Net 8

Get settled transactions from authorize.net.

Parameters

string $from_date: The settlement starting date in Y-m-d\TH:i:s format.

string $to_date: The settlement end date in Y-m-d\TH:i:s format.

Return value

\Drupal\commerce_payment\Entity\PaymentInterface[] An array of payments keyed by the entity id.

Overrides EcheckInterface::getSettledTransactions

File

src/Plugin/Commerce/PaymentGateway/Echeck.php, line 194

Class

Echeck
Provides the Authorize.net echeck payment gateway.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

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