View source
<?php
namespace Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\CreditCard;
use Drupal\commerce_payment\Entity\PaymentInterface;
use Drupal\commerce_payment\Entity\PaymentMethodInterface;
use Drupal\commerce_payment\PaymentMethodStorageInterface;
use Drupal\commerce_payment\PaymentMethodTypeManager;
use Drupal\commerce_payment\PaymentTypeManager;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface;
use Drupal\commerce_price\MinorUnitsConverterInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class StoredOffsiteRedirect extends OffsiteRedirect implements SupportsStoredPaymentMethodsInterface {
protected $messenger;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PaymentTypeManager $payment_type_manager, PaymentMethodTypeManager $payment_method_type_manager, TimeInterface $time, MinorUnitsConverterInterface $minor_units_converter, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time, $minor_units_converter);
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('plugin.manager.commerce_payment_type'), $container
->get('plugin.manager.commerce_payment_method_type'), $container
->get('datetime.time'), $container
->get('commerce_price.minor_units_converter'), $container
->get('messenger'));
}
public function createPaymentMethod(PaymentMethodInterface $payment_method, Request $request) {
$payment_method->card_type = 'visa';
$payment_method->card_number = '1111';
$payment_method->card_exp_month = '12';
$payment_method->card_exp_year = '26';
$expires = CreditCard::calculateExpirationTimestamp($payment_method->card_exp_month->value, $payment_method->card_exp_year->value);
$payment_method
->setExpiresTime($expires);
$payment_method
->setRemoteId('1234');
$payment_method
->save();
}
public function deletePaymentMethod(PaymentMethodInterface $payment_method) {
$payment_method
->delete();
}
public function createPayment(PaymentInterface $payment, $capture = TRUE) {
$this
->assertPaymentState($payment, [
'new',
]);
$payment_method = $payment
->getPaymentMethod();
$this
->assertPaymentMethod($payment_method);
$amount = $payment
->getAmount();
$payment_method_token = $payment_method
->getRemoteId();
$remote_id = '123456';
$next_state = $capture ? 'completed' : 'authorization';
$payment
->setState($next_state);
$payment
->setRemoteId($remote_id);
$payment
->save();
}
public function onReturn(OrderInterface $order, Request $request) {
$payment_method_storage = $this->entityTypeManager
->getStorage('commerce_payment_method');
assert($payment_method_storage instanceof PaymentMethodStorageInterface);
$payment_method = $payment_method_storage
->createForCustomer('credit_card', $this->parentEntity
->id(), $order
->getCustomerId(), $order
->getBillingProfile());
$this
->createPaymentMethod($payment_method, $request);
$payment_storage = $this->entityTypeManager
->getStorage('commerce_payment');
$payment = $payment_storage
->create([
'state' => 'completed',
'amount' => $order
->getBalance(),
'payment_gateway' => $this->parentEntity
->id(),
'order_id' => $order
->id(),
'remote_id' => $request->query
->get('txn_id'),
'remote_state' => $request->query
->get('payment_status'),
'payment_method' => $payment_method,
'avs_response_code' => 'Z',
]);
if (!$payment_method->card_type
->isEmpty()) {
$avs_response_code_label = $this
->buildAvsResponseCodeLabel('Z', $payment_method->card_type->value);
$payment
->setAvsResponseCodeLabel($avs_response_code_label);
}
$payment
->save();
}
public function buildAvsResponseCodeLabel($avs_response_code, $card_type) {
if ($card_type == 'dinersclub' || $card_type == 'jcb') {
if ($avs_response_code == 'Z') {
return $this
->t('Zip code.');
}
return NULL;
}
return parent::buildAvsResponseCodeLabel($avs_response_code, $card_type);
}
}