Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- public function callback()
- {
- Log::info('=== CALLBACK. ' . 'User id: '.Input::get('userid').', tx hash: '.Input::get('txid'));
- /* the url structure is different, so different segments of URI */
- if ( Input::get( 'cryptotype' ) ) {
- $this->crypto_type_id = Input::get( 'cryptotype' );
- }
- $server_callback_secret = Config::get( 'bitcoin.callback_secret' );
- if ( $server_callback_secret != Input::get( 'secret' )) {
- Log::error( '#callback: ' . SECRET_MISMATCH . ', full URL: ' . Request::fullUrl() );
- return Response::json( ['#callback: ' . SECRET_MISMATCH] );
- }
- /*--------------------------------------------*/
- /*sends callback on receive notification
- gets a transaction hash id
- calls bitcoind via RPC to get transaction info
- calls a web url specified in the user account
- called from /home/api/walletnotify.sh
- sudo curl http://127.0.0.1/api/callback/?txid=a6eb6a8c2a66dbdfeb87faf820492222a80c2db3422706bdc1eb3bff0dbe8ab1&local=n00nez&loginname=ammm&password=PsQWsO4sDLwqTxxx&debug=1*/
- $tx_id = Input::get( 'txid' ); // check if not null
- if ( ! $tx_id ) {
- Log::error( "#callback, no tx id: " . NO_TX_ID );
- return Response::json( ['error' => NO_TX_ID] );
- }
- $user_id = Input::get('userid');
- if ( ! $user_id ) {
- // TODO daaaaamn
- // return here with error
- }
- $this->user = User::find($user_id);
- // TODO if user is not set here. decide how to set user
- $this->bitcoin_core->setRpcConnection($this->user->rpc_connection);
- try {
- $tx_info = $this->bitcoin_core->gettransaction( $tx_id );
- } catch ( Exception $e ) {
- Log::error( '#callback: get transaction exception: ' . $e->getMessage() );
- return Response::json( ['error' => '#callback: get transaction exception: ' . $e->getMessage()] );
- }
- $confirms = $tx_info['confirmations'];
- $block_hash = isset( $tx_info['blockhash'] ) ? $tx_info['blockhash'] : null;
- $block_index = isset( $tx_info['blockindex'] ) ? $tx_info['blockindex'] : null;
- $block_time = isset( $tx_info['blocktime'] ) ? $tx_info['blocktime'] : null;
- $time = $tx_info['time'];
- $time_received = $tx_info['timereceived'];
- $fee = isset($tx_info['fee']) ? abs( bcmul($tx_info['fee'], SATOSHIS_FRACTION)) : null;
- $transaction_details = $tx_info["details"];
- /* Get input addresses */
- $addresses = array();
- $raw_tx = $this->bitcoin_core->getrawtransaction( $tx_id, 1 );
- foreach($raw_tx['vin'] as $i) {
- $i_raw_tx = $this->bitcoin_core->getrawtransaction( $i['txid'], 1 );
- $addresses[] = $i_raw_tx['vout'][$i['vout']]['scriptPubKey']['addresses'][0];
- }
- foreach ($transaction_details as $tx)
- {
- $to_address = $tx['address']; // address where transaction was sent to. from address may be multiple inputs which means many addresses
- $account_name = $tx['account'];
- $address_from = $addresses[0];
- $category = $tx['category'];
- $btc_amount = $tx["amount"];
- if ( ( Input::get( 'debug' ) or API_DEBUG == true ) ) {
- $this->print_debug( $tx_id, $tx_info, $block_hash, $block_index, $block_time, $account_name, $to_address, $category, $btc_amount );
- }
- /******************* START of checking if its outgoing transaction *******************/
- if ( $btc_amount < 0 )
- {
- $this->processOutgoingTransaction( $user_id, $btc_amount, $to_address, $tx_id, $confirms );
- continue; // loop more in case there is something
- }
- /******************* END of checking if its outgoing transaction *******************/
- Log::info( "Address $to_address, amount (BTC): $btc_amount, confirms: $confirms received transaction id $tx_id" );
- /* whether new transaction or notify was fired on 1st confirmation */
- $transaction_model = Transaction::getTransactionByTxIdAndAddress( $tx_id, $to_address );
- $satoshi_amount = bcmul( $btc_amount, SATOSHIS_FRACTION );
- $is_own_address = false; // if not own address, then unknown address received transaction
- /* create common data for transaction */
- $common_data = [
- 'tx_id' => $tx_id,
- 'user_id' => $this->user->id,
- 'crypto_amount' => $satoshi_amount,
- 'network_fee' => $fee,
- 'crypto_type_id' => $this->crypto_type_id,
- 'address_to' => $to_address,
- 'address_from' => $address_from,
- 'confirmations' => $confirms,
- 'block_hash' => $block_hash,
- 'block_index' => $block_index,
- 'block_time' => $block_time,
- 'tx_time' => $time,
- 'tx_timereceived' => $time_received,
- 'tx_category' => $category,
- 'address_account' => $account_name,
- ];
- /******************* START processing the invoicing callback **************/
- $invoice_address_model = InvoiceAddress::getAddress( $to_address );
- if ( $invoice_address_model ) {
- $is_own_address = true;
- $this->processInvoiceAddress($transaction_model, $invoice_address_model, $common_data, $satoshi_amount);
- continue; // continue into the next loop
- }
- /*************** END processing the invoicing callback **************/
- /*************************************************************************************/
- /* at this point its not the invoicing address, lookup address in address table
- /*************************************************************************************/
- /************* It is incoming transaction, because it is sent to some of the inner addresses *************/
- $address_model = Address::getAddress( $to_address );
- if ( $address_model )
- {
- $is_own_address = true;
- $this->processUserAddress($transaction_model, $address_model, $common_data, $satoshi_amount);
- continue;
- }
- /* The receiving address wasn't in the database, so its not tied to any API user, but generated outside API directly in bitcoin core*/
- if ( !$is_own_address )
- {
- /* This can be enabled when needed to save payment for unknown address received
- * but because we have change addresses that are used in 'sendmany', we don't want to bloat transactions table */
- //$this->processUnknownAddress( $confirms, $transaction_model, $common_data, $btc_amount, $to_address, $satoshi_amount );
- continue;
- }
- }
- return '*ok*'; // just dummy return, since it's not being processed on other end. the return is processed in fetchUrl()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement