/**
  * @param Order $order
  * @param string $status
  * @param string $state
  * @return void
  */
 protected function setOrderStateAndStatus(Order $order, $status, $state)
 {
     if (!$status) {
         $status = $order->getConfig()->getStateDefaultStatus($state);
     }
     $order->setState($state)->setStatus($status);
 }
Example #2
0
 /**
  * Receives webhook events from Roadrunner
  */
 public function execute()
 {
     $this->_logger->addDebug('paystandmagento/webhook/paystand endpoint was hit');
     $body = @file_get_contents('php://input');
     $json = json_decode($body);
     $this->_logger->addDebug(">>>>> body=" . print_r($body, TRUE));
     if (isset($json->resource->meta->source) && $json->resource->meta->source == "magento 2") {
         $quoteId = $json->resource->meta->quote;
         $this->_logger->addDebug('magento 2 webhook identified with quote id = ' . $quoteId);
         $this->_order->loadByAttribute('quote_id', $quoteId);
         if (!empty($this->_order->getIncrementId())) {
             $this->_logger->addDebug('current order increment id = ' . $this->_order->getIncrementId());
             $state = $this->_order->getState();
             $this->_logger->addDebug('current order state = ' . $state);
             $status = $this->_order->getStatus();
             $this->_logger->addDebug('current order status = ' . $status);
             $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
             if ($this->scopeConfig->getValue(self::USE_SANDBOX, $storeScope)) {
                 $base_url = 'https://api.paystand.co/v3';
             } else {
                 $base_url = 'https://api.paystand.com/v3';
             }
             $url = $base_url . "/events/" . $json->id . "/verify";
             $auth_header = array("x-publishable-key: " . $this->scopeConfig->getValue(self::PUBLISHABLE_KEY, $storeScope));
             $curl = $this->buildCurl("POST", $url, json_encode($json), $auth_header);
             $response = $this->runCurl($curl);
             $this->_logger->addDebug("http_response_code is " . $this->http_response_code);
             if (FALSE !== $response && $this->http_response_code == 200) {
                 if ($json->resource->object = "payment") {
                     switch ($json->resource->status) {
                         case 'posted':
                             $state = 'pending';
                             $status = 'pending';
                             break;
                         case 'paid':
                             $state = 'processing';
                             $status = 'processing';
                             break;
                         case 'failed':
                             $state = 'closed';
                             $status = 'closed';
                             break;
                         case 'canceled':
                             $state = 'canceled';
                             $status = 'canceled';
                             break;
                     }
                 }
                 $this->_order->setState($state);
                 $this->_order->setStatus($status);
                 $this->_order->save();
                 $this->_logger->addDebug('new order state = ' . $state);
                 $this->_logger->addDebug('new order status = ' . $status);
             } else {
                 $this->_logger->addDebug('event verify failed');
             }
         }
     }
 }
Example #3
0
 /**
  * @param $order
  */
 protected function _prepareInvoice()
 {
     $this->_debugData['_prepareInvoice'] = 'Prepare invoice for order';
     $payment = $this->_order->getPayment()->getMethodInstance();
     //Set order state to new because with order state payment_review it is not possible to create an invoice
     if (strcmp($this->_order->getState(), \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW) == 0) {
         $this->_order->setState(\Magento\Sales\Model\Order::STATE_NEW);
     }
     //capture mode
     if (!$this->_isAutoCapture()) {
         $this->_order->addStatusHistoryComment(__('Capture Mode set to Manual'));
         $this->_debugData['_prepareInvoice capture mode'] = 'Capture mode is set to Manual';
         // show message if order is in manual review
         if ($this->_fraudManualReview) {
             // check if different status is selected
             $fraudManualReviewStatus = $this->_getFraudManualReviewStatus();
             if ($fraudManualReviewStatus != "") {
                 $status = $fraudManualReviewStatus;
                 $comment = "Adyen Payment is in Manual Review check the Adyen platform";
                 $this->_order->addStatusHistoryComment(__($comment), $status);
             }
         }
         $createPendingInvoice = (bool) $this->_getConfigData('create_pending_invoice', 'adyen_abstract', $this->_order->getStoreId());
         if (!$createPendingInvoice) {
             $this->_debugData['_prepareInvoice done'] = 'Setting pending invoice is off so don\'t create an invoice wait for the capture notification';
             return;
         }
     }
     // validate if amount is total amount
     $orderCurrencyCode = $this->_order->getOrderCurrencyCode();
     $orderAmount = (int) $this->_adyenHelper->formatAmount($this->_order->getGrandTotal(), $orderCurrencyCode);
     if ($this->_isTotalAmount($orderAmount)) {
         $this->_createInvoice();
     } else {
         $this->_debugData['_prepareInvoice partial authorisation step1'] = 'This is a partial AUTHORISATION';
         // Check if this is the first partial authorisation or if there is already been an authorisation
         $paymentObj = $this->_order->getPayment();
         $authorisationAmount = $paymentObj->getAdyenAuthorisationAmount();
         if ($authorisationAmount != "") {
             $this->_debugData['_prepareInvoice partial authorisation step2'] = 'There is already a partial AUTHORISATION received check if this combined with the previous amounts match the total amount of the order';
             $authorisationAmount = (int) $authorisationAmount;
             $currentValue = (int) $this->_value;
             $totalAuthorisationAmount = $authorisationAmount + $currentValue;
             // update amount in column
             $paymentObj->setAdyenAuthorisationAmount($totalAuthorisationAmount);
             if ($totalAuthorisationAmount == $orderAmount) {
                 $this->_debugData['_prepareInvoice partial authorisation step3'] = 'The full amount is paid. This is the latest AUTHORISATION notification. Create the invoice';
                 $this->_createInvoice();
             } else {
                 // this can be multiple times so use envenData as unique key
                 $this->_debugData['_prepareInvoice partial authorisation step3'] = 'The full amount is not reached. Wait for the next AUTHORISATION notification. The current amount that is authorized is:' . $totalAuthorisationAmount;
             }
         } else {
             $this->_debugData['_prepareInvoice partial authorisation step2'] = 'This is the first partial AUTHORISATION save this into the adyen_authorisation_amount field';
             $paymentObj->setAdyenAuthorisationAmount($this->_value);
         }
     }
 }
