BxPaymentConfig.php 11 KB

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