Procházet zdrojové kódy

Ticket #259 - Subscriptions in Base module.

AntonLV před 10 roky
rodič
revize
996098bca7

+ 30 - 6
inc/classes/BxDolPayments.php

@@ -68,13 +68,13 @@ class BxDolPayments extends BxDol implements iBxDolSingleton
         return $aPayments;
     }
 
-    public function getProviders($iVendorId, $sProvider = '')
+    public function getProvidersCart($iVendorId)
     {
-    	if(!BxDolRequest::serviceExists($this->_sActive, 'get_providers'))
+    	if(!BxDolRequest::serviceExists($this->_sActive, 'get_providers_cart'))
     		return array();
 
-    	$aSrvParams = array($iVendorId, $sProvider);
-        return BxDolService::call($this->_sActive, 'get_providers', $aSrvParams);
+    	$aSrvParams = array($iVendorId);
+        return BxDolService::call($this->_sActive, 'get_providers_cart', $aSrvParams);
     }
 
 	public function getOption($sOption)
@@ -127,9 +127,9 @@ class BxDolPayments extends BxDol implements iBxDolSingleton
     	$aSrvParams = array($iVendorId, $iModuleId, $iItemId, $iItemCount);
 		return BxDolService::call($this->_sActive, 'get_cart_item_descriptor', $aSrvParams, 'Cart');
     }
-    
+
     /**
-     * Returns cart JavaScript code which should be included in the page to make "Add To Cart" buttons work properly.
+     * Returns cart JavaScript code which should be included in the page to make "Add To Cart" and "Subscribe" buttons work properly.
      */
     public function getCartJs($bWrapped = true)
     {
@@ -164,6 +164,30 @@ class BxDolPayments extends BxDol implements iBxDolSingleton
 		return BxDolService::call($this->_sActive, 'get_add_to_cart_link', $aSrvParams, 'Cart');
     }
 
