diff --git a/VKAPI/Handlers/Account.php b/VKAPI/Handlers/Account.php index 8c0cb343..dcdf18c0 100644 --- a/VKAPI/Handlers/Account.php +++ b/VKAPI/Handlers/Account.php @@ -190,4 +190,42 @@ final class Account extends VKAPIRequestHandler return $settings_list; } + + function sendVotes(int $receiver, int $value, string $message = ""): object + { + $this->requireUser(); + $this->willExecuteWriteAction(); + + if(!OPENVK_ROOT_CONF["openvk"]["preferences"]["commerce"]) + $this->fail(-105, "Commerce is disabled on this instance"); + + if($receiver < 0) + $this->fail(-248, "Invalid receiver id"); + + if($value < 1) + $this->fail(-248, "Invalid value"); + + if(iconv_strlen($message) > 255) + $this->fail(-249, "Message is too long"); + + if($this->getUser()->getCoins() < $value) + $this->fail(-252, "Not enough votes"); + + $receiver_entity = (new \openvk\Web\Models\Repositories\Users)->get($receiver); + if(!$receiver_entity || $receiver_entity->isDeleted()) + $this->fail(-250, "Invalid receiver"); + + if($receiver_entity->getId() === $this->getUser()->getId()) + $this->fail(-251, "Can't transfer votes to yourself"); + + $this->getUser()->setCoins($this->getUser()->getCoins() - $value); + $this->getUser()->save(); + + $receiver_entity->setCoins($receiver_entity->getCoins() + $value); + $receiver_entity->save(); + + (new \openvk\Web\Models\Entities\Notifications\CoinsTransferNotification($receiver_entity, $this->getUser(), $value, $message))->emit(); + + return (object) ['votes' => $this->getUser()->getCoins()]; + } }