You are here

protected function TestGateway::chargeCard in Ubercart 8.4

Called when a credit card should be processed.

@todo Replace the return array with a typed object.

Parameters

\Drupal\uc_order\OrderInterface $order: The order that is being processed. Credit card details supplied by the user are available in $order->payment_details[].

float $amount: The amount that should be charged.

string $txn_type: The transaction type, one of the UC_CREDIT_* constants.

string $reference: (optional) The payment reference, where needed for specific transaction types.

Return value

array Returns an associative array with the following members:

  • "success": TRUE if the transaction succeeded, FALSE otherwise.
  • "message": a human-readable message describing the result of the transaction.
  • "log_payment": TRUE if the transaction should be regarded as a successful payment.
  • "uid": The user ID of the person logging the payment, or 0 if the payment was processed automatically.
  • "comment": The comment string, markup allowed, to enter in the payment log.
  • "data": Any data that should be serialized and stored with the payment.

Overrides CreditCardPaymentMethodBase::chargeCard

File

payment/uc_credit/src/Plugin/Ubercart/PaymentMethod/TestGateway.php, line 66

Class

TestGateway
Defines the test gateway payment method.

Namespace

Drupal\uc_credit\Plugin\Ubercart\PaymentMethod

Code

protected function chargeCard(OrderInterface $order, $amount, $txn_type, $reference = NULL) {
  $user = \Drupal::currentUser();

  // cc_exp_month and cc_exp_year are also validated by
  // CreditCardPaymentMethodBase::validateExpirationDate().
  $month = $order->payment_details['cc_exp_month'];
  $year = $order->payment_details['cc_exp_year'];
  if ($year < 100) {
    $year = $year + 2000;
  }

  // Card is expired at 0:00 on the first day of the next month.
  $expiration_date = mktime(0, 0, 0, $month + 1, 1, $year);

  // Conditions for failure are described in file documentation block above.
  // All other transactions will succeed.
  if ($order->payment_details['cc_number'] == '0000000000000000') {
    $error = 'Invalid credit card number';
    $success = FALSE;
  }
  elseif (isset($order->payment_details['cc_cvv']) && $order->payment_details['cc_cvv'] == '000') {
    $error = 'Invalid CVV';
    $success = FALSE;
  }
  elseif ($expiration_date - REQUEST_TIME <= 0) {
    $error = 'Card is expired';
    $success = FALSE;
  }
  elseif ($amount == 12.34) {
    $error = 'Invalid payment amount';
    $success = FALSE;
  }
  elseif ($order->billing_first_name == 'Fictitious') {
    $error = 'Invalid customer name';
    $success = FALSE;
  }
  elseif ($order->billing_phone == '8675309') {
    $error = 'Obviously fake phone number';
    $success = FALSE;
  }
  else {
    $success = TRUE;
  }

  // The information for the payment is in the $order->payment_details array.
  if ($this->configuration['debug']) {
    \Drupal::logger('uc_credit')
      ->notice('Test gateway payment details @details.', [
      '@details' => print_r($order->payment_details, TRUE),
    ]);
  }
  if ($success) {
    $message = $this
      ->t('Credit card charged: @amount', [
      '@amount' => uc_currency_format($amount),
    ]);
    $comment = $this
      ->t('Card charged, resolution code: 0022548315');
  }
  else {
    $message = $this
      ->t('Credit card charge failed.');
    $comment = $this
      ->t('Card failed authorization, reason: @error', [
      '@error' => $error,
    ]);
  }
  uc_order_comment_save($order
    ->id(), $user
    ->id(), $message, 'admin');
  return [
    'success' => $success,
    'comment' => $comment,
    'message' => $message,
    'uid' => $user
      ->id(),
  ];
}