/**
  * @param int $id_currency : this parameter is optionnal but on 1.5 version of Prestashop, it will be REQUIRED
  * @return Currency
  */
 public function getCurrency($current_id_currency = null)
 {
     if (!(int) $current_id_currency) {
         $current_id_currency = Context::getContext()->currency->id;
     }
     if (!$this->currencies) {
         return false;
     }
     if ($this->currencies_mode == 'checkbox') {
         $currencies = Currency::getPaymentCurrencies($this->id);
         return $currencies;
     } elseif ($this->currencies_mode == 'radio') {
         $currencies = Currency::getPaymentCurrenciesSpecial($this->id);
         $currency = $currencies['id_currency'];
         if ($currency == -1) {
             $id_currency = (int) $current_id_currency;
         } elseif ($currency == -2) {
             $id_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
         } else {
             $id_currency = $currency;
         }
     }
     if (!isset($id_currency) || empty($id_currency)) {
         return false;
     }
     $currency = new Currency($id_currency);
     return $currency;
 }
 public function getCurrency()
 {
     global $cookie;
     if (!$this->currencies) {
         return false;
     }
     if ($this->currencies_mode == 'checkbox') {
         $currencies = Currency::getPaymentCurrencies($this->id);
         return $currencies;
     } elseif ($this->currencies_mode == 'radio') {
         $currencies = Currency::getPaymentCurrenciesSpecial($this->id);
         $currency = $currencies['id_currency'];
         if ($currency == -1) {
             $id_currency = intval($cookie->id_currency);
         } elseif ($currency == -2) {
             $id_currency = intval(Configuration::get('PS_CURRENCY_DEFAULT'));
         } else {
             $id_currency = $currency;
         }
     }
     if (!isset($id_currency) or empty($id_currency)) {
         return false;
     }
     return new Currency($id_currency);
 }
 */
include dirname(__FILE__) . '/../../config/config.inc.php';
include dirname(__FILE__) . '/../../init.php';
if (!defined('_PS_VERSION_')) {
    exit;
}
// To configure, add webhook in account storename.com/modules/conektaefectivo/notification.php
$body = Tools::file_get_contents('php://input');
$event_json = Tools::jsonDecode($body);
if ($event_json->type == 'charge.paid') {
    $charge = $event_json->data->object;
    $reference_id = (int) $charge->reference_id;
    $id_order = Order::getOrderByCartId($reference_id);
    $order = new Order($id_order);
    $order_fields = $order->getFields();
    $currency_payment = Currency::getPaymentCurrencies(Module::getModuleIdByName('conektaprestashop'), $order_fields['id_shop']);
    $total_order_amount = (int) $order->getOrdersTotalPaid();
    $str_total_order_amount = (string) $total_order_amount;
    $format_total_order_amount = str_pad($str_total_order_amount, strlen($str_total_order_amount) + 2, '0', STR_PAD_RIGHT);
    if ($currency_payment[0]['iso_code'] === $charge->currency) {
        if ($format_total_order_amount == $charge->amount) {
            $orderHistory = new OrderHistory();
            $orderHistory->id_order = $order;
            $orderHistory->changeIdOrderState(Configuration::get('PS_OS_PAYMENT'), $order);
            $orderHistory->addWithEmail();
            Db::getInstance()->Execute('UPDATE ' . _DB_PREFIX_ . 'conekta_transaction SET status = "paid" WHERE id_order = ' . pSQL($reference_id) . '');
        }
    }
}
header('HTTP/1.1 200 OK');
exit;
 /**
  * @param int $id_currency : this parameter is optionnal but on 1.5 version of Prestashop, it will be REQUIRED
  * @return Currency
  */
 public function getCurrency($current_id_currency = null)
 {
     if (!(int) $current_id_currency) {
         global $cookie;
     }
     if (!$this->currencies) {
         return false;
     }
     if ($this->currencies_mode == 'checkbox') {
         $currencies = Currency::getPaymentCurrencies($this->id);
         return $currencies;
     } elseif ($this->currencies_mode == 'radio') {
         $currencies = Currency::getPaymentCurrenciesSpecial($this->id);
         $currency = $currencies['id_currency'];
         if ($currency == -1) {
             // not use $cookie if $current_id_currency is set
             if ((int) $current_id_currency) {
                 $id_currency = (int) $current_id_currency;
             } else {
                 $id_currency = (int) $cookie->id_currency;
             }
         } elseif ($currency == -2) {
             $id_currency = (int) _PS_CURRENCY_DEFAULT_;
         } else {
             $id_currency = $currency;
         }
     }
     if (!isset($id_currency) or empty($id_currency)) {
         return false;
     }
     return new Currency($id_currency);
 }
 /**
  * @param int $id_currency : this parameter is optionnal but on 1.5 version of Prestashop, it will be REQUIRED
  * @return Currency
  */
 public function getCurrency($current_id_currency = NULL)
 {
     if (!$this->currencies) {
         return false;
     }
     if ($this->currencies_mode == 'checkbox') {
         $currencies = Currency::getPaymentCurrencies((int) $this->id);
         return $currencies;
     } elseif ($this->currencies_mode == 'radio') {
         $currencies = Currency::getPaymentCurrenciesSpecial((int) $this->id);
         $currency = $currencies['id_currency'];
         if ($currency == -1) {
             // not use $this->context->cookie if $current_id_currency is set
             if ((int) $current_id_currency) {
                 $id_currency = (int) $current_id_currency;
             } else {
                 $id_currency = (int) $this->context->cookie->id_currency;
             }
         } elseif ($currency == -2) {
             $id_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
         } else {
             $id_currency = (int) $currency;
         }
     }
     if (!isset($id_currency) || empty($id_currency)) {
         return false;
     }
     return new Currency($id_currency);
 }