BxPaymentConfig.php 11 KB

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