You are here

public function PayPalExpressCheckout::buildRedirectForm in Ubercart 8.4

Redirect to PayPal Express Checkout Mark Flow.

This is used when the user does not use the cart button, but follows the normal checkout process and selects Express Checkout as a payment method.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

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

Return value

array The form structure.

Overrides OffsitePaymentMethodPluginInterface::buildRedirectForm

File

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

Class

PayPalExpressCheckout
Defines the PayPal Express Checkout payment method.

Namespace

Drupal\uc_paypal\Plugin\Ubercart\PaymentMethod

Code

public function buildRedirectForm(array $form, FormStateInterface $form_state, OrderInterface $order = NULL) {
  $session = \Drupal::service('session');
  if ($session
    ->has('TOKEN') && $session
    ->has('PAYERID')) {

    // If the session variables are set, then the user already gave their
    // details via Shortcut Flow, so we do not need to redirect them here.
    return [];
  }
  $address = $order
    ->getAddress('delivery');
  $request = [
    'METHOD' => 'SetExpressCheckout',
    'RETURNURL' => Url::fromRoute('uc_paypal.ec_complete', [], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'CANCELURL' => Url::fromRoute('uc_cart.checkout_review', [], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'AMT' => uc_currency_format($order
      ->getTotal(), FALSE, FALSE, '.'),
    'CURRENCYCODE' => $order
      ->getCurrency(),
    'PAYMENTACTION' => $this->configuration['wpp_cc_txn_type'],
    'DESC' => $this
      ->t('Order @order_id at @store', [
      '@order_id' => $order
        ->id(),
      '@store' => uc_store_name(),
    ]),
    'INVNUM' => $order
      ->id() . '-' . REQUEST_TIME,
    'REQCONFIRMSHIPPING' => $this->configuration['ec_rqconfirmed_addr'],
    'ADDROVERRIDE' => 1,
    'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US',
    'NOTIFYURL' => Url::fromRoute('uc_paypal.ipn', [], [
      'absolute' => TRUE,
    ])
      ->toString(),
    'SHIPTONAME' => substr($address
      ->getFirstName() . ' ' . $address
      ->getLastName(), 0, 32),
    'SHIPTOSTREET' => substr($address
      ->getStreet1(), 0, 100),
    'SHIPTOSTREET2' => substr($address
      ->getStreet2(), 0, 100),
    'SHIPTOCITY' => substr($address
      ->getCity(), 0, 40),
    'SHIPTOSTATE' => $address
      ->getZone(),
    'SHIPTOCOUNTRYCODE' => $address
      ->getCountry(),
    'SHIPTOZIP' => substr($address
      ->getPostalCode(), 0, 20),
    'PHONENUM' => substr($address
      ->getPhone(), 0, 20),
    'LANDINGPAGE' => $this->configuration['ec_landingpage_style'],
  ];
  if (!$order
    ->isShippable()) {
    $request['NOSHIPPING'] = 1;
    unset($request['ADDROVERRIDE']);
  }
  $response = $this
    ->sendNvpRequest($request);
  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
    ->set('TOKEN', $response['TOKEN']);
  $sandbox = strpos($this->configuration['wpp_server'], 'sandbox') > 0 ? 'sandbox.' : '';
  $url = 'https://www.' . $sandbox . 'paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=' . $response['TOKEN'];
  $form['#action'] = $url;
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit order'),
  ];
  return $form;
}