You are here

public function StoredOffsiteRedirect::onReturn in Commerce Core 8.2

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 OffsiteRedirect::onReturn

File

modules/payment_example/src/Plugin/Commerce/PaymentGateway/StoredOffsiteRedirect.php, line 139

Class

StoredOffsiteRedirect
Provides an example offsite payment gateway with stored payment methods.

Namespace

Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway

Code

public function onReturn(OrderInterface $order, Request $request) {

  // This off-site payment gateway example creates a payment method as a
  // part of processing the payment data returned from the gateway.
  $payment_method_storage = $this->entityTypeManager
    ->getStorage('commerce_payment_method');
  assert($payment_method_storage instanceof PaymentMethodStorageInterface);
  $payment_method = $payment_method_storage
    ->createForCustomer('credit_card', $this->parentEntity
    ->id(), $order
    ->getCustomerId(), $order
    ->getBillingProfile());

  // The payment method is created first so that it can be attached to the
  // generated payment transaction.
  $this
    ->createPaymentMethod($payment_method, $request);
  $payment_storage = $this->entityTypeManager
    ->getStorage('commerce_payment');
  $payment = $payment_storage
    ->create([
    'state' => 'completed',
    'amount' => $order
      ->getBalance(),
    'payment_gateway' => $this->parentEntity
      ->id(),
    'order_id' => $order
      ->id(),
    'remote_id' => $request->query
      ->get('txn_id'),
    'remote_state' => $request->query
      ->get('payment_status'),
    'payment_method' => $payment_method,
    'avs_response_code' => 'Z',
  ]);
  if (!$payment_method->card_type
    ->isEmpty()) {
    $avs_response_code_label = $this
      ->buildAvsResponseCodeLabel('Z', $payment_method->card_type->value);
    $payment
      ->setAvsResponseCodeLabel($avs_response_code_label);
  }
  $payment
    ->save();
}