You are here

class PaymentMethodAddForm in Commerce Authorize.Net 8

Hierarchy

Expanded class hierarchy of PaymentMethodAddForm

File

src/PluginForm/AcceptJs/PaymentMethodAddForm.php, line 19

Namespace

Drupal\commerce_authnet\PluginForm\AcceptJs
View source
class PaymentMethodAddForm extends BasePaymentMethodAddForm {

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new PaymentMethodFormBase.
   *
   * @param \Drupal\commerce_store\CurrentStoreInterface $current_store
   *   The current store.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\InlineFormManager $inline_form_manager
   *   The inline form manager.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   */
  public function __construct(CurrentStoreInterface $current_store, EntityTypeManagerInterface $entity_type_manager, InlineFormManager $inline_form_manager, LoggerInterface $logger, RouteMatchInterface $route_match) {
    parent::__construct($current_store, $entity_type_manager, $inline_form_manager, $logger);
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('commerce_store.current_store'), $container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.commerce_inline_form'), $container
      ->get('logger.channel.commerce_payment'), $container
      ->get('current_route_match'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildCreditCardForm(array $element, FormStateInterface $form_state) {

    // Alter the form with AuthorizeNet Accept JS specific needs.
    $element['#attributes']['class'][] = 'authorize-net-accept-js-form';

    /** @var \Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway\AcceptJs $plugin */
    $plugin = $this->plugin;
    if ($plugin
      ->getMode() == 'test') {
      $element['#attached']['library'][] = 'commerce_authnet/accept-js-sandbox';
    }
    else {
      $element['#attached']['library'][] = 'commerce_authnet/accept-js-production';
    }

    // @todo Remove this line when
    // https://www.drupal.org/project/commerce/issues/2986599 gets fixed.
    $element['#attached']['library'][] = 'commerce_authnet/form-accept';
    $element['#attached']['drupalSettings']['commerceAuthorizeNet'] = [
      'clientKey' => $plugin
        ->getConfiguration()['client_key'],
      'apiLoginID' => $plugin
        ->getConfiguration()['api_login'],
      'paymentMethodType' => 'credit_card',
      'ccaStatus' => 0,
      'mode' => $plugin
        ->getMode(),
      'gatewayId' => $this
        ->getEntity()
        ->getPaymentGatewayId(),
    ];

    // Fields placeholder to be built by the JS.
    $element['number'] = [
      '#type' => 'textfield',
      '#title' => t('Card number'),
      '#attributes' => [
        'placeholder' => '•••• •••• •••• ••••',
        'autocomplete' => 'off',
        'autocorrect' => 'off',
        'autocapitalize' => 'none',
        'id' => 'credit-card-number',
        'required' => 'required',
      ],
      '#label_attributes' => [
        'class' => [
          'js-form-required',
          'form-required',
        ],
      ],
      '#maxlength' => 20,
      '#size' => 20,
    ];
    $element['expiration'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'credit-card-form__expiration',
        ],
      ],
    ];
    $element['expiration']['month'] = [
      '#type' => 'textfield',
      '#title' => t('Month'),
      '#attributes' => [
        'placeholder' => 'MM',
        'autocomplete' => 'off',
        'autocorrect' => 'off',
        'autocapitalize' => 'none',
        'id' => 'expiration-month',
        'required' => 'required',
      ],
      '#label_attributes' => [
        'class' => [
          'js-form-required',
          'form-required',
        ],
      ],
      '#maxlength' => 2,
      '#size' => 3,
    ];
    $element['expiration']['divider'] = [
      '#type' => 'item',
      '#title' => '',
      '#markup' => '<span class="credit-card-form__divider">/</span>',
    ];
    $element['expiration']['year'] = [
      '#type' => 'textfield',
      '#title' => t('Year'),
      '#attributes' => [
        'placeholder' => 'YY',
        'autocomplete' => 'off',
        'autocorrect' => 'off',
        'autocapitalize' => 'none',
        'id' => 'expiration-year',
        'required' => 'required',
      ],
      '#label_attributes' => [
        'class' => [
          'js-form-required',
          'form-required',
        ],
      ],
      '#maxlength' => 2,
      '#size' => 3,
    ];
    $element['security_code'] = [
      '#type' => 'textfield',
      '#title' => t('CVV'),
      '#attributes' => [
        'placeholder' => '•••',
        'autocomplete' => 'off',
        'autocorrect' => 'off',
        'autocapitalize' => 'none',
        'id' => 'cvv',
        'required' => 'required',
      ],
      '#label_attributes' => [
        'class' => [
          'js-form-required',
          'form-required',
        ],
      ],
      '#maxlength' => 4,
      '#size' => 4,
    ];

    // To display validation errors.
    $element['payment_errors'] = [
      '#type' => 'markup',
      '#markup' => '<div id="payment-errors"></div>',
      '#weight' => -200,
    ];

    // Populated by the JS library after receiving a response from AuthorizeNet.
    $element['data_descriptor'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'accept-js-data-descriptor',
        ],
      ],
    ];
    $element['data_value'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'accept-js-data-value',
        ],
      ],
    ];
    $element['last4'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'accept-js-data-last4',
        ],
      ],
    ];
    $element['expiration_month'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'accept-js-data-month',
        ],
      ],
    ];
    $element['expiration_year'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'accept-js-data-year',
        ],
      ],
    ];

    /** @var \Drupal\commerce_order\Entity\Order $order */
    if ($order = $this->routeMatch
      ->getParameter('commerce_order')) {
      if ($plugin
        ->getConfiguration()['cca_status']) {
        $element['cca_jwt_token'] = [
          '#type' => 'hidden',
          '#attributes' => [
            'class' => [
              'accept-js-data-cca-jwt-token',
            ],
          ],
          '#value' => (string) $this
            ->generateJwt(),
        ];
        $element['cca_jwt_response_token'] = [
          '#type' => 'hidden',
          '#attributes' => [
            'class' => [
              'accept-js-data-cca-jwt-response-token',
            ],
          ],
        ];
        $element['#attached']['drupalSettings']['commerceAuthorizeNet']['orderId'] = $order
          ->id();
        $element['#attached']['drupalSettings']['commerceAuthorizeNet']['orderAmount'] = $this
          ->toMinorUnits($order
          ->getTotalPrice());
        $element['#attached']['drupalSettings']['commerceAuthorizeNet']['orderCurrency'] = $order
          ->getTotalPrice()
          ->getCurrencyCode();
        $element['#attached']['drupalSettings']['commerceAuthorizeNet']['ccaStatus'] = 1;
      }
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  protected function validateCreditCardForm(array &$element, FormStateInterface $form_state) {

    // The JS library performs its own validation.
  }

  /**
   * {@inheritdoc}
   */
  public function submitCreditCardForm(array $element, FormStateInterface $form_state) {

    // The payment gateway plugin will process the submitted payment details.
    $values = $form_state
      ->getValues();
    if (!empty($values['contact_information']['email'])) {

      // Then we are dealing with anonymous user. Adding a customer email.
      $payment_details = $values['payment_information']['add_payment_method']['payment_details'];
      $payment_details['customer_email'] = $values['contact_information']['email'];
      $form_state
        ->setValue([
        'payment_information',
        'add_payment_method',
        'payment_details',
      ], $payment_details);
    }
  }

  /**
   * Create JWT token for CCA.
   *
   * @return \Lcobucci\JWT\Token
   *   The JWT token.
   */
  protected function generateJwt() {
    $current_time = time();
    $expire_time = 3600;

    /** @var \Drupal\commerce_order\Entity\Order $order */
    if ($order = $this->routeMatch
      ->getParameter('commerce_order')) {
      $order_details = [
        'OrderDetails' => [
          'OrderNumber' => $order
            ->getOrderNumber(),
        ],
      ];
    }

    /** @var \Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway\AcceptJs $plugin */
    $plugin = $this->plugin;
    $token = (new Builder())
      ->issuedBy($plugin
      ->getCcaApiId())
      ->identifiedBy(uniqid(), TRUE)
      ->issuedAt($current_time)
      ->expiresAt($current_time + $expire_time)
      ->withClaim('OrgUnitId', $plugin
      ->getCcaOrgUnitId())
      ->withClaim('Payload', $order_details)
      ->withClaim('ObjectifyPayload', TRUE)
      ->getToken(new Sha256(), new Key($plugin
      ->getCcaApiKey()));
    return $token;
  }

  /**
   * Converts the given amount to its minor units.
   *
   * This is a copypaste of PaymentGatewayBase::toMinorUnits() until that
   * method is made public.
   *
   * @todo Remove when https://www.drupal.org/project/commerce/issues/2944281
   * gets fixed.
   *
   * @param \Drupal\commerce_price\Price $amount
   *   The amount.
   *
   * @return int
   *   The amount in minor units, as an integer.
   */
  protected function toMinorUnits(Price $amount) {
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $currency_storage = $entity_type_manager
      ->getStorage('commerce_currency');

    /** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
    $currency = $currency_storage
      ->load($amount
      ->getCurrencyCode());
    $fraction_digits = $currency
      ->getFractionDigits();
    $number = $amount
      ->getNumber();
    if ($fraction_digits > 0) {
      $number = Calculator::multiply($number, pow(10, $fraction_digits));
    }
    return round($number, 0);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
PaymentGatewayFormBase::$entity protected property The form entity.
PaymentGatewayFormBase::getEntity public function Gets the form entity. Overrides PaymentGatewayFormInterface::getEntity
PaymentGatewayFormBase::setEntity public function Sets the form entity. Overrides PaymentGatewayFormInterface::setEntity
PaymentMethodAddForm::$routeMatch protected property The route match.
PaymentMethodAddForm::buildConfigurationForm public function Form constructor. Overrides PaymentMethodFormBase::buildConfigurationForm
PaymentMethodAddForm::buildCreditCardForm public function Builds the credit card form. Overrides PaymentMethodAddForm::buildCreditCardForm
PaymentMethodAddForm::buildPayPalForm protected function Builds the PayPal form.
PaymentMethodAddForm::create public static function Instantiates a new instance of this class. Overrides PaymentMethodFormBase::create
PaymentMethodAddForm::generateJwt protected function Create JWT token for CCA.
PaymentMethodAddForm::getErrorElement public function Gets the form element to which errors should be assigned. Overrides PaymentGatewayFormBase::getErrorElement
PaymentMethodAddForm::submitConfigurationForm public function Form submission handler. Overrides PaymentMethodFormBase::submitConfigurationForm
PaymentMethodAddForm::submitCreditCardForm public function Handles the submission of the credit card form. Overrides PaymentMethodAddForm::submitCreditCardForm
PaymentMethodAddForm::submitPayPalForm protected function Handles the submission of the PayPal form.
PaymentMethodAddForm::toMinorUnits protected function Converts the given amount to its minor units.
PaymentMethodAddForm::validateConfigurationForm public function Form validation handler. Overrides PaymentMethodFormBase::validateConfigurationForm
PaymentMethodAddForm::validateCreditCardForm protected function Validates the credit card form. Overrides PaymentMethodAddForm::validateCreditCardForm
PaymentMethodAddForm::validatePayPalForm protected function Validates the PayPal form.
PaymentMethodAddForm::__construct public function Constructs a new PaymentMethodFormBase. Overrides PaymentMethodFormBase::__construct
PaymentMethodFormBase::$currentStore protected property The current store.
PaymentMethodFormBase::$entityTypeManager protected property The entity type manager.
PaymentMethodFormBase::$inlineFormManager protected property The inline form manager.
PaymentMethodFormBase::$logger protected property The logger.
PluginFormBase::$plugin protected property The plugin this form is for. 3
PluginFormBase::setPlugin public function Sets the plugin for this object. Overrides PluginAwareInterface::setPlugin 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.