Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Функция для работы с QIWI WEBHOOK
- private function getReqParams(){
- //Make sure that it is a POST request.
- if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
- throw new Exception('Request method must be POST!');
- }
- //Receive the RAW post data.
- $content = trim(file_get_contents("php://input"));
- //Attempt to decode the incoming RAW post data from JSON.
- $decoded = json_decode($content, true);
- //If json_decode failed, the JSON is invalid.
- if(!is_array($decoded)){
- throw new Exception('Received content contained invalid JSON!');
- }
- //Check if test
- if ($decoded['test'] == 'true') {
- throw new Exception('Test!');
- }
- // Строка параметров
- $reqparams = $decoded['payment']['sum']['currency'] . '|' . $decoded['payment']['sum']['amount'] . '|'. $decoded['payment']['type'] . '|' . $decoded['payment']['account'] . '|' . $decoded['payment']['txnId'];
- // Подпись из запроса
- foreach ($decoded as $name=>$value) {
- if ($name == 'hash') {
- $SIGN_REQ = $value;
- }
- }
- return [$reqparams, $SIGN_REQ];
- }
- public function qiwi_hook()
- {
- $content = json_decode(trim(file_get_contents("php://input")));
- if (!$content->hookId || $content->hookId != config_item('qiwi_hookid')) {
- die(json_encode(['error' => 1]));
- }
- //Проверяем контрольную подпись запроса
- $request = getReqParams();
- $reqres = hash_hmac("sha256", $request[0], base64_decode(config_item('qiwi_hookkey')));
- if (!hash_equals($reqres, $request[1])) {
- die(json_encode(['error' => 2]));
- }
- //Проверяем на то, что не зачисли ранее баланс пользователю
- if ($this->paymentsystems_model->get_by(['uniq_id' => $content->payment->txnId])) {
- die(json_encode(['error' => 3]));
- }
- //Проверяем на нужную валюту и статус платежа
- if ($content->payment->total->currency != 643 || $content->payment->status != 'SUCCESS' || !$content->payment->comment) {
- die(json_encode(['error' => 4]));
- }
- //Проверяем наличие пользователя в базе данных
- $user = $this->user_model->get((int)$content->payment->comment);
- if (!$user->id) die(json_encode(['error' => 5]));
- //Зачисляем баланс пользовател.
- die(json_encode(['ok' => true]));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement