BxPaymentDetails.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 BxPaymentDetails extends BxBaseModPaymentDetails
  12. {
  13. protected $_bIsApi;
  14. protected $_sLangsPrefix;
  15. function __construct()
  16. {
  17. $this->MODULE = 'bx_payment';
  18. parent::__construct();
  19. $this->_bIsApi = bx_is_api();
  20. $this->_sLangsPrefix = $this->_oModule->_oConfig->getPrefix('langs');
  21. }
  22. /**
  23. * @page service Service Calls
  24. * @section bx_payment Payment
  25. * @subsection bx_payment-page_blocks Page Blocks
  26. * @subsubsection bx_payment-get_block_details get_block_details
  27. *
  28. * @code bx_srv('bx_payment', 'get_block_details', [...], 'Details'); @endcode
  29. *
  30. * Get page block with payment providers' configuration settings represented as subforms.
  31. *
  32. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  33. *
  34. * @see BxPaymentDetails::serviceGetBlockDetails
  35. */
  36. /**
  37. * @ref bx_payment-get_block_details "get_block_details"
  38. */
  39. public function serviceGetBlockDetails($iUserId = BX_PAYMENT_EMPTY_ID, $mixedPaymentProvider = false)
  40. {
  41. if(!$this->_oModule->isLogged())
  42. return ($sMsg = $this->_sLangsPrefix . 'err_required_login') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox(_t($sMsg));
  43. $iUserId = $iUserId != BX_PAYMENT_EMPTY_ID ? $iUserId : $this->_oModule->getProfileId();
  44. $aPaymentProviders = [];
  45. if(!$mixedPaymentProvider) {
  46. $aPaymentProviders = $this->_oModule->_oDb->getProviders([
  47. 'type' => 'all',
  48. 'active' => true,
  49. 'order' => true
  50. ]);
  51. if(!empty($aPaymentProviders) && is_array($aPaymentProviders))
  52. $aPaymentProviders = array_keys($aPaymentProviders);
  53. }
  54. else {
  55. if(is_array($mixedPaymentProvider))
  56. $aPaymentProviders = $mixedPaymentProvider;
  57. else if(is_string($mixedPaymentProvider))
  58. $aPaymentProviders = [$mixedPaymentProvider];
  59. }
  60. if(!$aPaymentProviders)
  61. return ($sMsg = $this->_sLangsPrefix . 'msg_no_results') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox(_t($sMsg));
  62. $mixedContent = $this->_bIsApi ? [] : '';
  63. foreach($aPaymentProviders as $iIndex => $sPaymentProvider) {
  64. $mixedForm = $this->getForm($iUserId, $sPaymentProvider);
  65. if($this->_bIsApi)
  66. $mixedContent[] = bx_api_get_block('form', $mixedForm, [
  67. 'id' => $iIndex + 1,
  68. 'ext' => [
  69. 'name' => $mixedForm['attrs']['name'] ?? $this->MODULE,
  70. 'request' => ['url' => '/api.php?r=' . $this->MODULE . '/get_block_details/Details', 'immutable' => true]
  71. ]
  72. ]);
  73. else
  74. $mixedContent .= $this->_oModule->_oTemplate->parseHtmlByName('details_form.html', [
  75. 'provider' => str_replace('_', '-', $sPaymentProvider),
  76. 'form' => $mixedForm
  77. ]);
  78. }
  79. if(empty($mixedContent))
  80. return ($sMsg = $this->_sLangsPrefix . 'msg_no_results') && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : MsgBox(_t($sMsg));
  81. return $this->_bIsApi ? $mixedContent : $mixedContent;
  82. }
  83. public function getForm($iProfileId, $sPaymentProvider)
  84. {
  85. $CNF = &$this->_oModule->_oConfig->CNF;
  86. $oForm = BxTemplFormView::getObjectInstance($CNF['OBJECT_FORM_DETAILES'], $CNF['OBJECT_FORM_DETAILES_EDIT']);
  87. $oForm->initWithParams($iProfileId, $sPaymentProvider);
  88. $oForm->initChecker();
  89. if($oForm->isSubmitted()) {
  90. if($oForm->isValid()) {
  91. $aOptions = $this->_oModule->_oDb->getOptionsByProvider($sPaymentProvider);
  92. foreach($aOptions as $aOption) {
  93. $sValue = ($_sValue = $oForm->getCleanValue($aOption['name'])) !== false ? $_sValue : '';
  94. $this->_oModule->_oDb->updateOption($iProfileId, $aOption['id'], bx_process_input($sValue));
  95. }
  96. if(!$this->_bIsApi) {
  97. header('Location: ' . bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=payment-details', ['pp' => $sPaymentProvider])));
  98. return;
  99. }
  100. }
  101. else
  102. foreach($oForm->aInputs as $aInput)
  103. if(!empty($aInput['error']) && !empty($aInput['attrs']['bx-data-provider'])) {
  104. $sProviderBlock = 'provider_' . (int)$aInput['attrs']['bx-data-provider'] . '_begin';
  105. if(!empty($oForm->aInputs[$sProviderBlock]))
  106. $oForm->aInputs[$sProviderBlock]['collapsed'] = false;
  107. }
  108. }
  109. return $this->_bIsApi ? $oForm->getCodeAPI() : $oForm->getCode();
  110. }
  111. }
  112. /** @} */