You are here

function stripe_customer_tokens in Stripe 7

Implements hook_tokens().

File

stripe_customer/stripe_customer.tokens.inc, line 56
Contains token integration.

Code

function stripe_customer_tokens($type, $tokens, $data = array(), $options = array()) {
  if (isset($options['language'])) {
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if ($type == 'stripe_customer') {
    $customer = isset($data['stripe_customer']) && $data['stripe_customer'] instanceof \Stripe\Customer ? $data['stripe_customer'] : NULL;
    $account = isset($data['account']) ? $data['account'] : NULL;
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'customer_id':
          $replacements[$original] = $customer->id;
          break;
        case 'owner':
          $replacements[$original] = $sanitize ? filter_xss($account->name) : $account->name;
          break;
        case 'email':
          $replacements[$original] = $customer->email;
          break;
        case 'metadata':
          $replacements[$original] = $customer->metadata;
          break;
        case 'currency':
          $replacements[$original] = $customer->currency;
          break;
        case 'created':
          $replacements[$original] = format_date($customer->created, 'medium', '', NULL, $language_code);
          break;
        case 'account_balance':
          $replacements[$original] = $customer->account_balance;
          break;
        case 'description':
          $replacements[$original] = $sanitize ? check_plain($customer->description) : $customer->description;
          break;
        case 'default_source':
          $replacements[$original] = $customer->default_source;
          break;
      }
    }
  }
  return $replacements;
}