BxPaymentFormDetails.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Payment Payment
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxPaymentDetailsFormCheckerHelper extends BxDolFormCheckerHelper
  12. {
  13. function checkHttps ($s)
  14. {
  15. return empty($s) || substr(BX_DOL_URL_ROOT, 0, 5) == 'https';
  16. }
  17. }
  18. class BxPaymentFormDetails extends BxTemplFormView
  19. {
  20. protected $_sModule;
  21. protected $_oModule;
  22. protected $_sLangsPrefix;
  23. protected $_bCollapseFirst;
  24. protected $_iProfileId;
  25. protected $_sPaymentProvider;
  26. function __construct($aInfo, $oTemplate = false)
  27. {
  28. parent::__construct($aInfo, $oTemplate);
  29. $this->_sModule = 'bx_payment';
  30. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  31. $this->_sLangsPrefix = $this->_oModule->_oConfig->getPrefix('langs');
  32. $this->_bCollapseFirst = true;
  33. $this->_iProfileId = $this->_oModule->getProfileId();
  34. $this->_sPaymentProvider = '';
  35. }
  36. public function setProfileId($iProfileId = BX_PAYMENT_EMPTY_ID)
  37. {
  38. if($iProfileId != BX_PAYMENT_EMPTY_ID)
  39. $this->_iProfileId = $iProfileId;
  40. }
  41. public function setPaymentProvider($sPaymentProvider)
  42. {
  43. $this->_sPaymentProvider = $sPaymentProvider;
  44. $this->aParams['db']['submit_name'] = $this->_getSubmitName();
  45. $sName = $this->_getName();
  46. $this->setId($sName);
  47. $this->setName($sName);
  48. }
  49. public function init()
  50. {
  51. $this->aInputs = [];
  52. $aInputs = $this->_oModule->_oDb->getForm($this->_sPaymentProvider);
  53. if(empty($aInputs))
  54. return false;
  55. $bSiteAdmin = $this->_oModule->_oConfig->isSiteAdmin();
  56. $bExtendedMode = $this->_oModule->_oConfig->isExtendedMode();
  57. $bSingleSeller = $this->_oModule->_oConfig->isSingleSeller();
  58. $bCollapsed = $this->_bCollapseFirst;
  59. $iProvider = 0;
  60. $sProvider = "";
  61. $oProvider = null;
  62. foreach($aInputs as $aInput) {
  63. if((int)$aInput['provider_for_owner_only'] != 0 && !$bSiteAdmin)
  64. continue;
  65. if((int)$aInput['provider_single_seller'] == 0 && $bSingleSeller)
  66. continue;
  67. if($iProvider != $aInput['provider_id']) {
  68. $sBlockHeaderName = 'provider_' . $aInput['provider_id'] . '_begin';
  69. $this->aInputs[$sBlockHeaderName] = array(
  70. 'type' => 'block_header',
  71. 'name' => $sBlockHeaderName,
  72. 'caption' => _t($aInput['provider_caption']),
  73. 'info' => _t($aInput['provider_description']),
  74. 'collapsable' => true,
  75. 'collapsed' => ($sSelected = bx_get('pp')) !== false && $sSelected == $aInput['provider_name'] ? false : $bCollapsed
  76. );
  77. $iProvider = $aInput['provider_id'];
  78. $sProvider = $aInput['provider_name'];
  79. $oProvider = $this->_oModule->getObjectProvider($sProvider, $this->_iProfileId);
  80. $bCollapsed = true;
  81. }
  82. $mixedValue = $oProvider->getOption($aInput['name']);
  83. if(!$bExtendedMode && ($iInpExt = (int)$aInput['extended']) != BX_PAYMENT_POPT_EXT_NONE)
  84. $this->aInputs[$aInput['name']] = [
  85. 'type' => 'hidden',
  86. 'name' => $aInput['name'],
  87. 'value' => $mixedValue !== false && $iInpExt == BX_PAYMENT_POPT_EXT_SAVE ? $mixedValue : $aInput['value'],
  88. 'attrs' => [
  89. 'bx-data-provider' => $iProvider
  90. ]
  91. ];
  92. else
  93. $this->aInputs[$aInput['name']] = [
  94. 'type' => $aInput['type'],
  95. 'name' => $aInput['name'],
  96. 'caption' => _t($aInput['caption']),
  97. 'value' => $mixedValue !== false ? $mixedValue : '',
  98. 'info' => _t($aInput['description']),
  99. 'attrs' => [
  100. 'bx-data-provider' => $iProvider
  101. ],
  102. 'checker' => [
  103. 'func' => $aInput['check_type'],
  104. 'params' => $aInput['check_params'],
  105. 'error' => _t($aInput['check_error']),
  106. ],
  107. 'db' => [
  108. 'pass' => 'Xss',
  109. ]
  110. ];
  111. //--- Make some field dependent actions ---//
  112. $aAddon = [];
  113. switch($this->aInputs[$aInput['name']]['type']) {
  114. case 'secret':
  115. $this->aInputs[$aInput['name']] = array_merge($this->aInputs[$aInput['name']], [
  116. 'type' => 'text',
  117. 'secret' => 1
  118. ]);
  119. break;
  120. case 'select':
  121. if(empty($aInput['extra']))
  122. break;
  123. $aAddon = ['values' => []];
  124. if(BxDolService::isSerializedService($aInput['extra']))
  125. $aAddon['values'] = BxDolService::callSerialized($aInput['extra']);
  126. else {
  127. $aPairs = explode(',', $aInput['extra']);
  128. foreach($aPairs as $sPair) {
  129. $aPair = explode('|', $sPair);
  130. $aAddon['values'][] = ['key' => $aPair[0], 'value' => _t($aPair[1])];
  131. }
  132. }
  133. break;
  134. case 'checkbox':
  135. if(in_array($this->aInputs[$aInput['name']]['value'], ['on', 1]))
  136. $aAddon = ['checked' => true];
  137. $this->aInputs[$aInput['name']]['value'] = $this->_bIsApi ? 1 : 'on';
  138. break;
  139. case 'value':
  140. $sMethod = 'get' . bx_gen_method_name(str_replace($aInput['provider_option_prefix'], '', $aInput['name']));
  141. if(method_exists($oProvider, $sMethod))
  142. $this->aInputs[$aInput['name']]['value'] = $oProvider->$sMethod($this->_iProfileId);
  143. break;
  144. case 'custom':
  145. $sMethod = 'get' . bx_gen_method_name(str_replace($aInput['provider_option_prefix'], '', $aInput['name']));
  146. if(method_exists($oProvider, $sMethod)) {
  147. $mixedContent = $oProvider->$sMethod($this->_iProfileId);
  148. if(is_array($mixedContent))
  149. $this->aInputs[$aInput['name']] = array_merge($this->aInputs[$aInput['name']], $mixedContent);
  150. else
  151. $this->aInputs[$aInput['name']]['content'] = $mixedContent;
  152. }
  153. break;
  154. }
  155. if(!empty($aAddon) && is_array($aAddon))
  156. $this->aInputs[$aInput['name']] = array_merge($this->aInputs[$aInput['name']], $aAddon);
  157. }
  158. $sSubmitName = $this->_getSubmitName();
  159. $this->aInputs[$sSubmitName] = [
  160. 'type' => 'submit',
  161. 'name' => $sSubmitName,
  162. 'value' => _t($this->_sLangsPrefix . 'form_details_input_do_submit'),
  163. ];
  164. $this->aInputs['provider_' . $iProvider . '_end'] = [
  165. 'type' => 'block_end'
  166. ];
  167. return true;
  168. }
  169. public function initWithParams($iProfileId, $sPaymentProvider)
  170. {
  171. $this->setProfileId($iProfileId);
  172. $this->setPaymentProvider($sPaymentProvider);
  173. $this->init();
  174. }
  175. protected function _getName()
  176. {
  177. return $this->_sModule . '_form_details' . ($this->_sPaymentProvider ? '_' . $this->_sPaymentProvider : '');
  178. }
  179. protected function _getSubmitName()
  180. {
  181. return 'submit' . ($this->_sPaymentProvider ? '_' . $this->_sPaymentProvider : '');
  182. }
  183. }
  184. /** @} */