瀏覽代碼

Ticket #5580 - API: Add full support for Market module.

AntonLV 2 月之前
父節點
當前提交
c3a856919f

+ 52 - 2
modules/boonex/market/classes/BxMarketMenuViewActions.php

@@ -38,12 +38,62 @@ class BxMarketMenuViewActions extends BxBaseModTextMenuViewActions
 
     protected function _getMenuItemAddToCart($aItem)
     {
-        return $this->_getMenuItemByNameActions($aItem);
+        $mixedResult = $this->_getMenuItemByNameActions($aItem);
+        if(!$mixedResult)
+            return $mixedResult;
+
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        if($this->_bIsApi) {
+            $oPayment = BxDolPayments::getInstance();
+
+            $iSeller = (int)$this->_aContentInfo[$CNF['FIELD_AUTHOR']];
+            $sDescriptor = $oPayment->getCartItemDescriptor($iSeller, $this->_oModule->_oConfig->getId(), (int)$this->_aContentInfo[$CNF['FIELD_ID']], 1);
+            return array_merge($mixedResult, [
+                'title' => _t('_bx_market_menu_item_title_add_to_cart', $this->_aContentInfo[$CNF['FIELD_PRICE_SINGLE']], ' ' . $oPayment->getCurrencyCode($iSeller)),
+                'display_type' => 'callback',
+                'data' => [
+                    'on_callback' => 'redirect',
+                    'request_url' => $this->_sModule . '/add_to_cart_api/&params[]=' . $sDescriptor, 
+                    'payment_type' => BX_PAYMENT_TYPE_SINGLE,
+                    'seller_id' => $iSeller,
+                    'items' => [$sDescriptor],  
+                ]
+            ]);
+        }
+
+        return $mixedResult;
     }
 
     protected function _getMenuItemSubscribe($aItem)
     {
-        return $this->_getMenuItemByNameActions($aItem);
+        $mixedResult = $this->_getMenuItemByNameActions($aItem);
+        if(!$mixedResult)
+            return $mixedResult;
+
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        if($this->_bIsApi) {
+            $oPayment = BxDolPayments::getInstance();
+
+            $iSeller = (int)$this->_aContentInfo[$CNF['FIELD_AUTHOR']];
+            $sProvider = 'stripe_v3'; //TODO: Payment Provider selector should be realized.
+            $sDescriptor = $oPayment->getCartItemDescriptor($iSeller, $this->_oModule->_oConfig->getId(), (int)$this->_aContentInfo[$CNF['FIELD_ID']], 1);
+            return array_merge($mixedResult, [
+                'title' => _t('_bx_market_menu_item_title_subscribe', $this->_aContentInfo[$CNF['FIELD_PRICE_RECURRING']], ' ' . $oPayment->getCurrencyCode($iSeller), _t('_bx_market_txt_per_' . $this->_aContentInfo[$CNF['FIELD_DURATION_RECURRING']])),
+                'display_type' => 'callback',
+                'data' => [
+                    'on_callback' => 'object',
+                    'request_url' => $this->_sModule . '/subscribe_api/&params[]=' . $sProvider . '&params[]=' . $sDescriptor,
+                    'object_name' => 'stripe_v3',
+                    'payment_type' => BX_PAYMENT_TYPE_RECURRING,
+                    'seller_id' => $iSeller,
+                    'items' => [$sDescriptor],
+                ]
+            ]);
+        }
+
+        return $mixedResult;
     }
 
     protected function _getMenuItemUnhideProduct($aItem)

+ 79 - 22
modules/boonex/market/classes/BxMarketModule.php

@@ -136,12 +136,14 @@ class BxMarketModule extends BxBaseModTextModule
 
     public function serviceGetSafeServices()
     {
-        $a = parent::serviceGetSafeServices();
-        return array_merge($a, array (
+        return array_merge(parent::serviceGetSafeServices(), [
             'EntityDownload' => '',
             'EntityAuthorEntities' => '',
             'BlockLicenses' => '',
-        ));
+
+            'AddToCartApi' => '',
+            'SubscribeApi' => '',
+        ]);
     }
 
     /**
@@ -245,12 +247,19 @@ class BxMarketModule extends BxBaseModTextModule
             array('id' => 'bx-market-show-more', 'name' => 'bx-market-show-more', 'class' => '', 'link' => 'javascript:void(0)', 'target' => '_self', 'onclick' => 'javascript:' . $sJsObject . '.showMore();', 'title' => _t('_bx_market_menu_item_title_downloads_more')),
         )));
 
-        return array(
+        $mixedResult = $this->_oTemplate->entryAttachmentsByStorage($CNF['OBJECT_STORAGE_FILES'], $aContentInfo, [
+            'filter_field' => ''
+        ]);
+
+        if($this->_bIsApi)
+            foreach($mixedResult as $iKey => $aBlock)
+                if($aBlock['type'] == 'entity_attachments')
+                    $mixedResult[$iKey]['type'] = 'market_attachments';
+
+        return [
             'menu' => $oMenu,
-            'content' => $this->_oTemplate->entryAttachmentsByStorage($CNF['OBJECT_STORAGE_FILES'], $aContentInfo, array(
-                'filter_field' => ''
-            ))
-        );
+            'content' => $mixedResult
+        ];
     }
 
     /**
@@ -294,22 +303,30 @@ class BxMarketModule extends BxBaseModTextModule
     /** 
      * @ref bx_market-entity_author_entities "entity_author_entities"
      */
