You are here

public function PayflowLink::referencePayment in Commerce PayPal 8

Creates a reference payment.

Parameters

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

\Drupal\commerce_price\Price|null $amount: The amount to use in reference payment.

Throws

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

Overrides PayflowLinkInterface::referencePayment

File

src/Plugin/Commerce/PaymentGateway/PayflowLink.php, line 658

Class

PayflowLink
Provides the PayPal Payflow Link payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function referencePayment(PaymentInterface $payment, Price $amount = NULL) {
  $amount = $amount ?: $payment
    ->getAmount();
  $order = $payment
    ->getOrder();

  // Prepare a name-value pair array to capture the requested amount.
  $nvp = [
    'BUTTONSOURCE' => self::BUTTON_SOURCE,
    'TRXTYPE' => 'S',
    'ORIGID' => $payment
      ->getRemoteId(),
    'AMT' => Calculator::trim($amount
      ->getNumber()),
    'TENDER' => 'C',
  ];

  // Submit the reference transaction request to Payflow Pro.
  $response = $this
    ->apiRequest('pro', $nvp, $order);
  if (isset($response['RESULT']) && intval($response['RESULT']) === 0) {

    // Create a new transaction to represent the reference transaction.

    /** @var \Drupal\commerce_payment\Entity\PaymentInterface $new_payment */
    $new_payment = $this->entityTypeManager
      ->getStorage('commerce_payment')
      ->create([
      'state' => 'completed',
      'amount' => $amount,
      'payment_gateway' => $payment
        ->getPaymentGatewayId(),
      'order_id' => $order
        ->id(),
      'remote_id' => $response['PNREF'] ?? '',
      'remote_state' => 'S',
    ]);
    $new_payment
      ->save();
  }
  else {
    throw new PaymentGatewayException($this
      ->t('Reference transaction failed: @reason.', [
      '@reason' => $response['RESPMSG'],
    ]), $response['RESULT']);
  }
}