0

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

wowkin2
  • 5,895
  • 5
  • 23
  • 66
Aaron Jonk
  • 473
  • 2
  • 7
  • 21
  • Please show us the code that generates the error (there's no echo anywhere in your code) and give us the full error message, including where you see it. Is this called through ajax? Do you get the message in the console? On the page? In your error log? We basically need more context. – M. Eriksson Feb 12 '19 at 09:14
  • @MagnusEriksson I just 100% echo the `$server_output`. And then only response is `ERROR: Invalid command!` – Aaron Jonk Feb 12 '19 at 09:15

1 Answers1

0

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;
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81