You are here

function _uc_stripe_move_customer_id in Ubercart Stripe 7.3

Same name and namespace in other branches
  1. 6.2 uc_stripe.install \_uc_stripe_move_customer_id()
  2. 7.2 uc_stripe.install \_uc_stripe_move_customer_id()

Move customer ids from uc_recurring_stripe into user account

1 call to _uc_stripe_move_customer_id()
uc_stripe_update_7202 in ./uc_stripe.install
Move customer IDs from uc_recurring_stripe into account

File

./uc_stripe.install, line 199
Installation file for the uc_stripe module.

Code

function _uc_stripe_move_customer_id(&$sandbox) {

  // Find the users with stripe customer ids that are active
  $query = '
    SELECT DISTINCT(urs.uid)
    FROM {users} u JOIN {uc_recurring_stripe} urs
    ON (u.uid = urs.uid)
    WHERE urs.active = 1';
  $result = db_query_range($query, 0, $sandbox['per_run'], array(), array(
    'fetch' => PDO::FETCH_ASSOC,
  ));
  foreach ($result as $item) {
    $sandbox['progress']++;
    $stripe_customer_id = db_query_range('
      SELECT urs.customer_id
      FROM {uc_recurring_stripe} urs
      WHERE urs.uid = :uid AND urs.active = 1
      ORDER BY urs.rfid DESC
      ', 0, 1, array(
      ':uid' => $item['uid'],
    ))
      ->fetchField();
    $account = user_load($item['uid']);

    // Set to inactive every subscription for this uid
    db_update('uc_recurring_stripe')
      ->fields(array(
      'active' => 0,
    ))
      ->condition('uid', $item['uid'])
      ->execute();
    if (empty($account->data['uc_stripe_customer_id'])) {
      user_save($account, array(
        'data' => array(
          'uc_stripe_customer_id' => $stripe_customer_id,
        ),
      ));
    }
  }
  if ($sandbox['progress'] >= $sandbox['max'] || $result
    ->rowCount() == 0) {
    $sandbox['#finished'] = 1;
  }
  else {
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  }
}