You are here

public function AcceptJs::createPaymentMethod in Commerce Authorize.Net 8

@todo Needs kernel test

Overrides SupportsCreatingPaymentMethodsInterface::createPaymentMethod

File

src/Plugin/Commerce/PaymentGateway/AcceptJs.php, line 487

Class

AcceptJs
Provides the Accept.js payment gateway.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {

  // We don't want 3DS on the user payment method form.
  if (!empty($this
    ->getConfiguration()['cca_status']) && !empty($payment_details['cca_jwt_token'])) {
    if (empty($payment_details['cca_jwt_response_token'])) {
      throw new PaymentGatewayException('Cannot continue when CCA is enabled but not used.');
    }

    /** @var \Lcobucci\JWT\Token $token */
    $token = (new Parser())
      ->parse($payment_details['cca_jwt_response_token']);
    $signer = new Sha256();
    if (!$token
      ->verify($signer, $this
      ->getCcaApiKey())) {
      throw new PaymentGatewayException('Response CCA JWT is not valid.');
    }
    $claims = $token
      ->getClaims();

    /** @var \Lcobucci\JWT\Claim $payload */
    $payload = $claims['Payload'];
    if (isset($payload
      ->getValue()->Payment->ExtendedData->SignatureVerification) && $payload
      ->getValue()->Payment->ExtendedData->SignatureVerification === 'N') {
      throw new PaymentGatewayException('Unsuccessful signature verification.');
    }
  }
  $required_keys = [
    'data_descriptor',
    'data_value',
  ];
  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));
    }
  }
  $remote_payment_method = $this
    ->doCreatePaymentMethod($payment_method, $payment_details);
  $payment_method->card_type = $this
    ->mapCreditCardType($remote_payment_method['card_type']);
  $payment_method->card_number = $remote_payment_method['last4'];
  $payment_method->card_exp_month = $remote_payment_method['expiration_month'];
  $payment_method->card_exp_year = $remote_payment_method['expiration_year'];
  $payment_method
    ->setRemoteId($remote_payment_method['remote_id']);
  $expires = CreditCard::calculateExpirationTimestamp($remote_payment_method['expiration_month'], $remote_payment_method['expiration_year']);
  $payment_method
    ->setExpiresTime($expires);
  $payment_method
    ->save();
  if (!empty($this
    ->getConfiguration()['cca_status']) && !empty($payment_details['cca_jwt_token'])) {
    $value = [];
    if (isset($payload
      ->getValue()->Payment->ExtendedData->CAVV)) {
      $value['cavv'] = $payload
        ->getValue()->Payment->ExtendedData->CAVV;
      $this->privateTempStore
        ->get('commerce_authnet')
        ->set($payment_method
        ->id(), $value);
    }
    if (isset($payload
      ->getValue()->Payment->ExtendedData->ECIFlag)) {
      $value['eci'] = $payload
        ->getValue()->Payment->ExtendedData->ECIFlag;
      $this->privateTempStore
        ->get('commerce_authnet')
        ->set($payment_method
        ->id(), $value);
    }
  }
}