You are here

function commerce_no_payment_update_7201 in Commerce No Payment 7.2

Creates Payment payments for Commerce Payment payment transactions that use Commerce No Payment.

1 call to commerce_no_payment_update_7201()
CommerceNoPaymentUpgrade::testUpgrade in tests/CommerceNoPaymentUpgrade.test
Tests the upgrade process.

File

./commerce_no_payment.install, line 61
Installation and uninstallation functions.

Code

function commerce_no_payment_update_7201(&$sandbox) {
  if (!isset($sandbox['run'])) {
    $sandbox['run'] = 0;

    // Count the number of Commerce Payment payment transactions for which
    // Payment payments need to be created.
    $sandbox['transaction_count'] = commerce_no_payment_commerce_payment_transaction_count('commerce_no_payment');
  }
  else {
    $sandbox['run']++;
  }
  if ($sandbox['transaction_count']) {
    $sandbox['#finished'] = 1 / ceil($sandbox['transaction_count'] / (($sandbox['run'] + 1) * COMMERCE_NO_PAYMENT_MIGRATE_PER_RUN));
    $payment_method = entity_load_single('payment_method', variable_get('commerce_no_payment_2_migration_pmid'));

    // Load the transaction IDs for this run.
    $payment_method_name = 'payment_commerce_' . $payment_method->pmid;
    $transaction_ids = db_select('commerce_payment_transaction', 'cpt')
      ->fields('cpt', array(
      'transaction_id',
    ))
      ->condition('payment_method', 'commerce_no_payment')
      ->condition('instance_id', 'commerce_no_payment|commerce_payment_commerce_no_payment')
      ->range($sandbox['run'] * COMMERCE_NO_PAYMENT_MIGRATE_PER_RUN, COMMERCE_NO_PAYMENT_MIGRATE_PER_RUN)
      ->execute()
      ->fetchCol();

    // Load the transactions for this run.
    $transactions = commerce_payment_transaction_load_multiple($transaction_ids);

    // Create a Payment payment per transaction.
    foreach ($transactions as $transaction) {

      // The Payment creation is mostly copied from
      // payment_commerce_payment_create().
      $payment = new Payment(array(
        'context' => 'payment_commerce',
        'context_data' => array(
          'balance_amount' => $transaction->amount,
          'order_id' => $transaction->order_id,
        ),
        'description' => t('Order #!order_id', array(
          '!order_id' => $transaction->order_id,
        )),
        'currency_code' => $transaction->currency_code,
        'method' => $payment_method,
        'finish_callback' => 'payment_commerce_payment_finish',
      ));
      $currency = commerce_currency_load($payment->currency_code);
      $amount = (double) $transaction->amount / pow(10, $currency['decimals']);
      $payment
        ->setLineItem(new PaymentLineItem(array(
        'amount' => $amount,
        'description' => 'Order !order_id',
        'description_arguments' => array(
          '!order_id' => $transaction->order_id,
        ),
        'name' => 'payment_commerce',
      )));
      $payment->statuses = array(
        new PaymentStatusItem(PAYMENT_STATUS_NEW, $transaction->created),
        new PaymentStatusItem(PAYMENT_STATUS_PENDING, $transaction->created),
        new PaymentStatusItem(commerce_no_payment_status_convert($transaction->status), $transaction->changed),
      );
      entity_save('payment', $payment);
    }
  }
}