Example #4
0
 public function testCanCancelState()
 {
     $paymentMock = $this->getMockBuilder('Magento\\Sales\\Model\\Resource\\Order\\Payment')->disableOriginalConstructor()->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])->getMock();
     $paymentMock->expects($this->any())->method('canReviewPayment')->will($this->returnValue(false));
     $paymentMock->expects($this->any())->method('canFetchTransactionInfo')->will($this->returnValue(false));
     $this->preparePaymentMock($paymentMock);
     $this->prepareItemMock(1);
     $this->order->setActionFlag(\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD, false);
     $this->order->setState(\Magento\Sales\Model\Order::STATE_CANCELED);
     $this->assertFalse($this->order->canCancel());
 }
Example #5
0
 /**
  * Check order status before save
  *
  * @param Order $order
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function check(Order $order)
 {
     if (!$order->getId()) {
         return $order;
     }
     if (!$order->isCanceled() && !$order->canUnhold() && !$order->canInvoice() && !$order->canShip()) {
         if (0 == $order->getBaseGrandTotal() || $order->canCreditmemo()) {
             if ($order->getState() !== Order::STATE_COMPLETE) {
                 $order->setState(Order::STATE_COMPLETE)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_COMPLETE));
             }
         } elseif (floatval($order->getTotalRefunded()) || !$order->getTotalRefunded() && $order->hasForcedCanCreditmemo()) {
             if ($order->getState() !== Order::STATE_CLOSED) {
                 $order->setState(Order::STATE_CLOSED)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_CLOSED));
             }
         }
     }
     if ($order->getState() == Order::STATE_NEW && $order->getIsInProcess()) {
         $order->setState(Order::STATE_PROCESSING)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING));
     }
     return $this;
 }
Example #6
0
 /**
  * Check order status before save
  *
  * @param Order $order
  * @return $this
  */
 public function check(Order $order)
 {
     if (!$order->getId()) {
         return $order;
     }
     $userNotification = $order->hasCustomerNoteNotify() ? $order->getCustomerNoteNotify() : null;
     if (!$order->isCanceled() && !$order->canUnhold() && !$order->canInvoice() && !$order->canShip()) {
         if (0 == $order->getBaseGrandTotal() || $order->canCreditmemo()) {
             if ($order->getState() !== Order::STATE_COMPLETE) {
                 $order->setState(Order::STATE_COMPLETE, true, '', $userNotification, false);
             }
         } elseif (floatval($order->getTotalRefunded()) || !$order->getTotalRefunded() && $order->hasForcedCanCreditmemo()) {
             if ($order->getState() !== Order::STATE_CLOSED) {
                 $order->setState(Order::STATE_CLOSED, true, '', $userNotification, false);
             }
         }
     }
     if ($order->getState() == Order::STATE_NEW && $order->getIsInProcess()) {
         $order->setState(Order::STATE_PROCESSING, true, '', $userNotification);
     }
     return $this;
 }
 /**
  * @param bool $cancelActionFlag
  * @dataProvider dataProviderActionFlag
  */
 public function testCanCancelActionFlag($cancelActionFlag)
 {
     $paymentMock = $this->getMockBuilder('Magento\\Sales\\Model\\Resource\\Order\\Payment')->disableOriginalConstructor()->setMethods(['isDeleted', 'canReviewPayment', 'canFetchTransactionInfo', '__wakeUp'])->getMock();
     $paymentMock->expects($this->any())->method('canReviewPayment')->will($this->returnValue(false));
     $paymentMock->expects($this->any())->method('canFetchTransactionInfo')->will($this->returnValue(false));
     $this->preparePaymentMock($paymentMock);
     $this->prepareItemMock(1);
     $actionFlags = [\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD => false, \Magento\Sales\Model\Order::ACTION_FLAG_CANCEL => $cancelActionFlag];
     foreach ($actionFlags as $action => $flag) {
         $this->order->setActionFlag($action, $flag);
     }
     $this->order->setState(\Magento\Sales\Model\Order::STATE_NEW);
     $this->assertEquals($cancelActionFlag, $this->order->canCancel());
 }
