You are here

function stripe_customer_install in Stripe 7

Try to import any existing customer_ids from other Stripe modules.

File

stripe_customer/stripe_customer.install, line 89
Drupal install hooks.

Code

function stripe_customer_install() {

  // Subscriptions first, since that module required this one to run.
  if (module_exists('stripe_subscription')) {
    stripe_load_library();
    $subs = db_query("SELECT * FROM {stripe_subscriptions}");
    while ($sub = $subs
      ->fetchObject()) {
      $update = new stdClass();
      $update->uid = $sub->uid;
      $update->customer_id = $sub->customer_id;
      $update->livemode = $sub->livemode;
      $update->created = $sub->created;
      $update->changed = $sub->last_sync;

      // If the customer has been deleted, set status to FALSE.
      // @todo: Do this by pinging Stripe API or queueing customer for resync.
      drupal_write_record('stripe_customers', $update);
    }
  }

  // If Commerce Stripe is configured for Card on file,
  // import existing customers and cards.
  if (module_exists('commerce_stripe') && module_exists('commerce_cardonfile')) {
    module_load_include('module', 'commerce_stripe');
    $livemode = tg_stripe_live();
    if (_commerce_stripe_load_setting('cardonfile')) {
      $cards = db_query("SELECT * FROM {commerce_cardonfile} WHERE payment_method = 'commerce_stripe'");
      stripe_load_library();
      while ($card = $cards
        ->fetchObject()) {
        try {
          list($customer_id, $card_id) = explode('|', $card->remote_id);
          $update = new stdClass();
          $update->uid = $card->uid;
          $update->customer_id = $customer_id;
          $update->livemode = $livemode;
          $update->default_source = $card_id;
          $update->created = $card->created;
          $update->status = TRUE;

          // If the customer has been deleted, set status to FALSE.
          // @todo: Do this by pinging Stripe API or queueing customer for resync.
          drupal_write_record('stripe_customers', $update);
        } catch (Exception $e) {

          // Fail silently?
        }
      }
    }
  }
}