You are here

public function Onsite::createPaymentMethod in Commerce Core 8.2

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

modules/payment_example/src/Plugin/Commerce/PaymentGateway/Onsite.php, line 187

Class

Onsite
Provides the On-site payment gateway.

Namespace

Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway

Code

public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
  $required_keys = [
    // The expected keys are payment gateway specific and usually match
    // the PaymentMethodAddForm form elements. They are expected to be valid.
    '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));
    }
  }

  // Add a built in test for testing decline exceptions.
  // Note: Since requires_billing_information is FALSE, the payment method
  // is not guaranteed to have a billing profile. Confirm tha
  // $payment_method->getBillingProfile() is not NULL before trying to use it.
  if ($billing_profile = $payment_method
    ->getBillingProfile()) {

    /** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $billing_address */
    $billing_address = $billing_profile
      ->get('address')
      ->first();
    if ($billing_address
      ->getPostalCode() == '53141') {
      throw new HardDeclineException('The payment method was declined');
    }
  }

  // If the remote API needs a remote customer to be created.
  $owner = $payment_method
    ->getOwner();
  if ($owner && $owner
    ->isAuthenticated()) {
    $customer_id = $this
      ->getRemoteCustomerId($owner);

    // If $customer_id is empty, create the customer remotely and then do
    // $this->setRemoteCustomerId($owner, $customer_id);
    // $owner->save();
  }

  // Perform the create request here, throw an exception if it fails.
  // See \Drupal\commerce_payment\Exception for the available exceptions.
  // You might need to do different API requests based on whether the
  // payment method is reusable: $payment_method->isReusable().
  // Non-reusable payment methods usually have an expiration timestamp.
  $payment_method->card_type = $payment_details['type'];

  // Only the last 4 numbers are safe to store.
  $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']);

  // The remote ID returned by the request.
  $remote_id = '789';
  $payment_method
    ->setRemoteId($remote_id);
  $payment_method
    ->setExpiresTime($expires);
  $payment_method
    ->save();
}