Example #8
0
 /**
  * @throws Exception
  */
 protected function _prepareInvoice()
 {
     $this->_adyenLogger->addAdyenNotificationCronjob('Prepare invoice for order');
     //Set order state to new because with order state payment_review it is not possible to create an invoice
     if (strcmp($this->_order->getState(), \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW) == 0) {
         $this->_order->setState(\Magento\Sales\Model\Order::STATE_NEW);
     }
     $paymentObj = $this->_order->getPayment();
     // set pspReference as transactionId
     $paymentObj->setCcTransId($this->_pspReference);
     $paymentObj->setLastTransId($this->_pspReference);
     // set transaction
     $paymentObj->setTransactionId($this->_pspReference);
     //capture mode
     if (!$this->_isAutoCapture()) {
         $this->_order->addStatusHistoryComment(__('Capture Mode set to Manual'));
         $this->_adyenLogger->addAdyenNotificationCronjob('Capture mode is set to Manual');
         // show message if order is in manual review
         if ($this->_fraudManualReview) {
             // check if different status is selected
             $fraudManualReviewStatus = $this->_getFraudManualReviewStatus();
             if ($fraudManualReviewStatus != "") {
                 $status = $fraudManualReviewStatus;
                 $comment = "Adyen Payment is in Manual Review check the Adyen platform";
                 $this->_order->addStatusHistoryComment(__($comment), $status);
             }
         }
         $createPendingInvoice = (bool) $this->_getConfigData('create_pending_invoice', 'adyen_abstract', $this->_order->getStoreId());
         if (!$createPendingInvoice) {
             $this->_adyenLogger->addAdyenNotificationCronjob('Setting pending invoice is off so don\'t create an invoice wait for the capture notification');
             return;
         }
     }
     // validate if amount is total amount
     $orderCurrencyCode = $this->_order->getOrderCurrencyCode();
     $amount = $this->_adyenHelper->originalAmount($this->_value, $orderCurrencyCode);
     // add to order payment
     $date = new \DateTime();
     $this->_adyenOrderPaymentFactory->create()->setPspreference($this->_pspReference)->setMerchantReference($this->_merchantReference)->setPaymentId($paymentObj->getId())->setPaymentMethod($this->_paymentMethod)->setAmount($amount)->setTotalRefunded(0)->setCreatedAt($date)->setUpdatedAt($date)->save();
     if ($this->_isTotalAmount($paymentObj->getEntityId(), $orderCurrencyCode)) {
         $this->_createInvoice();
     } else {
         $this->_adyenLogger->addAdyenNotificationCronjob('This is a partial AUTHORISATION and the full amount is not reached');
     }
 }
 /**
  * Instantiate
  *
  * @return void
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _initCheckout()
 {
     $pre = __METHOD__ . " : ";
     $this->_logger->debug($pre . 'bof');
     $this->_order = $this->_checkoutSession->getLastRealOrder();
     if (!$this->_order->getId()) {
         $this->getResponse()->setStatusHeader(404, '1.1', 'Not found');
         throw new \Magento\Framework\Exception\LocalizedException(__('We could not find "Order" for processing'));
     }
     if ($this->_order->getState() != \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT) {
         $this->_order->setState(\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT)->save();
     }
     if ($this->_order->getQuoteId()) {
         $this->_checkoutSession->setPayfastQuoteId($this->_checkoutSession->getQuoteId());
         $this->_checkoutSession->setPayfastSuccessQuoteId($this->_checkoutSession->getLastSuccessQuoteId());
         $this->_checkoutSession->setPayfastRealOrderId($this->_checkoutSession->getLastRealOrderId());
         $this->_checkoutSession->getQuote()->setIsActive(false)->save();
         //$this->_checkoutSession->clear();
     }
     $this->_logger->debug($pre . 'eof');
     //$this->_checkout = $this->_checkoutTypes[$this->_checkoutType];
 }
Example #10
0
 protected function _processInvoice()
 {
     if ($this->_order->canInvoice()) {
         $invoice = $this->_order->prepareInvoice();
         switch ($this->_paymentInst->getConfigPaymentAction()) {
             case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE:
                 $invoice->register();
                 $this->_order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, 'authorized');
                 break;
             case \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE_CAPTURE:
                 $this->_paymentInst->setAssistCaptureResponse(true);
                 $invoice->register()->capture();
                 break;
         }
         /** @var \Magento\Framework\DB\Transaction $transaction */
         $transaction = $this->_transactionFactory->create();
         $transaction->addObject($invoice)->addObject($invoice->getOrder())->save();
         $this->_invoiceSender->send($invoice);
     } elseif ($this->_order->isCanceled()) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Order canceled'));
     } else {
         throw new \Magento\Framework\Exception\LocalizedException(__('Order paid'));
     }
 }