-	public function serviceEntityAuthorEntities($iContentId = 0)
+    public function serviceEntityAuthorEntities($iContentId = 0)
     {
-    	$CNF = &$this->_oConfig->CNF;
+        $CNF = &$this->_oConfig->CNF;
 
-    	$aContentInfo = $this->_getContentInfo($iContentId);
-    	if($aContentInfo === false)
-    		return false;
+        $aContentInfo = $this->_getContentInfo($iContentId);
+        if($aContentInfo === false)
+            return false;
 
-		$oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]);
-        if (!$oProfile)
-            $oProfile = BxDolProfileUndefined::getInstance();
+        $mixedResult = $this->_serviceBrowse ('author', ['author' => $aContentInfo[$CNF['FIELD_AUTHOR']], 'except' => [$iContentId], 'per_page' => 2], BX_DB_PADDING_DEF, true);
+        if(!$mixedResult || !is_array($mixedResult))
+            return $mixedResult;
 
-		$aBlock = $this->_serviceBrowse ('author', array('author' => $aContentInfo[$CNF['FIELD_AUTHOR']], 'except' => array($iContentId), 'per_page' => 2), BX_DB_PADDING_DEF, true);
-		$aBlock['title'] = _t('_bx_market_page_block_title_entry_author_entries', $oProfile->getDisplayName());
+        $sAuthor = ($oProfile = BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']])) !== false || ($oProfile = BxDolProfileUndefined::getInstance()) !== false ? $oProfile->getDisplayName() : '';
+        $sTitle = _t('_bx_market_page_block_title_entry_author_entries', $sAuthor);
 
-    	return $aBlock;
+        if($this->_bIsApi)
+            return [
+                'title' => $sTitle,
+                'content' => $mixedResult
+            ];
+        else
+            $mixedResult['title'] = $sTitle;
+
+        return $mixedResult;
     }
 
     /**
@@ -1130,10 +1147,50 @@ class BxMarketModule extends BxBaseModTextModule
     /** 
      * @ref bx_market-cancel_subscription_item "cancel_subscription_item"
      */
-	public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
+    public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
     {
-    	//TODO: Do something if it's necessary.
-    	return true;
+        //TODO: Do something if it's necessary.
+        return true;
+    }
+
+    public function serviceAddToCartApi($sDescriptor)
+    {
+        if(!$this->_bIsApi)
+            return;
+
+        $oPayment = BxDolPayments::getInstance();
+
+        $aDescriptor = $oPayment->getActiveObject()->_oConfig->descriptorS2A($sDescriptor);
+        if(!$aDescriptor || !is_array($aDescriptor)) 
+            return [bx_api_get_msg(_t('_sys_txt_error_occured'))]; //TODO: Ask Roman How to return error message.
+
+        $iSeller = (int)$aDescriptor[0];
+        $aResult = $oPayment->addToCart($iSeller, $this->_oConfig->getName(), (int)$aDescriptor[2], 1, true);
+        if(($iCode = $aResult['code'] ?? false) && (int)$iCode != 0)
+            return [bx_api_get_msg($aResult['message'])]; //TODO: Ask Roman How to return error message.
+        else
+            return [
+                'url' => bx_api_get_relative_url($oPayment->getCartUrl($iSeller))
+            ];
+    }
+
+    public function serviceSubscribeApi($sProvider, $sDescriptor)
+    {
+        if(!$this->_bIsApi)
+            return;
+
+        $oPayment = BxDolPayments::getInstance();
+
+        $aDescriptor = $oPayment->getActiveObject()->_oConfig->descriptorS2A($sDescriptor);
+        if(!$aDescriptor || !is_array($aDescriptor)) 
+            return [bx_api_get_msg(_t('_sys_txt_error_occured'))];
+
+        $iSeller = (int)$aDescriptor[0];
+        $aResult = $oPayment->subscribeWithAddons($iSeller, $sProvider, $this->_oConfig->getName(), (int)$aDescriptor[2], 1, true);
+        if(($iCode = $aResult['code'] ?? false) && (int)$iCode != 0)
+            return [bx_api_get_msg($aResult['message'])];
+
+        return [];
     }
 
     protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)

+ 3 - 1
modules/boonex/market/install/sql/enable-app.sql

@@ -1,5 +1,7 @@
 -- PAGES: config_api
-UPDATE `sys_objects_page` SET `config_api`='{\r\n    layout: \'post\',\r\n   \r\n}' WHERE `object`='bx_market_view_entry';
+UPDATE `sys_objects_page` SET `config_api`='{"layout": "post"}' WHERE `object`='bx_market_view_entry';
+UPDATE `sys_pages_blocks` SET `config_api`='{"content_type":"browse_simple"}' WHERE `object`='bx_market_view_entry' AND `title`='_bx_market_page_block_title_entry_author_entries';
+
 
 -- PAGES: active_api
 UPDATE `sys_pages_blocks` SET `active_api`=1 WHERE `object`='bx_market_create_entry' AND `module`='bx_market' AND `title_system`='' AND `title`='_bx_market_page_block_title_create_entry';