Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1.     // coinbase api
  2.     $client         = $coinbase->getClient($data['cbkey'], $data['cbsecret']);
  3.     $buyPrice       = $client->getBuyPrice('BTC-USD');
  4.     $account        = $client->getPrimaryAccount();
  5.     $purchased_rate = (double)$buyPrice->getAmount();
  6.  
  7.     // apply user's rate to the price
  8.     $amount = floatval($transaction['amount']);
  9.     $cut = $amount * ((floatval($tdata['rate'])*.01));
  10.     $coinbase_buy_amount_usd = $amount - $cut;
  11.  
  12.     // find the price of $1 in BTC and multiply it by the USD buy amount to get our value
  13.     $one_usd_btc = (1.0 / $purchased_rate);
  14.     $coinbase_buy_amount_btc = $one_usd_btc * $coinbase_buy_amount_usd;
  15.  
  16.     file_put_contents("amount.txt", $coinbase_buy_amount_btc);
  17.  
  18.     try{
  19.         $balance = $account->getBalance()->getAmount();
  20.  
  21.         if ($balance < $one_usd_btc)
  22.         {
  23.             $coinbase_buy_amount_btc += $one_usd_btc;
  24.         }
  25.  
  26.         // buy the bitcoin
  27.         $buy = new Buy([
  28.             'bitcoinAmount' => $coinbase_buy_amount_btc,
  29.             'paymentMethodId' => $paymentMethodId
  30.         ]);
  31.  
  32.         $result = $client->createAccountBuy($account, $buy);
  33.  
  34.         //$buys = $client->getAccountBuys($account);
  35.  
  36.         /*
  37.             CODE HERE TO CHECK BUY STATUS
  38.         */
  39.  
  40.         $send_amount_btc = $coinbase_buy_amount_btc - 0.55 / $purchased_rate;
  41.         $fee_amount_btc  = $coinbase_buy_amount_btc - $send_amount_btc;
  42.  
  43.         $send_amount_btc = number_format((float)$send_amount_btc, 7, '.', '');
  44.         $fee_amount_btc = number_format((float)$fee_amount_btc, 7, '.', '');
  45.  
  46.         // transfer the bitcoin
  47.         $transaction = Transaction::send([
  48.             'toBitcoinAddress' => $tdata['btc'],
  49.             'idem'             => "$tid",
  50.             'bitcoinAmount'    => "$send_amount_btc"
  51.             //'description'      => 'Your first bitcoin!',
  52.             //'fee'              => "$fee_amount_btc" // only required for transactions under BTC0.0001
  53.         ]);
  54.  
  55.         $result = $client->createAccountTransaction($account, $transaction);
  56.  
  57.         if (is_array($result))
  58.             file_put_contents("transaction.txt", print_r($result, true));
  59.         else
  60.             file_put_contents("transaction.txt", $result);
  61.  
  62.         /*
  63.             CODE HERE TO CHECK TRANSACTION STATUS
  64.         */
  65.  
  66.         echo json_encode(array('success' => true));
  67.     }
  68.     catch(Exception $e){
  69.         echo json_encode(array('success' => false, 'error' => $e->getMessage()));
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement