1

How to change or add user role after getting post request data about pay?

qiwi2.php file in theme folder:

<?php
include($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

$qiwi = [
    "public_key" => "*",
    "secret_key" => "*",
    "successUrl" => "https://someurl/"
];

$secret_key = $qiwi['secret_key'];

$sha256_hash_header = $_SERVER['HTTP_X_API_SIGNATURE_SHA256']; 
$entity_body = file_get_contents('php://input');

$array_body = json_decode($entity_body, 1);

$amount_currency = $array_body['bill']['amount']['currency'];
$amount_value = $array_body['bill']['amount']['value'];
$billId = $array_body['bill']['billId'];
$siteId = $array_body['bill']['siteId'];
$status_value = $array_body['bill']['status']['value'];
$account = $array_body['bill']['customer']['account'];

$invoice_parameters = $amount_currency."|".$amount_value."|".$billId."|".$siteId."|".$status_value."|".$account;
$invoice_parameters=(string) $invoice_parameters;
    
$sha256_hash = hash_hmac('sha256', $invoice_parameters, $secret_key);

if (!empty($sha256_hash_header) && strcmp($sha256_hash_header, $sha256_hash) === 0 && $status_value == 'PAID') {

    $user = get_user_by( 'id', $account);
    $user->set_role( 'subscriber' );

} else {
    http_response_code(404);
    die();
}
?>

I try this lines:

$user = get_user_by( 'id', $account);
$user->set_role( 'subscriber' );

but got user role None;

and I add this line to the beginning of the file for these functions to work:

include($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

And I understand that it's looking something very not right. But don't know how to do it the correct way.

0

Browse other questions tagged or ask your own question.