Example #1
0
 public function initContent()
 {
     parent::initContent();
     $cart = $this->context->cart;
     $link = $this->context->link;
     $cookie = $this->context->cookie;
     //Payment methods
     $payment_method = Tools::getValue('payment_method', 'none');
     $payment_methods = $this->module->getPaymentMethods();
     if (!isset($payment_methods[$payment_method])) {
         Tools::redirect('index.php?controller=order');
     }
     $payment_method_name = $payment_methods[$payment_method];
     //Currency
     $currency = $this->context->currency;
     $valid_currencies = $this->module->getValidCurrencies();
     $shop_currencies = Currency::getCurrencies();
     $currencies = array();
     foreach ($shop_currencies as $c) {
         if (in_array($c['iso_code'], $valid_currencies)) {
             array_push($currencies, $c['name']);
         }
     }
     //Price
     $totalAmount = $cart->getOrderTotal();
     //Module configurations
     $cfg = $this->module->getConfigurations();
     //Common tpl varialbles
     $this->context->smarty->assign(array('cfg' => $cfg, 'currency' => $currency, 'validCurrencyNames' => implode(', ', $currencies), 'isValidCurrency' => in_array($currency->iso_code, $valid_currencies), 'total' => $totalAmount, 'paymentMethod' => $payment_method, 'paymentName' => $payment_method_name, 'this_path' => Tools::getShopDomain(true, true) . __PS_BASE_URI__ . MP_DIR, 'this_path_ssl' => Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . MP_DIR));
     if (!(bool) $cfg['MP_ORDER_CONFIRM'] || Tools::isSubmit('confirmOrder') || Tools::getValue('content_only', 0)) {
         if ((bool) $cfg['MP_ORDER_CREATE']) {
             $this->module->registerPaymentInfo($cart->id, $payment_method);
             //Create order
             $this->module->validateOrder($cart->id, Configuration::get('PS_OS_MASTERPAYMENT'), $totalAmount, $this->module->displayName, $this->module->l('Payment method') . ': ' . $payment_method_name, array(), $currency->id, false, $cart->secure_key);
         }
         $order = (int) $this->module->currentOrder ? new Order($this->module->currentOrder) : null;
         $customer = new Customer((int) $cart->id_customer);
         $address = new Address((int) $cart->id_address_invoice);
         //Language
         $language = strtoupper(Language::getIsoById($cookie->id_lang));
         //if language not found use default language
         if (!in_array($language, array_keys($this->module->getValidLanguages()))) {
             $language = $cfg['MP_LANGUAGE'];
         }
         //URL's
         $order_confirmation_url = $link->getModuleLink('masterpayment', 'confirmation', array('id_cart' => (int) $cart->id, 'id_module' => (int) $this->module->id, 'key' => $customer->secure_key));
         $order_error_url = $link->getModuleLink('masterpayment', 'error');
         $order_validation_url = Tools::getShopDomain(true, true) . __PS_BASE_URI__ . MP_DIR . 'validation.php';
         //	    $link->getModuleLink('masterpayment', 'validation', array());
         //MasterPayment API
         $api = new MasterPaymentApi();
         $api->iframeMode = $cfg['MP_MODE'] == 'iframe';
         $api->merchantName = $cfg['MP_MERCHANT_NAME'];
         $api->secretKey = $cfg['MP_SECRET_KEY'];
         $api->txId = MasterPayment::encodeTxID($cart);
         $api->orderId = $this->module->currentOrder;
         $api->basketDescription = str_replace(array('{order}', '{cart}', '{shop}'), array($this->module->currentOrder, $cart->id, Configuration::get('PS_SHOP_NAME')), $order ? $this->module->l('Shopping order #{order} - {shop}') : $this->module->l('Shopping cart #{cart} - {shop}'));
         $api->basketValue = Tools::ps_round($totalAmount, 2) * 100;
         $api->currency = $currency->iso_code;
         $api->language = $language;
         $api->paymentType = $payment_method;
         $api->gatewayStyle = $cfg['MP_GATEWAY_STYLE'];
         $api->UrlPatternSuccess = $order_validation_url;
         $api->UrlPatternFailure = $order_error_url;
         $api->UrlRedirectSuccess = $order_confirmation_url;
         $api->UrlRedirectFailure = $order_error_url;
         $api->UrlRedirectCancel = $order_error_url;
         $api->showCancelOption = (int) $cfg['MP_CANCEL_OPTION'];
         $api->userId = $customer->id;
         $api->sex = $customer->id_gender == 9 ? 'unknown' : $customer->id_gender == 1 ? 'man' : 'woman';
         $api->firstname = $customer->firstname;
         $api->lastname = $customer->lastname;
         $api->email = $customer->email;
         $api->street = $address->address1 . ($address->address2 != '' ? ' ' . $address->address2 : '');
         $api->houseNumber = '.';
         $api->zipCode = $address->postcode;
         $api->city = $address->city;
         $api->country = Country::getIsoById($address->id_country);
         $api->birthdate = $customer->birthday;
         $api->mobile = $address->phone ? $address->phone : $address->phone_mobile;
         $api->installmentsCount = $cfg['MP_INSTALLMENTS_COUNT'];
         $api->recurrentPeriod = $cfg['MP_RECURRENT_PERIOD'];
         $api->paymentDelay = $cfg['MP_PAYMENT_DELAY'];
         $api->dueDays = $cfg['MP_DUE_DAYS'];
         $api->invoiceNo = $order ? Configuration::get('PS_INVOICE_PREFIX') . $order->invoice_number : '';
         $api->createAsPending = 1;
         if ($cfg['MP_INSTALLMENTS_PERIOD'] == 'use_freq') {
             $api->installmentsFreq = $cfg['MP_INSTALLMENTS_FREQ'];
         } else {
             $api->installmentsPeriod = $cfg['MP_INSTALLMENTS_PERIOD'];
         }
         $this->context->smarty->assign('params', $api->getParams());
         $this->setTemplate('gateway.tpl');
     } else {
         $this->setTemplate('submit.tpl');
     }
 }
