You are here

public function PayflowLink::onReturn in Commerce PayPal 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/PayflowLink.php, line 417

Class

PayflowLink
Provides the PayPal Payflow Link payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

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

  /** @var \Symfony\Component\HttpFoundation\ParameterBag $parameter_bag */
  $parameter_bag = $request->request;
  $received_parameters = $parameter_bag
    ->all();
  $configuration = $this
    ->getConfiguration();
  $payment_storage = $this->entityTypeManager
    ->getStorage('commerce_payment');
  if (!empty($configuration['silent_post_logging']) && $configuration['silent_post_logging'] == 'full_post') {
    $this->logger
      ->notice('Customer returned from Payflow with the following POST data: !data', [
      '!data' => '<pre>' . Html::escape(print_r($received_parameters, TRUE)) . '</pre>',
    ]);
  }
  if (!empty($configuration['log']['response'])) {
    $this->logger
      ->notice('Payflow server response: @response', [
      '@response' => new FormattableMarkup('<pre>' . print_r($received_parameters, 1) . '</pre>', []),
    ]);
  }
  if (isset($received_parameters['RESULT']) && !in_array(intval($received_parameters['RESULT']), [
    0,
    126,
  ])) {
    $message = $this
      ->resultMessage($received_parameters['RESULT']);
    throw new PaymentGatewayException($message);
  }

  // Determine the type of transaction.
  if (!empty($received_parameters['TRXTYPE'])) {
    $trxtype = $received_parameters['TRXTYPE'];
  }
  elseif (!empty($received_parameters['TYPE'])) {
    $trxtype = $received_parameters['TYPE'];
  }
  else {
    $trxtype = $configuration['trxtype'];
  }
  $state = '';

  // Set the transaction status based on the type of transaction this was.
  if (intval($received_parameters['RESULT']) == 0) {
    switch ($trxtype) {
      case 'S':
        $state = 'completed';
        break;
      case 'A':
      default:
        $state = 'pending';
        break;
    }
  }
  elseif (intval($received_parameters['RESULT']) == 126) {
    $state = 'pending';
  }
  $commerce_payment = $payment_storage
    ->create([
    'state' => $state,
    'amount' => $order
      ->getBalance(),
    'payment_gateway' => $this->parentEntity
      ->id(),
    'order_id' => $order
      ->id(),
    'remote_id' => $received_parameters['PNREF'],
    'remote_state' => $trxtype,
  ]);
  if (!empty($received_parameters['PENDINGREASON']) && $received_parameters['PENDINGREASON'] != 'completed') {

    // And ensure the local and remote status are pending.
    $commerce_payment
      ->setState('pending');
  }
  $commerce_payment
    ->save();
}