/**
  * @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'])));
     }
 }
Ejemplo n.º 2
0
 /**
  * @return array
  */
 protected function getProfile()
 {
     if (!isset($this->session['PaypalProfile'])) {
         $profile = $this->getProfileData();
         $uri = 'payment-experience/web-profiles';
         $this->restClient->setAuthToken();
         $profileList = $this->restClient->get($uri);
         foreach ($profileList as $entry) {
             if ($entry['name'] == $profile['name']) {
                 $this->restClient->update("{$uri}/{$entry['id']}", $profile);
                 $this->session['PaypalProfile'] = array('id' => $entry['id']);
                 break;
             }
         }
         if (!isset($this->session['PaypalProfile'])) {
             $this->session['PaypalProfile'] = $this->restClient->create($uri, $profile);
         }
     }
     return $this->session['PaypalProfile'];
 }