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

Ticket #4971 - Build-in API support: Stripe Connect. Payments form.

AntonLV 1 год назад
Родитель
Сommit
4fc192de7b

+ 12 - 10
modules/base/payment/classes/BxBaseModPaymentConfig.php

@@ -29,7 +29,7 @@ class BxBaseModPaymentConfig extends BxBaseModGeneralConfig
     {
         parent::__construct($aModule);
 
-        $this->CNF = array_merge($this->CNF, array(
+        $this->CNF = array_merge($this->CNF, [
             // some params
             'PARAM_CURRENCY_CODE' => $this->_sName . '_default_currency_code',
 
@@ -40,6 +40,8 @@ class BxBaseModPaymentConfig extends BxBaseModGeneralConfig
             'PARAM_CMSN_DATE_TIME_FORMAT' => 'd.m.Y H:i',
 
             // objects
+            'OBJECT_FORM_DETAILES' => $this->_sName . '_form_details',
+            'OBJECT_FORM_DETAILES_EDIT' => $this->_sName . '_form_details_edit',
             'OBJECT_FORM_PRELISTS_CURRENCIES' => '',
 
             'PROVIDER_GENERIC' => 'generic',
@@ -50,17 +52,17 @@ class BxBaseModPaymentConfig extends BxBaseModGeneralConfig
             'DIVIDER_DESCRIPTOR' => '_',
             'DIVIDER_DESCRIPTORS' => ':',
             'DIVIDER_GRID_FILTERS' => '#-#',
-        ));
+        ]);
 
-        $this->_aPrefixes = array(
+        $this->_aPrefixes = [
             'general' => $this->_sName . '_',
             'langs' => '_' . $this->_sName . '_',
             'options' => $this->_sName . '_'
-        );
+        ];
 
-        $this->_aObjects = array_merge($this->_aObjects, array(
-            'form_details' => $this->_sName . '_form_details',
-            'form_display_details_edit' => $this->_sName . '_form_details_edit',
+        $this->_aObjects = array_merge($this->_aObjects, [
+            'form_details' => $this->CNF['OBJECT_FORM_DETAILES'],
+            'form_display_details_edit' => $this->CNF['OBJECT_FORM_DETAILES_EDIT'],
             'form_pendings' => $this->_sName . '_form_pendings',
             'form_processed' => $this->_sName . '_form_processed',
             'form_display_pendings_process' => $this->_sName . '_form_pendings_process',
@@ -88,10 +90,10 @@ class BxBaseModPaymentConfig extends BxBaseModGeneralConfig
             'grid_sbs_list_my' => $this->_sName . '_grid_sbs_list_my',
             'grid_sbs_list_all' => $this->_sName . '_grid_sbs_list_all',
             'grid_sbs_history' => $this->_sName . '_grid_sbs_history',
-        ));
+        ]);
 
-        $this->_aPerPage = array();
-        $this->_aHtmlIds = array();
+        $this->_aPerPage = [];
+        $this->_aHtmlIds = [];
 
         $this->_sAnimationEffect = 'fade';
         $this->_iAnimationSpeed = 'slow';

+ 4 - 4
modules/boonex/payment/classes/BxPaymentConfig.php

@@ -22,7 +22,7 @@ class BxPaymentConfig extends BxBaseModPaymentConfig
 
         $sBaseUrl = BX_DOL_URL_ROOT . $this->getBaseUri();
 
-        $this->CNF = array_merge($this->CNF, array(
+        $this->CNF = array_merge($this->CNF, [
             // module icon
             'ICON' => 'credit-card col-gray-dark',
 
@@ -68,7 +68,7 @@ class BxPaymentConfig extends BxBaseModPaymentConfig
             'MODULE_CREDITS' => 'bx_credits',
 
             // some language keys
-            'T' => array(
+            'T' => [
                 'MSG_ITEM_ADDED' => '_bx_payment_msg_item_added',
                 'MSG_ITEM_DELETED' => '_bx_payment_msg_item_deleted',
                 'MSG_SINGLE_SELLER_MODE' => '_bx_payment_msg_single_seller',
@@ -84,8 +84,8 @@ class BxPaymentConfig extends BxBaseModPaymentConfig
                 'POPUP_PROVIDERS_SELECT' => '_bx_payment_popup_title_crd_providers_select', 
 
                 'TXT_CART_PROVIDER' => '_bx_payment_txt_cart_'
-            )
-        ));
+            ]
+        ]);
 
         $this->_aHtmlIds = array(
             'cart' => array(

+ 28 - 6
modules/boonex/payment/classes/BxPaymentDb.php

@@ -25,8 +25,13 @@ class BxPaymentDb extends BxBaseModPaymentDb
     /**
      * Payment details methods
      */
-    public function getForm()
+    public function getForm($sPaymentProvider = '')
     {
+        $sWhereClause = '';
+
+        if(!empty($sPaymentProvider))
+            $sWhereClause .= $this->prepareAsString(" AND `tp`.`name`=?", $sPaymentProvider);
+        
         $sQuery = "SELECT
                 `tp`.`id` AS `provider_id`,
                 `tp`.`name` AS `provider_name`,
@@ -46,7 +51,7 @@ class BxPaymentDb extends BxBaseModPaymentDb
                 `tpo`.`check_error` AS `check_error`
             FROM `" . $this->_sPrefix . "providers` AS `tp`
             LEFT JOIN `" . $this->_sPrefix . "providers_options` AS `tpo` ON `tp`.`id`=`tpo`.`provider_id`
-            WHERE `tp`.`active`='1' 
+            WHERE `tp`.`active`='1'" . $sWhereClause . "
             ORDER BY `tp`.`order` ASC, `tpo`.`order` ASC";
 
         return $this->getAll($sQuery);
@@ -146,16 +151,16 @@ class BxPaymentDb extends BxBaseModPaymentDb
 
     public function getOptions($iUserId = BX_PAYMENT_EMPTY_ID, $iProviderId = 0)
     {
-    	$aBinding = array(
-    		'user_id' => $iUserId
-    	);
+        $aBinding = [
+            'user_id' => $iUserId
+        ];
 
         if($iUserId == BX_PAYMENT_EMPTY_ID && empty($iProviderId))
            return $this->getAll("SELECT `id`, `name`, `type` FROM `" . $this->_sPrefix . "providers_options`");
 
         $sWhereAddon = "";
         if(!empty($iProviderId)) {
-        	$aBinding['provider_id'] = $iProviderId;
+            $aBinding['provider_id'] = $iProviderId;
 
             $sWhereAddon = " AND `tpo`.`provider_id`=:provider_id";
         }
@@ -170,6 +175,23 @@ class BxPaymentDb extends BxBaseModPaymentDb
         return $this->getAllWithKey($sQuery, 'name', $aBinding);
     }
 
+    public function getOptionsByProvider($mixedProvider)
+    {
+        $aBindings = [
+            'provider' => $mixedProvider
+        ];
+
+        $sJoinClause = $sWhereClause = '';
+        if(!is_numeric($mixedProvider)) {
+            $sJoinClause = " LEFT JOIN `" . $this->_sPrefix . "providers` AS `tp` ON `tpo`.`provider_id`=`tp`.`id`";
+            $sWhereClause = " AND `tp`.`name`=:provider";
+        }
+        else
+            $sWhereClause = " AND `tpo`.`provider_id`=:provider";
+
+        return $this->getAll("SELECT `tpo`.`id`, `tpo`.`name`, `tpo`.`type` FROM `" . $this->_sPrefix . "providers_options` AS `tpo`" . $sJoinClause . " WHERE 1" . $sWhereClause, $aBindings);
+    }
+
     public function updateOption($iUserId, $iOptionId, $sValue)
     {
         $sQuery = $this->prepare("REPLACE INTO `" . $this->_sPrefix . "user_values` SET `user_id`=?, `option_id`=?, `value`=?", $iUserId, $iOptionId, $sValue);

+ 54 - 17
modules/boonex/payment/classes/BxPaymentDetails.php

@@ -11,6 +11,7 @@
 
 class BxPaymentDetails extends BxBaseModPaymentDetails
 {
+    protected $_bIsApi;
     protected $_sLangsPrefix;
 
     function __construct()
@@ -19,6 +20,7 @@ class BxPaymentDetails extends BxBaseModPaymentDetails
 
         parent::__construct();
 
+        $this->_bIsApi = bx_is_api();
         $this->_sLangsPrefix = $this->_oModule->_oConfig->getPrefix('langs');
     }
 
@@ -39,39 +41,74 @@ class BxPaymentDetails extends BxBaseModPaymentDetails
     /** 
      * @ref bx_payment-get_block_details "get_block_details"
      */
-    public function serviceGetBlockDetails($iUserId = BX_PAYMENT_EMPTY_ID)
+    public function serviceGetBlockDetails($iUserId = BX_PAYMENT_EMPTY_ID, $mixedPaymentProvider = false)
     {
         if(!$this->_oModule->isLogged())
-            return array(
-                'content' => MsgBox(_t($this->_sLangsPrefix . 'err_required_login'))
-            );
+            return ($sMsg = $this->_sLangsPrefix . 'err_required_login') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox(_t($sMsg));
 
         $iUserId = $iUserId != BX_PAYMENT_EMPTY_ID ? $iUserId : $this->_oModule->getProfileId();
 
-        $sContent = $this->getForm($iUserId);
-        if(empty($sContent))
-            $sContent = MsgBox(_t($this->_sLangsPrefix . 'msg_no_results'));
+        $aPaymentProviders = [];
+        if(!$mixedPaymentProvider) {
+            $aPaymentProviders = $this->_oModule->_oDb->getProviders([
+                'type' => 'all', 
+                'active' => true, 
+                'order' => true
+            ]);
 
-        return array(
-            'content' => $sContent
-        );
+            if(!empty($aPaymentProviders) && is_array($aPaymentProviders))
+                $aPaymentProviders = array_keys($aPaymentProviders);
+        }
+        else {
+            if(is_array($mixedPaymentProvider))
+                $aPaymentProviders = $mixedPaymentProvider;
+            else if(is_string($mixedPaymentProvider))
+                $aPaymentProviders = [$mixedPaymentProvider];
+        }
+
+        if(!$aPaymentProviders)
+            return ($sMsg = $this->_sLangsPrefix . 'msg_no_results') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox(_t($sMsg));
+
+        $mixedContent = $this->_bIsApi ? [] : '';
+        foreach($aPaymentProviders as $sPaymentProvider) {
+            $mixedForm = $this->getForm($iUserId, $sPaymentProvider);
+
+            if($this->_bIsApi)
+                $mixedContent[] = bx_api_get_block('form', $mixedForm, [
+                    'ext' => [
+                        'name' => $this->MODULE, 
+                        'request' => ['url' => '/api.php?r=' . $this->MODULE . '/get_block_details/Details', 'immutable' => true]
+                    ]
+                ]);
+            else
+                $mixedContent .= $this->_oModule->_oTemplate->parseHtmlByName('details_form.html', [
+                    'provider' => str_replace('_', '-', $sPaymentProvider),
+                    'form' => $mixedForm
+                ]);
+        }
+
+        if(empty($mixedContent))
+            return ($sMsg = $this->_sLangsPrefix . 'msg_no_results') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox(_t($sMsg));
+
+        return $this->_bIsApi ? [$mixedContent] : $mixedContent;
     }
 
-    public function getForm($iProfileId)
+    public function getForm($iProfileId, $sPaymentProvider)
     {
-        $oForm = BxTemplFormView::getObjectInstance($this->_oModule->_oConfig->getObject('form_details'), $this->_oModule->_oConfig->getObject('form_display_details_edit'));
-        $oForm->setProfileId($iProfileId);
-        $oForm->initChecker();
+        $CNF = &$this->_oModule->_oConfig->CNF;
 
+        $oForm = BxTemplFormView::getObjectInstance($CNF['OBJECT_FORM_DETAILES'], $CNF['OBJECT_FORM_DETAILES_EDIT']);
+        $oForm->initWithParams($iProfileId, $sPaymentProvider);
+        $oForm->initChecker();
         if($oForm->isSubmitted()) {
             if($oForm->isValid()) {
-                $aOptions = $this->_oModule->_oDb->getOptions();
+                $aOptions = $this->_oModule->_oDb->getOptionsByProvider($sPaymentProvider);
                 foreach($aOptions as $aOption) {
                     $sValue = bx_get($aOption['name']) !== false ? bx_get($aOption['name']) : '';
                     $this->_oModule->_oDb->updateOption($iProfileId, $aOption['id'], bx_process_input($sValue));
                 }
 
-                header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=payment-details')));
+                header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=payment-details', ['pp' => $sPaymentProvider])));
                 return;
             }
             else
@@ -83,7 +120,7 @@ class BxPaymentDetails extends BxBaseModPaymentDetails
                     }
         }
 
-        return $oForm->getCode();
+        return $this->_bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
     }
 }
 

+ 42 - 10
modules/boonex/payment/classes/BxPaymentFormDetails.php

@@ -26,6 +26,7 @@ class BxPaymentFormDetails extends BxTemplFormView
     protected $_bCollapseFirst;
     
     protected $_iProfileId;
+    protected $_sPaymentProvider;
 
     function __construct($aInfo, $oTemplate = false)
     {
@@ -38,8 +39,7 @@ class BxPaymentFormDetails extends BxTemplFormView
         $this->_bCollapseFirst = true;
         
         $this->_iProfileId = $this->_oModule->getProfileId();
-
-        $this->init();
+        $this->_sPaymentProvider = '';
     }
 
     public function setProfileId($iProfileId = BX_PAYMENT_EMPTY_ID)
@@ -48,9 +48,22 @@ class BxPaymentFormDetails extends BxTemplFormView
             $this->_iProfileId = $iProfileId;
     }
 
+    public function setPaymentProvider($sPaymentProvider)
+    {
+        $this->_sPaymentProvider = $sPaymentProvider;
+
+        $this->aParams['db']['submit_name'] = $this->_getSubmitName();
+
+        $sName = $this->_getName();
+        $this->setId($sName);
+        $this->setName($sName);
+    }
+
     public function init()
     {
-        $aInputs = $this->_oModule->_oDb->getForm();
+        $this->aInputs = [];
+                
+        $aInputs = $this->_oModule->_oDb->getForm($this->_sPaymentProvider);
         if(empty($aInputs))
             return false;
 
@@ -76,7 +89,7 @@ class BxPaymentFormDetails extends BxTemplFormView
                     'caption' => _t($aInput['provider_caption']),
                     'info' => _t($aInput['provider_description']),
                     'collapsable' => true,
-                    'collapsed' => $bCollapsed
+                    'collapsed' => ($sSelected = bx_get('pp')) !== false && $sSelected == $aInput['provider_name'] ? false : $bCollapsed
                 );
 
                 $iProvider = $aInput['provider_id'];
@@ -146,17 +159,36 @@ class BxPaymentFormDetails extends BxTemplFormView
                 $this->aInputs[$aInput['name']] = array_merge($this->aInputs[$aInput['name']], $aAddon);
         }
 
-        $this->aInputs['provider_' . $iProvider . '_end'] = array(
-            'type' => 'block_end'
-        );
-        $this->aInputs['submit'] = array(
+        $sSubmitName = $this->_getSubmitName();
+        $this->aInputs[$sSubmitName] = [
             'type' => 'submit',
-            'name' => 'submit',
+            'name' => $sSubmitName,
             'value' => _t($this->_sLangsPrefix . 'form_details_input_do_submit'),
-        );
+        ];
+
+        $this->aInputs['provider_' . $iProvider . '_end'] = [
+            'type' => 'block_end'
+        ];
 
         return true;
     }
+
+    public function initWithParams($iProfileId, $sPaymentProvider)
+    {
+        $this->setProfileId($iProfileId);
+        $this->setPaymentProvider($sPaymentProvider);
+        $this->init();
+    }
+
+    protected function _getName()
+    {
+        return $this->_sModule . '_form_details' . ($this->_sPaymentProvider ? '_' . $this->_sPaymentProvider : '');
+    }
+
+    protected function _getSubmitName()
+    {
+        return 'submit' . ($this->_sPaymentProvider ? '_' . $this->_sPaymentProvider : '');
+    }
 }
 
 /** @} */

+ 4 - 4
modules/boonex/payment/classes/BxPaymentModule.php

@@ -517,7 +517,7 @@ class BxPaymentModule extends BxBaseModPaymentModule
 
         $aCustom = array();
         if(!empty($sCustom)) {
-            $aCustom = json_decode(base64_decode($sCustom), true);
+            $aCustom = unserialize(base64_decode($sCustom));
             if(empty($aCustom) || !is_array($aCustom))
                 $aCustom = array();
         }
@@ -557,7 +557,7 @@ class BxPaymentModule extends BxBaseModPaymentModule
 
         $aCustom = array();
         if(!empty($sCustom)) {
-            $aCustom = json_decode(base64_decode($sCustom), true);
+            $aCustom = unserialize(base64_decode($sCustom));
             if(empty($aCustom) || !is_array($aCustom))
                 $aCustom = array();
         }
