You are here

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

Refunds the given payment.

Parameters

\Drupal\commerce_payment\Entity\PaymentInterface $payment: The payment to refund.

\Drupal\commerce_price\Price $amount: The amount to refund. If NULL, defaults to the entire payment amount.

Throws

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

Overrides SupportsRefundsInterface::refundPayment

File

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

Class

AcceptJs
Provides the Accept.js payment gateway.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

public function refundPayment(PaymentInterface $payment, Price $amount = NULL) {

  /** @var \Drupal\commerce_payment\Entity\PaymentMethod $payment_method */
  $payment_method = $payment
    ->getPaymentMethod();
  $this
    ->assertPaymentState($payment, [
    'completed',
    'partially_refunded',
  ]);

  // If not specified, refund the entire amount.
  $amount = $amount ?: $payment
    ->getAmount();
  $this
    ->assertRefundAmount($payment, $amount);
  $request = new CreateTransactionRequest($this->authnetConfiguration, $this->httpClient);
  $transaction_request = new TransactionRequest([
    'transactionType' => TransactionRequest::REFUND,
    'amount' => $amount
      ->getNumber(),
    'refTransId' => $payment
      ->getRemoteId(),
  ]);

  // Add billing information when available, to satisfy AVS.
  if ($billing_profile = $payment_method
    ->getBillingProfile()) {

    /** @var \Drupal\address\AddressInterface $address */
    $address = $billing_profile
      ->get('address')
      ->first();
    $bill_to = array_filter([
      // @todo how to allow customizing this.
      '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(),
    ]);
    $transaction_request
      ->addDataType(new BillTo($bill_to));
  }

  // Adding order information to the transaction.
  $order = $payment
    ->getOrder();
  $transaction_request
    ->addOrder(new OrderDataType([
    'invoiceNumber' => $order
      ->getOrderNumber() ?: $order
      ->id(),
  ]));
  $transaction_request
    ->addPayment(new CreditCardDataType([
    'cardNumber' => $payment_method->card_number->value,
    'expirationDate' => str_pad($payment_method->card_exp_month->value, 2, '0', STR_PAD_LEFT) . str_pad($payment_method->card_exp_year->value, 2, '0', STR_PAD_LEFT),
  ]));
  $request
    ->setTransactionRequest($transaction_request);
  $response = $request
    ->execute();
  if ($response
    ->getResultCode() != 'Ok') {
    $this
      ->logResponse($response);
    $message = $response
      ->getMessages()[0];
    throw new PaymentGatewayException($message
      ->getText());
  }
  $old_refunded_amount = $payment
    ->getRefundedAmount();
  $new_refunded_amount = $old_refunded_amount
    ->add($amount);
  if ($new_refunded_amount
    ->lessThan($payment
    ->getAmount())) {
    $payment
      ->setState('partially_refunded');
  }
  else {
    $payment
      ->setState('refunded');
  }
  $payment
    ->setRefundedAmount($new_refunded_amount);
  $payment
    ->save();
}