You are here

protected function PayPalBasic::doExecutePayment in PayPal for Payment 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Payment/Method/PayPalBasic.php \Drupal\paypal_payment\Plugin\Payment\Method\PayPalBasic::doExecutePayment()

Performs the actual payment execution.

Overrides Basic::doExecutePayment

File

src/Plugin/Payment/Method/PayPalBasic.php, line 79

Class

PayPalBasic
Abstract class for PayPal payment methods.

Namespace

Drupal\paypal_payment\Plugin\Payment\Method

Code

protected function doExecutePayment() {
  parent::doExecutePayment();
  $payer = new Payer();
  $payer
    ->setPaymentMethod('paypal');
  $itemList = new ItemList();
  $totalAmount = 0;
  $currency = self::PAYPAL_DEFAULT_CURRENCY;
  foreach ($this
    ->getPayment()
    ->getLineItems() as $line_item) {
    $totalAmount += $line_item
      ->getTotalAmount();
    $line_item_currency = $line_item
      ->getCurrencyCode();
    $item = new Item();
    $item
      ->setName($line_item
      ->getName())
      ->setCurrency($line_item_currency)
      ->setQuantity($line_item
      ->getQuantity())
      ->setPrice($line_item
      ->getAmount());
    $itemList
      ->addItem($item);
    if ($line_item_currency !== $currency) {
      if ($currency !== self::PAYPAL_DEFAULT_CURRENCY) {

        // This is the second time we are changing the currency which means
        // that our line items have mixed currencies. This aion't gonna work!

        # TODO: clarify with the payment maintainer how we should handle this
        $this
          ->messenger()
          ->addError($this
          ->t('Mixed currencies detected which is not yet supported.'));
        return;
      }
      $currency = $line_item_currency;
    }
  }
  $redirectSuccess = new Url('paypal_payment.redirect.success', [
    'payment' => $this
      ->getPayment()
      ->id(),
  ], [
    'absolute' => TRUE,
  ]);
  $redirectCancel = new Url('paypal_payment.redirect.cancel', [
    'payment' => $this
      ->getPayment()
      ->id(),
  ], [
    'absolute' => TRUE,
  ]);
  $redirectUrls = new RedirectUrls();
  $redirectUrls
    ->setReturnUrl($redirectSuccess
    ->toString(TRUE)
    ->getGeneratedUrl())
    ->setCancelUrl($redirectCancel
    ->toString(TRUE)
    ->getGeneratedUrl());
  $amount = new Amount();
  $amount
    ->setCurrency($currency)
    ->setTotal($totalAmount);
  $transaction = new Transaction();
  $transaction
    ->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription($this
    ->getPayment()
    ->id())
    ->setInvoiceNumber($this
    ->getPayment()
    ->id())
    ->setNotifyUrl($this
    ->getWebhookUrl());
  $payment = new Payment();
  $payment
    ->setIntent('sale')
    ->setPayer($payer)
    ->setRedirectUrls($redirectUrls)
    ->setTransactions([
    $transaction,
  ]);
  try {
    $payment
      ->create($this
      ->getApiContext(self::PAYPAL_CONTEXT_TYPE_CREATE));
    $this
      ->setPaymentId($payment
      ->getId());
    $url = Url::fromUri($payment
      ->getApprovalLink());
    $this->paymentExecutionResult = new Response($url);
  } catch (Exception $ex) {

    # TODO: clarify with the payment maintainer how we should handle Exceptions
    $this->paymentExecutionResult = NULL;
  }
}