Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // coinbase api
- $client = $coinbase->getClient($data['cbkey'], $data['cbsecret']);
- $buyPrice = $client->getBuyPrice('BTC-USD');
- $account = $client->getPrimaryAccount();
- $purchased_rate = (double)$buyPrice->getAmount();
- // apply user's rate to the price
- $amount = floatval($transaction['amount']);
- $cut = $amount * ((floatval($tdata['rate'])*.01));
- $coinbase_buy_amount_usd = $amount - $cut;
- // find the price of $1 in BTC and multiply it by the USD buy amount to get our value
- $one_usd_btc = (1.0 / $purchased_rate);
- $coinbase_buy_amount_btc = $one_usd_btc * $coinbase_buy_amount_usd;
- file_put_contents("amount.txt", $coinbase_buy_amount_btc);
- try{
- $balance = $account->getBalance()->getAmount();
- if ($balance < $one_usd_btc)
- {
- $coinbase_buy_amount_btc += $one_usd_btc;
- }
- // buy the bitcoin
- $buy = new Buy([
- 'bitcoinAmount' => $coinbase_buy_amount_btc,
- 'paymentMethodId' => $paymentMethodId
- ]);
- $result = $client->createAccountBuy($account, $buy);
- //$buys = $client->getAccountBuys($account);
- /*
- CODE HERE TO CHECK BUY STATUS
- */
- $send_amount_btc = $coinbase_buy_amount_btc - 0.55 / $purchased_rate;
- $fee_amount_btc = $coinbase_buy_amount_btc - $send_amount_btc;
- $send_amount_btc = number_format((float)$send_amount_btc, 7, '.', '');
- $fee_amount_btc = number_format((float)$fee_amount_btc, 7, '.', '');
- // transfer the bitcoin
- $transaction = Transaction::send([
- 'toBitcoinAddress' => $tdata['btc'],
- 'idem' => "$tid",
- 'bitcoinAmount' => "$send_amount_btc"
- //'description' => 'Your first bitcoin!',
- //'fee' => "$fee_amount_btc" // only required for transactions under BTC0.0001
- ]);
- $result = $client->createAccountTransaction($account, $transaction);
- if (is_array($result))
- file_put_contents("transaction.txt", print_r($result, true));
- else
- file_put_contents("transaction.txt", $result);
- /*
- CODE HERE TO CHECK TRANSACTION STATUS
- */
- echo json_encode(array('success' => true));
- }
- catch(Exception $e){
- echo json_encode(array('success' => false, 'error' => $e->getMessage()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement