Example #1
0
 /**
  * Prepare information specific to current payment method
  *
  * @param null|\Magento\Framework\DataObject|array $transport
  * @return \Magento\Framework\DataObject
  */
 protected function _prepareSpecificInformation($transport = null)
 {
     if (null !== $this->_paymentSpecificInformation) {
         return $this->_paymentSpecificInformation;
     }
     $transport = parent::_prepareSpecificInformation($transport);
     $module_name = $this->getRequest()->getModuleName();
     $action_name = $this->getRequest()->getActionName();
     if ($details_arr = $this->getInfo()->getAdditionalInformation() and !empty($details_arr['sp_method']) and $method_obj = $this->_s2pMethod->create()->load($details_arr['sp_method'])) {
         if (!empty($details_arr['sp_transaction']) and $transaction_obj = $this->_s2pTransaction->create()->load($details_arr['sp_transaction'])) {
             if (!($payment_id = $transaction_obj->getPaymentId())) {
                 $payment_id = __('N/A')->render();
             }
             $this->_paymentSpecificInformation->setData(__('Method')->render(), $method_obj->getDisplayName());
             $this->_paymentSpecificInformation->setData(__('Environment')->render(), $transaction_obj->getEnvironment());
             $this->_paymentSpecificInformation->setData(__('Payment ID')->render(), $payment_id);
             if ($transaction_extra_arr = $transaction_obj->getExtraDataArray() and $details_titles_arr = $this->_helper->transaction_logger_params_to_title()) {
                 foreach ($transaction_extra_arr as $title_key => $val) {
                     if (empty($details_titles_arr[$title_key])) {
                         continue;
                     }
                     $this->_paymentSpecificInformation->setData($details_titles_arr[$title_key], $val);
                 }
             }
         }
     }
     $this->_paymentSpecificInformation->setData('Test', 'Test value');
     return $this->_paymentSpecificInformation;
 }
 public function getAllActiveMethods($params = false)
 {
     if (empty($params) or !is_array($params)) {
         $params = array();
     }
     if (empty($params['include_countries'])) {
         $params['include_countries'] = false;
     }
     return $this->_methodFactory->create()->getAllActiveMethods($params);
 }
 public function beforeSave()
 {
     $methods_obj = $this->_methodFactory->create();
     if (!($form_s2p_enabled_methods = $this->_helper->getParam('s2p_enabled_methods', array())) or !is_array($form_s2p_enabled_methods)) {
         $form_s2p_enabled_methods = array();
     }
     if (!($form_s2p_surcharge = $this->_helper->getParam('s2p_surcharge', array())) or !is_array($form_s2p_surcharge)) {
         $form_s2p_surcharge = array();
     }
     if (!($form_s2p_fixed_amount = $this->_helper->getParam('s2p_fixed_amount', array())) or !is_array($form_s2p_fixed_amount)) {
         $form_s2p_fixed_amount = array();
     }
     $existing_methods_params_arr = array();
     $existing_methods_params_arr['method_ids'] = $form_s2p_enabled_methods;
     $existing_methods_params_arr['include_countries'] = false;
     if (!($db_existing_methods = $methods_obj->getAllActiveMethods($existing_methods_params_arr))) {
         $db_existing_methods = array();
     }
     $this->_methods_to_save = array();
     foreach ($db_existing_methods as $method_id => $method_details) {
         if (empty($form_s2p_surcharge[$method_id])) {
             $form_s2p_surcharge[$method_id] = 0;
         }
         if (empty($form_s2p_fixed_amount[$method_id])) {
             $form_s2p_fixed_amount[$method_id] = 0;
         }
         if (!is_numeric($form_s2p_surcharge[$method_id])) {
             $this->_dataSaveAllowed = false;
             throw new \Magento\Framework\Exception\LocalizedException(__('Please provide a valid percent for method %1.', $method_details['display_name']));
         }
         if (!is_numeric($form_s2p_fixed_amount[$method_id])) {
             $this->_dataSaveAllowed = false;
             throw new \Magento\Framework\Exception\LocalizedException(__('Please provide a valid fixed amount for method %1.', $method_details['display_name']));
         }
         if (empty($this->_methods_to_save[$method_id])) {
             $this->_methods_to_save[$method_id] = array();
         }
         // TODO: add country ids instead of only 0 (all countries)
         $this->_methods_to_save[$method_id][0]['surcharge'] = $form_s2p_surcharge[$method_id];
         $this->_methods_to_save[$method_id][0]['fixed_amount'] = $form_s2p_fixed_amount[$method_id];
     }
     return $this;
 }
 /**
  * @param int $method_id ID of method to get all configurations for
  * @param bool|array $params
  *
  * @return array $return_arr[{country_code}]['surcharge'], $return_arr[{country_ids}]['fixed_amount'], ...
  */
 public function getConfiguredMethodDetails($method_id, $params = false)
 {
     $method_id = intval($method_id);
     if (empty($method_id)) {
         return false;
     }
     if (empty($params) or !is_array($params)) {
         $params = array();
     }
     if (!isset($params['only_active'])) {
         $params['only_active'] = true;
     }
     if (!isset($params['country_code'])) {
         $params['country_code'] = '';
     }
     $our_country_id = 0;
     if (!empty($params['country_code'])) {
         $params['country_code'] = strtoupper(trim($params['country_code']));
         if (strlen($params['country_code']) != 2) {
             return false;
         }
         $c_collection = $this->_countryFactory->create()->getCollection();
         $c_collection->addFieldToSelect('*');
         $c_collection->addFieldToFilter('main_table.code', $params['country_code']);
         $c_collection->getSelect()->limit(1);
         if ($country_obj = $c_collection->fetchItem() and $country_arr = $country_obj->getData()) {
             $our_country_id = $country_arr['country_id'];
         }
         if (empty($our_country_id)) {
             return false;
         }
     }
     $collection = $this->getCollection();
     $collection->addFieldToSelect('*');
     $collection->addFieldToFilter('main_table.method_id', $method_id);
     if (!empty($our_country_id)) {
         $collection->addFieldToFilter(['main_table.country_id', 'main_table.country_id'], [0, $our_country_id]);
         $collection->setOrder('main_table.country_id', 'DESC');
     }
     $method_collection = $this->_methodFactory->create()->getCollection();
     $collection->getSelect()->join($method_collection->getMainTable(), 'main_table.method_id = ' . $method_collection->getMainTable() . '.method_id');
     if (!empty($params['only_active'])) {
         $collection->addFieldToFilter($method_collection->getMainTable() . '.active', 1);
     }
     $data_arr = array();
     $countries_ids_arr = array();
     while ($configured_method_obj = $collection->fetchItem() and $configured_method_arr = $configured_method_obj->getData()) {
         if (!empty($configured_method_arr['country_id'])) {
             $countries_ids_arr[] = $configured_method_arr['country_id'];
         }
         $data_arr[$configured_method_arr['country_id']] = $configured_method_arr;
     }
     if (empty($data_arr)) {
         return false;
     }
     $ids_to_codes_arr = [0 => Country::INTERNATIONAL_CODE];
     if (!empty($countries_ids_arr)) {
         //! TODO: query countries table to obtain country codes from ids
         // ATM all method settings are same for all countries...
     }
     $return_arr = array();
     foreach ($data_arr as $country_id => $details_arr) {
         if (!isset($ids_to_codes_arr[$country_id])) {
             continue;
         }
         $return_arr[$ids_to_codes_arr[$country_id]] = $details_arr;
     }
     return !empty($return_arr) ? $return_arr : false;
 }