Example #11
0
 /**
  * Set appropriate state to order or add status to order history
  *
  * @param Order $order
  * @param string $orderState
  * @param string $orderStatus
  * @param bool $isCustomerNotified
  * @return void
  */
 protected function updateOrder(Order $order, $orderState, $orderStatus, $isCustomerNotified)
 {
     // add message if order was put into review during authorization or capture
     $message = $order->getCustomerNote();
     $originalOrderState = $order->getState();
     $originalOrderStatus = $order->getStatus();
     switch (true) {
         case $message && $originalOrderState == Order::STATE_PAYMENT_REVIEW:
             $order->addStatusToHistory($originalOrderStatus, $message, $isCustomerNotified);
             break;
         case $message:
         case $originalOrderState && $message:
         case $originalOrderState != $orderState:
         case $originalOrderStatus != $orderStatus:
             $order->setState($orderState)->setStatus($orderStatus)->addStatusHistoryComment($message)->setIsCustomerNotified($isCustomerNotified);
             break;
         default:
             break;
     }
 }
Example #12
0
 /**
  * @param Order $order
  */
 public function validateCoinGateCallback(Order $order)
 {
     try {
         if (!$order || !$order->getIncrementId()) {
             $request_order_id = filter_input(INPUT_POST, 'order_id') ? filter_input(INPUT_POST, 'order_id') : filter_input(INPUT_GET, 'order_id');
             throw new \Exception('Order #' . $request_order_id . ' does not exists');
         }
         $payment = $order->getPayment();
         $get_token = filter_input(INPUT_GET, 'token');
         $token1 = $get_token ? $get_token : '';
         $token2 = $payment->getAdditionalInformation('coingate_order_token');
         if ($token2 == '' || $token1 != $token2) {
             throw new \Exception('Tokens do match.');
         }
         $request_id = filter_input(INPUT_POST, 'id') ? filter_input(INPUT_POST, 'id') : filter_input(INPUT_GET, 'id');
         $this->coingate->getOrder($request_id);
         if (!$this->coingate->success) {
             throw new \Exception('CoinGate Order #' . $request_id . ' does not exist');
         }
         if (!is_array($this->coingate->response)) {
             throw new \Exception('Something wrong with callback');
         }
         if ($this->coingate->response['status'] == 'paid') {
             $order->setState(Order::STATE_PROCESSING, TRUE)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING))->save();
         } elseif (in_array($this->coingate->response['status'], array('invalid', 'expired', 'canceled'))) {
             $order->setState(Order::STATE_CANCELED, TRUE)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_CANCELED))->save();
         }
     } catch (\Exception $e) {
         exit('Error occurred: ' . $e);
     }
 }
