Skip to content

Commit

Permalink
cleanup and better api v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
leviothan committed Jan 23, 2018
1 parent c9975f6 commit bedd891
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 211 deletions.
3 changes: 3 additions & 0 deletions classes/PaybearData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class PaybearData extends ObjectModel

public $token;

public $payment_add;

/**
* @see ObjectModel::$definition
*/
Expand All @@ -37,6 +39,7 @@ class PaybearData extends ObjectModel
'amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
'payment_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat'),
)
);

Expand Down
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>paybear</name>
<displayName><![CDATA[Crypto Payment Gateway for PrestaShop by PayBear.io]]></displayName>
<version><![CDATA[1.0.0]]></version>
<version><![CDATA[0.2.0]]></version>
<description><![CDATA[Allows to accept crypto payments such as Bitcoin (BTC) and Ethereum (ETH)]]></description>
<author><![CDATA[PayBear]]></author>
<tab><![CDATA[payments_gateways]]></tab>
Expand Down
12 changes: 10 additions & 2 deletions controllers/front/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function initContent()
$toPay = $paybearData->amount;
$amountPaid = $params->inTransaction->amount / pow(10, $params->inTransaction->exp);
$fiatPaid = $amountPaid * $sdk->getRate($params->blockchain);
$maxDifference = min($toPay * 0.005, 0.001);
$maxDifference = 0.00000001;
$paybear = Module::getInstanceByName('paybear');

PrestaShopLogger::addLog(sprintf('PayBear: to pay %s', $toPay), 1, null, 'Order', $order->id, true);
Expand All @@ -41,7 +41,7 @@ public function initContent()

if ($toPay > 0 && ($toPay - $fiatPaid) < $maxDifference) {
$orderTimestamp = strtotime($order->date_add);
$paymentTimestamp = time();
$paymentTimestamp = strtotime($paybearData->payment_add);
$deadline = $orderTimestamp + Configuration::get('PAYBEAR_EXCHANGE_LOCKTIME') * 60;
$orderStatus = Configuration::get('PS_OS_PAYMENT');

Expand Down Expand Up @@ -69,6 +69,14 @@ public function initContent()

echo $invoice; //stop further callbacks
die();
} elseif ($order->current_state != (int) Configuration::get('PAYBEAR_OS_WAITING_CONFIRMATIONS')) {
$paybearData->payment_add = date('Y-m-d H:i:s');
$paybearData->update();

$orderHistory = new OrderHistory();
$orderHistory->id_order = (int) $order->id;
$orderHistory->changeIdOrderState((int) Configuration::get('PAYBEAR_OS_WAITING_CONFIRMATIONS'), $order, false);
$order->add(true);
}
}
die();
Expand Down
15 changes: 2 additions & 13 deletions controllers/front/currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,15 @@ public function initContent()
$this->ajax = true;
$sdk = new PayBearSDK($this->context);

// $orderId = (int) $_GET['order'];
$orderId = Tools::getValue('order');
$fiatTotal = 19.99; //get from order

$tokens = ['ETH', 'BTC', 'LTC', 'BCH', 'BTG', 'DASH'];

if (isset($_GET['token'])) {
$token = $_GET['token'];
$data = $sdk->getCurrency($token, $orderId, true);
} else {
$data = [];
foreach ($tokens as $token) {
$enabled = Configuration::get('PAYBEAR_ENABLE_'.strtoupper($token));
$wallet = Configuration::get('PAYBEAR_' . strtoupper($token) . '_WALLET');
$confirmations = Configuration::get('PAYBEAR_' . strtoupper($token) . '_CONFIRMATIONS');

if (!$enabled || !$wallet || !$confirmations) {
continue;
}

$currencies = $sdk->getCurrencies();
foreach ($currencies as $token => $currency) {
$currency = $sdk->getCurrency($token, $orderId);
if ($currency) {
$data[] = $currency;
Expand Down
5 changes: 5 additions & 0 deletions controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public function initContent()

/** @var Order $order */
$order = Order::getByReference($orderReference)->getFirst();

if ($order->current_state != (int) Configuration::get('PAYBEAR_OS_WAITING')) {
Tools::redirect('index.php?controller=order');
}

$customer = $order->getCustomer();

$this->context->smarty->assign([
Expand Down
67 changes: 5 additions & 62 deletions paybear.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
<?php
/*
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

use PrestaShop\PrestaShop\Core\Payment\PaymentOption;

Expand All @@ -34,8 +10,6 @@

class PayBear extends PaymentModule
{
const DEBUG_MODE = true; // todo change

protected $_html = '';
protected $_postErrors = array();

Expand All @@ -44,17 +18,6 @@ class PayBear extends PaymentModule
public $address;
public $extra_mail_vars;



protected $cryptoCurrencies = array(
array('name' => 'eth', 'label' => 'ETH', 'defaults' => array('enabled' => 1, 'confirmations' => 3)),
array('name' => 'btc', 'label' => 'BTC', 'defaults' => array('enabled' => 1, 'confirmations' => 1)),
array('name' => 'bch', 'label' => 'BCH', 'defaults' => array('enabled' => 0, 'confirmations' => 3)),
array('name' => 'btg', 'label' => 'BTG', 'defaults' => array('enabled' => 0, 'confirmations' => 3)),
array('name' => 'dash', 'label' => 'DASH', 'defaults' => array('enabled' => 1, 'confirmations' => 3)),
array('name' => 'ltc', 'label' => 'LTC', 'defaults' => array('enabled' => 1, 'confirmations' => 3)),
);

public function __construct()
{
$this->name = 'paybear';
Expand All @@ -71,12 +34,8 @@ public function __construct()
$this->bootstrap = true;
parent::__construct();

// $this->displayName = Configuration::get('PAYBEAR_TITLE');
// $this->description = Configuration::get('PAYBEAR_DESCRIPTION');

$this->displayName = $this->l('Crypto Payment Gateway for PrestaShop by PayBear.io');
$this->description = $this->l('Allows to accept crypto payments such as Bitcoin (BTC) and Ethereum (ETH)');
// $this->module_link = $this->context->link->getAdminLink('AdminModules', true).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;

if (!count(Currency::checkPaymentCurrencies($this->id))) {
$this->warning = $this->l('No currency has been set for this module.');
Expand All @@ -85,7 +44,6 @@ public function __construct()

public function install()
{
// Registration order status
if (!$this->installOrderState()) {
return false;
}
Expand All @@ -106,19 +64,6 @@ public function install()
Configuration::updateValue('PAYBEAR_DESCRIPTION', 'Bitcoin (BTC), Ethereum (ETH) and other crypto currencies');
Configuration::updateValue('PAYBEAR_EXCHANGE_LOCKTIME', '15');

// foreach ($this->cryptoCurrencies as $cryptoCurrency) {
// if (!Configuration::updateValue('PAYBEAR_ENABLE_'.strtoupper($cryptoCurrency['name']), $cryptoCurrency['defaults']['enabled'])) {
// return false;
// }
// if (!Configuration::updateValue('PAYBEAR_' . strtoupper($cryptoCurrency['name']) . '_CONFIRMATIONS', $cryptoCurrency['defaults']['confirmations'])) {
// return false;
// }
//
// if (!Configuration::updateValue('PAYBEAR_' . strtoupper($cryptoCurrency['name']) . '_WALLET', '')) {
// return false;
// }
// }

return true;
}

Expand Down Expand Up @@ -280,11 +225,6 @@ public function displayForm()
$helper->fields_value['paybear_exchange_locktime'] = Configuration::get('PAYBEAR_EXCHANGE_LOCKTIME');
$helper->fields_value['paybear_api_secret'] = Configuration::get('PAYBEAR_API_SECRET');
$helper->fields_value['paybear_api_public'] = Configuration::get('PAYBEAR_API_PUBLIC');
// foreach ($this->cryptoCurrencies as $currency) {
// $helper->fields_value['paybear_enable_' . $currency['name']] = Configuration::get('PAYBEAR_ENABLE_' . strtoupper($currency['name']));
// $helper->fields_value['paybear_' . $currency['name'] . '_confirmations'] = Configuration::get('PAYBEAR_' . strtoupper($currency['name']) . '_CONFIRMATIONS');
// $helper->fields_value['paybear_' . $currency['name'] . '_wallet'] = Configuration::get('PAYBEAR_' . strtoupper($currency['name']) . '_WALLET');
// }

return $helper->generateForm($fields_form);
}
Expand All @@ -299,10 +239,13 @@ private function installSQL()
`token` VARCHAR(256) NULL DEFAULT NULL,
`address` VARCHAR(256),
`invoice` VARCHAR(256),
`amount` DECIMAL(20, 7),
`amount` DECIMAL(20, 8),
`confirmations` INT(2) NULL DEFAULT NULL,
`date_add` DATETIME NULL DEFAULT NULL,
`date_upd` DATETIME NULL DEFAULT NULL
`date_upd` DATETIME NULL DEFAULT NULL,
`payment_add` DATETIME NULL DEFAULT NULL,
KEY `order_reference` (`order_reference`),
KEY `token` (`token`)
) ENGINE = "._MYSQL_ENGINE_;

foreach ($sql as $q) {
Expand Down
Loading

0 comments on commit bedd891

Please sign in to comment.