You are here

private function WebformCivicrmPostProcess::createBillingContact in Webform CiviCRM Integration 8.5

Create contact 1 if not already existing (required by contribution.transact)

Return value

int

1 call to WebformCivicrmPostProcess::createBillingContact()
WebformCivicrmPostProcess::validate in src/WebformCivicrmPostProcess.php
Called after a webform is submitted Or, for a multipage form, called after each page

File

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

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function createBillingContact() {
  $cid = wf_crm_aval($this->existing_contacts, 1);
  $utils = \Drupal::service('webform_civicrm.utils');
  if (!$cid) {
    $contact = $this->data['contact'][1];

    // Only use middle name from billing if we are using the rest of the billing name as well
    if (empty($contact['contact'][1]['first_name']) && !empty($this->billing_params['middle_name'])) {
      $contact['contact'][1]['middle_name'] = $this->billing_params['middle_name'];
    }
    $contact['contact'][1] += [
      'first_name' => $this->billing_params['first_name'] ?? NULL,
      'last_name' => $this->billing_params['last_name'] ?? NULL,
    ];
    $cid = $this
      ->findDuplicateContact($contact);
  }
  $address = [
    'street_address' => $this->billing_params['street_address'] ?? NULL,
    'city' => $this->billing_params['city'] ?? NULL,
    'country_id' => $this->billing_params['country_id'] ?? NULL,
    'state_province_id' => wf_crm_aval($this->billing_params, 'state_province_id'),
    'postal_code' => $this->billing_params['postal_code'] ?? NULL,
    'location_type_id' => 'Billing',
  ];
  $email = [
    'email' => $this->billing_params['email'],
    'location_type_id' => 'Billing',
  ];
  if (!$cid) {
    $cid = $this
      ->createContact($contact);
    $this->billing_contact = $cid;
  }
  else {
    foreach ([
      'address',
      'email',
    ] as $loc) {
      $result = $utils
        ->wf_civicrm_api($loc, 'get', [
        'contact_id' => $cid,
        'location_type_id' => 'Billing',
      ]);

      // Use first id if we have any results
      if (!empty($result['values'])) {
        $ids = array_keys($result['values']);
        ${$loc}['id'] = $ids[0];
      }
    }
  }
  if ($cid) {
    $address['contact_id'] = $email['contact_id'] = $this->ent['contact'][1]['id'] = $cid;
    $utils
      ->wf_civicrm_api('address', 'create', $address);
    $utils
      ->wf_civicrm_api('email', 'create', $email);
  }
  return $cid;
}