You are here

function commerce_stripe_refund_form_validate in Commerce Stripe 7

Same name and namespace in other branches
  1. 7.3 includes/commerce_stripe.admin.inc \commerce_stripe_refund_form_validate()

Validation callback for submitting refunds to Stripe.

File

includes/commerce_stripe.admin.inc, line 79
Provides admin forms and functions for commerce_stripe.

Code

function commerce_stripe_refund_form_validate($form, &$form_state) {
  $transaction = $form_state['transaction'];
  $amount = commerce_currency_decimal_to_amount($form_state['values']['amount'], $transaction->currency_code);

  // Calculate the amount left available for a refund.
  $amount_refunded = !empty($form_state['stripe_charge']->amount_refunded) ? $form_state['stripe_charge']->amount_refunded : 0;
  $remaining = $form_state['stripe_charge']->amount - $amount_refunded;

  // Make sure the refund amount is valid and available.
  if ($amount <= 0 || $amount > $remaining || !is_numeric($amount)) {
    form_set_error('amount', t('Please enter a valid return amount that is less than or equal to the remaining balance available for refund of @remaining.', array(
      '@remaining' => commerce_currency_format($remaining, $transaction->currency_code),
    )));
  }
}