You are here

public function Payflow::createPaymentMethod in Commerce PayPal 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/Payflow.php, line 496

Class

Payflow
Provides the PayPal Payflow payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

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

    /** @var \Drupal\address\Plugin\Field\FieldType\AddressItem $address */
    $address = $payment_method
      ->getBillingProfile()
      ->get('address')
      ->first();
    $data = $this
      ->executeTransaction([
      'trxtype' => 'A',
      'amt' => 0,
      'verbosity' => 'HIGH',
      'acct' => $payment_details['number'],
      'expdate' => $this
        ->getExpirationDate($payment_details),
      'cvv2' => $payment_details['security_code'],
      'billtoemail' => $payment_method
        ->getOwner()
        ->getEmail(),
      'billtofirstname' => $address
        ->getGivenName(),
      'billtolastname' => $address
        ->getFamilyName(),
      'billtostreet' => $address
        ->getAddressLine1(),
      'billtocity' => $address
        ->getLocality(),
      'billtostate' => $address
        ->getAdministrativeArea(),
      'billtozip' => $address
        ->getPostalCode(),
      'billtocountry' => $address
        ->getCountryCode(),
    ]);
    if ($data['result'] !== '0') {
      throw new HardDeclineException("Unable to verify the credit card: " . $data['respmsg'], $data['result']);
    }
    $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']);

    // Store the remote ID returned by the request.
    $payment_method
      ->setRemoteId($data['pnref'])
      ->setExpiresTime($expires)
      ->save();
  } catch (RequestException $e) {
    throw new HardDeclineException("Unable to store the credit card");
  }
}