Esempio n. 1
0
 /**
  * Prepares block data
  *
  * @return void
  */
 protected function prepareBlockData()
 {
     $order_is_ok = true;
     $order_error_message = '';
     if (!($order = $this->_checkoutSession->getLastRealOrder())) {
         $order_error_message = __('Couldn\'t extract order information.');
     } elseif ($order->getState() != Order::STATE_NEW) {
         $order_error_message = __('Order was already processed or session information expired.');
     } elseif (!($additional_info = $order->getPayment()->getAdditionalInformation()) or !is_array($additional_info) or empty($additional_info['sp_method']) or empty($additional_info['sp_transaction'])) {
         $order_error_message = __('Couldn\'t extract payment information from order.');
     }
     if (!empty($order_error_message)) {
         $order_is_ok = false;
     }
     $smart2pay_config = $this->_s2pModel->getFullConfigArray();
     $merchant_transaction_id = $order->getRealOrderId();
     // assume live environment if we don't get something valid from config
     if (empty($smart2pay_config['environment']) or !($environment = Environment::validEnvironment($smart2pay_config['environment']))) {
         $environment = Environment::ENV_LIVE;
     }
     if ($environment == Environment::ENV_DEMO) {
         $merchant_transaction_id = $this->_helper->convert_to_demo_merchant_transaction_id($merchant_transaction_id);
     }
     $form_data = $smart2pay_config;
     if ($order_is_ok) {
         $form_data['environment'] = $environment;
         $form_data['method_id'] = !empty($additional_info['sp_method']) ? intval($additional_info['sp_method']) : 0;
         $form_data['order_id'] = $merchant_transaction_id;
         $form_data['currency'] = $order->getOrderCurrency()->getCurrencyCode();
         $form_data['amount'] = number_format($order->getGrandTotal(), 2, '.', '') * 100;
         //anonymous user, get the info from billing details
         if ($order->getCustomerId() === null) {
             $form_data['customer_last_name'] = $this->_helper->s2p_mb_substr($order->getBillingAddress()->getLastname(), 0, 30);
             $form_data['customer_first_name'] = $this->_helper->s2p_mb_substr($order->getBillingAddress()->getFirstname(), 0, 30);
             $form_data['customer_name'] = $this->_helper->s2p_mb_substr($form_data['customer_first_name'] . ' ' . $form_data['customer_last_name'], 0, 30);
         } else {
             $form_data['customer_name'] = $this->_helper->s2p_mb_substr($order->getCustomerName(), 0, 30);
             $form_data['customer_last_name'] = $this->_helper->s2p_mb_substr($order->getCustomerLastname(), 0, 30);
             $form_data['customer_first_name'] = $this->_helper->s2p_mb_substr($order->getCustomerFirstname(), 0, 30);
         }
         $form_data['customer_email'] = trim($order->getCustomerEmail());
         $form_data['country'] = $order->getBillingAddress()->getCountryId();
         $messageToHash = 'MerchantID' . $form_data['mid'] . 'MerchantTransactionID' . $form_data['order_id'] . 'Amount' . $form_data['amount'] . 'Currency' . $form_data['currency'] . 'ReturnURL' . $form_data['return_url'];
         if ($form_data['site_id']) {
             $messageToHash .= 'SiteID' . $form_data['site_id'];
         }
         $messageToHash .= 'CustomerName' . $form_data['customer_name'];
         $messageToHash .= 'CustomerLastName' . $form_data['customer_last_name'];
         $messageToHash .= 'CustomerFirstName' . $form_data['customer_first_name'];
         $messageToHash .= 'CustomerEmail' . $form_data['customer_email'];
         $messageToHash .= 'Country' . $form_data['country'];
         $messageToHash .= 'MethodID' . $form_data['method_id'];
         $form_data['order_description'] = 'Ref. no.: ' . $form_data['order_id'];
         if (empty($form_data['product_description_ref'])) {
             $form_data['order_description'] = $form_data['product_description_custom'];
         }
         $messageToHash .= 'Description' . $form_data['order_description'];
         $form_data['skip_hpp'] = 0;
         if ($form_data['skip_payment_page'] and (!in_array($form_data['method_id'], [Smart2Pay::PAYMENT_METHOD_BT, Smart2Pay::PAYMENT_METHOD_SIBS]) or $form_data['notify_payment_instructions'])) {
             $form_data['skip_hpp'] = 1;
             $messageToHash .= 'SkipHpp1';
         }
         if ($form_data['redirect_in_iframe']) {
             $messageToHash .= 'RedirectInIframe1';
         }
         if ($form_data['skin_id']) {
             $messageToHash .= 'SkinID' . $form_data['skin_id'];
         }
         $messageToHash .= $form_data['signature'];
         $form_data['message_to_hash'] = $this->_helper->s2p_mb_strtolower($messageToHash);
         $form_data['hash'] = $this->_helper->computeSHA256Hash($messageToHash);
         $this->_s2pLogger->write('Form hash: [' . $messageToHash . ']', 'info');
         $s2p_transaction = $this->_s2pTransaction->create();
         $s2p_transaction->setID($additional_info['sp_transaction'])->setMethodID($form_data['method_id'])->setMerchantTransactionID($form_data['order_id'])->setSiteID($form_data['site_id'])->setEnvironment($form_data['environment']);
         $s2p_transaction->save();
         $order->addStatusHistoryComment('Smart2Pay :: redirecting to payment page with MethodID: ' . $form_data['method_id']);
         $order->save();
     }
     $this->addData(['order_ok' => $order_is_ok, 'error_message' => $order_error_message, 'order_id' => $order->getIncrementId(), 'form_data' => $form_data]);
 }
