Просмотр исходного кода

Ticket #4113 - Credits: Add multi-currency support.

AntonLV 3 лет назад
Родитель
Сommit
989f593177

+ 62 - 23
modules/boonex/credits/classes/BxCreditsModule.php

@@ -68,9 +68,11 @@ class BxCreditsModule extends BxBaseModGeneralModule
         if(!$iBuyerId)
             return echoJson(['code' => 1, 'msg' => _t('_bx_credits_err_unknown_buyer')]);
 
+        $sErrIncorrectData = _t('_bx_credits_err_incorrect_data');
+
         $aData = $this->_oConfig->getCheckoutData();
         if(empty($aData) || !is_array($aData))
-            return echoJson(['code' => 2, 'msg' => _t('_bx_credits_err_incorrect_data')]);
+            return echoJson(['code' => 2, 'msg' => $sErrIncorrectData]);
 
         $fConversion = $this->_oConfig->getConversionRateUse();
         $iPrecision = $this->_oConfig->getPrecision();
@@ -79,11 +81,20 @@ class BxCreditsModule extends BxBaseModGeneralModule
         $fAmount = (float)$aData['amount'];
         $aCustomData = $this->_oConfig->deconstructCheckoutCustomData($aData['custom']);
 
-        $aPpOrders = BxDolPayments::getInstance()->getPendingOrdersInfo(['id' => (int)$aCustomData[1]]);
+        $oPayments = BxDolPayments::getInstance();
+        $aPpOrders = $oPayments->getPendingOrdersInfo(['id' => (int)$aCustomData[1]]);
         if(!empty($aPpOrders) && is_array($aPpOrders)) {
             $aPpOrder = reset($aPpOrders);
-            if(!empty($aPpOrder) && is_array($aPpOrder) && ((int)$aPpOrder['seller_id'] != $iSellerId || $this->_oConfig->convertM2C((float)$aPpOrder['amount'], $fConversion, $iPrecision) != $fAmount))
-                return echoJson(['code' => 3, 'msg' => _t('_bx_credits_err_incorrect_data')]);
+            if(empty($aPpOrder) || !is_array($aPpOrder) || (int)$aPpOrder['seller_id'] != $iSellerId)
+                return echoJson(['code' => 3, 'msg' => $sErrIncorrectData]);
+
+            $fAmountM = (float)$aPpOrder['amount'];
+            $sCurrencyDefault = $oPayments->getCurrencyCode();
+            if(strcmp($aPpOrder['currency'], $sCurrencyDefault) != 0)
+                $fAmountM = $oPayments->convert ($fAmountM, $aPpOrder['currency'], $sCurrencyDefault);
+
+            if($this->_oConfig->convertM2C($fAmountM, $fConversion, $iPrecision) != $fAmount)
+                return echoJson(['code' => 3, 'msg' => $sErrIncorrectData]);
         }
 
         $fBalance = (float)$this->_oDb->getProfile(['type' => 'balance', 'id' => $iBuyerId]);
@@ -168,6 +179,16 @@ class BxCreditsModule extends BxBaseModGeneralModule
         return $this->_oConfig->getCheckoutUrl();
     }
 
+    public function serviceConvertMoneyToCredits($fMoney, $fRate = false, $iPrecision = false)
+    {
+        return $this->_oConfig->convertM2C($fMoney, $fRate, $iPrecision);
+    }
+
+    public function serviceConvertCreditsToMoney($fCredits, $fRate = false, $iPrecision = false)
+    {
+        return $this->_oConfig->convertC2M($fCredits, $fRate, $iPrecision);
+    }
+
     public function serviceValidateCheckout($iSeller, $iBuyer, $fAmount, $sOrder)
     {
         $fAmount = (float)$fAmount;
@@ -202,33 +223,40 @@ class BxCreditsModule extends BxBaseModGeneralModule
         if(!$oSeller)
             return MsgBox(_t('_bx_credits_err_unknown_seller'));
 
-        $fAmountM = (float)bx_get('amount');
-        $fAmountC = $this->_oConfig->convertM2C($fAmountM);
+        $fAmountM = $fAmountMInDef = (float)bx_get('amount');
+
+        $oPayments = BxDolPayments::getInstance();
+        $sCurrency = bx_process_input(bx_get('currency_code'));
+        $sCurrencyDefault = $oPayments->getCurrencyCode();
+        if(strcmp($sCurrency, $sCurrencyDefault) != 0)
+            $fAmountMInDef = $oPayments->convert($fAmountM, $sCurrency, $sCurrencyDefault);
+
+        $fAmountC = $this->_oConfig->convertM2C($fAmountMInDef);
 
-        $aData = array(
+        $aData = [
             'seller' => $iSeller,
-            'currency' => array(
-                'code' => bx_process_input(bx_get('currency_code')),
+            'currency' => [
+                'code' => $sCurrency,
                 'sign' => bx_process_input(bx_get('currency_sign')),
-            ),
+            ],
             'amountm' => $fAmountM,
             'amountc' => $fAmountC
-        );
+        ];
 
         $iItemsCount = (int)bx_get('items_count');
         for($i = 0; $i < $iItemsCount; $i++) {
-            $aData['items'][] = array(
+            $aData['items'][] = [
                 'title' => bx_process_input(bx_get('item_title_' . $i)),
                 'quantity' => (int)bx_get('item_quantity_' . $i)
-            );
+            ];
         }
 
-        $this->_oConfig->setCheckoutData(array(
+        $this->_oConfig->setCheckoutData([
             'seller' => $iSeller,
             'amount' => $fAmountC,
             'custom' => bx_process_input(bx_get('custom')),
             'return_data_url' => bx_process_input(bx_get('return_data_url')),
-        ));
+        ]);
         return $this->_oTemplate->getBlockCheckout($oBuyer, $oSeller, $aData);
     }
 
