BxStripeConnectConfig.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 StripeConnect Stripe Connect
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. class BxStripeConnectConfig extends BxBaseModGeneralConfig
  12. {
  13. protected $_oDb;
  14. protected $_iMainSeller;
  15. protected $_sMode;
  16. protected $_sApiPublicKey;
  17. protected $_sApiSecretKey;
  18. protected $_sAccountType;
  19. protected $_sPayMode;
  20. protected $_sRefreshLink;
  21. protected $_sReturnLink;
  22. public function __construct($aModule)
  23. {
  24. parent::__construct($aModule);
  25. $this->CNF = [
  26. // module icon
  27. 'ICON' => 'cc-stripe col-blue1',
  28. // database tables
  29. 'TABLE_ENTRIES' => $aModule['db_prefix'] . 'accounts',
  30. 'TABLE_COMMISSIONS' => $aModule['db_prefix'] . 'commissions',
  31. // database fields
  32. 'FIELD_ID' => 'id',
  33. 'FIELD_ADDED' => 'added',
  34. 'FIELD_CHANGED' => 'changed',
  35. 'FIELD_PROFILE_ID' => 'profile_id',
  36. 'FIELD_LIVE_ACCOUNT_ID' => 'live_account_id',
  37. 'FIELD_LIVE_DETAILS' => 'live_details',
  38. 'FIELD_TEST_ACCOUNT_ID' => 'test_account_id',
  39. 'FIELD_TEST_DETAILS' => 'test_details',
  40. 'FIELD_CMS_ID' => 'id',
  41. 'FIELD_CMS_NAME' => 'name',
  42. 'FIELD_CMS_ACL_ID' => 'acl_id',
  43. 'FIELD_CMS_FEE_SINGLE' => 'fee_single',
  44. 'FIELD_CMS_FEE_RECURRING' => 'fee_recurring',
  45. // page URIs
  46. 'URL_API_AUTHORIZE' => 'https://connect.stripe.com/oauth/authorize',
  47. 'URL_API_DEAUTHORIZE' => 'https://connect.stripe.com/oauth/deauthorize',
  48. 'URL_API_TOKEN' => 'https://connect.stripe.com/oauth/token',
  49. 'URI_REDIRECT' => 'result',
  50. 'URI_NOTIFY' => 'notify',
  51. // some params
  52. 'PARAM_MODE' => 'bx_stripe_connect_mode',
  53. 'PARAM_API_PUBLIC_LIVE' => 'bx_stripe_connect_api_public_live',
  54. 'PARAM_API_SECRET_LIVE' => 'bx_stripe_connect_api_secret_live',
  55. 'PARAM_API_PUBLIC_TEST' => 'bx_stripe_connect_api_public_test',
  56. 'PARAM_API_SECRET_TEST' => 'bx_stripe_connect_api_secret_test',
  57. 'PARAM_PMODE' => 'bx_stripe_connect_pmode',
  58. 'PARAM_FEE_SINGLE' => 'bx_stripe_connect_fee_single',
  59. 'PARAM_FEE_RECURRING' => 'bx_stripe_connect_fee_recurring',
  60. // objects
  61. 'OBJECT_FORM_COMMISSIONS' => 'bx_stripe_connect_form_commissions',
  62. 'OBJECT_FORM_COMMISSIONS_DISPLAY_ADD' => 'bx_stripe_connect_form_commissions_add',
  63. 'OBJECT_FORM_COMMISSIONS_DISPLAY_EDIT' => 'bx_stripe_connect_form_commissions_edit',
  64. 'OBJECT_GRID_ACCOUNTS' => 'bx_stripe_connect_accounts',
  65. 'OBJECT_GRID_COMMISSIONS' => 'bx_stripe_connect_commissions',
  66. // Related Stripe payment provider name in Payments module
  67. 'STRIPE' => 'stripe_connect',
  68. ];
  69. $this->_aJsClasses = [
  70. 'main' => 'BxStripeConnectMain',
  71. 'embeds' => 'BxStripeConnectEmbeds',
  72. ];
  73. $this->_aJsObjects = [
  74. 'main' => 'oStripeConnectMain',
  75. 'embeds' => 'oBxStripeConnectEmbeds',
  76. ];
  77. $sPrefix = str_replace('_', '-', $this->_sName);
  78. $this->_aHtmlIds = [
  79. 'balances' => $sPrefix . '-balances',
  80. 'notification-banner' => $sPrefix . '-notification-banner',
  81. 'payments' => $sPrefix . '-payments',
  82. 'reporting-chart' => $sPrefix . '-reporting-chart',
  83. 'commissions_form' => $sPrefix . '-commissions-form-',
  84. 'commissions_popup' => $sPrefix . '-commissions-popup-',
  85. ];
  86. $this->_iMainSeller = 0;
  87. $this->_sAccountType = 'standard'; //'express'
  88. }
  89. public function init(&$oDb)
  90. {
  91. $this->_oDb = &$oDb;
  92. $sOptionPrefix = $this->getName();
  93. $this->_sMode = $this->_oDb->getParam($this->CNF['PARAM_MODE']);
  94. $this->_sApiPublicKey = $this->_oDb->getParam($this->CNF['PARAM_API_PUBLIC_' . ($this->_sMode == BX_STRIPE_CONNECT_MODE_LIVE ? 'LIVE' : 'TEST')]);
  95. $this->_sApiSecretKey = $this->_oDb->getParam($this->CNF['PARAM_API_SECRET_' . ($this->_sMode == BX_STRIPE_CONNECT_MODE_LIVE ? 'LIVE' : 'TEST')]);
  96. $this->_sPayMode = getParam($this->CNF['PARAM_PMODE']);
  97. $this->_sRefreshLink = $this->_getLink();
  98. $this->_sReturnLink = $this->_sRefreshLink;
  99. }
  100. public function getMainSeller()
  101. {
  102. if(empty($this->_iMainSeller))
  103. $this->_iMainSeller = (int)BxDolPayments::getInstance()->getOption('site_admin');
  104. return $this->_iMainSeller;
  105. }
  106. public function getMode()
  107. {
  108. return $this->_sMode;
  109. }
  110. public function getApiPublicKey()
  111. {
  112. return $this->_sApiPublicKey;
  113. }
  114. public function getApiSecretKey()
  115. {
  116. return $this->_sApiSecretKey;
  117. }
  118. public function getAccountType()
  119. {
  120. return $this->_sAccountType;
  121. }
  122. public function getPayMode()
  123. {
  124. return $this->_sPayMode;
  125. }
  126. public function getFee($sType, $iVendorId, $fAmount = 0)
  127. {
  128. $iResult = 0;
  129. $aVendorCms = [];
  130. if(($aVendorAcl = BxDolAcl::getInstance()->getMemberMembershipInfo($iVendorId)) && is_array($aVendorAcl))
  131. $aVendorCms = $this->_oDb->getCommissions(['type' => 'acl_id', 'acl_id' => (int)$aVendorAcl['id']]);
  132. $bVendorCms = !empty($aVendorCms) && is_array($aVendorCms);
  133. switch($sType) {
  134. case BX_PAYMENT_TYPE_SINGLE:
  135. if($bVendorCms && !empty($aVendorCms['fee_single']))
  136. $iResult = $this->_getFeeSingle($aVendorCms['fee_single'], $fAmount);
  137. if(!$iResult && ($mixedFee = getParam($this->CNF['PARAM_FEE_SINGLE'])))
  138. $iResult = $this->_getFeeSingle($mixedFee, $fAmount);
  139. break;
  140. case BX_PAYMENT_TYPE_RECURRING:
  141. if($bVendorCms && !empty($aVendorCms['fee_recurring']))
  142. $iResult = $this->_getFeeRecurring($aVendorCms['fee_recurring']);
  143. if(!$iResult && ($mixedFee = getParam($this->CNF['PARAM_FEE_RECURRING'])))
  144. $iResult = $this->_getFeeRecurring($mixedFee);
  145. break;
  146. }
  147. return $iResult;
  148. }
  149. public function getRefreshLink()
  150. {
  151. return $this->_sRefreshLink;
  152. }
  153. public function getReturnLink()
  154. {
  155. return $this->_sReturnLink;
  156. }
  157. protected function _getLink()
  158. {
  159. if($this->_bIsApi && ($sRootUrl = getParam('sys_api_url_root_email')) !== '') {
  160. if(substr(BX_DOL_URL_ROOT, -1) == '/' && substr($sRootUrl, -1) != '/')
  161. $sRootUrl .= '/';
  162. }
  163. else
  164. $sRootUrl = BX_DOL_URL_ROOT;
  165. return $sRootUrl . BxDolPermalinks::getInstance()->permalink('page.php?i=payment-details');
  166. }
  167. public function getHtmlIds($sKey = '')
  168. {
  169. if(empty($sKey))
  170. return $this->_aHtmlIds;
  171. return isset($this->_aHtmlIds[$sKey]) ? $this->_aHtmlIds[$sKey] : '';
  172. }
  173. protected function _getFeeSingle($mixedFee, $fAmount = false)
  174. {
  175. $iResult = 0;
  176. if(strpos($mixedFee, '%') !== false) {
  177. $iResult = (int)trim($mixedFee, '%');
  178. if($fAmount !== false)
  179. $iResult = (int)round($fAmount * $iResult / 100);
  180. }
  181. else if(is_numeric($mixedFee))
  182. $iResult = (int)(100 * (float)$mixedFee);
  183. return $iResult;
  184. }
  185. protected function _getFeeRecurring($mixedFee)
  186. {
  187. return (int)trim($mixedFee, '%');
  188. }
  189. }
  190. /** @} */