Esempio n. 1
0
 /**
  * Orders endpoint
  */
 protected function orders($args)
 {
     // Manage informations
     $datas = $this->data;
     if (is_null($datas)) {
         $error = PowaTagErrorType::$BAD_REQUEST;
         return PowaTagAPI::powaError($error);
     }
     if ($this->verb == 'costs') {
         if (isset($datas->order)) {
             $customer = $datas->order->customer;
         } else {
             $customer = current($datas->orders)->customer;
         }
         if (PowaTagAPI::apiLog()) {
             PowaTagLogs::initAPILog('Process calculate Costs', PowaTagLogs::IN_PROGRESS, 'Customer : ' . $customer->firstName . ' ' . $customer->lastName);
         }
         if (PowaTagAPI::requestLog()) {
             PowaTagLogs::initRequestLog('Process calculate Costs', PowaTagLogs::IN_PROGRESS, $datas);
         }
         $powatagcosts = new PowaTagCosts($datas);
         if ($error = $powatagcosts->getError()) {
             $message = $error['message'];
             if (PowaTagAPI::apiLog()) {
                 PowaTagLogs::initAPILog('Process order', PowaTagLogs::ERROR, $message);
             }
             return PowaTagAPI::powaError($error);
         }
         if ($value = $powatagcosts->getSummary()) {
             if (PowaTagAPI::apiLog()) {
                 PowaTagLogs::initAPILog('Process calculate Costs', PowaTagLogs::SUCCESS, 'Customer : ' . $customer->firstName . ' ' . $customer->lastName);
             }
             if (PowaTagAPI::requestLog()) {
                 PowaTagLogs::initRequestLog('Process calculate Costs', PowaTagLogs::SUCCESS, $value);
             }
             return $value;
         } else {
             $error = $powatagcosts->getError();
             if (PowaTagAPI::apiLog()) {
                 PowaTagLogs::initAPILog('Process calculate Costs', PowaTagLogs::ERROR, $error['message']);
             }
             return PowaTagAPI::powaError($error);
         }
     } else {
         if (count($args) == 2 && Validate::isInt($args[0]) && ($args[1] = 'confirm-payment')) {
             //Three step payment confirmation
             if (PowaTagAPI::apiLog()) {
                 PowaTagLogs::initAPILog('Process payment', PowaTagLogs::IN_PROGRESS, 'Order ID : ' . $args[0]);
             }
             if (PowaTagAPI::requestLog()) {
                 PowaTagLogs::initRequestLog('Create payment', PowaTagLogs::IN_PROGRESS, $datas);
             }
             $payment = new PowaTagPayment($datas, (int) $args[0]);
             if ($id_order = $payment->confirmPayment()) {
                 if (PowaTagAPI::apiLog()) {
                     PowaTagLogs::initAPILog('Process payment', PowaTagLogs::SUCCESS, 'ID Order : ' . $id_order);
                 }
                 if (PowaTagAPI::requestLog()) {
                     PowaTagLogs::initRequestLog('Process payment', PowaTagLogs::SUCCESS, $id_order);
                 }
                 $data = array('providerTxCode' => isset($datas->paymentResult->providerTxCode) ? $datas->paymentResult->providerTxCode : 'providerTxCode Empty', 'message' => 'Authorization success order ' . $id_order . ' created');
                 if ($payment->checkOrderState($id_order, $data) == 'error') {
                     $this->setResponse($data['response']);
                 }
                 return $data;
             } else {
                 $error = $payment->getError();
                 if (PowaTagAPI::apiLog()) {
                     PowaTagLogs::initAPILog('Process payment', PowaTagLogs::ERROR, $error['message']);
                 }
                 return PowaTagAPI::powaError($error);
             }
         } else {
             if (!count($args)) {
                 //Two step payment or three step payment
                 if (isset($datas->order)) {
                     $customer = $datas->order->customer;
                 } else {
                     $customer = current($datas->orders)->customer;
                 }
                 if (PowaTagAPI::apiLog()) {
                     PowaTagLogs::initAPILog('Process order 123', PowaTagLogs::IN_PROGRESS, 'Customer : ' . $customer->firstName . ' ' . $customer->lastName);
                 }
                 if (PowaTagAPI::requestLog()) {
                     PowaTagLogs::initRequestLog('Create order', PowaTagLogs::IN_PROGRESS, $datas);
                 }
                 $order = new PowaTagOrders($datas);
                 if ($error = $order->getError()) {
                     $message = $error['message'];
                     if (PowaTagAPI::apiLog()) {
                         PowaTagLogs::initAPILog('Process order', PowaTagLogs::ERROR, $message);
                     }
                     return PowaTagAPI::powaError($error);
                 }
                 list($id_cart, $id_order, $message) = $order->validateOrder();
                 if ($id_order || $id_cart) {
                     if (PowaTagAPI::apiLog()) {
                         PowaTagLogs::initAPILog('Process order', PowaTagLogs::SUCCESS, 'Order has been created : ' . $id_order);
                     }
                     $link = new Link();
                     $cart = new Cart($id_cart);
                     $data = array('orderResults' => array(array('orderId' => $id_order ? $id_order : $id_cart, 'message' => $message, 'redirectUrl' => $link->getModuleLink('powatag', 'confirmation', array('id_cart' => (int) $id_cart, 'id_customer' => (int) $cart->id_customer)))));
                     if ($error = $order->getError()) {
                         return PowaTagAPI::powaError($error);
                     }
                     if ($order->checkOrderState($id_order, $data)) {
                         $this->setResponse($data['response']);
                     }
                     return $data;
                 } else {
                     $message = '';
                     $errorCode = '';
                     if ($error = $order->getError()) {
                         $message = $error['message'];
                     }
                     if (PowaTagAPI::apiLog()) {
                         PowaTagLogs::initAPILog('Process order', PowaTagLogs::ERROR, $message);
                     }
                     return PowaTagAPI::powaError($error);
                 }
             }
         }
     }
 }
 public function validateOrder()
 {
     $id_cart = $this->createCart();
     $id_order = false;
     if (isset($this->datas->paymentResult) && $id_cart) {
         $order_state = (int) Configuration::get('PS_OS_PAYMENT');
         $this->datas->customer = $this->datas->customer;
         $payment = new PowaTagPayment($this->datas, $id_cart);
         $id_order = $payment->confirmPayment(true);
         if ($id_order) {
             $message = Configuration::get('POWATAG_SUCCESS_MSG', $this->context->language->id) != '' ? Configuration::get('POWATAG_SUCCESS_MSG', $this->context->language->id) : 'Success';
         } else {
             $message = 'Error on order creation';
         }
     }
     if ($id_cart) {
         $transaction = new PowaTagTransaction();
         $transaction->id_cart = (int) $this->cart->id;
         $transaction->id_order = (int) $id_order;
         $transaction->id_customer = (int) $this->customer->id;
         if (isset($this->datas->device)) {
             $transaction->id_device = isset($this->datas->device->deviceID) ? $this->datas->device->deviceID : '';
             $transaction->ip_address = isset($this->datas->device->ipAddress) ? $this->datas->device->ipAddress : '';
         }
         $transaction->order_state = isset($order_state) ? (int) $order_state : 0;
         $transaction->save();
         $message = Configuration::get('POWATAG_SUCCESS_MSG', $this->context->language->id) != '' ? Configuration::get('POWATAG_SUCCESS_MSG', $this->context->language->id) : 'Success';
     } else {
         $message = 'Cart has not been created';
     }
     return array($id_cart, $id_order, $message);
 }
 private function checkTotalToPaid($amountPaid, $currency)
 {
     if (!$currency instanceof Currency) {
         if (Validate::isInt($currency)) {
             $currency = new Currency((int) $currency);
         } else {
             $currencyCode = $currency;
             if (!($currency = PowaTagPayment::getCurrencyByIsoCode($currency))) {
                 $currency = $currencyCode;
             }
         }
     }
     if (!PowaTagValidate::currencyEnable($currency)) {
         $this->addError(sprintf($this->module->l('Currency is not enable : %s'), isset($currency->iso_code) ? $currency->iso_code : $currency), PowaTagErrorType::$CURRENCY_NOT_SUPPORTED);
         return false;
     }
     //We change context currency to be sure that calculs are made with correct currency
     $context = Context::getContext();
     $context->currency = $currency;
     $context->country = $this->getCountry($this->datas->customer->shippingAddress->country->alpha2Code);
     $price_cart = $this->cart->getOrderTotal(true, Cart::BOTH, null, Configuration::get('POWATAG_SHIPPING'));
     if (abs($price_cart - $amountPaid) >= 0.01) {
         $msg = "Cart: " . $price_cart . " != Payment: " . $amountPaid;
         $this->addError($this->module->l('Amount paid is not same as the cart: ' . $msg), PowaTagErrorType::$INTERNAL_ERROR);
         if (PowaTagAPI::apiLog()) {
             PowaTagLogs::initAPILog('Amount paid is not same as the cart', PowaTagLogs::ERROR, $msg);
         }
         return false;
     }
     return true;
 }