Exemplo n.º 1
0
 /**
  * @param Bootstrap $bootstrap
  */
 public function __construct(Bootstrap $bootstrap)
 {
     $this->bootstrap = $bootstrap;
     $this->paypalBootstrap = $bootstrap->Collection()->get('SwagPaymentPaypal');
     $this->config = $this->paypalBootstrap->Config();
     $this->session = $bootstrap->get('session');
     $this->restClient = $bootstrap->get('paypalRestClient');
     $this->restClient->setHeaders('PayPal-Partner-Attribution-Id', 'ShopwareAG_Cart_PayPalPlus_1017');
 }
 /**
  * @param Bootstrap $bootstrap
  */
 public function __construct(Bootstrap $bootstrap)
 {
     $this->bootstrap = $bootstrap;
     $this->paypalBootstrap = $bootstrap->Collection()->get('SwagPaymentPaypal');
     $this->config = $this->paypalBootstrap->Config();
     $this->session = $bootstrap->get('session');
     $this->restClient = $bootstrap->get('paypal_plus.rest_client');
     $this->pluginLogger = $bootstrap->get('pluginlogger');
 }
 /**
  * @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'])));
     }
 }
 /**
  * Returns the article list parameter data.
  *
  * @return array
  */
 protected function getBasketParameter()
 {
     $params = array();
     $user = $this->getUser();
     $params['CURRENCYCODE'] = $this->getCurrencyShortName();
     if ($user !== null) {
         $basket = $this->getBasket();
         if (!empty($basket['sShippingcosts'])) {
             $params['SHIPPINGAMT'] = $this->getShipment();
         }
         $params['AMT'] = $this->getAmount();
     } else {
         $basket = $this->get('modules')->Basket()->sGetBasket();
         if (!empty($basket['sShippingcosts'])) {
             $params['SHIPPINGAMT'] = !empty($basket['sShippingcostsWithTax']) ? $basket['sShippingcostsWithTax'] : $basket['sShippingcosts'];
             $params['SHIPPINGAMT'] = str_replace(',', '.', $params['SHIPPINGAMT']);
         }
         if (!empty($user['additional']['charge_vat']) && !empty($item['AmountWithTaxNumeric'])) {
             $params['AMT'] = $basket['AmountWithTaxNumeric'];
         } else {
             $params['AMT'] = $basket['AmountNumeric'];
         }
         $params['AMT'] = $basket['AmountNumeric'];
     }
     $params['AMT'] = number_format($params['AMT'], 2, '.', '');
     $params['SHIPPINGAMT'] = number_format($params['SHIPPINGAMT'], 2, '.', '');
     $params['ITEMAMT'] = number_format($params['AMT'] - $params['SHIPPINGAMT'], 2, '.', '');
     $params['TAXAMT'] = number_format(0, 2, '.', '');
     $config = $this->plugin->Config();
     if ($config->get('paypalTransferCart') && $params['ITEMAMT'] != '0.00' && count($basket['content']) < 25) {
         foreach ($basket['content'] as $key => $item) {
             if (!empty($user['additional']['charge_vat']) && !empty($item['amountWithTax'])) {
                 $amount = round($item['amountWithTax'], 2);
                 $quantity = 1;
             } else {
                 $amount = str_replace(',', '.', $item['amount']);
                 $quantity = $item['quantity'];
                 $amount = $amount / $item['quantity'];
             }
             $amount = round($amount, 2);
             $article = array('L_NUMBER' . $key => $item['ordernumber'], 'L_NAME' . $key => $item['articlename'], 'L_AMT' . $key => $amount, 'L_QTY' . $key => $quantity);
             $params = array_merge($params, $article);
         }
     }
     if ($params['ITEMAMT'] == '0.00') {
         $params['ITEMAMT'] = $params['SHIPPINGAMT'];
         $params['SHIPPINGAMT'] = '0.00';
     }
     return $params;
 }
 /**
  * Do payment action
  */
 public function doActionAction()
 {
     $transactionId = $this->Request()->getParam('transactionId');
     // Load the correct shop in order to use the correct api credentials
     $this->registerShopByTransactionId($transactionId);
     $config = $this->plugin->Config();
     $client = $this->get('paypalClient');
     $action = $this->Request()->getParam('paymentAction');
     $amount = $this->Request()->getParam('paymentAmount');
     $amount = str_replace(',', '.', $amount);
     $currency = $this->Request()->getParam('paymentCurrency');
     $orderNumber = $this->Request()->getParam('orderNumber');
     $full = $this->Request()->getParam('paymentFull') === 'true';
     $last = $this->Request()->getParam('paymentLast') === 'true';
     $note = $this->Request()->getParam('note');
     $invoiceId = null;
     if ($config->get('paypalSendInvoiceId') === true) {
         $prefix = $config->get('paypalPrefixInvoiceId');
         if (!empty($prefix)) {
             // Set prefixed invoice id - Remove special chars and spaces
             $prefix = str_replace(' ', '', $prefix);
             $prefix = preg_replace('/[^A-Za-z0-9\\-]/', '', $prefix);
             $invoiceId = $prefix . $orderNumber;
         } else {
             $invoiceId = $orderNumber;
         }
     }
     try {
         switch ($action) {
             case 'refund':
                 $data = array('TRANSACTIONID' => $transactionId, 'REFUNDTYPE' => $full ? 'Full' : 'Partial', 'AMT' => $full ? '' : $amount, 'CURRENCYCODE' => $full ? '' : $currency, 'NOTE' => $note);
                 if ($invoiceId) {
                     $data['INVOICEID'] = $invoiceId;
                 }
                 $result = $client->RefundTransaction($data);
                 break;
             case 'auth':
                 $result = $client->doReAuthorization(array('AUTHORIZATIONID' => $transactionId, 'AMT' => $amount, 'CURRENCYCODE' => $currency));
                 break;
             case 'capture':
                 $data = array('AUTHORIZATIONID' => $transactionId, 'AMT' => $amount, 'CURRENCYCODE' => $currency, 'COMPLETETYPE' => $last ? 'Complete' : 'NotComplete', 'NOTE' => $note);
                 if ($invoiceId) {
                     $data['INVOICEID'] = $invoiceId;
                 }
                 $result = $client->doCapture($data);
                 break;
             case 'void':
                 $result = $client->doVoid(array('AUTHORIZATIONID' => $transactionId, 'NOTE' => $note));
                 break;
             case 'book':
                 $result = $client->doAuthorization(array('TRANSACTIONID' => $transactionId, 'AMT' => $amount, 'CURRENCYCODE' => $currency));
                 if ($result['ACK'] == 'Success') {
                     $data = array('AUTHORIZATIONID' => $result['TRANSACTIONID'], 'AMT' => $amount, 'CURRENCYCODE' => $currency, 'COMPLETETYPE' => $last ? 'Complete' : 'NotComplete', 'NOTE' => $note);
                     if ($invoiceId) {
                         $data['INVOICEID'] = $invoiceId;
                     }
                     $result = $client->doCapture($data);
                 }
                 break;
             default:
                 return;
         }
         if ($result['ACK'] != 'Success') {
             throw new Exception('[' . $result['L_SEVERITYCODE0'] . '] ' . $result['L_SHORTMESSAGE0'] . " " . $result['L_LONGMESSAGE0'] . "<br>\n");
         }
         // Switch transaction id
         if ($action !== 'book' && $action !== 'capture' && (isset($result['TRANSACTIONID']) || isset($result['AUTHORIZATIONID']))) {
             $sql = '
                 UPDATE s_order SET transactionID=?
                 WHERE transactionID=? LIMIT 1
             ';
             $this->get('db')->query($sql, array(isset($result['TRANSACTIONID']) ? $result['TRANSACTIONID'] : $result['AUTHORIZATIONID'], $transactionId));
             $transactionId = $result['TRANSACTIONID'];
         }
         if ($action == 'void') {
             $paymentStatus = 'Voided';
         } elseif ($action == 'refund') {
             $paymentStatus = 'Refunded';
         } elseif (isset($result['PAYMENTSTATUS'])) {
             $paymentStatus = $result['PAYMENTSTATUS'];
         }
         if (isset($paymentStatus)) {
             try {
                 $this->plugin->setPaymentStatus($transactionId, $paymentStatus, $note);
             } catch (Exception $e) {
                 $result['SW_STATUS_ERROR'] = $e->getMessage();
             }
         }
         $this->View()->assign(array('success' => true, 'result' => $result));
     } catch (Exception $e) {
         $this->View()->assign(array('message' => $e->getMessage(), 'success' => false));
     }
 }
 public function __construct(Bootstrap $bootstrap)
 {
     $this->bootstrap = $bootstrap;
     $this->config = $bootstrap->Config();
 }
 /**
  * @param \Enlight_Controller_ActionEventArgs $args
  */
 public function onPreDispatchPaymentPaypal(\Enlight_Controller_ActionEventArgs $args)
 {
     $request = $args->getRequest();
     /** @var \Shopware_Controllers_Frontend_PaymentPaypal $controller */
     $controller = $args->getSubject();
     if ($request->getActionName() != 'return') {
         return;
     }
     $paymentId = $request->get('paymentId');
     if (!$paymentId) {
         return;
     }
     $payerId = $request->getParam('PayerID');
     $uri = 'payments/payment/' . $paymentId;
     $payment = array();
     try {
         $payment = $this->restClient->get($uri, array('payer_id' => $payerId));
     } catch (Exception $e) {
         $this->logException('An error occurred on getting the payment on returning from PayPal', $e);
     }
     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 = $controller->getAmount();
     $swCurrency = $controller->getCurrencyShortName();
     if (abs($swAmount - $ppAmount) >= 0.01 || $ppCurrency != $swCurrency) {
         $controller->redirect(array('controller' => 'checkout', 'action' => 'confirm'));
         return;
     }
     $paypalConfig = $this->paypalBootstrap->Config();
     $orderNumber = null;
     if ($payment['state'] == 'created') {
         if ($paypalConfig->get('paypalSendInvoiceId')) {
             $orderNumber = $controller->saveOrder($payment['id'], sha1($payment['id']));
             $params = array(array('op' => 'add', 'path' => '/transactions/0/invoice_number', 'value' => $orderNumber));
             $prefix = $paypalConfig->get('paypalPrefixInvoiceId');
             if ($prefix) {
                 // Set prefixed invoice id - Remove special chars and spaces
                 $prefix = str_replace(' ', '', $prefix);
                 $prefix = preg_replace('/[^A-Za-z0-9\\_]/', '', $prefix);
                 $params[0]['value'] = $prefix . $orderNumber;
             }
             $uri = 'payments/payment/' . $paymentId;
             try {
                 $this->restClient->patch($uri, $params);
             } catch (Exception $e) {
                 $this->logException('An error occurred on patching the order number to the payment', $e);
             }
         }
         $uri = "payments/payment/{$paymentId}/execute";
         try {
             $payment = $this->restClient->create($uri, array('payer_id' => $payerId));
         } catch (Exception $e) {
             $this->logException('An error occurred on executing the payment', $e);
         }
     }
     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'];
         }
         if (!$orderNumber) {
             $orderNumber = $controller->saveOrder($transactionId, sha1($payment['id']));
         } else {
             $sql = 'UPDATE s_order
                     SET transactionID = ?
                     WHERE ordernumber = ?;';
             $controller->get('db')->query($sql, array($transactionId, $orderNumber));
         }
         if (!empty($payment['transactions'][0]['related_resources'][0]['sale']['state'])) {
             $paymentStatus = ucfirst($payment['transactions'][0]['related_resources'][0]['sale']['state']);
             $this->paypalBootstrap->setPaymentStatus($transactionId, $paymentStatus);
         }
         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
             ';
             $controller->get('db')->query($sql, array($orderNumber));
         } catch (Exception $e) {
         }
         $controller->redirect(array('controller' => 'checkout', 'action' => 'finish', 'sUniqueID' => sha1($payment['id'])));
     }
 }