/** * Constructor for PayzenResponse class. Prepare to analyse check url or return url call. * @param array[string]string $parameters $_REQUEST by default * @param string $ctx_mode * @param string $key_test * @param string $key_prod * @param string $encoding */ function __construct($parameters = null, $ctx_mode = null, $key_test = null, $key_prod = null) { $this->api = new PayzenApi(); // Use default API encoding (UTF-8) since the payment platform returns UTF-8 data if (is_null($parameters)) { $parameters = $_REQUEST; } $parameters = $this->api->uncharm($parameters); // Load site credentials if provided if (!is_null($ctx_mode)) { $this->api->set('vads_ctx_mode', $ctx_mode); } if (!is_null($key_test)) { $this->api->set('vads_key_test', $key_test); } if (!is_null($key_prod)) { $this->api->set('vads_key_prod', $key_prod); } $this->load($parameters, $this->api->getCertificate()); }
/** * Validate and save module admin parameters */ private function _postProcess() { $api = new PayzenApi(); // new instance of PayzenApi for parameters validation // load and validate from request foreach ($this->_getAdminParameters() as $param) { $key = $param['key']; $label = $this->l($param['label'], 'payzenadmindisplay', null); $value = Tools::getValue($key, null); if (in_array($key, $this->_multiLangFields)) { $value = array(); foreach (Language::getLanguages(false) as $language) { $value[$language['id_lang']] = Tools::getValue($key . '_' . $language['id_lang'], ''); } } elseif ($key === 'PAYZEN_MULTI_OPTIONS') { if (!is_array($value) || empty($value)) { $value = array(); } else { $error = false; foreach ($value as $opt => $option) { if (!$option['label'] || !is_numeric($option['count']) || !is_numeric($option['period']) || $option['first'] && (!is_numeric($option['first']) || $option['first'] < 0 || $option['first'] > 100)) { unset($value[$opt]); // not save this option $error = true; } } if ($error) { $this->_errors[] = $this->l('One or more values are invalid for field "Payment options". Only valid lines are saved.'); } } $value = serialize($value); } elseif ($key === 'PAYZEN_AVAILABLE_LANGUAGES' || $key === 'PAYZEN_PAYMENT_CARDS') { $value = is_array($value) && count($value) > 0 ? implode(';', $value) : ''; if ($value == '' && $key === 'PAYZEN_PAYMENT_CARDS') { // empty values for PAYZEN_PAYMENT_CARDS are not allowed $this->_errors[] = $this->l('You must select at least one card type.'); continue; } } elseif ($key === 'PAYZEN_ONEY_SHIP_OPTIONS') { if (!is_array($value) || empty($value)) { $value = array(); } else { foreach ($value as $id => $option) { $carrier = $option['carrier_label'] . ($option['address'] ? ' ' . $option['address'] : ''); if (!preg_match(self::DELIVERY_COMPANY_REGEX, $carrier)) { unset($value[$id]['address']); // not save this option $this->_errors[] = sprintf($this->l('Invalid value "%s" for field "%s".'), $option['carrier_label'] . ' ' . $option['address'], $label); } } } $value = serialize($value); } elseif ($key === 'PAYZEN_ONEY_PROD_CAT') { if (!is_array($value) || empty($value)) { $value = array(); } $value = serialize($value); } elseif ($key === 'PAYZEN_ONEY_ENABLED' && $value == 'True') { $oneyErrors = $this->_validateOney(); if (is_array($oneyErrors) && !empty($oneyErrors)) { $this->_errors = array_merge($this->_errors, $oneyErrors); $value = 'False'; // there is errors, not allow Oney activation } } elseif (in_array($key, $this->_amountFields)) { if (!empty($value) && (!is_numeric($value) || $value < 0)) { $this->_errors[] = sprintf($this->l('Invalid value "%s" for field "%s".'), $value, $label); continue; } } elseif ($key === 'PAYZEN_STD_CARD_DATA_MODE' && $value == '3' && !$this->_checkSsl()) { $value = '1'; $this->_errors[] = $this->l('The card data entry on merchant site cannot be used without enabling SSL.'); } // validate with PayzenApi if (key_exists('name', $param) && isset($param['name']) && !$api->set($param['name'], $value)) { if (empty($value)) { $this->_errors[] = sprintf($this->l('The field "%s" is mandatory.'), $label); } else { $this->_errors[] = sprintf($this->l('Invalid value "%s" for field "%s".'), $value, $label); } continue; } // valid field : try to save into DB if (!Configuration::updateValue($key, $value)) { $this->_errors[] = sprintf($this->l('Problem occured while saving field "%s".'), $label); } else { // temporary variable set to update PrestaShop cache Configuration::set($key, $value); } } }
/** * Prepare data and redirect to PayZen payment platform * * @param string $order_number * @param object $orderData * @param string $return_context the session id * @param string $html the form to display * @param bool $new_status false if it should not be changed, otherwise new staus * @return NULL */ function plgVmConfirmedOrder($cart, $order) { if (!($method = $this->getVmPluginMethod($order['details']['BT']->virtuemart_paymentmethod_id))) { return NULL; // Another method was selected, do nothing } if (!$this->selectedThisElement($method->payment_element)) { return FALSE; } $this->_debug = $method->debug; // enable debug $session = JFactory::getSession(); $return_context = $session->getId(); $this->logInfo('plgVmOnConfirmedOrderGetPaymentForm -- order number: ' . $order['details']['BT']->order_number, 'message'); if (!class_exists('PayzenApi')) { require JPATH_VMPAYMENTPLUGIN_PAYZEN . DS . 'payzen' . DS . 'payzen_api.php'; } $api = new PayzenApi('UTF-8'); // set config parameters $paramNames = array('platform_url', 'key_test', 'key_prod', 'capture_delay', 'ctx_mode', 'site_id', 'validation_mode', 'redirect_enabled', 'redirect_success_timeout', 'redirect_success_message', 'redirect_error_timeout', 'redirect_error_message', 'return_mode'); foreach ($paramNames as $name) { $api->set($name, $method->{$name}); } // Set urls $url_return = JROUTE::_(JURI::root() . 'index.php?option=com_virtuemart&view=pluginresponse&task=pluginresponsereceived'); $uri = JURI::getInstance($url_return); //$uri->setVar('pelement', $this->payment_element); $uri->setVar('pm', $order['details']['BT']->virtuemart_paymentmethod_id); $uri->setVar('Itemid', JRequest::getInt('Itemid')); $api->set('url_return', $uri->toString()); $url_cancel = JROUTE::_(JURI::root() . 'index.php?option=com_virtuemart&view=pluginresponse&task=pluginUserPaymentCancel'); $uri = JURI::getInstance($url_cancel); $uri->setVar('on', $order['details']['BT']->order_number); $uri->setVar('pm', $order['details']['BT']->virtuemart_paymentmethod_id); $uri->setVar('Itemid', JRequest::getInt('Itemid')); $api->set('url_cancel', $uri->toString()); // Set the language code $lang = JFactory::getLanguage(); $lang->load('plg_vmpayment_' . $this->_name, JPATH_ADMINISTRATOR); $tag = substr($lang->get('tag'), 0, 2); $language = in_array($tag, $api->getSupportedLanguages()) ? $tag : ($method->language ? $method->language : 'fr'); $api->set('language', $language); // Set currency if (!class_exists('VirtueMartModelCurrency')) { require JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'currency.php'; } $currencyModel = new VirtueMartModelCurrency(); $currencyObj = $currencyModel->getCurrency($cart->pricesCurrency); $currency = $api->findCurrencyByNumCode($currencyObj->currency_numeric_code); if ($currency == NULL) { $this->logInfo('plgVmOnConfirmedOrderGetPaymentForm -- Could not find currency numeric code for currency : ' . $currencyObj->currency_numeric_code, 'error'); vmInfo(JText::_('VMPAYMENT_' . $this->_name . '_CURRENCY_NOT_SUPPORTED')); return NULL; } $api->set('currency', $currency->num); // payment_cards may be one value or array $cards = $method->payment_cards; $cards = !is_array($cards) ? $cards : (in_array("", $cards) ? "" : implode(";", $cards)); $api->set('payment_cards', $cards); // available_languages may be one value or array $available_languages = $method->available_languages; $available_languages = !is_array($available_languages) ? $available_languages : (in_array("", $available_languages) ? "" : implode(";", $available_languages)); $api->set('available_languages', $available_languages); $api->set('contrib', 'VirtueMart2.0.8_1.3c/' . vmVersion::$RELEASE); // Set customer info // $usr = JFactory::getUser(); $usrBT = $order['details']['BT']; $usrST = isset($order['details']['ST']) ? $order['details']['ST'] : $order['details']['BT']; $api->set('cust_email', $usrBT->email); // $api->set('cust_id', ''); $api->set('cust_title', @$usrBT->title); $api->set('cust_first_name', $usrBT->first_name); $api->set('cust_last_name', $usrBT->last_name); $api->set('cust_address', $usrBT->address_1 . ' ' . $usrBT->address_2); $api->set('cust_zip', $usrBT->zip); $api->set('cust_city', $usrBT->city); $api->set('cust_state', @ShopFunctions::getStateByID($usrBT->virtuemart_state_id)); $api->set('cust_country', @ShopFunctions::getCountryByID($usrBT->virtuemart_country_id, 'country_2_code')); $api->set('cust_phone', $usrBT->phone_1); $api->set('cust_cell_phone', $usrBT->phone_2); $api->set('ship_to_first_name', $usrST->first_name); $api->set('ship_to_last_name', $usrST->last_name); $api->set('ship_to_city', $usrST->city); $api->set('ship_to_street', $usrST->address_1); $api->set('ship_to_street2', $usrST->address_2); $api->set('ship_to_state', @ShopFunctions::getStateByID($usrST->virtuemart_state_id)); $api->set('ship_to_country', @ShopFunctions::getCountryByID($usrST->virtuemart_country_id, 'country_2_code')); $api->set('ship_to_phone_num', $usrST->phone_1); $api->set('ship_to_zip', $usrST->zip); // Set order_id $api->set('order_id', $order['details']['BT']->order_number); // Set the amount to pay $exchangeRate = $currencyObj->currency_exchange_rate; if ($exchangeRate == 0) { $this->logInfo('plgVmOnConfirmedOrderGetPaymentForm -- Exchange rate for currency ' . $currencyObj->currency_numeric_code . ' must not be null.', 'warn'); if ($method->ctx_mode == 'TEST') { vmInfo(JText::_('VMPAYMENT_' . $this->_name . '_EXCHANGE_RATE_NULL')); return NULL; } else { // not consider exchange rate $exchangeRate = 1; } } $amount = $order['details']['BT']->order_total * $exchangeRate; $api->set('amount', $currency->convertAmountToInteger($amount)); // 3DS activation according to amount $threeds_mpi = null; if ($method->threeds_min_amount != '' && $amount < $method->threeds_min_amount) { $threeds_mpi = '2'; } $api->set('threeds_mpi', $threeds_mpi); // Prepare data that should be stored in the database $dbValues['order_number'] = $order['details']['BT']->order_number; $dbValues['payment_name'] = $this->renderPluginName($method, $order); $dbValues['virtuemart_paymentmethod_id'] = $cart->virtuemart_paymentmethod_id; $dbValues[$this->_name . '_custom'] = $return_context; $this->storePSPluginInternalData($dbValues); $this->logInfo('plgVmOnConfirmedOrderGetPaymentForm -- payment data saved to table ' . $this->_tablename, 'message'); // echo the redirect form $form = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Redirection</title></head><body><div style="margin: auto; text-align: center;">'; $form .= '<p>' . JText::_('VMPAYMENT_' . $this->_name . '_PLEASE_WAIT') . '</p>'; $form .= '<p>' . JText::_('VMPAYMENT_' . $this->_name . '_CLICK_BUTTON_IF_NOT_REDIRECTED') . '</p>'; $form .= '<form action="' . $api->platformUrl . '" method="POST" name="vm_' . $this->_name . '_form" >'; $form .= '<input type="image" name="submit" src="' . JURI::base(TRUE) . '/images/stories/virtuemart/payment/' . $this->_name . '.png" alt="' . JText::_('VMPAYMENT_' . $this->_name . '_BTN_ALT') . '" title="' . JText::_('VMPAYMENT_PAYZEN_BTN_ALT') . '"/>'; $form .= $api->getRequestFieldsHtml(); $form .= '</form></div>'; $form .= '<script type="text/javascript">document.forms[0].submit();</script></body></html>'; $this->logInfo('plgVmOnConfirmedOrderGetPaymentForm -- user redirected to ' . $this->_name, 'message'); echo $form; $cart->_confirmDone = FALSE; $cart->_dataValidated = FALSE; $cart->setCartIntoSession(); die; // not save order, not send mail, do redirect }