You are here

public function Payflow::createPayment in Commerce PayPal 8

Creates a payment.

Parameters

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

bool $capture: Whether the created payment should be captured (VS authorized only). Allowed to be FALSE only if the plugin supports authorizations.

Throws

\InvalidArgumentException If $capture is FALSE but the plugin does not support authorizations.

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

Overrides SupportsStoredPaymentMethodsInterface::createPayment

File

src/Plugin/Commerce/PaymentGateway/Payflow.php, line 354

Class

Payflow
Provides the PayPal Payflow payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function createPayment(PaymentInterface $payment, $capture = TRUE) {
  $this
    ->validatePayment($payment, 'new');
  try {
    $data = $this
      ->executeTransaction([
      'trxtype' => $capture ? 'S' : 'A',
      'amt' => $this->rounder
        ->round($payment
        ->getAmount())
        ->getNumber(),
      'currencycode' => $payment
        ->getAmount()
        ->getCurrencyCode(),
      'origid' => $payment
        ->getPaymentMethod()
        ->getRemoteId(),
      'verbosity' => 'HIGH',
    ]);
    if ($data['result'] !== '0') {
      throw new HardDeclineException('Could not charge the payment method. Response: ' . $data['respmsg'], $data['result']);
    }
    $next_state = $capture ? 'completed' : 'authorization';
    $payment
      ->setState($next_state);
    if (!$capture) {
      $payment
        ->setExpiresTime($this->time
        ->getRequestTime() + 86400 * 29);
    }
    $payment
      ->setRemoteId($this
      ->prepareRemoteId($data))
      ->setRemoteState('3')
      ->save();
  } catch (RequestException $e) {
    throw new HardDeclineException('Could not charge the payment method.');
  }
}