Example #13
0
 public function procesWebhook($json = null)
 {
     /**
      * Se valida el request y se transforma con la cadena a un objeto de tipo CpOrderInfo con el Factory
      */
     if (empty($json) || !($resp_webhook = Factory::cpOrderInfo($json))) {
         die('Tipo de Request no Valido');
     }
     /**
      * Gurdamos la informacion necesaria para el Cliente
      * las llaves de compropago y el modo de ejecucion de la tienda
      */
     $publickey = $this->model->getPublicKey();
     $privatekey = $this->model->getPrivateKey();
     $live = $this->model->getLiveMode();
     // si es modo pruebas cambiar por 'false'
     /**
      * Se valida que las llaves no esten vacias (No es obligatorio pero si recomendado)
      */
     //keys set?
     if (empty($publickey) || empty($privatekey)) {
         die("Se requieren las llaves de compropago");
     }
     try {
         /**
          * Se incializa el cliente
          */
         $client = new Client($publickey, $privatekey, $live);
         /**
          * Validamos que nuestro cliente pueda procesar informacion
          */
         Validations::validateGateway($client);
     } catch (\Throwable $e) {
         //something went wrong at sdk lvl
         die($e->getMessage());
     }
     /**
      * Verificamos si recivimos una peticion de prueba
      */
     if ($resp_webhook->getId() == "ch_00000-000-0000-000000") {
         die("Probando el WebHook?, <b>Ruta correcta.</b>");
     }
     try {
         /**
          * Verificamos la informacion del Webhook recivido
          */
         $response = $client->api->verifyOrder($resp_webhook->getId());
         /**
          * Comprovamos que la verificacion fue exitosa
          */
         if ($response->getType() == 'error') {
             die('Error procesando el nĂºmero de orden');
         }
         $this->orderManager->loadByIncrementId($response->getOrderInfo()->getOrderId());
         /**
          * Generamos las rutinas correspondientes para cada uno de los casos posible del webhook
          */
         switch ($response->getType()) {
             case 'charge.success':
                 $this->orderManager->setState('processing');
                 break;
             case 'charge.pending':
                 $this->orderManager->setState('pending_payment');
                 break;
             case 'charge.declined':
                 $this->orderManager->setState('canceled');
                 break;
             case 'charge.expired':
                 $this->orderManager->setState('canceled');
                 break;
             case 'charge.deleted':
                 $this->orderManager->setState('canceled');
                 break;
             case 'charge.canceled':
                 $this->orderManager->setState('canceled');
                 break;
             default:
                 die('Invalid Response type');
         }
     } catch (\Exception $e) {
         //something went wrong at sdk lvl
         die($e->getMessage());
     }
 }
Example #14
0
 /**
  * @param Order $order
  */
 public function validateKhipuCallback(Order $order)
 {
     try {
         if (!$order || !$order->getIncrementId()) {
             throw new \Exception('Order #' . $_REQUEST['order_id'] . ' does not exists');
         }
         $payment = $order->getPayment();
         $notificationToken = isset($_POST['notification_token']) ? $_POST['notification_token'] : '';
         if ($notificationToken == '') {
             throw new \Exception('Invalid notification token.');
         }
         $configuration = new \Khipu\Configuration();
         $configuration->setSecret($this->getConfigData('merchant_secret'));
         $configuration->setReceiverId($this->getConfigData('merchant_id'));
         $configuration->setPlatform('magento2-khipu', Payment::KHIPU_MAGENTO_VERSION);
         $client = new \Khipu\ApiClient($configuration);
         $payments = new \Khipu\Client\PaymentsApi($client);
         try {
             $paymentResponse = $payments->paymentsGet($notificationToken);
         } catch (\Khipu\ApiException $exception) {
             throw new \Exception(print_r($exception->getResponseObject(), TRUE));
         }
         if ($paymentResponse->getReceiverId() != $this->getConfigData('merchant_id')) {
             throw new \Exception('Invalid receiver id');
         }
         if ($paymentResponse->getTransactionId() != $payment->getAdditionalInformation('khipu_order_token')) {
             throw new \Exception('Invalid transaction id');
         }
         if ($paymentResponse->getStatus() != 'done') {
             throw new \Exception('Payment not done');
         }
         if ($paymentResponse->getAmount() != number_format($order->getGrandTotal(), $this->getDecimalPlaces($order->getOrderCurrencyCode()), '.', '')) {
             throw new \Exception('Amount mismatch');
         }
         if ($paymentResponse->getCurrency() != $order->getOrderCurrencyCode()) {
             throw new \Exception('Currency mismatch');
         }
         $order->setState(Order::STATE_PROCESSING, TRUE)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING))->save();
     } catch (\Exception $e) {
         exit('Error occurred: ' . $e);
     }
 }
Example #15
0
 /**
  * Update Order Status and State
  * @param \Magento\Sales\Model\Order $order
  * @param string $state
  */
 public function setOrderStatusByState($order, $state)
 {
     $order->setState($state)->setStatus($order->getConfig()->getStateDefaultStatus($state));
 }