Skip to content

Commit

Permalink
add more order states. Support more fiat currencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
leviothan committed Feb 7, 2018
1 parent 716a8c7 commit b00af07
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 47 deletions.
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[0.4.1]]></version>
<version><![CDATA[0.5.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
43 changes: 41 additions & 2 deletions controllers/front/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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')) {
Expand Down
90 changes: 47 additions & 43 deletions paybear.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -86,6 +86,10 @@ public function hookPaymentOptions($params)
return;
}

if (!Configuration::get('PAYBEAR_API_SECRET')) {
return;
}

$payment_options = [
$this->getEmbeddedPaymentOption(),
];
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion sdk/PayBearSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b00af07

Please sign in to comment.