You are here

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

Updates the given payment method.

Parameters

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

Throws

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

Overrides SupportsUpdatingStoredPaymentMethodsInterface::updatePaymentMethod

File

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

Class

AcceptJs
Provides the Accept.js payment gateway.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

public function updatePaymentMethod(PaymentMethodInterface $payment_method) {
  $request = new UpdateCustomerPaymentProfileRequest($this->authnetConfiguration, $this->httpClient);
  $customer_profile_id = $this
    ->getRemoteCustomerId($payment_method
    ->getOwner());
  if (empty($customer_profile_id)) {
    $customer_profile_id = $this
      ->getPaymentMethodCustomerId($payment_method);
  }
  $request
    ->setCustomerProfileId($customer_profile_id);
  $payment_profile = new PaymentProfile([
    'customerType' => 'individual',
  ]);
  $payment_profile
    ->addCustomerPaymentProfileId($this
    ->getRemoteProfileId($payment_method));
  if ($billing_profile = $payment_method
    ->getBillingProfile()) {

    /** @var \Drupal\address\AddressInterface $address */
    $address = $billing_profile
      ->get('address')
      ->first();
    $bill_to = array_filter([
      'firstName' => $address
        ->getGivenName(),
      'lastName' => $address
        ->getFamilyName(),
      'company' => $address
        ->getOrganization(),
      'address' => substr($address
        ->getAddressLine1() . ' ' . $address
        ->getAddressLine2(), 0, 60),
      'country' => $address
        ->getCountryCode(),
      'city' => $address
        ->getLocality(),
      'state' => $address
        ->getAdministrativeArea(),
      'zip' => $address
        ->getPostalCode(),
    ]);
    $payment_profile
      ->addBillTo(new BillTo($bill_to));
  }
  $request
    ->setPaymentProfile($payment_profile);
  $payment_profile
    ->addPayment(new AuthnetCreditCard([
    'cardNumber' => 'XXXX' . $payment_method
      ->get('card_number')->value,
    'expirationDate' => sprintf('%s-%s', $payment_method
      ->get('card_exp_month')->value, $payment_method
      ->get('card_exp_year')->value),
  ]));
  $response = $request
    ->execute();
  if ($response
    ->getResultCode() != 'Ok') {
    $this
      ->logResponse($response);
    $error = $response
      ->getMessages()[0];
    throw new DeclineException('Unable to update the payment method.');
  }
}