You are here

public function PayPalExpressCheckout::orderSubmit in Ubercart 8.4

Called when an order is being submitted with this payment method.

Parameters

\Drupal\uc_order\OrderInterface $order: The order that is being submitted.

Return value

string|null An error message that can be shown to the user if the payment failed, or NULL if everything was successful.

Overrides PaymentMethodPluginBase::orderSubmit

File

payment/uc_paypal/src/Plugin/Ubercart/PaymentMethod/PayPalExpressCheckout.php, line 208

Class

PayPalExpressCheckout
Defines the PayPal Express Checkout payment method.

Namespace

Drupal\uc_paypal\Plugin\Ubercart\PaymentMethod

Code

public function orderSubmit(OrderInterface $order) {
  $session = \Drupal::service('session');
  $shipping = 0;
  if (is_array($order->line_items)) {
    foreach ($order->line_items as $item) {
      if ($item['type'] == 'shipping') {
        $shipping += $item['amount'];
      }
    }
  }
  $tax = 0;
  if (\Drupal::moduleHandler()
    ->moduleExists('uc_tax')) {
    foreach (uc_tax_calculate($order) as $tax_item) {
      $tax += $tax_item->amount;
    }
  }
  $subtotal = $order
    ->getTotal() - $tax - $shipping;
  $response = $this
    ->sendNvpRequest([
    'METHOD' => 'DoExpressCheckoutPayment',
    'TOKEN' => $session
      ->get('TOKEN'),
    'PAYMENTACTION' => $this->configuration['wpp_cc_txn_type'],
    'PAYERID' => $session
      ->get('PAYERID'),
    'AMT' => uc_currency_format($order
      ->getTotal(), FALSE, FALSE, '.'),
    'DESC' => $this
      ->t('Order @order_id at @store', [
      '@order_id' => $order
        ->id(),
      '@store' => uc_store_name(),
    ]),
    'INVNUM' => $order
      ->id() . '-' . REQUEST_TIME,
    'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US',
    'NOTIFYURL' => Url::fromRoute('uc_paypal.ipn', [], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'ITEMAMT' => uc_currency_format($subtotal, FALSE, FALSE, '.'),
    'SHIPPINGAMT' => uc_currency_format($shipping, FALSE, FALSE, '.'),
    'TAXAMT' => uc_currency_format($tax, FALSE, FALSE, '.'),
    'CURRENCYCODE' => $order
      ->getCurrency(),
  ]);
  if ($response['ACK'] != 'Success') {
    \Drupal::logger('uc_paypal')
      ->error('NVP API request failed with @code: @message', [
      '@code' => $response['L_ERRORCODE0'],
      '@message' => $response['L_LONGMESSAGE0'],
    ]);
    return $this
      ->t('PayPal reported an error: @code: @message', [
      '@code' => $response['L_ERRORCODE0'],
      '@message' => $response['L_LONGMESSAGE0'],
    ]);
  }
  $session
    ->remove('TOKEN');
  $session
    ->remove('PAYERID');
}