+	/**
+     * Returns "Subscribe" JavaScript code to use in onclick attribute.
+     */
+    public function getSubscribeJs($iVendorId, $mixedModuleId, $iItemId, $iItemCount = 1, $bWrapped = true)
+    {
+    	if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscribe_js', 'Cart'))
+			return array();
+
+		$aSrvParams = array($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bWrapped);
+		return BxDolService::call($this->_sActive, 'get_subscribe_js', $aSrvParams, 'Cart');
+    }
+
+    /**
+     * Returns the whole "Subscribe" link including HTML tag BUTTON and cart JavaScript code.
+     */
+    public function getSubscribeLink($iVendorId, $mixedModuleId, $iItemId, $iItemCount = 1)
+    {
+		if(!BxDolRequest::serviceExists($this->_sActive, 'get_subscribe_link', 'Cart'))
+			return '';
+
+		$aSrvParams = array($iVendorId, $mixedModuleId, $iItemId, $iItemCount);
+		return BxDolService::call($this->_sActive, 'get_subscribe_link', $aSrvParams, 'Cart');
+    }
+
     public function initializeCheckout($iVendorId, $sProvider, $aItems = array())
     {
     	if(!BxDolRequest::serviceExists($this->_sActive, 'initialize_checkout'))

+ 3 - 3
inc/utils.inc.php

@@ -1162,12 +1162,12 @@ function bx_site_hash($sSalt = '', $isSkipVersion = false)
 
 /**
  * Transform string to method name string, for example it changes 'some_method' string to 'SomeMethod' string
- * @param string where words are separated with underscore
+ * @param array where words are separated with underscore
  * @return string where every word begins with capital letter
  */
-function bx_gen_method_name ($s, $sWordsDelimiter = '_')
+function bx_gen_method_name ($s, $aWordsDelimiter = array('_'))
 {
-    return str_replace(' ', '', ucwords(str_replace($sWordsDelimiter, ' ', $s)));
+    return str_replace(' ', '', ucwords(str_replace($aWordsDelimiter, ' ', $s)));
 }
 
 /**

+ 2 - 0
modules/base/general/classes/BxBaseModGeneralConfig.php

@@ -23,6 +23,8 @@ class BxBaseModGeneralConfig extends BxDolModuleConfig
     {
         parent::__construct($aModule);
 
+        $this->CNF = array();
+
         $this->_aObjects = array();
         $this->_aPrefixes = array();
         $this->_aJsClasses = array();

+ 22 - 4
modules/base/payment/classes/BxBaseModPaymentCart.php

@@ -26,7 +26,7 @@ class BxBaseModPaymentCart extends BxDol
     	if(!$this->_oModule->isLogged())
             return '';
 
-    	return $this->_oModule->_oConfig->getUrl('cart');
+    	return $this->_oModule->_oConfig->getUrl('URL_CART');
     }
 
     public function serviceGetCartJs($bWrapped = true)
@@ -34,6 +34,15 @@ class BxBaseModPaymentCart extends BxDol
         return $this->_oModule->_oTemplate->displayCartJs($bWrapped);
     }
 
+    public function serviceGetAddToCartJs($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect = false, $bWrapped = true)
+    {
+		$iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
+        if(empty($iModuleId))
+            return '';
+
+        return $this->_oModule->_oTemplate->displayAddToCartJs($iVendorId, $iModuleId, $iItemId, $iItemCount, $bNeedRedirect, $bWrapped);
+    }
+
 	public function serviceGetAddToCartLink($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect = false)
     {
         $iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
@@ -43,13 +52,22 @@ class BxBaseModPaymentCart extends BxDol
 		return $this->_oModule->_oTemplate->displayAddToCartLink($iVendorId, $iModuleId, $iItemId, $iItemCount, $bNeedRedirect);
     }
 
-    public function serviceGetAddToCartJs($iVendorId, $mixedModuleId, $iItemId, $iItemCount, $bNeedRedirect = false, $bWrapped = true)
+	public function serviceGetSubscribeJs($iVendorId, $mixedModuleId, $iItemId, $iItemCount = 1, $bWrapped = true)
     {
 		$iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
         if(empty($iModuleId))
             return '';
 
-        return $this->_oModule->_oTemplate->displayAddToCartJs($iVendorId, $iModuleId, $iItemId, $iItemCount, $bNeedRedirect, $bWrapped);
+        return $this->_oModule->_oTemplate->displaySubscribeJs($iVendorId, $iModuleId, $iItemId, $iItemCount, $bWrapped);
+    }
+
+	public function serviceGetSubscribeLink($iVendorId, $mixedModuleId, $iItemId, $iItemCount = 1)
+    {
+        $iModuleId = $this->_oModule->_oConfig->getModuleId($mixedModuleId);
+        if(empty($iModuleId))
+            return '';
+
+		return $this->_oModule->_oTemplate->displaySubscribeLink($iVendorId, $iModuleId, $iItemId, $iItemCount);
     }
 
 	public function serviceGetCartItemDescriptor($iVendorId, $iModuleId, $iItemId, $iItemCount)
@@ -63,7 +81,7 @@ class BxBaseModPaymentCart extends BxDol
         if(empty($iUserId))
             return 0;
 
-        $aInfo = $this->getInfo($iUserId);
+        $aInfo = $this->getInfo(BX_PAYMENT_TYPE_SINGLE, $iUserId);
 
         $iCount = 0;
         foreach($aInfo as $iVendorId => $aVendorCart)

+ 19 - 28
modules/base/payment/classes/BxBaseModPaymentConfig.php

@@ -17,10 +17,6 @@ class BxBaseModPaymentConfig extends BxBaseModGeneralConfig
 	protected $_sCurrencySign;
     protected $_sCurrencyCode;
 
-    protected $_aKeys;
-    protected $_aUrls;
-    protected $_aDividers;
-
     protected $_aPerPage;
     protected $_aHtmlIds;
 
@@ -31,17 +27,13 @@ class BxBaseModPaymentConfig extends BxBaseModGeneralConfig
     {
         parent::__construct($aModule);
 
-        $this->_aKeys = array(
-        	'session_key_pending' => $this->_sName . '_pending_id',
-        	'request_key_pending' => $this->_sName . '_pending_id'
-        );
-
-        $this->_aUrls = array();
-
-        $this->_aDividers = array(
-        	'descriptor' => '_',
-        	'descriptors' => ':'
-        );
+        $this->CNF = array_merge($this->CNF, array(
+        	'KEY_SESSION_PENDING' => $this->_sName . '_pending_id',
+        	'KEY_REQUEST_PENDING' => $this->_sName . '_pending_id',
+        	
+        	'DIVIDER_DESCRIPTOR' => '_',
+        	'DIVIDER_DESCRIPTORS' => ':',
+        ));
 
 		$this->_aPrefixes = array(
 			'general' => $this->_sName . '_',
@@ -100,35 +92,34 @@ class BxBaseModPaymentConfig extends BxBaseModGeneralConfig
 
 	public function getKey($sType)
     {
-    	if(empty($sType))
-            return $this->_aKeys;
+    	$sResult = '';
+    	if(empty($sType) || !isset($this->CNF[$sType]))
+            return $sResult;
 
-        return isset($this->_aKeys[$sType]) ? $this->_aKeys[$sType] : '';
+        return $this->CNF[$sType];
     }
 
     public function getUrl($sType, $aParams = array(), $bSsl = false)
     {
-    	if(empty($sType))
-            return $this->_aUrls;
-
 		$sResult = '';
-		if(!isset($this->_aUrls[$sType]))
+		if(empty($sType) || !isset($this->CNF[$sType]))
 			return $sResult;
 
-		if(strncmp($this->_aUrls[$sType], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) == 0)
-			$sResult = bx_append_url_params($this->_aUrls[$sType], $aParams);
+		if(strncmp($this->CNF[$sType], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) == 0)
+			$sResult = bx_append_url_params($this->CNF[$sType], $aParams);
 		else
-			$sResult = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($this->_aUrls[$sType], $aParams);
+			$sResult = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($this->CNF[$sType], $aParams);
 
     	return $bSsl ? $this->http2https($sResult) : $sResult;
     }
 
 	public function getDivider($sType)
     {
-    	if(empty($sType))
-            return $this->_aDividers;
+    	$sResult = '';
+    	if(empty($sType) || !isset($this->CNF[$sType]))
+            return $sResult;
 
-        return isset($this->_aDividers[$sType]) ? $this->_aDividers[$sType] : '';
+        return $this->CNF[$sType];
     }
 
 	public function getPerPage($sType = 'default')

+ 25 - 3
modules/base/payment/classes/BxBaseModPaymentModule.php

@@ -49,9 +49,9 @@ class BxBaseModPaymentModule extends BxDolModule
     	return $this->_oDb->getParam($this->_oConfig->getPrefix('options') . $sOption);
     }
 
-	public function serviceGetProviders($iVendorId, $sProvider = '')
+	public function serviceGetProvidersCart($iVendorId)
 	{
-		$aVendorProviders = $this->_oDb->getVendorInfoProviders($iVendorId, $sProvider);
+		$aVendorProviders = $this->_oDb->getVendorInfoProvidersCart($iVendorId);
 
 		$aResult = array();
 		foreach($aVendorProviders as $aProvider) {
@@ -79,10 +79,12 @@ class BxBaseModPaymentModule extends BxDolModule
 	public function getProfileInfo($iUserId = 0)
     {
         $oProfile = $this->getObjectUser($iUserId);
+        $oAccount = $oProfile->getAccountObject();
 
         return array(
         	'id' => $iUserId,
             'name' => $oProfile->getDisplayName(),
+        	'email' => $oAccount->getEmail(),
             'link' => $oProfile->getUrl(),
             'icon' => $oProfile->getIcon(),
             'unit' => $oProfile->getUnit(),
@@ -148,7 +150,7 @@ class BxBaseModPaymentModule extends BxDolModule
 
 	public function getObjectProvider($sProvider, $mixedVendorId = BX_PAYMENT_EMPTY_ID)
 	{
-		$aProvider = is_numeric($mixedVendorId) && (int)$mixedVendorId != BX_PAYMENT_EMPTY_ID ? $this->_oDb->getVendorInfoProviders((int)$mixedVendorId, $sProvider) : $this->_oDb->getProviders($sProvider);
+		$aProvider = is_numeric($mixedVendorId) && (int)$mixedVendorId != BX_PAYMENT_EMPTY_ID ? $this->_oDb->getVendorInfoProvider((int)$mixedVendorId, $sProvider) : $this->_oDb->getProviders(array('type' => 'by_name', 'name' => $sProvider));
         $sClassPath = !empty($aProvider['class_file']) ? BX_DIRECTORY_PATH_ROOT . $aProvider['class_file'] : $this->_oConfig->getClassPath() . $aProvider['class_name'] . '.php';
         if(empty($aProvider) || !file_exists($sClassPath))
         	return false;
@@ -180,6 +182,26 @@ class BxBaseModPaymentModule extends BxDolModule
     {
     	return BxDolService::call($mixedModule, 'register_cart_item', $aParams);
     }
+
+	public function callRegisterSubscriptionItem($mixedModule, $aParams)
+    {
+    	return BxDolService::call($mixedModule, 'register_subscription_item', $aParams);
+    }
+
+    public function callUnregisterCartItem($mixedModule, $aParams)
+    {
+		return BxDolService::call($mixedModule, 'unregister_cart_item', $aParams);
+    }
+
+	public function callUnregisterSubscriptionItem($mixedModule, $aParams)
+    {
+		return BxDolService::call($mixedModule, 'unregister_subscription_item', $aParams);
+    }
+
+    public function callCancelSubscriptionItem($mixedModule, $aParams)
+    {
+    	return BxDolService::call($mixedModule, 'cancel_subscription_item', $aParams);
+    }
 }
 
 /** @} */

+ 1 - 1
modules/base/payment/classes/BxBaseModPaymentOrders.php

@@ -26,7 +26,7 @@ class BxBaseModPaymentOrders extends BxDol
     	if(!$this->_oModule->isLogged())
             return '';
 
-    	return $this->_oModule->_oConfig->getUrl('orders');
+    	return $this->_oModule->_oConfig->getUrl('URL_ORDERS');
     }
 }
 

