public function PayPalExpressCheckout::submitExpressForm in Ubercart 8.4
Submit callback for the express checkout button.
File
- payment/
uc_paypal/ src/ Plugin/ Ubercart/ PaymentMethod/ PayPalExpressCheckout.php, line 271
Class
- PayPalExpressCheckout
- Defines the PayPal Express Checkout payment method.
Namespace
Drupal\uc_paypal\Plugin\Ubercart\PaymentMethodCode
public function submitExpressForm(array &$form, FormStateInterface $form_state) {
$items = \Drupal::service('uc_cart.manager')
->get()
->getContents();
if (empty($items)) {
$this
->messenger()
->addMessage($this
->t('You do not have any items in your shopping cart.'));
return;
}
$order = Order::create([
'uid' => \Drupal::currentUser()
->id(),
'payment_method' => $this->methodId,
]);
$order->products = [];
foreach ($items as $item) {
$order->products[] = $item
->toOrderProduct();
}
$order
->save();
$response = $this
->sendNvpRequest([
'METHOD' => 'SetExpressCheckout',
'RETURNURL' => Url::fromRoute('uc_paypal.ec_review', [], [
'absolute' => TRUE,
])
->toString(),
'CANCELURL' => Url::fromRoute('uc_cart.cart', [], [
'absolute' => TRUE,
])
->toString(),
'AMT' => uc_currency_format($order
->getSubtotal(), 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'],
'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US',
'NOTIFYURL' => Url::fromRoute('uc_paypal.ipn', [], [
'absolute' => TRUE,
])
->toString(),
'LANDINGPAGE' => $this->configuration['ec_landingpage_style'],
]);
if ($response['ACK'] != 'Success') {
\Drupal::logger('uc_paypal')
->error('NVP API request failed with @code: @message', [
'@code' => $response['L_ERRORCODE0'],
'@message' => $response['L_LONGMESSAGE0'],
]);
$this
->messenger()
->addError($this
->t('PayPal reported an error: @code: @message', [
'@code' => $response['L_ERRORCODE0'],
'@message' => $response['L_LONGMESSAGE0'],
]));
return;
}
$session = \Drupal::service('session');
$session
->set('cart_order', $order
->id());
$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&token=' . $response['TOKEN'];
$form_state
->setResponse(new TrustedRedirectResponse($url));
}