You are here

private function WebformCivicrmPostProcess::contributionParams in Webform CiviCRM Integration 8.5

Format contribution params for transact/pay-later

Return value

array

2 calls to WebformCivicrmPostProcess::contributionParams()
WebformCivicrmPostProcess::createDeferredPayment in src/WebformCivicrmPostProcess.php
Create Pending (Pay Later) Contribution
WebformCivicrmPostProcess::submitLivePayment in src/WebformCivicrmPostProcess.php
Execute payment processor transaction This happens during form validation and sets a form error if unsuccessful

File

src/WebformCivicrmPostProcess.php, line 2159
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function contributionParams() {
  $params = $this->billing_params + $this->data['contribution'][1]['contribution'][1];
  $params['financial_type_id'] = wf_crm_aval($this->data, 'contribution:1:contribution:1:financial_type_id');
  $params['currency'] = $params['currencyID'] = wf_crm_aval($this->data, "contribution:1:currency");
  $params['skipRecentView'] = $params['skipLineItem'] = 1;
  $params['contact_id'] = $this->ent['contact'][1]['id'];
  $params['total_amount'] = round($this->totalContribution, 2);
  $utils = \Drupal::service('webform_civicrm.utils');

  // Most payment processors expect this (normally be set by contribution page processConfirm)
  $params['contactID'] = $params['contact_id'];

  // Some processors use this for matching and updating the contribution status
  if (!$this->contributionIsPayLater) {
    $params['invoice_id'] = $this->data['contribution'][1]['contribution'][1]['invoiceID'] = md5(uniqid(mt_rand(), TRUE));
  }

  // Sales tax integration
  $params['tax_amount'] = 0;
  if (isset($this->form_state
    ->getStorage()['civicrm']['line_items'])) {
    foreach ($this->form_state
      ->getStorage()['civicrm']['line_items'] as $lineItem) {
      if (!empty($lineItem['tax_amount'])) {
        $params['tax_amount'] += $lineItem['tax_amount'];
      }
    }
    $params['tax_amount'] = round($params['tax_amount'], 2);
  }
  $params['description'] = t('Webform Payment: @title', [
    '@title' => $this->node
      ->label(),
  ]);
  if (!isset($params['source'])) {
    $params['source'] = $this->settings['new_contact_source'];
  }
  $financialTypeId = $params['financial_type_id'];
  $financialTypeDetails = $utils
    ->wf_civicrm_api('FinancialType', 'get', [
    'return' => "is_deductible,name",
    'id' => $financialTypeId,
  ]);

  // Some payment processors expect this to be set (eg. smartdebit)
  $params['financialType_name'] = $financialTypeDetails['values'][$financialTypeId]['name'];
  $params['financialTypeID'] = $financialTypeId;

  // Calculate non-deductible amount for income tax purposes
  if ($financialTypeDetails['values'][$financialTypeId]['is_deductible'] == 1) {

    // deductible
    $params['non_deductible_amount'] = 0;
  }
  else {
    $params['non_deductible_amount'] = $params['total_amount'];
  }

  // Pass all submitted values to payment processor.
  $request = \Drupal::request();
  foreach ($request->request
    ->all() as $key => $value) {
    if (empty($params[$key])) {
      $params[$key] = $value;
    }
  }

  // Fix bug for testing.
  // @todo Pay Later causes issues as it returns `0`.
  if ($params['is_test'] == 1 && $params['payment_processor_id'] !== '0') {
    $liveProcessorName = $utils
      ->wf_civicrm_api('payment_processor', 'getvalue', [
      'id' => $params['payment_processor_id'],
      'return' => 'name',
    ]);

    // Lookup current domain for multisite support
    static $domain = 0;
    if (!$domain) {
      $domain = $utils
        ->wf_civicrm_api('domain', 'get', [
        'current_domain' => 1,
        'return' => 'id',
      ]);
      $domain = wf_crm_aval($domain, 'id', 1);
    }
    $params['payment_processor_id'] = $utils
      ->wf_civicrm_api('payment_processor', 'getvalue', [
      'return' => 'id',
      'name' => $liveProcessorName,
      'is_test' => 1,
      'domain_id' => $domain,
    ]);
  }
  if (empty($params['payment_instrument_id']) && !empty($params['payment_processor_id'])) {
    $params['payment_instrument_id'] = $this
      ->getPaymentInstrument($params['payment_processor_id']);
  }

  // doPayment requries payment_processor and payment_processor_mode fields.
  $params['payment_processor'] = wf_crm_aval($params, 'payment_processor_id');
  if (!empty($params['credit_card_number'])) {
    if (!empty($params['credit_card_type'])) {
      $result = $utils
        ->wf_civicrm_api('OptionValue', 'get', [
        'return' => [
          'value',
        ],
        'option_group_id.name' => 'accept_creditcard',
        'name' => ucfirst($params['credit_card_type']),
      ]);
      if (!empty($result['id'])) {
        $params['card_type_id'] = $result['values'][$result['id']]['value'];
      }
    }
    $params['pan_truncation'] = substr($params['credit_card_number'], -4);
  }

  // Save this stuff for later
  unset($params['soft'], $params['honor_contact_id'], $params['honor_type_id']);
  return $params;
}