Esempio n. 2
0
 protected function _loadCache()
 {
     if (!($raw_input = @file_get_contents('php://input'))) {
         $this->_s2pLogger->write('No input');
         echo 'No input';
         exit;
     }
     $this->_s2pLogger->write(' ### Notification START');
     $method_config = $this->_s2pModel->getFullConfigArray();
     parse_str($raw_input, $response);
     $recomposedHashString = '';
     if (!empty($raw_input)) {
         $pairs = explode('&', $raw_input);
         foreach ($pairs as $pair) {
             $nv = explode("=", $pair, 2);
             if (!isset($nv[1])) {
                 continue;
             }
             if (strtolower($nv[0]) != 'hash') {
                 $recomposedHashString .= $nv[0] . $nv[1];
             }
         }
     }
     $recomposedHashString .= $method_config['signature'];
     $this->_s2pLogger->write('NotificationRecevied: "' . $raw_input . '"');
     if (empty($response['Hash'])) {
         $response['Hash'] = '';
     }
     if (empty($response['StatusID'])) {
         $response['StatusID'] = 0;
     }
     $log_message = '';
     /* @var \Magento\Sales\Model\Order $order */
     $order = $this->_orderFactory->create();
     // Message is intact
     if ($this->_helper->computeSHA256Hash($recomposedHashString) != $response['Hash']) {
         $this->_s2pLogger->write('Hashes do not match! received: [' . $response['Hash'] . '] recomposed [' . $this->_helper->computeSHA256Hash($recomposedHashString) . ']', 'error');
     } elseif (empty($response['MerchantTransactionID'])) {
         $this->_s2pLogger->write('Unknown merchant transaction ID in request', 'error');
     } elseif (!$order->loadByIncrementId($response['MerchantTransactionID']) or !$order->getEntityId()) {
         $this->_s2pLogger->write('Unknown order', 'error');
     } else {
         $this->_s2pLogger->write('Hashes match');
         $order->addStatusHistoryComment('Smart2Pay notification : "' . $raw_input . '"');
         /**
          * Check status ID
          */
         switch ($response['StatusID']) {
             case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_OPEN:
                 if (!empty($response['MethodID']) and $method_config['notify_payment_instructions'] and in_array($response['MethodID'], [\Smart2Pay\GlobalPay\Model\Smart2Pay::PAYMENT_METHOD_BT, \Smart2Pay\GlobalPay\Model\Smart2Pay::PAYMENT_METHOD_SIBS])) {
                     $payment_details_arr = self::defaultPaymentDetailsParams();
                     if (isset($response['ReferenceNumber'])) {
                         $payment_details_arr['reference_number'] = $response['ReferenceNumber'];
                     }
                     if (isset($response['AmountToPay'])) {
                         $payment_details_arr['amount_to_pay'] = $response['AmountToPay'];
                     }
                     if (isset($response['AccountHolder'])) {
                         $payment_details_arr['account_holder'] = $response['AccountHolder'];
                     }
                     if (isset($response['BankName'])) {
                         $payment_details_arr['bank_name'] = $response['BankName'];
                     }
                     if (isset($response['AccountNumber'])) {
                         $payment_details_arr['account_number'] = $response['AccountNumber'];
                     }
                     if (isset($response['AccountCurrency'])) {
                         $payment_details_arr['account_currency'] = $response['AccountCurrency'];
                     }
                     if (isset($response['SWIFT_BIC'])) {
                         $payment_details_arr['swift_bic'] = $response['SWIFT_BIC'];
                     }
                     if (isset($response['IBAN'])) {
                         $payment_details_arr['iban'] = $response['IBAN'];
                     }
                     if (isset($response['EntityNumber'])) {
                         $payment_details_arr['entity_number'] = $response['EntityNumber'];
                     }
                     // Inform customer
                     if ($this->sendPaymentDetails($order, $payment_details_arr)) {
                         $order->addStatusHistoryComment('Smart2Pay :: Sending payment details to client.');
                     }
                 }
                 break;
             case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_SUCCESS:
                 // cheking amount  and currency
                 $orderAmount = number_format($order->getGrandTotal(), 2, '.', '') * 100;
                 $orderCurrency = $order->getOrderCurrency()->getCurrencyCode();
                 if (strcmp($orderAmount, $response['Amount']) != 0 or $orderCurrency != $response['Currency']) {
                     $order->addStatusHistoryComment('Smart2Pay :: Notification has different amount [' . $orderAmount . '/' . $response['Amount'] . '] and/or currency [' . $orderCurrency . '/' . $response['Currency'] . ']! Please contact support@smart2pay.com', $method_config['order_status_on_4']);
                     $this->_s2pLogger->write('Currency or amount doesn\'t match for order [' . $order->getRealOrderId() . '].');
                 } else {
                     $order->addStatusHistoryComment('Smart2Pay :: Order has been paid.', $method_config['order_status_on_2']);
                     $this->_s2pLogger->write('Order paid');
                     // Generate invoice
                     if ($method_config['auto_invoice']) {
                         // Create and pay Order Invoice
                         if (!$order->canInvoice()) {
                             $this->_s2pLogger->write('Order can not be invoiced', 'warning');
                         } else {
                             /** @var \Magento\Sales\Model\Order\Invoice $invoice */
                             $invoice = $order->prepareInvoice();
                             $invoice->setRequestedCaptureCase(Invoice::CAPTURE_OFFLINE);
                             $invoice->register();
                             $this->_dbTransaction->addObject($invoice)->addObject($invoice->getOrder());
                             $this->_dbTransaction->save();
                             $order->addStatusHistoryComment('Smart2Pay :: Order has been automatically invoiced.', $method_config['order_status_on_2']);
                         }
                     }
                     // Check shipment
                     if (!empty($method_config['auto_ship'])) {
                         if (!$order->canShip()) {
                             $this->_s2pLogger->write('Order can not be shipped', 'warning');
                         } else {
                             //! TODO: Find how to do auto-shipping
                             //$itemQty =  $order->getItemsCollection()->count();
                             //$shipment = Mage::getModel( 'sales/service_order', $order )->prepareShipment( $itemQty );
                             //$shipment = new Mage_Sales_Model_Order_Shipment_Api();
                             //$shipmentId = $shipment->create( $order->getIncrementId() );
                             //$order->addStatusHistoryComment( 'Smart2Pay :: order has been automatically shipped.', $method_config['order_status_on_2'] );
                         }
                     }
                     // Inform customer
                     if ($method_config['notify_customer']) {
                         if ($this->informCustomer($order, $response['Amount'], $response['Currency'])) {
                             $order->addStatusHistoryComment('Smart2Pay :: Customer informed about successful payment.');
                         }
                     }
                 }
                 break;
                 // Status = canceled
             // Status = canceled
             case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_CANCELLED:
                 $order->addStatusHistoryComment('Smart2Pay :: payment has been canceled.', $method_config['order_status_on_3']);
                 if (!$order->canCancel()) {
                     $this->_s2pLogger->write('Can not cancel the order', 'warning');
                 } else {
                     $order->cancel();
                 }
                 break;
                 // Status = failed
             // Status = failed
             case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_FAILED:
                 $order->addStatusHistoryComment('Smart2Pay :: payment has failed.', $method_config['order_status_on_4']);
                 break;
                 // Status = expired
             // Status = expired
             case \Smart2Pay\GlobalPay\Model\Smart2Pay::S2P_STATUS_EXPIRED:
                 $order->addStatusHistoryComment('Smart2Pay :: payment has expired.', $method_config['order_status_on_5']);
                 break;
             default:
                 $order->addStatusHistoryComment('Smart2Pay status "' . $response['StatusID'] . '" occurred.', $method_config['order_status']);
                 break;
         }
         $order->save();
         if ($s2p_transaction_obj = $this->_s2pTransaction->create()->loadByMerchantTransactionId($response['MerchantTransactionID']) and $s2p_transaction_obj->getID()) {
             if (isset($response['PaymentID'])) {
                 $s2p_transaction_obj->setPaymentID($response['PaymentID']);
             }
             if (isset($response['StatusID'])) {
                 $s2p_transaction_obj->setPaymentStatus($response['StatusID']);
             }
             $s2p_transaction_extra_arr = array();
             $s2p_default_transaction_extra_arr = \Smart2Pay\GlobalPay\Helper\Smart2Pay::defaultTransactionLoggerExtraParams();
             foreach ($s2p_default_transaction_extra_arr as $key => $val) {
                 if (array_key_exists($key, $response)) {
                     $s2p_transaction_extra_arr[$key] = $response[$key];
                 }
             }
             if (!empty($s2p_transaction_extra_arr)) {
                 $s2p_transaction_obj->setExtraDataArray($s2p_transaction_extra_arr);
             }
             $s2p_transaction_obj->save();
         }
         // NotificationType IS payment
         if (strtolower($response['NotificationType']) == 'payment') {
             // prepare string for 'da hash
             $responseHashString = "notificationTypePaymentPaymentId" . $response['PaymentID'] . $method_config['signature'];
             // prepare response data
             $responseData = array('NotificationType' => 'Payment', 'PaymentID' => $response['PaymentID'], 'Hash' => $this->_helper->computeSHA256Hash($responseHashString));
             // output response
             echo 'NotificationType=payment&PaymentID=' . $responseData['PaymentID'] . '&Hash=' . $responseData['Hash'];
         }
     }
     if (!empty($error_message)) {
         $this->_s2pLogger->write($error_message);
         $this->_s2pLogger->write(' ### Notification END');
         echo $error_message;
         exit;
     }
     $this->_s2pLogger->write(' ### Notification END');
     exit;
 }