BxDonationsModule.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 Donations Donations
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_DONATIONS_BTYPE_SINGLE', 'single');
  12. define('BX_DONATIONS_BTYPE_RECURRING', 'recurring');
  13. class BxDonationsModule extends BxBaseModGeneralModule
  14. {
  15. function __construct($aModule)
  16. {
  17. parent::__construct($aModule);
  18. $this->_oConfig->init($this->_oDb);
  19. }
  20. /**
  21. * ACTION METHODS
  22. */
  23. public function actionCheckName()
  24. {
  25. $CNF = &$this->_oConfig->CNF;
  26. $sName = bx_process_input(bx_get('name'));
  27. if(empty($sName))
  28. return echoJson(array());
  29. $sResult = '';
  30. $iId = (int)bx_get('id');
  31. if(!empty($iId)) {
  32. $aPrice = $this->_oDb->getTypes(array('type' => 'by_id', 'value' => $iId));
  33. if(strcmp($sName, $aPrice[$CNF['FIELD_NAME']]) == 0)
  34. $sResult = $sName;
  35. }
  36. echoJson(array(
  37. 'name' => !empty($sResult) ? $sResult : $this->_oConfig->getTypeName($sName)
  38. ));
  39. }
  40. public function actionMakeOther()
  41. {
  42. $CNF = &$this->_oConfig->CNF;
  43. $sBillingType = bx_process_input(bx_get('btype'));
  44. $bBillingTypeRecurring = $sBillingType == BX_DONATIONS_BTYPE_RECURRING;
  45. $fAmount = bx_process_input(bx_get('amount'), BX_DATA_FLOAT);
  46. if(empty($sBillingType) || empty($fAmount))
  47. return echoJson(array());
  48. if($fAmount < $CNF['PARAM_OTHER_PRICE_MIN'])
  49. return echoJson(array('msg' => _t('_bx_donations_err_min_value', _t_format_currency($CNF['PARAM_OTHER_PRICE_MIN'], getParam($CNF['PARAM_AMOUNT_PRECISION'])))));
  50. $iOwner = $this->_oConfig->getOwner();
  51. $sModule = $this->getName();
  52. $iTypeId = $this->_oDb->insertType(array(
  53. $CNF['FIELD_NAME'] => $this->_oConfig->getTypeNameCustom(),
  54. $CNF['FIELD_PERIOD'] => $bBillingTypeRecurring ? $CNF['PARAM_OTHER_PERIOD'] : 0,
  55. $CNF['FIELD_PERIOD_UNIT'] => $bBillingTypeRecurring ? $CNF['PARAM_OTHER_PERIOD_UNIT'] : '',
  56. $CNF['FIELD_AMOUNT'] => $fAmount,
  57. $CNF['FIELD_CUSTOM'] => 1
  58. ));
  59. $oPayments = BxDolPayments::getInstance();
  60. $aResult = array();
  61. switch($sBillingType) {
  62. case BX_DONATIONS_BTYPE_SINGLE:
  63. $aResultSrv = $oPayments->addToCart($iOwner, $sModule, $iTypeId, 1);
  64. if(!empty($aResultSrv['code']))
  65. $aResult = array('msg' => isset($aResultSrv['message']) ? $aResultSrv['message'] : _t('_bx_donations_err_cannot_perform'));
  66. else
  67. $aResult = array('redirect' => $oPayments->getCartUrl($iOwner));
  68. break;
  69. case BX_DONATIONS_BTYPE_RECURRING:
  70. $aResultSrv = $oPayments->subscribeWithAddons($iOwner, '', $sModule, $iTypeId, 1);
  71. if(!empty($aResultSrv['code']))
  72. $aResult = array('msg' => isset($aResultSrv['message']) ? $aResultSrv['message'] : _t('_bx_donations_err_cannot_perform'));
  73. else
  74. $aResult = isset($aResultSrv['popup']) || isset($aResultSrv['redirect']) ? $aResultSrv : array('redirect' => $oPayments->getSubscriptionsUrl());
  75. break;
  76. }
  77. return echoJson($aResult);
  78. }
  79. /**
  80. * SERVICE METHODS
  81. */
  82. public function serviceGetTypesBy($aParams)
  83. {
  84. return $this->_oDb->getTypes($aParams);
  85. }
  86. public function serviceIncludeCssJs()
  87. {
  88. return $this->_oTemplate->getIncludeCssJs();
  89. }
  90. public function serviceGetBlockMake()
  91. {
  92. return $this->_oTemplate->getBlockMake();
  93. }
  94. public function serviceGetBlockList()
  95. {
  96. return $this->_getBlockList();
  97. }
  98. public function serviceGetBlockListAll()
  99. {
  100. return $this->_getBlockList('all');
  101. }
  102. public function serviceGetPaymentData()
  103. {
  104. $CNF = &$this->_oConfig->CNF;
  105. $oPermalink = BxDolPermalinks::getInstance();
  106. return array_merge($this->_aModule, [
  107. 'url_browse_order_common' => bx_absolute_url(bx_append_url_params($oPermalink->permalink($CNF['URL_LIST']), ['filter' => '{order}'], false)),
  108. 'url_browse_order_administration' => bx_absolute_url(bx_append_url_params($oPermalink->permalink($CNF['URL_LIST_ALL']), ['filter' => '{order}'], false))
  109. ]);
  110. }
  111. public function serviceGetCartItem($mixedItemId)
  112. {
  113. $CNF = &$this->_oConfig->CNF;
  114. if(!$mixedItemId)
  115. return array();
  116. $aItem = $this->_oDb->getTypes(array(
  117. 'type' => 'by_' . (is_numeric($mixedItemId) ? 'id' : 'name'),
  118. 'value' => $mixedItemId
  119. ));
  120. if(empty($aItem) || !is_array($aItem))
  121. return array();
  122. return array (
  123. 'id' => $aItem[$CNF['FIELD_ID']],
  124. 'author_id' => $this->_oConfig->getOwner(),
  125. 'name' => $aItem[$CNF['FIELD_NAME']],
  126. 'title' => _t($this->_oConfig->isShowTitle() && !empty($aItem[$CNF['FIELD_TITLE']]) ? $aItem[$CNF['FIELD_TITLE']] : '_bx_donations_txt_cart_item_title'),
  127. 'description' => _t('_bx_donations_txt_cart_item_description', getParam('site_title')),
  128. 'url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_MAKE'])),
  129. 'price_single' => $aItem[$CNF['FIELD_AMOUNT']],
  130. 'price_recurring' => $aItem[$CNF['FIELD_AMOUNT']],
  131. 'period_recurring' => $aItem[$CNF['FIELD_PERIOD']],
  132. 'period_unit_recurring' => $aItem[$CNF['FIELD_PERIOD_UNIT']],
  133. 'trial_recurring' => ''
  134. );
  135. }
  136. public function serviceGetCartItems($iSellerId)
  137. {
  138. $CNF = &$this->_oConfig->CNF;
  139. $iSellerId = (int)$iSellerId;
  140. if(empty($iSellerId))
  141. return array();
  142. $aItems = $this->_oDb->getTypes(array('type' => 'all'));
  143. if(empty($aItems) || !is_array($aItems))
  144. return array();
  145. $bShowTitle = $this->_oConfig->isShowTitle();
  146. $aResult = array();
  147. foreach($aItems as $aItem)
  148. $aResult[] = array(
  149. 'id' => $aItem[$CNF['FIELD_ID']],
  150. 'author_id' => $this->_oConfig->getOwner(),
  151. 'name' => $aItem[$CNF['FIELD_NAME']],
  152. 'title' => _t($bShowTitle && !empty($aItem[$CNF['FIELD_TITLE']]) ? $aItem[$CNF['FIELD_TITLE']] : '_bx_donations_txt_cart_item_title'),
  153. 'description' => _t('_bx_donations_txt_cart_item_description', getParam('site_title')),
  154. 'url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_MAKE'])),
  155. 'price_single' => $aItem[$CNF['FIELD_AMOUNT']],
  156. 'price_recurring' => $aItem[$CNF['FIELD_AMOUNT']],
  157. 'period_recurring' => $aItem[$CNF['FIELD_PERIOD']],
  158. 'period_unit_recurring' => $aItem[$CNF['FIELD_PERIOD_UNIT']],
  159. 'trial_recurring' => ''
  160. );
  161. return $aResult;
  162. }
  163. public function serviceRegisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  164. {
  165. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_DONATIONS_BTYPE_SINGLE);
  166. }
  167. public function serviceRegisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  168. {
  169. return $this->_serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_DONATIONS_BTYPE_RECURRING);
  170. }
  171. public function serviceReregisterCartItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  172. {
  173. return array();
  174. }
  175. public function serviceReregisterSubscriptionItem($iClientId, $iSellerId, $iItemIdOld, $iItemIdNew, $sOrder)
  176. {
  177. return array();
  178. }
  179. public function serviceUnregisterCartItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  180. {
  181. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_DONATIONS_BTYPE_SINGLE);
  182. }
  183. public function serviceUnregisterSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense)
  184. {
  185. return $this->_serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, BX_DONATIONS_BTYPE_RECURRING);
  186. }
  187. public function serviceCancelSubscriptionItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder)
  188. {
  189. return true;
  190. }
  191. protected function _serviceRegisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  192. {
  193. $CNF = &$this->_oConfig->CNF;
  194. $aItem = $this->serviceGetCartItem($iItemId);
  195. if(empty($aItem) || !is_array($aItem))
  196. return array();
  197. if(!$this->_oDb->registerEntry($iClientId, $iItemId, $iItemCount, $sOrder, $sLicense))
  198. return array();
  199. /**
  200. * @hooks
  201. * @hookdef hook-bx_donations-donation_register 'bx_donations', 'donation_register' - hook after the donation payment was processed with payment processing module
  202. * - $unit_name - equals `bx_donations`
  203. * - $action - equals `donation_register`
  204. * - $object_id - not used
  205. * - $sender_id - not used
  206. * - $extra_params - array of additional params with the following array keys:
  207. * - `donation_id` - [int] donation id
  208. * - `profile_id` - [int] donator profile id
  209. * - `order` - [string] order number provided with payment processing module
  210. * - `type` - [string] payment type ('single' or 'recurring')
  211. * - `amount` - [float] donated amount
  212. * - `count` - [int] number of items in order
  213. * @hook @ref hook-bx_donations-donation_register
  214. */
  215. bx_alert($this->getName(), 'donation_register', 0, false, [
  216. 'donation_id' => $iItemId,
  217. 'profile_id' => $iClientId,
  218. 'order' => $sOrder,
  219. 'type' => $sType,
  220. 'amount' => (float)$aItem['price_' . $sType],
  221. 'count' => $iItemCount
  222. ]);
  223. $oClient = BxDolProfile::getInstanceMagic($iClientId);
  224. sendMailTemplate($CNF['ETEMPLATE_DONATED'], 0, $iClientId, array(
  225. 'client_name' => $oClient->getDisplayName(),
  226. ));
  227. return $aItem;
  228. }
  229. protected function _serviceUnregisterItem($iClientId, $iSellerId, $iItemId, $iItemCount, $sOrder, $sLicense, $sType)
  230. {
  231. if(!$this->_oDb->unregisterEntry($iClientId, $iItemId, $sOrder, $sLicense))
  232. return false;
  233. /**
  234. * @hooks
  235. * @hookdef hook-bx_donations-donation_unregister 'bx_donations', 'donation_unregister' - hook after the donation payment was refunded with payment processing module
  236. * It's equivalent to @ref hook-bx_donations-donation_register
  237. * except `amount` parameter in $extra_params is missing
  238. * @hook @ref hook-bx_donations-donation_unregister
  239. */
  240. bx_alert($this->getName(), 'donation_unregister', 0, false, [
  241. 'donation_id' => $iItemId,
  242. 'profile_id' => $iClientId,
  243. 'order' => $sOrder,
  244. 'type' => $sType,
  245. 'count' => $iItemCount
  246. ]);
  247. return true;
  248. }
  249. /*
  250. * INTERNAL METHODS
  251. */
  252. protected function _getBlockList($sType = '')
  253. {
  254. $CNF = &$this->_oConfig->CNF;
  255. $sGrid = $CNF['OBJECT_GRID_LIST' . (!empty($sType) ? '_' . strtoupper($sType) : '')];
  256. $oGrid = BxDolGrid::getObjectInstance($sGrid);
  257. if(!$oGrid)
  258. return '';
  259. return $oGrid->getCode();
  260. }
  261. }
  262. /** @} */