Skip to content
This repository was archived by the owner on Jan 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from btcpayserver/feat-txspeed-and-ordermode
Browse files Browse the repository at this point in the history
Feat txspeed and ordermode
  • Loading branch information
Adrien Bensaïbi authored Sep 17, 2018
2 parents 2d8c604 + 8222972 commit a6ff243
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 135 deletions.
95 changes: 80 additions & 15 deletions modules/btcpay/btcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ public function __construct() {

$this->name = 'btcpay';
$this->tab = 'payments_gateways';
$this->version = '0.1.0';
$this->version = '0.3.1';
$this->author = 'ADAPP';
$this->className = 'btcpay';
$this->currencies = true;
$this->currencies_mode = 'checkbox';
$this->display = 'view';
$this->btcpayurl = '';
$this->is_eu_compatible = 1;
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => '1.7');

Expand Down Expand Up @@ -133,22 +134,22 @@ public function install() {
// to be sure not other plugin do that.
// TODO maybe take the last number available

$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('39','1','Want to pay in Bitcoin','emitbtcpayment');";
$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('39','1','Want to pay in Bitcoin','bitcoin_want');";
$db->Execute($query);
$query = "INSERT INTO `"._DB_PREFIX_."order_state` (`id_order_state`, `invoice`, `send_email`, `module_name`, `color`, `unremovable`, `hidden`, `logable`, `delivery`, `shipped`, `paid`, `pdf_invoice`, `pdf_delivery`, `deleted`) VALUES ('39', '0', '0', 'btcpay', '#FFFF00', '1', '0', '0', '0', '0', '0', '0', '0', '0');";
$db->Execute($query);

$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('40','1','Waiting Bitcoin confirmations','waitingbtcconfirmation');";
$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('40','1','Waiting Bitcoin confirmations','bitcoin_waiting');";
$db->Execute($query);
$query = "INSERT INTO `"._DB_PREFIX_."order_state` (`id_order_state`, `invoice`, `send_email`, `module_name`, `color`, `unremovable`, `hidden`, `logable`, `delivery`, `shipped`, `paid`, `pdf_invoice`, `pdf_delivery`, `deleted`) VALUES ('40', '0', '0', 'btcpay', '#FFFF00', '1', '0', '0', '0', '0', '0', '0', '0', '0');";
$db->Execute($query);

$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('41','1','Bitcoin transaction invalid','invalidbtcconfirmation');";
$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('41','1','Bitcoin transaction invalid','bitcoin_invalid');";
$db->Execute($query);
$query = "INSERT INTO `"._DB_PREFIX_."order_state` (`id_order_state`, `invoice`, `send_email`, `module_name`, `color`, `unremovable`, `hidden`, `logable`, `delivery`, `shipped`, `paid`, `pdf_invoice`, `pdf_delivery`, `deleted`) VALUES ('41', '0', '0', 'btcpay', '#FFCE1C', '1', '0', '1', '0', '0', '1', '0', '0', '0');";
$query = "INSERT INTO `"._DB_PREFIX_."order_state` (`id_order_state`, `invoice`, `send_email`, `module_name`, `color`, `unremovable`, `hidden`, `logable`, `delivery`, `shipped`, `paid`, `pdf_invoice`, `pdf_delivery`, `deleted`) VALUES ('41', '0', '0', 'btcpay', '#565656', '1', '0', '1', '0', '0', '1', '0', '0', '0');";
$db->Execute($query);

$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('42','1','Bitcoin payment confirm','paidbtcconfirmation') ON CONFICT (`id_order_state`);";
$query = "INSERT INTO `"._DB_PREFIX_."order_state_lang` (`id_order_state`,`id_lang`,`name`,`template`) VALUES ('42','1','Bitcoin payment confirm','bitcoin_confirm');";
$db->Execute($query);
$query = "INSERT INTO `"._DB_PREFIX_."order_state` (`id_order_state`, `invoice`, `send_email`, `module_name`, `color`, `unremovable`, `hidden`, `logable`, `delivery`, `shipped`, `paid`, `pdf_invoice`, `pdf_delivery`, `deleted`) VALUES ('42', '0', '0', 'btcpay', '#FFCE00', '1', '0', '1', '0', '0', '1', '1', '0', '0');";
$db->Execute($query);
Expand All @@ -165,11 +166,15 @@ public function install() {
Configuration::updateValue('btcpay_PUB', "");
Configuration::updateValue('btcpay_SIN', "");
Configuration::updateValue('btcpay_TOKEN', "");
Configuration::updateValue('btcpay_TXSPEED', "");
Configuration::updateValue('btcpay_ORDERMODE', "");

return true;
}

public function uninstall() {
Configuration::deleteByName('btcpay_ORDERMODE');
Configuration::deleteByName('btcpay_TXSPEED');
Configuration::deleteByName('btcpay_TOKEN');
Configuration::deleteByName('btcpay_SIN');
Configuration::deleteByName('btcpay_PUB');
Expand Down Expand Up @@ -273,11 +278,46 @@ function loadTab(id){}
private function _getSettingsTabHtml() {
global $cookie;

// default set a test btcpayserver
$btcpayserver_url = Configuration::get('btcpay_URL');
if (true === empty($btcpayserver_url)) {
$btcpayserver_url = "https://btcpay-server-testnet.azurewebsites.net";
}

// select list for bitcoin confirmation
// 'default' => 'Keep store level configuration',
// 'high' => '0 confirmation on-chain',
// 'medium' => '1 confirmation on-chain',
// 'low-medium' => '2 confirmations on-chain',
// 'low' => '6 confirmations on-chain',
$lowSelected = '';
$mediumSelected = '';
$highSelected = '';

// Remember which speed has been selected and display that upon reaching the settings page; default to low
if (Configuration::get('btcpay_TXSPEED') == "high") {
$highSelected = "selected=\"selected\"";
} elseif (Configuration::get('btcpay_TXSPEED') == "medium") {
$mediumSelected = "selected=\"selected\"";
} else {
$lowSelected = "selected=\"selected\"";
}

// delayed order mecanism
// create a 'prestashop order' when you create btcpay invoice
// or
// create a 'prestashop order' when you receive bitcoin payment
// or
// create a 'prestashop order' when you receive bitcoin payment and confirmation
$orderBeforePaymentSelected = '';
$orderAfterPaymentSelected = '';

if (Configuration::get('btcpay_ORDERMODE') == "afterpayment") {
$orderAfterPaymentSelected = "selected=\"selected\"";
} else {
$orderBeforePaymentSelected = "selected=\"selected\"";
}

$html = '<h2>'.$this->l('Settings').'</h2>
<style type="text/css" src="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"></style>
<style type="text/css" src="../modules/btcpay/assets/css/style.css"></style>
Expand All @@ -288,6 +328,23 @@ private function _getSettingsTabHtml() {
<input name="form_btcpay_url" type="text" value="'.htmlentities(Tools::getValue('serverurl', Configuration::get('btcpay_URL')),ENT_COMPAT, 'UTF-8').'" placeholder="BTCPay Url (eg. '.$btcpayserver_url.')" class="bitpay-url"> <br />
</div>
<div style="clear:both;margin-bottom:30px;">
<h3 style="clear:both;">'.$this->l('Transaction Speed').'</h3>
<select name="form_btcpay_txspeed">
<option value="low" '.$lowSelected.'>Low</option>
<option value="medium" '.$mediumSelected.'>Medium</option>
<option value="high" '.$highSelected.'>High</option>
</select>
</div>
<div style="clear:both;margin-bottom:30px;">
<h3 style="clear:both;">'.$this->l('Order Mode').'</h3>
<select name="form_btcpay_ordermode">
<option value="beforepayment" '.$orderBeforePaymentSelected.'>Order before payment</option>
<option value="afterpayment" '.$orderAfterPaymentSelected.'>Order after payment</option>
</select>
</div>
<div style="clear:both;margin-bottom:30px;">
<h3 style="clear:both;">'.$this->l('Pairing Code').'</h3>
<input type="text" style="width:400px;" name="form_btcpay_pairingcode" value="'.htmlentities(Tools::getValue('pairingcode', Configuration::get('btcpay_PAIRINGCODE')),ENT_COMPAT, 'UTF-8').'" />
Expand Down Expand Up @@ -390,6 +447,10 @@ private function _ajax_bitpay_pair_code($pairing_code, $btcpay_url) {
return;

} else {
if ( false === isset($token)) {
$this->_errors[] = $this->l("Failed to create token, you are maybe using an already activated pairing code.");
return;
}
Configuration::updateValue('btcpay_URL', $_btcpay_url);
Configuration::updateValue('btcpay_LABEL', $label);
Configuration::updateValue('btcpay_PUB', (string)$this->bitpay_encrypt($pub));
Expand All @@ -413,6 +474,11 @@ private function _postProcess() {
$this->_errors[] = $this->l('Missing BTCPay server url');
}

$this->_ajax_bitpay_pair_code(
Tools::getValue('form_btcpay_pairingcode'),
Tools::getValue('form_btcpay_url')
);

if (count($this->_errors) > 0) {
$error_msg = '';

Expand All @@ -423,14 +489,11 @@ private function _postProcess() {
return;

} else {
$this->_ajax_bitpay_pair_code(
Tools::getValue('form_btcpay_pairingcode'),
Tools::getValue('form_btcpay_url')
);

Configuration::updateValue('btcpay_ORDERMODE', trim(Tools::getValue('form_btcpay_ordermode')));
Configuration::updateValue('btcpay_TXSPEED', trim(Tools::getValue('form_btcpay_txspeed')));
Configuration::updateValue('btcpay_PAIRINGCODE', trim(Tools::getValue('form_btcpay_pairingcode')));
Configuration::updateValue('btcpay_URL', trim(Tools::getValue('form_btcpay_url')));
$this->_html = $this->displayConfirmation($this->l('Pairing created'));
$this->_html = $this->displayConfirmation($this->l('Pairing done'));
}
}
}
Expand All @@ -441,6 +504,8 @@ public function execPayment($cart) {
// Get shopping currency, currently tested with be EUR
$currency = Currency::getCurrencyInstance((int)$cart->id_currency);

$transaction_speed = Configuration::get('btcpay_TXSPEED');

// get the cart id to fetch cart information
$cart_id = $cart->id;
if (true === empty($cart_id)) {
Expand Down Expand Up @@ -515,7 +580,6 @@ public function execPayment($cart) {
$this->_errors[] = $this->l('[Error] The BTCPay payment plugin was called to process a payment but could not instantiate an item object.');
}


$customer = new Customer((int)$cart->id_customer);
$email = $customer->email;

Expand Down Expand Up @@ -550,7 +614,7 @@ public function execPayment($cart) {
// Add the Redirect and Notification URLs
$invoice->setRedirectUrl($redirect_url);
$invoice->setNotificationUrl($notification_url);
$this->transaction_speed = 'default';
$invoice->setTransactionSpeed($transaction_speed);

// If another BTCPay invoice was created before, returns the original one
$redirect = $this->get_btcpay_redirect($cart_id, $client);
Expand Down Expand Up @@ -581,7 +645,6 @@ public function execPayment($cart) {

$responseData = json_decode($client->getResponse()->getBody());


// register invoice url and rate into order_bitcoin table
$this->update_order_field($cart_id, 'amount', $cart_total);
$this->update_order_field($cart_id, 'rate', $invoice->getRate());
Expand Down Expand Up @@ -723,6 +786,7 @@ public function bitpay_encrypt($data)
{
if (false === isset($data) || true === empty($data)) {
$this->_errors[] = $this->l('The BTCPay payment plugin was called to encrypt data but no data was passed!');
return;
}

$openssl_ext = new \Bitpay\Crypto\OpenSSLExtension();
Expand Down Expand Up @@ -754,6 +818,7 @@ public function bitpay_decrypt($encrypted)
{
if (false === isset($encrypted) || true === empty($encrypted)) {
$this->_errors[] = $this->l('The BTCPay payment plugin was called to decrypt data but no data was passed!');
return;
}
$openssl_ext = new \Bitpay\Crypto\OpenSSLExtension();
$fingerprint = sha1(sha1(__DIR__));
Expand Down
2 changes: 1 addition & 1 deletion modules/btcpay/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>btcpay</name>
<displayName><![CDATA[BTCPay]]></displayName>
<version><![CDATA[0.1.0]]></version>
<version><![CDATA[0.3.1]]></version>
<description><![CDATA[Accepts Bitcoin payments via BTCPay.]]></description>
<author><![CDATA[ADAPP]]></author>
<tab><![CDATA[payments_gateways]]></tab>
Expand Down
2 changes: 1 addition & 1 deletion modules/btcpay/config_fr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>btcpay</name>
<displayName><![CDATA[btcpay]]></displayName>
<version><![CDATA[0.1.0]]></version>
<version><![CDATA[0.3.1]]></version>
<description><![CDATA[Acceptez les paiements en bitcoin via BTCPay.]]></description>
<author><![CDATA[ADAPP]]></author>
<tab><![CDATA[payments_gateways]]></tab>
Expand Down
8 changes: 3 additions & 5 deletions modules/btcpay/controllers/front/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ class btcpayPaymentModuleFrontController extends ModuleFrontController
*/
public function initContent()
{
parent::initContent();

$cart = $this->context->cart;

echo $this->module->execPayment($cart);
parent::initContent();
$cart = $this->context->cart;
echo $this->module->execPayment($cart);
}
}

Expand Down
63 changes: 32 additions & 31 deletions modules/btcpay/controllers/front/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,38 @@ class btcpayValidationModuleFrontController extends ModuleFrontController
/**
* @see FrontController::postProcess()
*/
public function postProcess()
{
$cart = $this->context->cart;
if ($cart->id_customer == 0
|| $cart->id_address_delivery == 0
|| $cart->id_address_invoice == 0
|| !$this->module->active)
Tools::redirect('index.php?controller=order&step=1');

// Check that this payment option is still available in case the customer changed his address just before the end of the checkout process
$authorized = false;
foreach (Module::getPaymentModules() as $module)
if ($module['name'] == 'btcpay')
{
$authorized = true;
break;
}
if (!$authorized)
die($this->module->l('This payment method is not available.', 'validation'));

$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer))
Tools::redirect('index.php?controller=order&step=1');

$currency = $this->context->currency;
$total = (float)$cart->getOrderTotal(true, Cart::BOTH);

$mailVars = array(
);

Tools::redirect('index.php?controller=order-confirmation&id_cart='.(int)$cart->id.'&id_module='.(int)$this->module->id.'&id_order='.$this->module->currentOrder.'&key='.$customer->secure_key);
public function postProcess() {
$cart = $this->context->cart;
if ($cart->id_customer == 0
|| $cart->id_address_delivery == 0
|| $cart->id_address_invoice == 0
|| !$this->module->active)
Tools::redirect('index.php?controller=order&step=1');

// Check that this payment option is still available in case the customer changed his address just before the end of the checkout process
$authorized = false;
foreach (Module::getPaymentModules() as $module) {
if ($module['name'] == 'btcpay')
{
$authorized = true;
break;
}
}
if (!$authorized) {
die($this->module->l('This payment method is not available.', 'validation'));
}

$customer = new Customer($cart->id_customer);
if (!Validate::isLoadedObject($customer)) {
Tools::redirect('index.php?controller=order&step=1');
}

$currency = $this->context->currency;
$total = (float)$cart->getOrderTotal(true, Cart::BOTH);

$mailVars = array();

Tools::redirect('index.php?controller=order-confirmation&id_cart='.(int)$cart->id.'&id_module='.(int)$this->module->id.'&id_order='.$this->module->currentOrder.'&key='.$customer->secure_key);
}
}

2 changes: 1 addition & 1 deletion modules/btcpay/invoice_block.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</tr>
<tr>
<td align="left" valign="top">{l s='Status:' mod='btcpay'}</td>
<td>{$status}</td>
<td>-----{$status}</td>
</tr>
</table>
</div>
Expand Down
Loading

0 comments on commit a6ff243

Please sign in to comment.