openvk/VKAPI/Handlers/Pay.php

50 lines
1.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2022-08-20 21:07:54 +03:00
namespace openvk\VKAPI\Handlers;
2022-08-20 21:07:54 +03:00
use openvk\Web\Models\Repositories\Applications;
final class Pay extends VKAPIRequestHandler
{
public function getIdByMarketingId(string $marketing_id): int
2022-08-20 21:07:54 +03:00
{
[$hexId, $signature] = explode("_", $marketing_id);
try {
$key = CHANDLER_ROOT_CONF["security"]["secret"];
if (sodium_memcmp(base64_decode($signature), hash_hmac("sha512/224", $hexId, $key, true)) == -1) {
2022-08-20 21:07:54 +03:00
$this->fail(4, "Invalid marketing id");
}
2022-08-20 21:07:54 +03:00
} catch (\SodiumException $e) {
$this->fail(4, "Invalid marketing id");
}
2022-08-20 21:07:54 +03:00
return hexdec($hexId);
}
public function verifyOrder(int $app_id, float $amount, string $signature): bool
2022-08-20 21:07:54 +03:00
{
$this->requireUser();
2022-08-20 21:07:54 +03:00
$app = (new Applications())->get($app_id);
if (!$app) {
2022-08-20 21:07:54 +03:00
$this->fail(26, "No app found with this id");
} elseif ($app->getOwner()->getId() != $this->getUser()->getId()) {
2022-08-20 21:07:54 +03:00
$this->fail(15, "Access error");
}
2022-08-20 21:07:54 +03:00
[$time, $signature] = explode(",", $signature);
try {
$key = CHANDLER_ROOT_CONF["security"]["secret"];
if (sodium_memcmp($signature, hash_hmac("whirlpool", "$app_id:$amount:$time", $key)) == -1) {
2022-08-20 21:07:54 +03:00
$this->fail(4, "Invalid order");
}
2022-08-20 21:07:54 +03:00
} catch (\SodiumException $e) {
$this->fail(4, "Invalid order");
}
2022-08-20 21:07:54 +03:00
return true;
}
}