diff --git a/config.xml b/config.xml index 4c16f0a..25a2ff2 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ paybear - + diff --git a/controllers/front/callback.php b/controllers/front/callback.php index f8640ca..b0f34a0 100644 --- a/controllers/front/callback.php +++ b/controllers/front/callback.php @@ -9,10 +9,20 @@ public function initContent() $orderReference = $_GET['order']; /** @var Order $order */ $order = Order::getByReference($orderReference)->getFirst(); + $currency = new Currency($order->id_currency); + $customer = $order->getCustomer(); $sdk = new PayBearSDK($this->context); $data = file_get_contents('php://input'); + if (in_array($order->current_state, array( + Configuration::get('PAYBEAR_OS_MISPAID'), + Configuration::get('PAYBEAR_OS_LATE_PAYMENT_RATE_CHANGED'), + Configuration::get('PS_OS_PAYMENT') + ))) { + die(); + } + if ($data) { $params = json_decode($data); $paybearData = PaybearData::getByOrderRefenceAndToken($orderReference, $params->blockchain); @@ -34,7 +44,8 @@ public function initContent() PrestaShopLogger::addLog(sprintf('PayBear: paid %s', $amountPaid), 1, null, 'Order', $order->id, true); PrestaShopLogger::addLog(sprintf('PayBear: maxDifference %s', $maxDifference), 1, null, 'Order', $order->id, true); - $orderStatus = Configuration::get('PS_OS_ERROR'); + $orderStatus = Configuration::get('PAYBEAR_OS_MISPAID'); + $message = false; if ($toPay > 0 && ($toPay - $amountPaid) < $maxDifference) { $orderTimestamp = strtotime($order->date_add); @@ -43,12 +54,13 @@ public function initContent() $orderStatus = Configuration::get('PS_OS_PAYMENT'); if ($paymentTimestamp > $deadline) { - $orderStatus = Configuration::get('PS_OS_ERROR'); + $orderStatus = Configuration::get('PAYBEAR_OS_LATE_PAYMENT_RATE_CHANGED'); PrestaShopLogger::addLog('PayBear: late payment', 1, null, 'Order', $order->id, true); $fiatPaid = $amountPaid * $sdk->getRate($params->blockchain); if ($order->total_paid < $fiatPaid) { PrestaShopLogger::addLog('PayBear: rate changed', 1, null, 'Order', $order->id, true); + $message = sprintf('Late Payment / Rate changed (%s %s paid, %s %s expected)', $fiatPaid, $currency->iso_code, $order->total_paid, $currency->iso_code); } else { $orderStatus = Configuration::get('PS_OS_PAYMENT'); $order->addOrderPayment($amountPaid, $paybear->displayName, $params->inTransaction->hash); @@ -57,10 +69,37 @@ public function initContent() } } else { PrestaShopLogger::addLog(sprintf('PayBear: wrong amount %s', $amountPaid), 2, null, 'Order', $order->id, true); + $underpaid = round(($toPay-$amountPaid)*$sdk->getRate($params->blockchain), 2); + $message = sprintf('Wrong Amount Paid (%s %s received, %s %s expected) - %s %s underpaid', $amountPaid, $params->blockchain, $toPay, $params->blockchain, $currency->sign, $underpaid); } $order->setCurrentState($orderStatus); + if ($message) { + $idCustomerThread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($customer->email, $order->id); + if (!$idCustomerThread) { + $customerThread = new CustomerThread(); + $customerThread->id_contact = 0; + $customerThread->id_customer = (int)$order->id_customer; + $customerThread->id_shop = (int)$this->context->shop->id; + $customerThread->id_order = (int)$order->id; + $customerThread->id_lang = (int)$this->context->language->id; + $customerThread->email = $customer->email; + $customerThread->status = 'open'; + $customerThread->token = Tools::passwdGen(12); + $customerThread->add(); + } else { + $customerThread = new CustomerThread((int)$idCustomerThread); + } + + $customerMessage = new CustomerMessage(); + $customerMessage->id_customer_thread = $customerThread->id; + // $customerMessage->id_employee = (int)$this->context->employee->id; + $customerMessage->message = $message; + $customerMessage->private = 0; + $customerMessage->add(); + } + echo $invoice; //stop further callbacks die(); } elseif ($order->current_state != (int) Configuration::get('PAYBEAR_OS_WAITING_CONFIRMATIONS')) { diff --git a/paybear.php b/paybear.php index 5fc6a1f..b46452c 100755 --- a/paybear.php +++ b/paybear.php @@ -60,7 +60,7 @@ public function install() return false; } - Configuration::updateValue('PAYBEAR_TITLE', 'Crypto Payments'); + Configuration::updateValue('PAYBEAR_TITLE', 'Crypto Payments (BTC/ETH/LTC and others)'); Configuration::updateValue('PAYBEAR_DESCRIPTION', 'Bitcoin (BTC), Ethereum (ETH) and other crypto currencies'); Configuration::updateValue('PAYBEAR_EXCHANGE_LOCKTIME', '15'); @@ -86,6 +86,10 @@ public function hookPaymentOptions($params) return; } + if (!Configuration::get('PAYBEAR_API_SECRET')) { + return; + } + $payment_options = [ $this->getEmbeddedPaymentOption(), ]; @@ -264,50 +268,50 @@ private function installSQL() */ public function installOrderState() { - if (!Configuration::get('PAYBEAR_OS_WAITING') - || !Validate::isLoadedObject(new OrderState(Configuration::get('PAYBEAR_OS_WAITING')))) { - $orderState = new OrderState(); - $orderState->name = array(); - foreach (Language::getLanguages() as $language) { - if (Tools::strtolower($language['iso_code']) == 'fr') { - $orderState->name[$language['id_lang']] = 'En attente de paiement PayBear'; - } else { - $orderState->name[$language['id_lang']] = 'Awaiting for PayBear payment'; - } - } - $orderState->send_email = false; - $orderState->color = '#4775de'; - $orderState->hidden = false; - $orderState->delivery = false; - $orderState->logable = false; - $orderState->invoice = false; - if ($orderState->add()) { - $source = _PS_MODULE_DIR_.'paybear/logo_os.png'; - $destination = _PS_ROOT_DIR_.'/img/os/'.(int) $orderState->id.'.gif'; - copy($source, $destination); - } - Configuration::updateValue('PAYBEAR_OS_WAITING', (int) $orderState->id); - } + $states = array( + array( + 'name' => 'PAYBEAR_OS_WAITING', + 'color' => '#4775de', + 'title' => 'Awaiting for PayBear payment' + ), + array( + 'name' => 'PAYBEAR_OS_WAITING_CONFIRMATIONS', + 'color' => '#4775de', + 'title' => 'Awaiting for PayBear payment confirmations' + ), + array( + 'name' => 'PAYBEAR_OS_MISPAID', + 'color' => '#8f0621', + 'title' => 'Mispaid' + ), + array( + 'name' => 'PAYBEAR_OS_LATE_PAYMENT_RATE_CHANGED', + 'color' => '#8f0621', + 'title' => 'Late Payment/Rate changed' + ), + ); - if (!Configuration::get('PAYBEAR_OS_WAITING_CONFIRMATIONS') - || !Validate::isLoadedObject(new OrderState(Configuration::get('PAYBEAR_OS_WAITING_CONFIRMATIONS')))) { - $orderState = new OrderState(); - $orderState->name = array(); - foreach (Language::getLanguages() as $language) { - $orderState->name[$language['id_lang']] = 'Awaiting for PayBear payment confirmations'; - } - $orderState->send_email = false; - $orderState->color = '#4775de'; - $orderState->hidden = false; - $orderState->delivery = false; - $orderState->logable = false; - $orderState->invoice = false; - if ($orderState->add()) { - $source = _PS_MODULE_DIR_.'paybear/logo_os.png'; - $destination = _PS_ROOT_DIR_.'/img/os/'.(int) $orderState->id.'.gif'; - copy($source, $destination); + foreach ($states as $state) { + if (!Configuration::get($state['name']) + || !Validate::isLoadedObject(new OrderState(Configuration::get($state['name'])))) { + $orderState = new OrderState(); + $orderState->name = array(); + foreach (Language::getLanguages() as $language) { + $orderState->name[$language['id_lang']] = $state['title']; + } + $orderState->send_email = false; + $orderState->color = $state['color']; + $orderState->hidden = false; + $orderState->delivery = false; + $orderState->logable = false; + $orderState->invoice = false; + if ($orderState->add()) { + $source = _PS_MODULE_DIR_.'paybear/logo_os.png'; + $destination = _PS_ROOT_DIR_.'/img/os/'.(int) $orderState->id.'.gif'; + copy($source, $destination); + } + Configuration::updateValue($state['name'], (int) $orderState->id); } - Configuration::updateValue('PAYBEAR_OS_WAITING_CONFIRMATIONS', (int) $orderState->id); } return true; diff --git a/sdk/PayBearSDK.php b/sdk/PayBearSDK.php index ca7ce8c..5007f55 100644 --- a/sdk/PayBearSDK.php +++ b/sdk/PayBearSDK.php @@ -124,7 +124,8 @@ public function getRates() static $rates = null; if (empty($rates)) { - $url = "https://api.paybear.io/v2/exchange/usd/rate"; + $currency = $this->context->currency; + $url = sprintf("https://api.paybear.io/v2/exchange/%s/rate", strtolower($currency->iso_code)); if ($response = file_get_contents($url)) { $response = json_decode($response);