<?php

include dirname(__FILE__) . '/../../config/config.inc.php';
require_once dirname(__FILE__) . '/lib/api.php';
require_once dirname(__FILE__) . '/masterpayment.php';
function dieWithError($error)
{
    $l = date('Y-m-d H:i:s') . ' "' . $error . '" data:' . json_encode(array('post' => $_POST, 'get' => $_GET)) . "\n";
    file_put_contents(dirname(__FILE__) . '/error.log', $l, FILE_APPEND);
    die($error);
}
$module = new MasterPayment();
//Setup MasterPayment API to handle request
$api = new MasterPaymentApi();
$api->secretKey = Configuration::get('MP_SECRET_KEY');
//Get request status
$status = $api->getRequestStatus();
//Check status
if ($status == 'INVALID') {
    dieWithError('Invalid request');
}
//Extract cart ID from transaction ID
$id_cart = MasterPayment::decodeTxID($api->txId);
$totalAmount = (double) $api->basketValue / 100;
$cart = new Cart($id_cart);
if (!Validate::isLoadedObject($cart)) {
    dieWithError('Cart not found');
}
$id_order = Order::getOrderByCartId($cart->id);
$order = $id_order ? new Order($id_order) : null;
$currency = new Currency($cart->id_currency);
 public function hookAdminOrder($params)
 {
     $order = new Order((int) $params['id_order']);
     $msg = null;
     if ($this->name != $order->module) {
         return;
     }
     $cart = new Cart($order->id_cart);
     $currency = new Currency($order->id_currency);
     if (Tools::isSubmit('submitMasterPaymentRefund')) {
         $amount = (double) Tools::getValue('amount', 0);
         if ($amount > 0 && $amount <= $order->total_paid) {
             require_once dirname(__FILE__) . '/lib/api.php';
             $api = new MasterPaymentApi();
             $api->merchantName = Configuration::get('MP_MERCHANT_NAME');
             $api->secretKey = Configuration::get('MP_SECRET_KEY');
             $api->basketValue = $amount * 100;
             $api->txId = self::encodeTxID($cart);
             $comment = Tools::getValue('comment', '');
             $status = $api->refundRequest($comment);
             if ($status == MasterPaymentApi::STATUS_REFUNDED) {
                 // Update order state
                 $order->setCurrentState(Configuration::get('PS_OS_REFUND'), $this->context->employee->id);
                 // Add refund amount message
                 $msg = new Message();
                 $msg->message = $comment . ' - ' . $this->l('Refund amount') . ': ' . Tools::displayPrice($amount, $currency);
                 $msg->id_order = $order->id;
                 $msg->id_customer = $cart->id_customer;
                 $msg->private = true;
                 $msg->add();
                 // Redirect to order
                 Tools::redirectAdmin('#');
             } else {
                 $msg = '<p class="error">' . $comment . '</p>';
             }
         } else {
             $msg = '<p class="error">' . $this->l('Ivalid amount') . '</p>';
         }
     }
     $this->tplAssign('msg', $msg);
     $this->tplAssign('order', $order);
     $this->tplAssign('amount', Tools::ps_round($order->total_paid, 2));
     $this->tplAssign('currency', $currency);
     $this->_html .= $this->tplDisplay('adminOrder');
     return $this->_html;
 }