You are here

public function VisaCheckout::onReturn in Commerce Authorize.Net 8

Processes the "return" request.

This method should only be concerned with creating/completing payments, the parent order does not need to be touched. The order state is updated automatically when the order is paid in full, or manually by the merchant (via the admin UI).

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

\Symfony\Component\HttpFoundation\Request $request: The request.

Throws

\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the request is invalid or the payment failed.

Overrides OffsitePaymentGatewayBase::onReturn

File

src/Plugin/Commerce/PaymentGateway/VisaCheckout.php, line 206

Class

VisaCheckout
Provides the Authorize.net payment gateway.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

public function onReturn(OrderInterface $order, Request $request) {
  if ($request->request
    ->has('error')) {
    $error = $request->request
      ->get('error');
    return;
  }

  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
  $payment = $request->request
    ->get('payment');
  $opaque_data_values = [
    'dataDescriptor' => OpaqueData::VISA_CHECKOUT,
    'dataValue' => $payment['encPaymentData'],
    'dataKey' => $payment['encKey'],
  ];
  $opaque_data = new OpaqueData($opaque_data_values);
  $decrypt_payment_data_request = new DecryptPaymentDataRequest($this->authnetConfiguration, $this->httpClient, $opaque_data, '', $payment['callid']);
  $response = $decrypt_payment_data_request
    ->execute();
  if ($response
    ->getResultCode() == 'Ok') {

    /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */
    $checkout_flow = $order
      ->get('checkout_flow')->entity;
    $capture = $checkout_flow
      ->getPlugin()
      ->getConfiguration()['panes']['payment_process']['capture'];
    $values = [
      'transactionType' => $capture ? 'authCaptureTransaction' : 'authOnlyTransaction',
      'amount' => $response
        ->contents()->paymentDetails->amount,
      'callId' => $payment['callid'],
    ];
    $transaction_request = new TransactionRequest($values);
    $payment_data = [
      'opaqueData' => $opaque_data_values,
    ];
    $transaction_request
      ->addData('payment', $payment_data);
    $retail = [
      'marketType' => 0,
      'deviceType' => 5,
    ];
    $transaction_request
      ->addData('retail', $retail);
    $create_transaction_request = new CreateTransactionRequest($this->authnetConfiguration, $this->httpClient);
    $create_transaction_request
      ->setTransactionRequest($transaction_request);
    $create_transaction_response = $create_transaction_request
      ->execute();
    if ($create_transaction_response
      ->getResultCode() != 'Ok') {
      $this
        ->logResponse($create_transaction_response);
      $message = $create_transaction_response
        ->getMessages()[0];
      throw new PaymentGatewayException($message
        ->getText());
    }
    if (!empty($create_transaction_response
      ->getErrors())) {
      $message = $create_transaction_response
        ->getErrors()[0];
      throw new HardDeclineException($message
        ->getText());
    }
    $payment_storage = $this->entityTypeManager
      ->getStorage('commerce_payment');
    $payment = $payment_storage
      ->create([
      'state' => $capture ? 'completed' : 'authorization',
      'amount' => new Price($response
        ->contents()->paymentDetails->amount, $response
        ->contents()->paymentDetails->currency),
      'payment_gateway' => $this->entityId,
      'order_id' => $order
        ->id(),
      'test' => $this
        ->getMode() == 'test',
      'remote_id' => $create_transaction_response
        ->contents()->transactionResponse->transId,
      'remote_state' => $create_transaction_response
        ->contents()->messages->message->text,
      'authorized' => $this->time
        ->getRequestTime(),
    ]);
    $payment
      ->save();
  }
}