Skip to content

Instantly share code, notes, and snippets.

@AdityaEXP
Created October 14, 2021 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdityaEXP/27408bd361d5b8fcc9ac2bf459eb5303 to your computer and use it in GitHub Desktop.
Save AdityaEXP/27408bd361d5b8fcc9ac2bf459eb5303 to your computer and use it in GitHub Desktop.
Send bitcoin from your coinbase email to another person email by php.
<?php
function sendTransaction($private_key, $api_key , $to_email , $amount , $currency, $URL, $account_id){
$json = new stdClass();
$json->method = "POST";
$json->path = "/v2/accounts/".$account_id."/transactions";
$json->secretapikey = $private_key;
$json->apikey = $api_key;
$body = new stdClass();
$body->type = "send";
$body->to = $to_email;
$body->amount = $amount;
$body->currency = $currency;
$result = json_encode($json);
$body = json_encode($body);
$time= time();
$sign = $time.$json->method.$json->path.$body;
$hmac = hash_hmac("SHA256", $sign, $json->secretapikey);
$header = array(
"CB-ACCESS-KEY:".$api_key,
"CB-ACCESS-SIGN:".$hmac,
"CB-ACCESS-TIMESTAMP:".time(),
"CB-VERSION:2019-11-15",
"Content-Type:application/json"
);
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
};
//Creating URL For Sending Request
$account_id = '8fde3e0a-xxxxxxx'; //Coinbase Account ID
$private_key = 'Doadlraixxxxxx'; //Coinbase Private Key
$api_key = 'ysEZjjBxxxxx'; //Coinbase API KEY
$to_email = 'xxxxx@gmail.com'; //Reciver Email
$amount = 0.000001; //Amount To Send
$currency = 'BTC'; //Crypto To Send
$url = "https://api.coinbase.com/v2/accounts/".$account_id."/transactions";
$tranx_id = sendTransaction(
$private_key,
$api_key,
$to_email,
$amount,
$currency,
$url,
$account_id
);
print_r($tranx_id);
?>
@aditya-dev201
Copy link

Good !!, Working

@MrBrightside11
Copy link

Hi! I got your code working mostly on my site. I am running into an issue where it seems to always double send payments to users.
I have tried both LTC and BTC payments, and always there is a double send. Any ideas?

@AdityaEXP
Copy link
Author

can you explain more with photos etc?

@MrBrightside11
Copy link

Sure. I have your above API coded on a .php file. I run it in my web browser to process the API.
If I put an actual BTC or LTC address in where it says "$to_email = 'xxxxx@gmail.com'; //Reciver Email", it processes the transaction fine.
I just discovered this yesterday.

However, if I put a user's email (no matter what user), it always sends the payment twice for some reason. It sends it, and then sends it again.
image

The attached image shows 2 API calls, each which ended up with a duplicate payment.

I even tried to add the "idem" parameter on the API call to ensure it was unique, but that was to no avail.

I noticed the API version has been updated on Coinbase, but I still used the version you had in your code here:
"CB-VERSION:2019-11-15",

Anyhow, not sure what is causing the double pay.

@MrBrightside11
Copy link

image

image

@AdityaEXP
Copy link
Author

hmm, I tested the code before it send transactions only one time & there is no issue with the API version. i will test it again

@AdityaEXP
Copy link
Author

image_2021-11-23_10-53-43
mypr

Yup tested again its working just fine

@MrBrightside11
Copy link

Weird, well maybe there is something not in the code. Like maybe because I ran mine online through my website, it ran twice when interacting with Coinbase email addresses? It looks like you are running yours on a command prompt? What program are you running to edit / run the file?

Also, maybe it is one of the API permissions I have checked causing it to double send. I checked them all. Are there ones you don't have checked?

image

@MrBrightside11
Copy link

I think I figured out the issue. For some reason, it auto refreshes and runs the call twice. If I put a timer in, the 2nd call I can manually block.
Also, my idempotent variable I was passing in, most likely is doing nothing, since the time is differing by a some milliseconds, so it looks like two unique calls.

@AdityaEXP
Copy link
Author

oh no problem, i just run code as PHP <filename.php>

@shoaibgme
Copy link

i am getting problem while sending money through this code and getting error message.
{"errors":[{"id":"not_found","message":"Not found"}]}

Can someone plz guide me how i can get the account id from coinbase account please its urgent.??

@shoaibgme
Copy link

Please someone reply me.

@shoaibgme
Copy link

shoaibgme commented Apr 1, 2022

@MrBrightside11 can you please tell me how to get the account id mentioned in url of code:

$url = "https://api.coinbase.com/v2/accounts/".$account_id."/transactions";

its urgent please.

@Jobians
Copy link

Jobians commented Jul 14, 2022

@MrBrightside11 can you please tell me how to get the account id mentioned in url of code:

$url = "https://api.coinbase.com/v2/accounts/".$account_id."/transactions";

its urgent please.

Click https://www.coinbase.com/home

Screenrecording_20220714_154019.mp4

@MrBrightside11
Copy link

MrBrightside11 commented Jul 14, 2022 via email

@Jobians
Copy link

Jobians commented Jul 14, 2022

How to check transaction status in php using api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment