You are here

function commerce_cop_confirm_payment_form in Commerce Custom Offline Payments 7

Form to Receive the Payment

1 string reference to 'commerce_cop_confirm_payment_form'
commerce_cop_menu in ./commerce_cop.module
Implements hook_menu().

File

./commerce_cop.module, line 102
Custom offline payment methods for Drupal Commerce.

Code

function commerce_cop_confirm_payment_form($form, &$form_state, $order, $transaction) {
  $form_state['order'] = $order;
  $form_state['transaction'] = $transaction;

  // Load and store the payment method instance for this transaction.
  $payment_method = commerce_payment_method_instance_load($transaction->instance_id);
  $form_state['payment_method'] = $payment_method;
  $balance = commerce_payment_order_balance($order);
  if ($balance['amount'] > 0 && $balance['amount'] < $transaction->amount) {
    $default_amount = $balance['amount'];
  }
  else {
    $default_amount = $transaction->amount;
  }

  // Convert the price amount to a user friendly decimal value.
  $default_amount = commerce_currency_amount_to_decimal($default_amount, $transaction->currency_code);
  $description = implode('<br />', array(
    t('Order balance: @balance', array(
      '@balance' => commerce_currency_format($balance['amount'], $balance['currency_code']),
    )),
  ));
  $payment = custom_offline_payment_load($transaction->payment_method);
  $form['amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Confirm %payment amount', array(
      '%payment' => $payment['title'],
    )),
    '#description' => $description,
    '#default_value' => $default_amount,
    '#field_suffix' => check_plain($transaction->currency_code),
    '#size' => 16,
  );

  // Fieldable payment transactions.
  if (module_exists('commerce_payment_fields') && !empty($payment_method['fieldable'])) {
    $instances = field_info_instances('commerce_payment_transaction', $transaction->payment_method);
    if (!empty($instances)) {

      // Add the field widgets for the profile.
      field_attach_form('commerce_payment_transaction', $transaction, $form, $form_state);
    }
  }
  $form = confirm_form($form, t('What amount do you want to confirm?'), 'admin/commerce/orders/' . $order->order_id . '/payment', '', t('Confirm'), t('Cancel'), 'confirm');
  return $form;
}