@@ -608,7 +608,7 @@ class BxPaymentModule extends BxBaseModPaymentModule
 
         $aCustom = array();
         if(!empty($sCustom)) {
-            $aCustom = json_decode(base64_decode($sCustom), true);
+            $aCustom = unserialize(base64_decode($sCustom));
             if(empty($aCustom) || !is_array($aCustom))
                 $aCustom = array();
         }
@@ -1763,7 +1763,7 @@ class BxPaymentModule extends BxBaseModPaymentModule
                 'iItemCount' => $iItemCount,
                 'sItemAddons' => $sItemAddons,
                 'sRedirect' => $sRedirect,
-                'sCustom' => base64_encode(json_encode($aCustom))
+                'sCustom' => base64_encode(serialize($aCustom))
             );
 
             $aCartInfo = $this->getObjectCart()->getInfo(BX_PAYMENT_TYPE_RECURRING, $iClientId, $iSellerId, $this->_oConfig->descriptorA2S($aCartItem));

+ 4 - 4
modules/boonex/payment/classes/BxPaymentTemplate.php

@@ -99,7 +99,7 @@ class BxPaymentTemplate extends BxBaseModPaymentTemplate
             'item_id' => $iItemId,
             'item_count' => $iItemCount,
             'need_redirect' => (int)$bNeedRedirect,
-            'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(json_encode($aCustom)) : ''
+            'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(serialize($aCustom)) : ''
         ));
 
         return array($sJsCode, $sJsMethod);
@@ -117,7 +117,7 @@ class BxPaymentTemplate extends BxBaseModPaymentTemplate
             'item_id' => $iItemId,
             'item_count' => $iItemCount,
             'need_redirect' => (int)$bNeedRedirect,
-            'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(json_encode($aCustom)) : ''
+            'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(serialize($aCustom)) : ''
         ));
     }
 
@@ -146,7 +146,7 @@ class BxPaymentTemplate extends BxBaseModPaymentTemplate
                 'item_id' => $iItemId,
                 'item_count' => $iItemCount,
                 'redirect' => !empty($sRedirect) ? $sRedirect : '',
-                'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(json_encode($aCustom)) : ''
+                'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(serialize($aCustom)) : ''
             ))
         );
     }
@@ -164,7 +164,7 @@ class BxPaymentTemplate extends BxBaseModPaymentTemplate
             'item_id' => $iItemId,
             'item_count' => $iItemCount,
             'redirect' => !empty($sRedirect) ? $sRedirect : '',
-            'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(json_encode($aCustom)) : ''
+            'custom' => !empty($aCustom) && is_array($aCustom) ? base64_encode(serialize($aCustom)) : ''
         ));
     }
 

+ 1 - 0
modules/boonex/payment/template/details_form.html

@@ -0,0 +1 @@
+<div class="bx-payment-details bx-payment-details-__provider__ pt-4 first:pt-0">__form__</div>