BxPaymentConfig.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 BxPaymentConfig extends BxBaseModPaymentConfig
  12. {
  13. protected $_bCreditsOnly;
  14. protected $_iPayAttemptsMax;
  15. protected $_iPayAttemptsInterval;
  16. function __construct($aModule)
  17. {
  18. parent::__construct($aModule);
  19. $sBaseUrl = BX_DOL_URL_ROOT . $this->getBaseUri();
  20. $this->CNF = array_merge($this->CNF, array(
  21. // module icon
  22. 'ICON' => 'credit-card col-gray-dark',
  23. // database tables
  24. 'TABLE_COMMISSIONS' => $aModule['db_prefix'] . 'commissions',
  25. 'TABLE_INVOICES' => $aModule['db_prefix'] . 'invoices',
  26. // page URIs
  27. 'URL_JOIN' => 'page.php?i=payment-join',
  28. 'URL_CARTS' => 'page.php?i=payment-carts',
  29. 'URL_CART' => 'page.php?i=payment-cart',
  30. 'URL_CART_CHECKOUT' => $sBaseUrl . 'initialize_checkout/' . BX_PAYMENT_TYPE_SINGLE . '/',
  31. 'URL_SUBSCRIBE' => $sBaseUrl . 'subscribe/',
  32. 'URL_SUBSCRIBE_JSON' => $sBaseUrl . 'subscribe_json/',
  33. 'URL_SUBSCRIPTIONS' => 'page.php?i=payment-sbs-list-my',
  34. 'URL_HISTORY' => 'page.php?i=payment-history',
  35. 'URL_ORDERS' => 'page.php?i=payment-orders',
  36. 'URL_INVOICES' => 'page.php?i=payment-invoices',
  37. 'URL_DETAILS' => 'page.php?i=payment-details',
  38. 'URL_RETURN' => 'page.php?i=payment-cart-thank-you',
  39. 'URL_RETURN_DATA' => $sBaseUrl . 'finalize_checkout/',
  40. 'URL_NOTIFY' => $sBaseUrl . 'notify/',
  41. 'URL_CHECKOUT_OFFLINE' => 'page.php?i=payment-checkout-offline',
  42. 'KEY_ARRAY_PRICE_SINGLE' => 'price_single',
  43. 'KEY_ARRAY_PRICE_RECURRING' => 'price_recurring',
  44. 'KEY_ARRAY_TRIAL_RECURRING' => 'trial_recurring',
  45. // objects
  46. 'OBJECT_FORM_PRELISTS_CURRENCIES' => 'bx_payment_currencies',
  47. 'OBJECT_PP_OFFLINE' => 'offline', //Offline payment provider
  48. 'OBJECT_PP_CREDITS' => 'credits', //Credits payment provider
  49. 'MODULE_CREDITS' => 'bx_credits',
  50. // some language keys
  51. 'T' => array(
  52. 'MSG_ITEM_ADDED' => '_bx_payment_msg_item_added',
  53. 'MSG_ITEM_DELETED' => '_bx_payment_msg_item_deleted',
  54. 'MSG_SINGLE_SELLER_MODE' => '_bx_payment_msg_single_seller',
  55. 'ERR_WRONG_DATA' => '_bx_payment_err_wrong_data',
  56. 'ERR_REQUIRED_LOGIN' => '_bx_payment_err_required_login',
  57. 'ERR_NOT_ACCEPT_PAYMENTS' => '_bx_payment_err_not_accept_payments',
  58. 'ERR_SELF_PURCHASE' => '_bx_payment_err_self_purchase',
  59. 'ERR_INACTIVE_VENDOR' => '_bx_payment_err_inactive_vendor',
  60. 'ERR_UNKNOWN_VENDOR' => '_bx_payment_err_unknown_vendor',
  61. 'BLOCK_TITLE_CART' => '_bx_payment_page_block_title_cart',
  62. 'POPUP_PROVIDERS_SELECT' => '_bx_payment_popup_title_crd_providers_select',
  63. 'TXT_CART_PROVIDER' => '_bx_payment_txt_cart_'
  64. )
  65. ));
  66. $this->_aHtmlIds = array(
  67. 'cart' => array(
  68. 'providers_select' => $this->_sName . '-cart-providers-select-',
  69. ),
  70. 'history' => array(
  71. 'order_history_view' => $this->_sName . '-order-view-history',
  72. ),
  73. 'subscription' => array(
  74. 'order_subscription_view' => $this->_sName . '-order-view-subscription',
  75. 'order_subscription_get_details' => $this->_sName . '-order-get-details-subscription',
  76. 'order_subscription_change_details' => $this->_sName . '-order-change-details-subscription',
  77. 'order_subscription_get_billing' => $this->_sName . '-order-get-billing-subscription',
  78. 'order_subscription_change_billing' => $this->_sName . '-order-change-billing-subscription',
  79. 'form_subscription_change_billing' => $this->_sName . '-form-change-billing-subscription',
  80. 'form_subscription_change_details' => $this->_sName . '-form-change-details-subscription',
  81. ),
  82. 'pending' => array(
  83. 'order_pending_view' => $this->_sName . '-order-view-pending',
  84. 'order_pending_process' => $this->_sName . '-order-process',
  85. ),
  86. 'processed' => array(
  87. 'order_processed_view' => $this->_sName . '-order-view-processed',
  88. 'order_processed_add' => $this->_sName . '-order-add',
  89. 'order_processed_client_id' => $this->_sName . '-oa-client-id',
  90. 'order_processed_client' => $this->_sName . '-oa-client',
  91. 'order_processed_items' => $this->_sName . '-oa-items',
  92. ),
  93. 'commission' => array(
  94. 'popup_add' => $this->_sName . '-commission-popup-add',
  95. 'popup_edit' => $this->_sName . '-commission-popup-edit',
  96. ),
  97. 'invoice' => array(
  98. 'popup_edit' => $this->_sName . '-invoice-popup-edit',
  99. )
  100. );
  101. $this->_aPerPage = array(
  102. 'orders' => 10,
  103. 'history' => 10
  104. );
  105. $this->_aHandlers = array();
  106. $this->_aJsClasses = array(
  107. 'cart' => 'BxPaymentCart',
  108. 'history' => 'BxPaymentOrders',
  109. 'subscription' => 'BxPaymentSubscriptions',
  110. 'pending' => 'BxPaymentOrders',
  111. 'processed' => 'BxPaymentOrders',
  112. 'chargebee_v3' => 'BxPaymentProviderChargebeeV3',
  113. 'stripe' => 'BxPaymentProviderStripe',
  114. 'stripe_v3' => 'BxPaymentProviderStripeV3'
  115. );
  116. $this->_aJsObjects = array(
  117. 'cart' => 'oPaymentCart',
  118. 'history' => 'oPaymentOrders',
  119. 'subscription' => 'oBxPaymentSubscriptions',
  120. 'pending' => 'oPaymentOrders',
  121. 'processed' => 'oPaymentOrders',
  122. 'chargebee_v3' => 'oPaymentProviderChargebeeV3',
  123. 'stripe' => 'oPaymentProviderStripe',
  124. 'stripe_v3' => 'oPaymentProviderStripeV3'
  125. );
  126. $this->_bCreditsOnly = false;
  127. $this->_iPayAttemptsMax = 3;
  128. $this->_iPayAttemptsInterval = 86400; //--- in sec
  129. }
  130. public function init(&$oDb)
  131. {
  132. parent::init($oDb);
  133. $sPrefix = $this->getPrefix('options');
  134. $this->_bCreditsOnly = $this->_oDb->getParam($sPrefix . 'credits_only') == 'on';
  135. }
  136. public function isCreditsOnly()
  137. {
  138. return $this->_bCreditsOnly;
  139. }
  140. public function getPayAttemptsMax()
  141. {
  142. return $this->_iPayAttemptsMax;
  143. }
  144. public function getPayAttemptsInterval()
  145. {
  146. return $this->_iPayAttemptsInterval;
  147. }
  148. public function getPrice($sType, $aItem)
  149. {
  150. $fPrice = 0;
  151. switch($sType) {
  152. case BX_PAYMENT_TYPE_SINGLE:
  153. $fPrice = $aItem[$this->getKey('KEY_ARRAY_PRICE_SINGLE')];
  154. break;
  155. case BX_PAYMENT_TYPE_RECURRING:
  156. $fPrice = $aItem[$this->getKey('KEY_ARRAY_PRICE_RECURRING')];
  157. break;
  158. }
  159. return (float)$fPrice;
  160. }
  161. public function getTrial($sType, $aItem)
  162. {
  163. $iTrial = 0;
  164. switch($sType) {
  165. case BX_PAYMENT_TYPE_RECURRING:
  166. $iTrial = $aItem[$this->getKey('KEY_ARRAY_TRIAL_RECURRING')];
  167. break;
  168. }
  169. return (int)$iTrial;
  170. }
  171. public function getModuleId($mixedId)
  172. {
  173. if(is_numeric($mixedId))
  174. return (int)$mixedId;
  175. if(is_string($mixedId)) {
  176. $aInfo = $this->_oDb->getModuleByName($mixedId);
  177. if(!is_array($aInfo) || empty($aInfo['id']))
  178. $aInfo = $this->_oDb->getModuleByUri($mixedId);
  179. if(is_array($aInfo) && !empty($aInfo['id']))
  180. return (int)$aInfo['id'];
  181. }
  182. return 0;
  183. }
  184. public function a2s($a)
  185. {
  186. return base64_encode(serialize($a));
  187. }
  188. public function s2a($s)
  189. {
  190. return unserialize(base64_decode($s));
  191. }
  192. public function descriptorA2S($a)
  193. {
  194. return implode($this->getDivider('DIVIDER_DESCRIPTOR'), $a);
  195. }
  196. public function descriptorS2A($s)
  197. {
  198. return explode($this->getDivider('DIVIDER_DESCRIPTOR'), $s);
  199. }
  200. /**
  201. * Conver items to array with necessary structure.
  202. *
  203. * @param string/array $mixed - string with cart items divided with (:) or an array of cart items.
  204. * @return array with items.
  205. */
  206. public function descriptorsM2A($mixed)
  207. {
  208. $aResults = array();
  209. if(is_string($mixed))
  210. $aItems = explode($this->getDivider('DIVIDER_DESCRIPTORS'), $mixed);
  211. else if(is_array($mixed))
  212. $aItems = $mixed;
  213. else
  214. $aItems = array();
  215. foreach($aItems as $mixedItem) {
  216. $aItem = is_array($mixedItem) ? $mixedItem : $this->descriptorS2A($mixedItem);
  217. $aResult = array('vendor_id' => $aItem[0], 'module_id' => $aItem[1], 'item_id' => $aItem[2], 'item_count' => $aItem[3]);
  218. if(isset($aItem[4]))
  219. $aResult['item_addons'] = $aItem[4];
  220. $aResults[] = $aResult;
  221. }
  222. return $aResults;
  223. }
  224. public function putCustom($mDsc, $aCustom, &$aCustoms)
  225. {
  226. if(empty($aCustom) || !is_array($aCustom))
  227. return;
  228. if(is_array($mDsc))
  229. $mDsc = $this->descriptorA2S(array_slice($mDsc, 0, 3));
  230. $aCustoms[$mDsc] = !empty($aCustoms[$mDsc]) && is_array($aCustoms[$mDsc]) ? array_merge($aCustoms[$mDsc], $aCustom) : $aCustom;
  231. }
  232. public function getCustom($mDsc, &$aCustoms)
  233. {
  234. if(is_array($mDsc))
  235. $mDsc = $this->descriptorA2S(array_slice($mDsc, 0, 3));
  236. if(empty($aCustoms) || !is_array($aCustoms) || empty($aCustoms[$mDsc]))
  237. return array();
  238. return $aCustoms[$mDsc];
  239. }
  240. public function pullCustom($mDsc, &$aCustoms)
  241. {
  242. if(is_array($mDsc))
  243. $mDsc = $this->descriptorA2S(array_slice($mDsc, 0, 3));
  244. if(empty($aCustoms) || !is_array($aCustoms) || empty($aCustoms[$mDsc]))
  245. return array();
  246. $aResult = $aCustoms[$mDsc];
  247. unset($aCustoms[$mDsc]);
  248. return $aResult;
  249. }
  250. public function http2https($s)
  251. {
  252. if(strncmp($s, 'https://', 8) === 0)
  253. return $s;
  254. return 'https://' . bx_ltrim_str($s, 'http://');
  255. }
  256. public function sortByColumn($sColumn, &$aValues)
  257. {
  258. return usort($aValues, function($aV1, $aV2) use ($sColumn) {
  259. if($aV1[$sColumn] == $aV2[$sColumn])
  260. return 0;
  261. return $aV1[$sColumn] < $aV2[$sColumn] ? -1 : 1;
  262. });
  263. }
  264. }
  265. /** @} */