/**
  * @param ControllerAction $action
  */
 public function onPaypalPlus(ControllerAction $action)
 {
     $config = $this->config;
     $router = $action->Front()->Router();
     $view = $action->View();
     $user = $view->getAssign('sUserData');
     $basket = $view->getAssign('sBasket');
     $cancelUrl = $router->assemble(array('controller' => 'payment_paypal', 'action' => 'cancel', 'forceSecure' => true));
     $returnUrl = $router->assemble(array('controller' => 'payment_paypal', 'action' => 'return', 'forceSecure' => true));
     $profile = $this->getProfile();
     $this->restClient->setAuthToken();
     $uri = 'payments/payment';
     $params = array('intent' => 'sale', 'experience_profile_id' => $profile['id'], 'payer' => array('payment_method' => 'paypal'), 'transactions' => $this->getTransactionData($basket, $user), 'redirect_urls' => array('return_url' => $returnUrl, 'cancel_url' => $cancelUrl));
     $payment = $this->restClient->create($uri, $params);
     if (!empty($payment['links'][1]['href'])) {
         $view->assign('PaypalPlusApprovalUrl', $payment['links'][1]['href']);
         $view->assign('PaypalPlusModeSandbox', $config->get('paypalSandbox'));
         $view->assign('PaypalLocale', $this->paypalBootstrap->getLocaleCode());
         $db = $this->bootstrap->get('db');
         $sql = 'SELECT paymentmeanID AS id, paypal_plus_media AS media
                 FROM s_core_paymentmeans_attributes WHERE paypal_plus_active = 1';
         $paymentMethods = $db->fetchAssoc($sql);
         $view->assign('PaypalPlusThirdPartyPaymentMethods', $paymentMethods);
         $this->session->PaypalPlusPayment = $payment['id'];
     }
 }
 /**
  * @param \Enlight_Controller_ActionEventArgs $args
  */
 public function onPreDispatchPaymentPaypal($args)
 {
     $request = $args->getRequest();
     /** @var \Shopware_Controllers_Frontend_PaymentPaypal $action */
     $action = $args->getSubject();
     if ($request->getActionName() != 'return') {
         return;
     }
     $paymentId = $this->session->PaypalPlusPayment;
     if (empty($paymentId)) {
         return;
     }
     $payerId = $request->getParam('PayerID');
     $this->restClient->setAuthToken();
     $uri = 'payments/payment/' . $paymentId;
     $payment = $this->restClient->get($uri, array('payer_id' => $payerId));
     $statusId = $this->paypalBootstrap->Config()->get('paypalStatusId', 12);
     if (!empty($payment['transactions'][0]['amount']['total'])) {
         $ppAmount = floatval($payment['transactions'][0]['amount']['total']);
         $ppCurrency = floatval($payment['transactions'][0]['amount']['currency']);
     } else {
         $ppAmount = 0;
         $ppCurrency = '';
     }
     $swAmount = $action->getAmount();
     $swCurrency = $action->getCurrencyShortName();
     if (abs($swAmount - $ppAmount) >= 0.01 || $ppCurrency != $swCurrency) {
         $action->redirect(array('controller' => 'checkout', 'action' => 'confirm'));
         return;
     }
     if ($payment['state'] == 'created') {
         $uri = "payments/payment/{$paymentId}/execute";
         $payment = $this->restClient->create($uri, array('payer_id' => $payerId));
     }
     if ($payment['state'] == 'approved') {
         if (!empty($payment['transactions'][0]['related_resources'][0]['sale']['id'])) {
             $transactionId = $payment['transactions'][0]['related_resources'][0]['sale']['id'];
         } else {
             $transactionId = $payment['id'];
         }
         $orderNumber = $action->saveOrder($transactionId, sha1($payment['id']), $statusId);
         if ($payment['payment_instruction']) {
             $this->saveInvoiceInstructions($orderNumber, $payment);
         }
         try {
             $sql = '
                 INSERT INTO s_order_attributes (orderID, swag_payal_express)
                 SELECT id, 2 FROM s_order WHERE ordernumber = ?
                 ON DUPLICATE KEY UPDATE swag_payal_express = 2
             ';
             $action->get('db')->query($sql, array($orderNumber));
         } catch (\Exception $e) {
         }
         $action->redirect(array('controller' => 'checkout', 'action' => 'finish', 'sUniqueID' => sha1($payment['id'])));
     }
 }