+ 42 - 2
modules/base/payment/classes/BxBaseModPaymentProvider.php

@@ -11,9 +11,9 @@
 
 interface iBxBaseModPaymentProvider
 {
-	public function initializeCheckout($iPendingId, $aCartInfo, $bRecurring = false, $iRecurringDays = 0);
+	public function initializeCheckout($iPendingId, $aCartInfo);
     public function finalizeCheckout(&$aData);
-    public function checkoutFinished();
+    public function finalizedCheckout();
 }
 
 class BxBaseModPaymentProvider extends BxDol
@@ -29,6 +29,7 @@ class BxBaseModPaymentProvider extends BxDol
     protected $_sPrefix;
     protected $_aOptions;
     protected $_bRedirectOnResult;
+    protected $_sLogFile;
 
     function __construct($aConfig)
     {
@@ -46,6 +47,11 @@ class BxBaseModPaymentProvider extends BxDol
         $this->_bRedirectOnResult = false;
     }
 
+    public function getReturnDataUrl($iVendorId)
+    {
+		return $this->_oModule->_oConfig->getUrl('URL_RETURN_DATA') . $this->_sName . '/' . $iVendorId;
+    }
+
     /**
      * TODO: Check whether the method is needed or not. 
      * Is used on success only.
@@ -55,6 +61,8 @@ class BxBaseModPaymentProvider extends BxDol
         return $this->_bRedirectOnResult;
     }
 
+	public function finalizedCheckout() {}
+
     protected function getOptionsByPending($iPendingId)
     {
         $aPending = $this->_oModule->_oDb->getPending(array(
@@ -68,6 +76,38 @@ class BxBaseModPaymentProvider extends BxDol
     {
         return isset($this->_aOptions[$this->_sPrefix . $sName]) ? $this->_aOptions[$this->_sPrefix . $sName]['value'] : "";
     }
+
+    /**
+	 *
+	 * Writes $contents to a log file specified in the bp_options file or, if missing,
+	 * defaults to a standard filename of 'bplog.txt'.
+	 *
+	 * @param mixed $contents
+	 * @return
+	 * @throws Exception $e 
+	 *
+	 */
+	protected function log($sContents)
+	{
+		try {
+			if($this->_sLogFile != '')
+				$file = $this->_sLogFile;
+	    	else 
+				$file = dirname(__FILE__) . '/bx_pp_' . $this->_sName . '.log';
+	
+			file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
+	
+			if (is_array($sContents))
+				$sContents = var_export($sContents, true);	
+			else if (is_object($sContents))
+				$sContents = json_encode($sContents);
+	
+			file_put_contents($file, $sContents."\n", FILE_APPEND);
+	  	} 
+	  	catch (Exception $e) {
+			echo 'Error: ' . $e->getMessage();
+	  	}
+	}
 }
 
 /** @} */

+ 1 - 7
template/scripts/BxBaseFormView.php

@@ -424,13 +424,7 @@ EOS;
     {
         $sAttr = isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? bx_convert_array2attrs($aInput['attrs_wrapper']) : '';
 
-        $sCode = <<<BLAH
-                <div class="bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}" $sAttr>
-                    $sContent
-                </div>
-BLAH;
-
-        return $sCode;
+        return "<div class=\"bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}\" $sAttr>$sContent</div>";
     }
 
     /**