@@ -236,28 +264,39 @@ class BxCreditsModule extends BxBaseModGeneralModule
     {
         $oBuyer = BxDolProfile::getInstance();
         if(!$oBuyer)
-            return array('msg' => _t('_bx_credits_err_unknown_buyer'));
+            return ['msg' => _t('_bx_credits_err_unknown_buyer')];
 
         $iSeller = (int)$aData['seller'];
         $oSeller = BxDolProfile::getInstance($iSeller);
         if(!$oSeller)
-            return array('msg' => _t('_bx_credits_err_unknown_seller'));
+            return ['msg' => _t('_bx_credits_err_unknown_seller')];
+
+        $fAmountM = $fAmountMInDef = (float)$aData['amount'];
 
-        $fAmountM = (float)$aData['amount'];
-        $fAmountC = $this->_oConfig->convertM2C($fAmountM);
+        $oPayments = BxDolPayments::getInstance();
+        $sCurrency = $aData['currency_code'];
+        $sCurrencyDefault = $oPayments->getCurrencyCode();
+        if(strcmp($sCurrency, $sCurrencyDefault) != 0)
+            $fAmountMInDef = $oPayments->convert($fAmountM, $sCurrency, $sCurrencyDefault);
 
-        $aData = array_merge($aData, array(
+        $fAmountC = $this->_oConfig->convertM2C($fAmountMInDef);
+
+        $aData = array_merge($aData, [
+            'currency' => [
+                'code' => $sCurrency,
+                'sign' => $aData['currency_sign'],
+            ],
             'amountm' => $fAmountM,
             'amountc' => $fAmountC
-        ));
+        ]);
 
-        $this->_oConfig->setCheckoutData(array(
+        $this->_oConfig->setCheckoutData([
             'seller' => $iSeller,
             'amount' => $fAmountC,
             'trial' => (int)$aData['trial'] > 0,
             'custom' => bx_process_input($aData['custom']),
             'return_data_url' => bx_process_input($aData['return_data_url']),
-        ));
+        ]);
         return $this->_oTemplate->getPopupSubscribe($oBuyer, $oSeller, $aData);
     }
 

+ 2 - 2
modules/boonex/credits/install/langs/en.xml

@@ -10,8 +10,8 @@
     <string name="_bx_credits_option_withdraw_minimum"><![CDATA[Minimum amount of cleared credits which can be withdrawn]]></string>
     <string name="_bx_credits_option_withdraw_remaining"><![CDATA[Minimum credits balance after withdrawal]]></string>
     <string name="_bx_credits_option_precision"><![CDATA[Precision (number of digits after the decimal point)]]></string>
-    <string name="_bx_credits_option_conversion_rate_use"><![CDATA[Conversion rate to pay with credits]]></string>
-    <string name="_bx_credits_option_conversion_rate_withdraw"><![CDATA[Conversion rate to withdraw credits]]></string>
+    <string name="_bx_credits_option_conversion_rate_use"><![CDATA[Conversion rate to pay with credits (for site currency)]]></string>
+    <string name="_bx_credits_option_conversion_rate_withdraw"><![CDATA[Conversion rate to withdraw credits (for site currency)]]></string>
     <string name="_bx_credits_option_code"><![CDATA[Code to prefix amount]]></string>
     <string name="_bx_credits_option_icon"><![CDATA[Icon to prefix amount]]></string>
     <string name="_bx_credits_enable_provider"><![CDATA[Auto activate 'Credits' provider for all newly created profiles]]></string>

+ 31 - 0
modules/boonex/payment/classes/BxPaymentCronCurrency.php

@@ -0,0 +1,31 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    Payment Payment
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxPaymentCronCurrency extends BxDolCron
+{
+    protected $_sModule;
+    protected $_oModule;
+
+    public function __construct()
+    {
+        parent::__construct();
+
+    	$this->_sModule = 'bx_payment';
+    	$this->_oModule = BxDolModule::getInstance($this->_sModule);
+    }
+
+    function processing()
+    {
+        $this->_oModule->updateCurrencyExchangeRates();
+    }
+}
+
+/** @} */

+ 2 - 2
modules/boonex/russian/data/langs/bx_credits/ru.xml

@@ -10,8 +10,8 @@
     <string name="_bx_credits_option_withdraw_minimum"><![CDATA[Минимальная сумма очищенных кредитов, которая может быть снята]]></string>
     <string name="_bx_credits_option_withdraw_remaining"><![CDATA[Минимальный баланс кредитов после снятия]]></string>
     <string name="_bx_credits_option_precision"><![CDATA[Точность (количество цифр после десятичной точки)]]></string>
-    <string name="_bx_credits_option_conversion_rate_use"><![CDATA[Коэффициент конвертации для оплаты кредитами]]></string>
-    <string name="_bx_credits_option_conversion_rate_withdraw"><![CDATA[Коэффициент конвертации для снятия кредитов]]></string>
+    <string name="_bx_credits_option_conversion_rate_use"><![CDATA[Коэффициент конвертации для оплаты кредитами (для валюты сайта)]]></string>
+    <string name="_bx_credits_option_conversion_rate_withdraw"><![CDATA[Коэффициент конвертации для снятия кредитов (для валюты сайта)]]></string>
     <string name="_bx_credits_option_code"><![CDATA[Код для префикса суммы]]></string>
     <string name="_bx_credits_option_icon"><![CDATA[Иконка для префикса суммы]]></string>
     <string name="_bx_credits_enable_provider"><![CDATA[Активировать платежную систему 'Credits' для всех новосозданных профилей]]></string>