BxPaymentConfig.php 12 KB

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