You are here

function PaymentMethodController::validate in Payment 7

Validate a payment against a payment method and this controller. Don't call directly. Use PaymentMethod::validate() instead.

Parameters

Payment $payment:

PaymentMethod $payment_method:

boolean $strict: Whether to validate everything a payment method needs or to validate the most important things only. Useful when finding available payment methods, for instance, which does not require unimportant things to be a 100% valid.

Throws

PaymentValidationException

See also

PaymentMethod::validate()

1 call to PaymentMethodController::validate()
PaymentMethodBasicController::validate in modules/paymentmethodbasic/paymentmethodbasic.module
Validate a payment against a payment method and this controller. Don't call directly. Use PaymentMethod::validate() instead.
2 methods override PaymentMethodController::validate()
PaymentMethodBasicController::validate in modules/paymentmethodbasic/paymentmethodbasic.module
Validate a payment against a payment method and this controller. Don't call directly. Use PaymentMethod::validate() instead.
PaymentMethodControllerUnavailable::validate in ./payment.classes.inc
Validate a payment against a payment method and this controller. Don't call directly. Use PaymentMethod::validate() instead.

File

./payment.classes.inc, line 898
The API and related functions for executing and managing payments.

Class

PaymentMethodController
A payment method controller, e.g. the logic behind a payment method.

Code

function validate(Payment $payment, PaymentMethod $payment_method, $strict) {

  // Confirm the payment method is enabled, and thus available in general.
  if (!$payment_method->enabled) {
    throw new PaymentValidationPaymentMethodDisabledException(t('The payment method is disabled.'));
  }
  if (!$payment->currency_code) {
    throw new PaymentValidationMissingCurrencyException(t('The payment has no currency set.'));
  }
  $currencies = $payment_method->controller->currencies;

  // Confirm the payment's currency is supported.
  if (!empty($this->currencies) && !isset($this->currencies[$payment->currency_code])) {
    throw new PaymentValidationUnsupportedCurrencyException(t('The currency is not supported by this payment method.'));
  }

  // Confirm the payment's description is set and valid.
  if (empty($payment->description)) {
    throw new PaymentValidationDescriptionMissing(t('The payment description is not set.'));
  }
  elseif (drupal_strlen($payment->description) > 255) {
    throw new PaymentValidationDescriptionTooLong(t('The payment description exceeds 255 characters.'));
  }

  // Confirm the finish callback is set and the function exists.
  if (empty($payment->finish_callback) || !function_exists($payment->finish_callback)) {
    throw new PaymentValidationMissingFinishCallback(t('The finish callback is not set or not callable.'));
  }

  // Confirm the payment amount is higher than the supported minimum.
  $minimum = isset($currencies[$payment->currency_code]['minimum']) ? $currencies[$payment->currency_code]['minimum'] : PAYMENT_MINIMUM_AMOUNT;
  if ($payment
    ->totalAmount(TRUE) < $minimum) {
    throw new PaymentValidationAmountBelowMinimumException(t('The amount should be higher than !minimum.', array(
      '!minimum' => payment_amount_human_readable($minimum, $payment->currency_code),
    )));
  }

  // Confirm the payment amount does not exceed the maximum.
  if (isset($currencies[$payment->currency_code]['maximum']) && $payment
    ->totalAmount(TRUE) > $currencies[$payment->currency_code]['maximum']) {
    throw new PaymentValidationAmountExceedsMaximumException(t('The amount should be lower than !maximum.', array(
      '!maximum' => payment_amount_human_readable($currencies[$payment->currency_code]['maximum'], $payment->currency_code),
    )));
  }
}