'Coinpayments create_transaction "ERROR: Invalid command!"

I am trying to let people pay on my site with a simple API of Coinpayments (I tought it was simple). So, I found this page of the official Coinpayments website on how to create a transaction and receive money.

So, I am trying to receive the response as JSON en just echo it for now, so I can see what further steps I will take. Now, this is my code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.coinpayments.net/index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "cmd=create_transaction&amount=".$amount."&currency1=USD&currency2=BTC&buyer_email=".$email."&version=1&key=b07f0fee01909b919235d58a950378b8c0e5266fa2174e007b24220a592aa92e&format=json");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);

When I try to echo the $server_output, it gives this: ERROR: Invalid command!. I have searched around, and I couldn't find anybody having the same issue as I do.

Thanks for the help! <3



Solution 1:[1]

This is what works for me:

 function curl_coin_payment ($postdata = array()) {
        $url = "https://www.coinpayments.net/api.php";
        
        $payload = $postdata;
        $payload['key'] = 'public_key';
        $payload['version'] = 1;
        $payload = http_build_query($payload, '', '&');
        $api_secret = 'private_key';
        $apiseal = hash_hmac('sha512', ($payload), $api_secret);
        
        $ch = curl_init();   curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ($payload));   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $headers =   [
            "HMAC: $apiseal",
            "Content-Type:  application/x-www-form-urlencoded",
        ];                 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     $request = curl_exec ($ch);     curl_close ($ch);
        return $request;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Codemaker