BxPaymentConfig.php 13 KB

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