BxPaymentConfig.php 13 KB

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