View source
<?php
namespace Drupal\uc_credit\Plugin\Ubercart\PaymentMethod;
use Drupal\Core\Form\FormStateInterface;
use Drupal\uc_credit\CreditCardPaymentMethodBase;
use Drupal\uc_order\OrderInterface;
class TestGateway extends CreditCardPaymentMethodBase {
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'debug' => FALSE,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Debug'),
'#description' => $this
->t('Log debug payment information to dblog when card is "charged" by this gateway.'),
'#default_value' => $this->configuration['debug'],
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['debug'] = $form_state
->getValue('debug');
return parent::submitConfigurationForm($form, $form_state);
}
protected function chargeCard(OrderInterface $order, $amount, $txn_type, $reference = NULL) {
$user = \Drupal::currentUser();
$month = $order->payment_details['cc_exp_month'];
$year = $order->payment_details['cc_exp_year'];
if ($year < 100) {
$year = $year + 2000;
}
$expiration_date = mktime(0, 0, 0, $month + 1, 1, $year);
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;
}
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(),
];
}
}