Skip to content

Commit

Permalink
add crypto-crypto convertion via usd, fix exchange rate timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihor Sereda committed Jul 12, 2018
1 parent 5f452b1 commit ae555fc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
63 changes: 55 additions & 8 deletions app/code/local/Paybear/Payment/Model/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public function checkPayBearResponse() {
public function getCurrency($token, $orderId, $getAddress = false)
{
$token = $this->sanitize_token($token);
$rate = $this->getRate($token);
$order = new Mage_Sales_Model_Order();
$order->loadByIncrementId($orderId);

$rate = $this->getRate($token, $order->getOrderCurrencyCode());

if ($rate) {
$order = new Mage_Sales_Model_Order();
$order->loadByIncrementId($orderId);


$fiatValue = (float)$order->getGrandTotal();

$coinsValue = round($fiatValue / $rate, 8);
Expand Down Expand Up @@ -80,14 +83,58 @@ public function getCurrency($token, $orderId, $getAddress = false)
return null;
}

public function getRate($curCode)
public function getRate($curCode, $order_currency_code)
{
$rates = $this->getRates();
$curCode = strtolower($curCode);
$order_currency_code = strtolower($order_currency_code);
if ($curCode == $order_currency_code)
return 1;

/**
* Check, is order currency crypto ?
*/
if (in_array($order_currency_code, $this->getCryptoCurrencies())) {

$rateCryptoToCrypto = $this->getCryptoToCrypto($curCode, $order_currency_code);

return $rateCryptoToCrypto;

}

$rates = $this->getRates($curCode);

return isset($rates->$curCode) ? $rates->$curCode->mid : false;
}

/**
* Get Cryptocurrency codes
* @return array
*/
public function getCryptoCurrencies() {
return array_keys($this->getCurrencies());
}

/**
* Get rate of one cryptocurrency to another cryptocurrency
* @param $curCode
* @param $order_currency_code
* @return bool|float|int
*/
public function getCryptoToCrypto($curCode, $order_currency_code) {

// get USD rates
$rates = $this->getRates('usd');

$curCodeRate = ($rates[$curCode]) ? $rates[$curCode]['mid'] : null;
$orderCurrencyRate = ($rates[$order_currency_code]) ? $rates[$order_currency_code]['mid'] : null;

if (($curCodeRate > 0) && ($orderCurrencyRate > 0)) {
return $curCodeRate/$orderCurrencyRate;
}

return false;
}

public function getRates()
{
if (self::$_rates === null) {
Expand All @@ -114,7 +161,7 @@ public function getTokenAddress($orderId, $token)
$apiSecret = Mage::getStoreConfig('payment/paybear/api_secret');
$currencies = $this->getCurrencies();
$token = $this->sanitize_token($token);
$rate = $this->getRate($token);
$rate = $this->getRate($token, $order->getOrderCurrencyCode());
$fiatAmount = $order->getGrandTotal();
$coinsAmount = round($fiatAmount / $rate, 8);

Expand Down Expand Up @@ -207,12 +254,12 @@ public function getBlockExplorerUrl($token, $address) {
}
}

public function getAlreadyPaid($orderId) {
public function getAlreadyPaid($orderId, $order_currency_code) {
try {
$paybear_payment = Mage::getModel('paybear/payment')->load($orderId, 'order_increment_id');
if ($paybear_payment->getPaybearId()) {
$token = $paybear_payment->getToken();
$rate = $this->getRate($token);
$rate = $this->getRate($token, $order_currency_code);
$already_paid = Mage::getModel('paybear/paymenttxn')->getTotalPaid($orderId);
return round($already_paid*$rate, 2);
}
Expand Down
25 changes: 15 additions & 10 deletions app/code/local/Paybear/Payment/controllers/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ public function indexAction()
{
$order = new Mage_Sales_Model_Order();
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();

if (!$orderId) {
Mage::throwException($this->__('Order not found'));
Mage::log('Order not found');
$this->_redirect("/");
return;
}

$order->loadByIncrementId($orderId);
Expand All @@ -34,7 +36,7 @@ public function indexAction()
$underpayment = !empty($underpayment) ? $underpayment : 0.01;

$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','paybear',array('template' => 'paybear/form.phtml'));
$total_paid = $paybear_payment->getAlreadyPaid($orderId);
$total_paid = $paybear_payment->getAlreadyPaid($orderId, $order->getOrderCurrencyCode());
$fiat_value = round(max((float)$order->getGrandTotal() - $total_paid, 0), 2);

$payment_status = 'Pending Payment';
Expand Down Expand Up @@ -86,6 +88,8 @@ public function indexAction()
]);
}

$exchange_locktime = Mage::getStoreConfig('payment/paybear/exchange_locktime') * 60;

$block->addData([
'currencies' => $currency_url,
'status' => Mage::getUrl('paybear/payment/status', [
Expand All @@ -98,7 +102,8 @@ public function indexAction()
'currency_sign' => $currency_sign,
'overpayment' => $overpayment,
'underpayment' => $underpayment,
'order_overview' => $order_overview
'order_overview' => $order_overview,
'exchange_locktime' => !empty($exchange_locktime) ? $exchange_locktime : 15*60
]);

$this->getLayout()->getBlock('head')->addCss('css/paybear.css');
Expand Down Expand Up @@ -174,7 +179,7 @@ public function statusAction () {
$maxDifference_coins = 0;

if($maxDifference_fiat) {
$maxDifference_coins = round($maxDifference_fiat/$paybear_payment->getRate($paybear_payment->getToken()) , 8);
$maxDifference_coins = round($maxDifference_fiat/$paybear_payment->getRate($paybear_payment->getToken(), $order->getOrderCurrencyCode()) , 8);
$maxDifference_coins = max($maxDifference_coins, 0.00000001);
}

Expand All @@ -197,7 +202,7 @@ public function statusAction () {
$email_flag = $paybear_payment->getEmailStatus();
if (($underpayment > 0) && (empty($email_flag))) {

if ($paybear_payment->sendEmail('underpayment', $underpayment, round($underpayment * $paybear_payment->getRate($paybear_payment->getToken()),2), $paybear_payment->getToken(), $order )) {
if ($paybear_payment->sendEmail('underpayment', $underpayment, round($underpayment * $paybear_payment->getRate($paybear_payment->getToken(), $order->getOrderCurrencyCode()),2), $paybear_payment->getToken(), $order )) {
$paybear_payment->setEmailStatus(1);
$paybear_payment->save();
}
Expand Down Expand Up @@ -264,7 +269,7 @@ public function callbackAction () {
$maxDifference_coins = 0;

if($maxDifference_fiat) {
$maxDifference_coins = round($maxDifference_fiat/$paybear_payment->getRate($params->blockchain) , 8);
$maxDifference_coins = round($maxDifference_fiat/$paybear_payment->getRate($params->blockchain, $order->getOrderCurrencyCode()) , 8);
$maxDifference_coins = max($maxDifference_coins, 0.00000001);
}

Expand Down Expand Up @@ -321,7 +326,7 @@ public function callbackAction () {
if ($paymentTimestamp > $deadline) {
$orderStatus = Mage::getStoreConfig('payment/paybear/late_payment_status');

$fiatPaid = $totalConfirmed * $paybear_payment->getRate($params->blockchain);
$fiatPaid = $totalConfirmed * $paybear_payment->getRate($params->blockchain, $order->getOrderCurrencyCode());
if ((float) $fiatPaid < $order->getData('grand_total')) {
$message = sprintf('Late Payment / Rate changed (%s %s paid, %s %s expected)', round($fiatPaid,2), $currency->getData('currency_code'), round($order->getData('grand_total'),2), $currency->getData('currency_code'));
$order->addStatusHistoryComment($message, $orderStatus);
Expand All @@ -344,7 +349,7 @@ public function callbackAction () {
//check overpaid
$minoverpaid = Mage::getStoreConfig('payment/paybear/minoverpaymentfiat');
$minoverpaid = !empty($minoverpaid) ? $minoverpaid : 1;
$overpaid = (round(($totalConfirmed - $toPay)*$paybear_payment->getRate($params->blockchain), 2));
$overpaid = (round(($totalConfirmed - $toPay)*$paybear_payment->getRate($params->blockchain, $order->getOrderCurrencyCode()), 2));
if ( ($minoverpaid > 0) && ($overpaid > $minoverpaid) ) {

if ($paybear_payment->sendEmail('overpayment', $totalConfirmed - $toPay, $overpaid, $paybear_payment->getToken(), $order )) {
Expand All @@ -364,7 +369,7 @@ public function callbackAction () {
$orderStatus = Mage::getStoreConfig('payment/paybear/mispaid_status');
if ($order->getStatus() != $orderStatus) {

$underpaid = round(($toPay - $totalConfirmed) * $paybear_payment->getRate($params->blockchain), 2);
$underpaid = round(($toPay - $totalConfirmed) * $paybear_payment->getRate($params->blockchain, $order->getOrderCurrencyCode()), 2);
$message = sprintf('Wrong Amount Paid (%s %s is received, %s %s is expected) - %s %s is underpaid', $amountPaid, $params->blockchain, $toPay, $params->blockchain, $currency->getData('currency_code'), $underpaid);
$order->addStatusHistoryComment($message, $orderStatus);
$order->save();
Expand Down
4 changes: 2 additions & 2 deletions app/code/local/Paybear/Payment/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@
<show_in_store>0</show_in_store>
<sort_order>40</sort_order>
</api_public>
<!--<testnet translate="label">
<testnet translate="label">
<label>Testnet</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<sort_order>41</sort_order>
</testnet>-->
</testnet>
<exchange_locktime translate="label">
<label>Exchange Rate Lock Time </label>
<frontend_type>text</frontend_type>
Expand Down
2 changes: 2 additions & 0 deletions app/design/frontend/base/default/template/paybear/form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
data-currency-sign="<?php echo $this->currency_sign ?>"
data-min-overpayment-fiat="<?php echo $this->overpayment ?>"
data-max-underpayment-fiat="<?php echo $this->underpayment ?>"
data-exchange-locktime="<?php echo $this->exchange_locktime ?>"
data-autoopen = "true">
<div class="p30 PayBear-spinner" style="display: none;">
<p>Loading payment details...</p>
Expand Down Expand Up @@ -405,6 +406,7 @@
fiatSign: $container.data('currency-sign'),
minOverpaymentFiat: $container.data('min-overpayment-fiat'),
maxUnderpaymentFiat: parseFloat($container.data('max-underpayment-fiat')),
timer: $container.data('exchange-locktime'),
modal: true,
enablePoweredBy: true,
enableFiatTotal: true,
Expand Down

0 comments on commit ae555fc

Please sign in to comment.