api.payments.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * Returns balance for some login
  4. *
  5. * @param string $login Existing user login
  6. * @return float
  7. */
  8. function zb_CashGetUserBalance($login) {
  9. $login = vf($login);
  10. $query = "SELECT `Cash` from `users` WHERE `login`='" . $login . "'";
  11. $cash = simple_query($query);
  12. return($cash['Cash']);
  13. }
  14. /**
  15. * Checks is input number valid money format or not?
  16. *
  17. * @param $number an string to check
  18. *
  19. * @return bool
  20. */
  21. function zb_checkMoney($number) {
  22. return preg_match("/^-?[0-9]+(?:\.[0-9]{1,9})?$/", $number);
  23. }
  24. /**
  25. * Add some cash to user login in stargazer, and creates payment record in registry
  26. *
  27. * @global object $billing Pre-initialized low-level stargazer handlers
  28. * @param string $login Existing users login
  29. * @param float $cash Amount of money to put/set on user login
  30. * @param string $operation Operation type: add, correct,set,mock
  31. * @param int $cashtype Existing cashtype ID for payment registry
  32. * @param string $note Payment notes
  33. *
  34. * @return void
  35. */
  36. function zb_CashAdd($login, $cash, $operation, $cashtype, $note) {
  37. global $billing;
  38. $login = mysql_real_escape_string($login);
  39. $cash = mysql_real_escape_string($cash);
  40. $cash = preg_replace("#[^0-9\-\.]#Uis", '', $cash);
  41. $cash = trim($cash);
  42. $cashtype = vf($cashtype);
  43. $note = mysql_real_escape_string($note);
  44. $date = curdatetime();
  45. $balance = zb_CashGetUserBalance($login);
  46. $admin = whoami();
  47. $noteprefix = '';
  48. /**
  49. * They wanna fuck you for free and explode ya
  50. * I gonna waiting no time let me show ya
  51. * You gonna be kidding Couse nothing is happening
  52. * You wanna be happy So follow me
  53. */
  54. switch ($operation) {
  55. case 'add':
  56. $targettable = 'payments';
  57. $billing->addcash($login, $cash);
  58. log_register('BALANCEADD (' . $login . ') ON ' . $cash);
  59. break;
  60. case 'correct':
  61. $targettable = 'paymentscorr';
  62. $billing->addcash($login, $cash);
  63. log_register('BALANCECORRECT (' . $login . ') ON ' . $cash);
  64. break;
  65. case 'set':
  66. $targettable = 'payments';
  67. $billing->setcash($login, $cash);
  68. log_register("BALANCESET (" . $login . ') ON ' . $cash);
  69. $noteprefix = 'BALANCESET:';
  70. break;
  71. case 'mock':
  72. $targettable = 'payments';
  73. log_register("BALANCEMOCK (" . $login . ') ON ' . $cash);
  74. $noteprefix = 'MOCK:';
  75. break;
  76. }
  77. //push dat payment to payments registry
  78. $query = "INSERT INTO `" . $targettable . "` (
  79. `id` ,
  80. `login` ,
  81. `date` ,
  82. `admin` ,
  83. `balance` ,
  84. `summ` ,
  85. `cashtypeid` ,
  86. `note`
  87. )
  88. VALUES (
  89. NULL , '" . $login . "', '" . $date . "', '" . $admin . "', '" . $balance . "', '" . $cash . "', '" . $cashtype . "', '" . ($noteprefix . $note) . "'
  90. );";
  91. nr_query($query);
  92. }
  93. /**
  94. * Signup payments processing and addcash function inside
  95. *
  96. * @global object $ubillingConfig Ubilling config helper object
  97. * @param string $login Existing users login
  98. * @param float $cash Amount of money to put/set on user login
  99. * @param string $operation Operation type: add, correct,set,mock
  100. * @param int $cashtype Existing cashtype ID for payment registry
  101. * @param string $note Payment notes
  102. *
  103. * @return void
  104. */
  105. function zb_CashAddWithSignup($login, $cash, $operation, $cashtype, $note) {
  106. switch ($operation) {
  107. case 'add':
  108. $signup_payment = zb_UserGetSignupPrice($login);
  109. $signup_paid = zb_UserGetSignupPricePaid($login);
  110. $signup_left = $signup_payment - $signup_paid;
  111. if ($signup_left > 0 && $cash > 0) {
  112. global $ubillingConfig;
  113. $alter = $ubillingConfig->getAlter();
  114. if ($cash > $signup_left) {
  115. $signup_cash = $signup_left;
  116. $balance_cash = $cash - $signup_cash;
  117. zb_CashAdd($login, $signup_cash, $operation, $alter['SIGNUP_TYPEID'], __('Signup payment'));
  118. zb_CashAdd($login, $balance_cash, $operation, $cashtype, $note);
  119. } else
  120. zb_CashAdd($login, $cash, $operation, $alter['SIGNUP_TYPEID'], __('Signup payment'));
  121. } else
  122. zb_CashAdd($login, $cash, $operation, $cashtype, $note);
  123. break;
  124. default:
  125. zb_CashAdd($login, $cash, $operation, $cashtype, $note);
  126. break;
  127. }
  128. }
  129. /**
  130. * Returns all of available cashtypes array
  131. *
  132. * @return array
  133. */
  134. function zb_CashGetAlltypes() {
  135. $query = "SELECT * from `cashtype`";
  136. $alltypes = simple_queryall($query);
  137. return($alltypes);
  138. }
  139. /**
  140. * Returns array of available cashtypes as id=>localized name
  141. *
  142. * @return array
  143. */
  144. function zb_CashGetTypesNamed() {
  145. $result = array();
  146. $allCashTypesRaw = zb_CashGetAlltypes();
  147. if (!empty($allCashTypesRaw)) {
  148. foreach ($allCashTypesRaw as $io => $each) {
  149. $result[$each['id']] = __($each['cashtype']);
  150. }
  151. }
  152. return ($result);
  153. }
  154. /**
  155. * Returns name of some existing cashtype by its DB id
  156. *
  157. * @param int $typeid Existing cashtype ID
  158. * @return string
  159. */
  160. function zb_CashGetTypeName($typeid) {
  161. $typeid = vf($typeid, 3);
  162. $query = "SELECT `cashtype` from `cashtype` WHERE `id`='" . $typeid . "'";
  163. $result = simple_query($query);
  164. $result = $result['cashtype'];
  165. return($result);
  166. }
  167. /**
  168. * Returns all payments array by some login
  169. *
  170. * @param string $login
  171. * @return array
  172. */
  173. function zb_CashGetUserPayments($login) {
  174. $login = vf($login);
  175. /**
  176. * I`m on dead line
  177. * Keeping fucking funny smile.
  178. * Do you wanna quit the system
  179. * Or you wanna break it inside
  180. * Broken souls people insane
  181. * People insane people insane
  182. */
  183. $query = "SELECT * from `payments` WHERE `login`='" . $login . "' ORDER BY `id` DESC";
  184. $allpayments = simple_queryall($query);
  185. return($allpayments);
  186. }
  187. /**
  188. * Return array of all available cashtypes as id=>name
  189. *
  190. * @return array
  191. */
  192. function zb_CashGetAllCashTypes() {
  193. $query = "SELECT * from `cashtype`";
  194. $result = array();
  195. $alltypes = simple_queryall($query);
  196. if (!empty($alltypes)) {
  197. foreach ($alltypes as $io => $eachtype) {
  198. $result[$eachtype['id']] = $eachtype['cashtype'];
  199. }
  200. }
  201. return($result);
  202. }
  203. /**
  204. * Creates new cashtype in database
  205. *
  206. * @param string $cashtype Cashtype name to create
  207. */
  208. function zb_CashCreateCashType($cashtype) {
  209. $cashtype = mysql_real_escape_string($cashtype);
  210. $query = "INSERT INTO `cashtype` (`id` , `cashtype`) VALUES (NULL , '" . $cashtype . "'); ";
  211. nr_query($query);
  212. log_register("CREATE CashType `" . $cashtype . "`");
  213. }
  214. /**
  215. * Deletes cashtype from database
  216. *
  217. * @param int $cashtypeid Existing cashtype ID
  218. */
  219. function zb_CashDeleteCashtype($cashtypeid) {
  220. $cashtypeid = vf($cashtypeid);
  221. $query = "DELETE FROM `cashtype` WHERE `id`='" . $cashtypeid . "'";
  222. nr_query($query);
  223. log_register("DELETE CashType " . $cashtypeid);
  224. }
  225. /**
  226. * Returns year payments summ
  227. *
  228. * @param int $year
  229. * @return float
  230. */
  231. function zb_PaymentsGetYearSumm($year) {
  232. $year = vf($year);
  233. $query = "SELECT SUM(`summ`) from `payments` WHERE `date` LIKE '" . $year . "-%' AND `summ` > 0";
  234. $result = simple_query($query);
  235. return($result['SUM(`summ`)']);
  236. }
  237. /**
  238. * Returns year-month pair payments summ
  239. *
  240. * @param int $year
  241. * @param int $month
  242. * @return float
  243. */
  244. function zb_PaymentsGetMonthSumm($year, $month) {
  245. $year = vf($year);
  246. $query = "SELECT SUM(`summ`) from `payments` WHERE `date` LIKE '" . $year . "-" . $month . "%' AND `summ` > 0";
  247. $result = simple_query($query);
  248. return($result['SUM(`summ`)']);
  249. }
  250. /**
  251. * Returns payment count for year-month
  252. *
  253. * @param int $year
  254. * @param int $month
  255. * @return int
  256. */
  257. function zb_PaymentsGetMonthCount($year, $month) {
  258. $year = vf($year);
  259. $query = "SELECT COUNT(`id`) from `payments` WHERE `date` LIKE '" . $year . "-" . $month . "%' AND `summ` > 0";
  260. $result = simple_query($query);
  261. return($result['COUNT(`id`)']);
  262. }
  263. /**
  264. * Returns payment ID for some user from op_customers view
  265. *
  266. * @param string $login
  267. * @return string
  268. */
  269. function zb_PaymentIDGet($login) {
  270. global $ubillingConfig;
  271. $result = '';
  272. if ($ubillingConfig->getAlterParam('OPENPAYZ_SUPPORT')) {
  273. $login = mysql_real_escape_string($login);
  274. $query = "SELECT `virtualid` from `op_customers` WHERE `realid`='" . $login . "'";
  275. $result = simple_query($query);
  276. if (!empty($result)) {
  277. $result = $result['virtualid'];
  278. }
  279. }
  280. return ($result);
  281. }
  282. // SIGNUP_PAYMENTS
  283. /**
  284. * Returns signup payment summ for some login
  285. *
  286. * @param string $login
  287. * @return float
  288. */
  289. function zb_UserGetSignupPrice($login) {
  290. $login = vf($login);
  291. $query = "SELECT `price` FROM `signup_prices_users` WHERE `login` = '" . $login . "'";
  292. $result = simple_query($query);
  293. if (isset($result['price'])) {
  294. $price = $result['price'];
  295. } else {
  296. $price = 0;
  297. zb_UserCreateSignupPrice($login, $price);
  298. }
  299. return ($price);
  300. }
  301. /**
  302. * Returns already payed summ of signup payment
  303. *
  304. * @param string $login
  305. * @return float
  306. */
  307. function zb_UserGetSignupPricePaid($login) {
  308. $login = vf($login);
  309. $alter = parse_ini_file(CONFIG_PATH . 'alter.ini');
  310. $query = "SELECT SUM(`summ`) AS `paid` FROM `payments` WHERE `login` = '" . $login . "' AND `cashtypeid` = '" . $alter['SIGNUP_TYPEID'] . "'";
  311. $result = simple_query($query);
  312. return !empty($result['paid']) ? $result['paid'] : 0;
  313. }
  314. /**
  315. * Creates user signup price record in database
  316. *
  317. * @param string $login
  318. * @param float $price
  319. */
  320. function zb_UserCreateSignupPrice($login, $price) {
  321. $query = "INSERT INTO `signup_prices_users` (`login`, `price`) VALUES ('" . $login . "', '" . $price . "')";
  322. nr_query($query);
  323. }
  324. /**
  325. * Deletes user signup price record from database
  326. *
  327. * @param string $login
  328. */
  329. function zb_UserDeleteSignupPrice($login) {
  330. $query = "DELETE FROM `signup_prices_users` WHERE `login` = '" . $login . "'";
  331. nr_query($query);
  332. }
  333. /**
  334. * Changes user signup price in database
  335. *
  336. * @param string $login
  337. * @param float $new_price
  338. */
  339. function zb_UserChangeSignupPrice($login, $new_price) {
  340. $old_price = zb_UserGetSignupPrice($login);
  341. zb_UserDeleteSignupPrice($login);
  342. zb_UserCreateSignupPrice($login, $new_price);
  343. log_register('CHANGE SignupPrice (' . $login . ') FROM ' . $old_price . ' TO ' . $new_price);
  344. }
  345. ?>