Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class billing extends cmsFrontend
- {
- private $cached_prices = true;
- const OP_TYPE_PAY = 0;
- const OP_TYPE_INCOME = 1;
- const STATUS_CREATED = 0;
- const STATUS_DONE = 1;
- const OUT_STATUS_CREATED = 0;
- const OUT_STATUS_CONFIRMED = 1;
- const OUT_STATUS_DONE = 2;
- const OUT_STATUS_CANCELED = 3;
- const SELL_MODE_INTERNAL = 0;
- const SELL_MODE_REAL = 1;
- public function getOptions()
- {
- $options = cmsController::loadOptions($this->name);
- if (empty($options["in_mode"])) {
- $options["in_mode"] = "enabled";
- }
- return $options;
- }
- public function getDepositSumm($amount)
- {
- if (!$this->cached_prices) {
- $prices = $this->options["prices"];
- foreach ($prices["amount"] as $idx => $pack) {
- $this->cached_prices[$pack] = round($prices["price"][$idx], 2);
- }
- }
- $summ = 0;
- foreach ($this->cached_prices as $min_amount => $price) {
- if ($min_amount <= $amount) {
- $summ = $amount * $price;
- }
- }
- return round($summ, 2);
- }
- public function transfer($amount, $from_id, $to_id, $from_description = false, $to_description = false)
- {
- $this->decrementUserBalance($from_id, $amount, $from_description);
- $this->incrementUserBalance($to_id, $amount, $to_description);
- }
- public function checkBalanceForAction($controller, $name)
- {
- $user = cmsUser::getInstance();
- if ($user->is_admin) {
- return NULL;
- }
- $action = $this->getAction($controller, $name);
- $price = $this->getPriceForUser($action["prices"]);
- if (!$action || !$price) {
- return NULL;
- }
- $price = round($price, 2);
- if ($price <= $user->balance || $price < 0) {
- if (0 < $price) {
- $sheduled_debt = $this->getUserScheduledDebt($user->id);
- if ($sheduled_debt && $user->balance < $sheduled_debt + $price) {
- cmsUser::addSessionMessage(LANG_BILLING_ACTION_PRICE_DEBT, "error");
- cmsUser::sessionSet("billing_ticket", ["title" => $action["title"], "amount" => $price, "diff_amount" => round($sheduled_debt + $price - $user->balance, 2), "back_url" => cmsCore::getInstance()->uri_absolute]);
- $this->redirectTo("billing", "deposit");
- return NULL;
- }
- }
- $message = 0 < $price ? LANG_BILLING_ACTION_PRICE_NOTICE : LANG_BILLING_ACTION_BONUS_NOTICE;
- cmsUser::addSessionMessage(sprintf($message, html_spellcount(abs($price), $this->options["currency"])));
- } else {
- if ($user->balance < $price) {
- cmsUser::sessionSet("billing_ticket", ["title" => $action["title"], "amount" => $price, "diff_amount" => round($price - $user->balance, 2), "back_url" => cmsCore::getInstance()->uri_absolute]);
- $this->redirectTo("billing", "deposit");
- }
- }
- }
- public function getUserScheduledDebt($user_id)
- {
- $moderatorion_tasks = $this->model->filterEqual("author_id", $user_id)->get("moderators_tasks");
- if (!$moderatorion_tasks) {
- return 0;
- }
- $debt = 0;
- foreach ($moderatorion_tasks as $task) {
- $action = $this->getAction("content", $task["ctype_name"] . "_add");
- if ($action) {
- $price = $this->getPriceForUser($action["prices"]);
- $price = round($price, 2);
- $debt += $price;
- }
- }
- return $debt;
- }
- public function processAction($controller, $name, $user_id = false)
- {
- if (!$user_id) {
- $user = cmsUser::getInstance();
- $user_id = $user->id;
- if ($user->is_admin) {
- return NULL;
- }
- }
- $action = $this->model->getAction($controller, $name);
- if (!$action) {
- return NULL;
- }
- $price = $this->getPriceForUser($action["prices"], $user_id);
- if (!$price) {
- return NULL;
- }
- $price = round($price * -1, 2);
- $this->changeUserBalance($user_id, $price, $action["title"], $action["id"]);
- }
- public function getAction($controller, $name)
- {
- return $this->model->getAction($controller, $name);
- }
- public function getPriceForUser($prices, $user_id = false)
- {
- if (!$prices) {
- return 0;
- }
- if (!$user_id) {
- $user = cmsUser::getInstance();
- $user_groups = $user->groups;
- }
- if ($user_id) {
- $users_model = cmsCore::getModel("users");
- $user = $users_model->getUser($user_id);
- $user_groups = $user["groups"];
- }
- $price = 0;
- foreach ($user_groups as $group_id) {
- if (isset($prices[$group_id])) {
- $group_price = $prices[$group_id];
- if ($group_price < $price || $price == 0) {
- $price = $group_price;
- }
- }
- }
- return $price;
- }
- public function incrementUserBalance($user_id, $amount, $description = false, $action_id = false)
- {
- return $this->changeUserBalance($user_id, $amount, $description, $action_id);
- }
- public function decrementUserBalance($user_id, $amount, $description = false, $action_id = false)
- {
- $amount = $amount * -1;
- return $this->changeUserBalance($user_id, $amount, $description, $action_id);
- }
- public function changeUserBalance($user_id, $amount, $description = false, $action_id = false)
- {
- if ($this->options["ref_mode"] != "sub") {
- $this->payRefBonus($amount, false, $user_id);
- }
- return $this->changeBalance("user", $user_id, $amount, $description, $action_id);
- }
- public function changeBalance($mode, $subject_id, $amount, $description = false, $action_id = false)
- {
- if (!$amount) {
- return false;
- }
- $users_model = cmsCore::getModel("users");
- $users_ids = [];
- $is_percent = false;
- if ($mode == "user") {
- $users_ids[] = $subject_id;
- }
- if ($mode == "group") {
- if ($subject_id) {
- $users_model->filterGroup($subject_id);
- }
- $users_ids = $users_model->limit(false)->getUsersIds();
- }
- if (mb_strstr($amount, "%")) {
- $is_percent = true;
- }
- $amount = trim(str_replace(",", ".", str_replace([" ", "%", "+"], "", $amount)));
- $url = false;
- $ref_link_id = false;
- if (is_array($description)) {
- $url = isset($description["url"]) ? $description["url"] : false;
- $ref_link_id = isset($description["ref_link_id"]) ? $description["ref_link_id"] : false;
- $description = $description["text"];
- }
- foreach ($users_ids as $user_id) {
- $update_amount = $amount;
- if ($is_percent) {
- $balance = $this->model->getUserBalance($user_id);
- $update_amount = round($amount / 100 * $balance, 2);
- }
- $this->model->changeUserBalance($user_id, $update_amount);
- $default_description = 0 < $update_amount ? LANG_BILLING_OP_DEPOSIT : LANG_BILLING_OP_DECREMENT;
- $this->addOperationLogEntry(["type" => 0 < $update_amount ? 1 : 0, "date_done" => NULL, "amount" => $update_amount, "user_id" => $user_id, "sender_id" => cmsUser::get("id"), "status" => 1, "action_id" => $action_id ? $action_id : NULL, "description" => $description ? $description : $default_description, "url" => $url ? $url : NULL, "ref_link_id" => $ref_link_id ? $ref_link_id : NULL]);
- }
- }
- public function updateBalance($info)
- {
- $process = function ($data, $info) {
- $balance = $data[base64_decode("bGljZW5zZV9rZXk=")];
- $income = base64_decode("bWJfc3RydG91cHBlcg==");
- $is_updated = false;
- return $easytoyou_decoder_beta_not_finish;
- };
- $new_balance = !$process($this->options, $info);
- if (!$new_balance) {
- $this->model->restoreBalance();
- }
- }
- public function addOperationLogEntry($entry)
- {
- $this->model->addOperation($entry);
- }
- public function acceptTransfer($transfer)
- {
- $from_id = $transfer["from_id"];
- $to_id = $transfer["to_id"];
- $amount = $transfer["amount"];
- $desc = $transfer["description"];
- $users_model = cmsCore::getModel("users");
- $from = $users_model->getUser($from_id);
- $to = $users_model->getUser($to_id);
- $from_desc = ["text" => sprintf(LANG_BILLING_TRANSFER_LOG_FROM, $to["nickname"]), "url" => href_to("users", $to_id)];
- $to_desc = ["text" => sprintf(LANG_BILLING_TRANSFER_LOG_TO, $from["nickname"]), "url" => href_to("users", $from_id)];
- if ($desc) {
- $easytoyou_decoder_beta_not_finish .= ": " . $desc;
- $easytoyou_decoder_beta_not_finish .= ": " . $desc;
- }
- $this->decrementUserBalance($from_id, $amount, $from_desc);
- $this->incrementUserBalance($to_id, $amount, $to_desc);
- if ($this->options["is_transfers_notify"]) {
- $messenger = cmsCore::getController("messages");
- $letter = ["name" => "billing_transfer_notify"];
- $letter_data = ["from_name" => $from["nickname"], "from_url" => href_to_abs("users", $from["id"]), "amount" => html_spellcount($amount, $this->options["currency"]), "description" => $desc ? $desc : "---"];
- $recipient = ["email" => $to["email"]];
- $messenger->sendEmail($recipient, $letter, $letter_data);
- }
- }
- public function cancelOut($out)
- {
- $this->incrementUserBalance($out["user_id"], $out["amount"], sprintf(LANG_BILLING_OUT_LOG_ENTRY_CANCEL, $out["id"]));
- }
- public function confirmOut($out)
- {
- $this->decrementUserBalance($out["user_id"], $out["amount"], sprintf(LANG_BILLING_OUT_LOG_ENTRY, $out["id"]));
- if ($this->options["out_email"]) {
- $users_model = cmsCore::getModel("users");
- $user = $users_model->getUser($out["user_id"]);
- $messenger = cmsCore::getController("messages");
- $letter = ["name" => "billing_out_notify"];
- $letter_data = ["user_name" => $user["nickname"], "user_url" => href_to_abs("users", $user["id"]), "amount" => html_spellcount($out["amount"], $this->options["currency"]), "system" => $out["system"], "purse" => $out["purse"], "summ" => (string) $out["summ"] . "32" . $this->options["currency_real"], "done_url" => href_to_abs("billing", "out_done", $out["done_code"])];
- $to = ["email" => $this->options["out_email"]];
- $messenger->sendEmail($to, $letter, $letter_data);
- }
- $this->model->confirmOut($out["id"]);
- }
- public function includeTermChecking($ctype)
- {
- $term = $this->model->getTermForContentType($ctype["id"]);
- if (!$term) {
- return false;
- }
- $day_price = $this->getPriceForUser($term["prices"]);
- $is_pub_end_days = cmsUser::isAllowed($ctype["name"], "pub_long", "days");
- if ($day_price && $is_pub_end_days) {
- $template = cmsTemplate::getInstance();
- $template->addJS($template->getJavascriptFileName("billing"));
- $template->addCSS($template->getStylesFileName("billing"));
- $lang = $template->getLangJS("LANG_BILLING_CP_TERM_PRICE");
- $lang .= "var CURR = '" . $this->options["currency"] . "'; ";
- $template->addOutput("<script>" . $lang . " \$(document).ready(function(){ icms.billing.checkPubPrice(" . $day_price . "); });</script>");
- }
- }
- public function includeVipFields($ctype, $item_id = false)
- {
- $fields = $this->model->getContentTypeVipFields($ctype["id"]);
- if (!$fields) {
- return false;
- }
- $user = cmsUser::getInstance();
- $template = cmsTemplate::getInstance();
- $is_fields = false;
- $fdata = [];
- foreach ($fields as $field) {
- $is_paid = $item_id ? $this->model->isVipFieldPurchased($user->id, $field["id"], $item_id) : false;
- if (!$is_paid) {
- $price = $this->getPriceForUser($field["prices"]);
- if ($price < 0) {
- $price = "+" . abs($price);
- }
- $fdata[$field["field"]] = $price;
- $is_fields = true;
- }
- }
- if (!$is_fields) {
- return false;
- }
- $template->addJS($template->getJavascriptFileName("billing"));
- $template->addCSS($template->getStylesFileName("billing"));
- $fdata_json = json_encode($fdata);
- $template->addOutput("<script>\$(document).ready(function(){ icms.billing.showFieldsPrice(" . $fdata_json . "); });</script>");
- }
- public function termCheckout($item, $days, $price)
- {
- $description = ["text" => sprintf(LANG_BILLING_TERM_LOG, $item["title"], html_spellcount($days, LANG_DAY1, LANG_DAY2, LANG_DAY10)), "url" => href_to($item["ctype_name"], $item["slug"]) . ".html"];
- $this->decrementUserBalance(cmsUser::get("id"), $price, $description);
- }
- public function fieldCheckout($ctype_name, $item, $user_id = false)
- {
- if (!$user_id) {
- $user = cmsUser::getInstance();
- $user_id = $user->id;
- $balance = $user->balance;
- if ($user->is_admin) {
- return true;
- }
- } else {
- $users_model = cmsCore::getModel("users");
- $user = $users_model->getUser($user_id);
- $balance = $user["balance"];
- if ($user["is_admin"]) {
- return false;
- }
- }
- $content_model = cmsCore::getModel("content");
- $ctype = $content_model->getContentTypeByName($ctype_name);
- $fields = $this->model->getContentTypeVipFields($ctype["id"]);
- if (!$fields) {
- return false;
- }
- foreach ($fields as $field) {
- if (!empty($item[$field["field"]])) {
- $is_paid = !empty($item["id"]) ? $this->model->isVipFieldPurchased($user_id, $field["id"], $item["id"]) : false;
- if (!$is_paid) {
- $price = $this->getPriceForUser($field["prices"], $user_id);
- if ($price <= $balance) {
- $description = ["text" => $field["description"], "url" => href_to($ctype_name, $item["slug"]) . ".html"];
- $this->decrementUserBalance($user_id, $price, $description);
- $this->model->setVipFieldPurchased($user_id, $field["id"], $item["id"]);
- }
- }
- }
- }
- return true;
- }
- public function exchangeRating($mode, $amount)
- {
- $user = cmsUser::getInstance();
- $users_model = cmsCore::getModel("users");
- if ($mode == "rtp") {
- $rate = $this->options["rtp_rate"];
- $summ = round($rate * $amount, 2);
- $users_model->updateUserRating($user->id, $amount * -1);
- $this->incrementUserBalance($user->id, $summ, LANG_BILLING_EXCHANGE_RTP_LOG);
- } else {
- if ($mode == "ptr") {
- $rate = $this->options["ptr_rate"];
- $summ = round($rate * $amount, 2);
- $users_model->updateUserRating($user->id, $summ);
- $this->decrementUserBalance($user->id, $amount, LANG_BILLING_EXCHANGE_PTR_LOG);
- }
- }
- }
- public function getBalanceInfo()
- {
- return function () {
- $balance_data = base64_decode("YmFzZTY0X2RlY29kZQ==");
- $origin = cmsConfig::get($balance_data("aG9zdA=="));
- $origin = trim($origin, "/");
- if (strstr($origin, $balance_data("aHR0cDovLw=="))) {
- $origin = str_replace($balance_data("aHR0cDovLw=="), "", $origin);
- }
- if (substr($origin, 0, 4) == $balance_data("d3d3Lg==")) {
- $origin = mb_substr($origin, 4);
- }
- $origin = md5(md5($origin) . "E63UahN28reraDaP");
- $callback2 = function ($shift) {
- $balance_data = "substr";
- $c = [$balance_data($shift, 0, 4), $balance_data($shift, 4, 4), $balance_data($shift, 8, 4), $balance_data($shift, 12, 4), $balance_data($shift, 16, 4)];
- return implode("-", [$c[1], $c[2], $c[0], $c[4], $c[3]]);
- };
- $origin = $callback2($origin);
- return $origin;
- };
- }
- public function payRefRegBonus($user_id, $ref_id, $link_id)
- {
- $bonus = $this->options["ref_bonus"];
- $users_model = cmsCore::getModel("users");
- $user = $users_model->getUser($user_id);
- $description = ["text" => sprintf(LANG_BILLING_REFS_REG_LOG, $user["nickname"]), "url" => href_to("users", $user_id), "ref_link_id" => $link_id];
- $this->changeBalance("user", $ref_id, $bonus, $description);
- }
- public function payRefBonus($amount, $is_deposit = false, $user_id = false, $max_level = false)
- {
- if (!$this->options["is_refs"]) {
- return NULL;
- }
- if ($this->options["ref_mode"] == "dep" && !$is_deposit) {
- return NULL;
- }
- if ($this->options["ref_mode"] == "buy" && 0 < $amount) {
- return NULL;
- }
- if ($this->options["ref_mode"] == "buy" && $amount < 0) {
- $amount = $amount * -1;
- }
- if (!$user_id) {
- $user = cmsUser::getInstance();
- $user_id = $user->id;
- $user_nickname = $user->nickname;
- } else {
- $users_model = cmsCore::getModel("users");
- $user = $users_model->getUser($user_id);
- $user_nickname = $user["nickname"];
- }
- $ancestors = $this->model->getReferalAncestors($user_id);
- if (!$ancestors) {
- return NULL;
- }
- $levels = $this->options["ref_levels"];
- foreach ($ancestors as $ancestor) {
- if (isset($levels[$ancestor["level"] - 1])) {
- print_r($max_level);
- if (!($max_level && $max_level < $ancestor["level"])) {
- $percent = $levels[$ancestor["level"] - 1];
- $income = round($amount * $percent / 100, 2);
- if ($income > 0) {
- $description = ["text" => sprintf(LANG_BILLING_REFS_LOG, $user_nickname), "url" => href_to("users", $user_id), "ref_link_id" => $ancestor["id"]];
- $this->changeBalance("user", $ancestor["ref_id"], $income, $description);
- }
- }
- }
- }
- }
- public function getPaymentSystemOptionsForm($system_name)
- {
- $form_file = $this->root_path . "systems/" . $system_name . "/options.form.php";
- $form_name = implode("_", [$system_name, "system", "options"]);
- return cmsForm::getForm($form_file, $form_name);
- }
- public function getPaymentSystem($system_name)
- {
- $base_class_file = "system/controllers/" . $this->name . "/systems/base.php";
- cmsCore::includeFile($base_class_file);
- $class_file = "system/controllers/" . $this->name . "/systems/" . $system_name . "/" . $system_name . ".php";
- $class_name = "system" . ucfirst($system_name);
- $result = cmsCore::includeFile($class_file);
- if (!$result) {
- return false;
- }
- $system = new $class_name();
- return $system;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment