View source
<?php
namespace Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway;
use Drupal\commerce_payment\CreditCard;
use Drupal\commerce_payment\Entity\PaymentInterface;
use Drupal\commerce_payment\Entity\PaymentMethodInterface;
use Drupal\commerce_payment\Exception\HardDeclineException;
use Drupal\commerce_payment\PaymentMethodTypeManager;
use Drupal\commerce_payment\PaymentTypeManager;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayBase;
use Drupal\commerce_price\MinorUnitsConverterInterface;
use Drupal\commerce_price\Price;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
class Onsite extends OnsitePaymentGatewayBase implements OnsiteInterface {
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) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time, $minor_units_converter);
}
public function defaultConfiguration() {
return [
'api_key' => '',
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['api_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('API key'),
'#default_value' => $this->configuration['api_key'],
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['api_key'] = $values['api_key'];
}
}
public function createPayment(PaymentInterface $payment, $capture = TRUE) {
$this
->assertPaymentState($payment, [
'new',
]);
$payment_method = $payment
->getPaymentMethod();
$this
->assertPaymentMethod($payment_method);
if ($billing_profile = $payment_method
->getBillingProfile()) {
$billing_address = $billing_profile
->get('address')
->first();
if ($billing_address
->getPostalCode() == '53140') {
throw new HardDeclineException('The payment was declined');
}
}
$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
->setAvsResponseCode('A');
if (!$payment_method->card_type
->isEmpty()) {
$avs_response_code_label = $this
->buildAvsResponseCodeLabel('A', $payment_method->card_type->value);
$payment
->setAvsResponseCodeLabel($avs_response_code_label);
}
$payment
->save();
}
public function capturePayment(PaymentInterface $payment, Price $amount = NULL) {
$this
->assertPaymentState($payment, [
'authorization',
]);
$amount = $amount ?: $payment
->getAmount();
$remote_id = $payment
->getRemoteId();
$number = $amount
->getNumber();
$payment
->setState('completed');
$payment
->setAmount($amount);
$payment
->save();
}
public function voidPayment(PaymentInterface $payment) {
$this
->assertPaymentState($payment, [
'authorization',
]);
$remote_id = $payment
->getRemoteId();
$payment
->setState('authorization_voided');
$payment
->save();
}
public function refundPayment(PaymentInterface $payment, Price $amount = NULL) {
$this
->assertPaymentState($payment, [
'completed',
'partially_refunded',
]);
$amount = $amount ?: $payment
->getAmount();
$this
->assertRefundAmount($payment, $amount);
$remote_id = $payment
->getRemoteId();
$number = $amount
->getNumber();
$old_refunded_amount = $payment
->getRefundedAmount();
$new_refunded_amount = $old_refunded_amount
->add($amount);
if ($new_refunded_amount
->lessThan($payment
->getAmount())) {
$payment
->setState('partially_refunded');
}
else {
$payment
->setState('refunded');
}
$payment
->setRefundedAmount($new_refunded_amount);
$payment
->save();
}
public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$required_keys = [
'type',
'number',
'expiration',
];
foreach ($required_keys as $required_key) {
if (empty($payment_details[$required_key])) {
throw new \InvalidArgumentException(sprintf('$payment_details must contain the %s key.', $required_key));
}
}
if ($billing_profile = $payment_method
->getBillingProfile()) {
$billing_address = $billing_profile
->get('address')
->first();
if ($billing_address
->getPostalCode() == '53141') {
throw new HardDeclineException('The payment method was declined');
}
}
$owner = $payment_method
->getOwner();
if ($owner && $owner
->isAuthenticated()) {
$customer_id = $this
->getRemoteCustomerId($owner);
}
$payment_method->card_type = $payment_details['type'];
$payment_method->card_number = substr($payment_details['number'], -4);
$payment_method->card_exp_month = $payment_details['expiration']['month'];
$payment_method->card_exp_year = $payment_details['expiration']['year'];
$expires = CreditCard::calculateExpirationTimestamp($payment_details['expiration']['month'], $payment_details['expiration']['year']);
$remote_id = '789';
$payment_method
->setRemoteId($remote_id);
$payment_method
->setExpiresTime($expires);
$payment_method
->save();
}
public function deletePaymentMethod(PaymentMethodInterface $payment_method) {
$payment_method
->delete();
}
public function updatePaymentMethod(PaymentMethodInterface $payment_method) {
}
public function buildAvsResponseCodeLabel($avs_response_code, $card_type) {
if ($card_type == 'dinersclub' || $card_type == 'jcb') {
if ($avs_response_code == 'A') {
return $this
->t('Approved.');
}
return NULL;
}
return parent::buildAvsResponseCodeLabel($avs_response_code, $card_type);
}
}