You are here

public function OnsiteBase::deletePaymentMethod in Commerce Authorize.Net 8

@todo Needs kernel test

Overrides SupportsStoredPaymentMethodsInterface::deletePaymentMethod

File

src/Plugin/Commerce/PaymentGateway/OnsiteBase.php, line 275

Class

OnsiteBase
Provides the Authorize.net payment gateway base class.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

public function deletePaymentMethod(PaymentMethodInterface $payment_method) {
  $owner = $payment_method
    ->getOwner();
  $customer_id = $this
    ->getRemoteCustomerId($owner);
  if (empty($customer_id)) {
    $customer_id = $this
      ->getPaymentMethodCustomerId($payment_method);
  }
  $request = new DeleteCustomerPaymentProfileRequest($this->authnetConfiguration, $this->httpClient);
  $request
    ->setCustomerProfileId($customer_id);
  $request
    ->setCustomerPaymentProfileId($this
    ->getRemoteProfileId($payment_method));
  $response = $request
    ->execute();
  if ($response
    ->getResultCode() != 'Ok') {
    $this
      ->logResponse($response);
    $message = $response
      ->getMessages()[0];

    // If the error is not "record not found" throw an error.
    if ($message
      ->getCode() != 'E00040') {
      throw new InvalidResponseException("Unable to delete payment method");
    }
  }
  $payment_method
    ->delete();
}