You are here

public function HostedFields::createPaymentMethod in Commerce Braintree 8

Creates a payment method with the given payment details.

Parameters

\Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method: The payment method.

array $payment_details: The gateway-specific payment details provided by the payment method form for on-site gateways, or the incoming request for off-site gateways.

Throws

\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the transaction fails for any reason.

Overrides SupportsCreatingPaymentMethodsInterface::createPaymentMethod

File

src/Plugin/Commerce/PaymentGateway/HostedFields.php, line 333

Class

HostedFields
Provides the HostedFields payment gateway.

Namespace

Drupal\commerce_braintree\Plugin\Commerce\PaymentGateway

Code

public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
  $payment_method_type = $payment_method
    ->getType()
    ->getPluginId();
  $required_keys = [
    'payment_method_nonce',
  ];
  $paypal_payment_method_types = [
    'paypal',
    'paypal_credit',
  ];
  if (!in_array($payment_method_type, $paypal_payment_method_types)) {
    $required_keys += [
      'card_type',
      'last2',
    ];
  }

  // Use PaymentGatewayException instead of InvalidArgumentException to handle missing data items.
  foreach ($required_keys as $required_key) {
    if (empty($payment_details[$required_key])) {
      throw new PaymentGatewayException(sprintf('In HostedFields::createPaymentMethod(), $payment_details must contain the %s key.', $required_key));
    }
  }
  if (!$payment_method
    ->isReusable()) {
    $payment_method->card_type = $this
      ->mapCreditCardType($payment_details['card_type']);
    $payment_method->card_number = $payment_details['last2'];
    $remote_id = $payment_details['payment_method_nonce'];

    // Nonces expire after 3h. We reduce that time by 5s to account for the
    // time it took to do the server request after the JS tokenization.
    $expires = $this->time
      ->getRequestTime() + 3600 * 3 - 5;
  }
  else {
    $remote_payment_method = $this
      ->doCreatePaymentMethod($payment_method, $payment_details);
    $remote_id = $remote_payment_method['token'];
    if (in_array($payment_method_type, $paypal_payment_method_types)) {
      $payment_method->paypal_mail = $remote_payment_method['email'];
      $expires = 0;
    }
    else {
      $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'];
      $expires = CreditCard::calculateExpirationTimestamp($remote_payment_method['expiration_month'], $remote_payment_method['expiration_year']);
    }
  }
  $payment_method
    ->setRemoteId($remote_id);
  $payment_method
    ->setExpiresTime($expires);
  $payment_method
    ->save();
}