function BWWC__generate_new_bitcoin_address_for_electrum_wallet($bwwc_settings = false, $electrum_mpk = false) { global $wpdb; $btc_addresses_table_name = $wpdb->prefix . 'bwwc_btc_addresses'; if (!$bwwc_settings) { $bwwc_settings = BWWC__get_settings(); } if (!$electrum_mpk) { // Try to retrieve it from copy of settings. $electrum_mpk = @$bwwc_settings['gateway_settings']['electrum_master_public_key']; if (!$electrum_mpk || @$bwwc_settings['gateway_settings']['service_provider'] != 'electrum-wallet') { // Bitcoin gateway settings either were not saved $ret_info_array = array('result' => 'error', 'message' => 'No MPK passed and either no MPK present in copy-settings or service provider is not Electrum', 'host_reply_raw' => '', 'generated_bitcoin_address' => false); return $ret_info_array; } } $origin_id = 'electrum.mpk.' . md5($electrum_mpk); $funds_received_value_expires_in_secs = $bwwc_settings['funds_received_value_expires_in_mins'] * 60; $assigned_address_expires_in_secs = $bwwc_settings['assigned_address_expires_in_mins'] * 60; $clean_address = false; // Find next index to generate $next_key_index = $wpdb->get_var("SELECT MAX(`index_in_wallet`) AS `max_index_in_wallet` FROM `{$btc_addresses_table_name}` WHERE `origin_id`='{$origin_id}';"); if ($next_key_index === NULL) { $next_key_index = $bwwc_settings['starting_index_for_new_btc_addresses']; } else { $next_key_index = $next_key_index + 1; } // Continue with next index $total_new_keys_generated = 0; $blockchains_api_failures = 0; do { $new_btc_address = BWWC__MATH_generate_bitcoin_address_from_mpk($electrum_mpk, $next_key_index); $ret_info_array = BWWC__getreceivedbyaddress_info($new_btc_address, 0, $bwwc_settings['blockchain_api_timeout_secs']); $total_new_keys_generated++; if ($ret_info_array['balance'] === false) { $status = 'unknown'; } else { if ($ret_info_array['balance'] == 0) { $status = 'unused'; } else { $status = 'used'; } } // Generated address that was already used to receive money. $funds_received = $ret_info_array['balance'] === false ? -1 : $ret_info_array['balance']; $received_funds_checked_at_time = $ret_info_array['balance'] === false ? 0 : time(); // Insert newly generated address into DB $query = "INSERT INTO `{$btc_addresses_table_name}`\r\n (`btc_address`, `origin_id`, `index_in_wallet`, `total_received_funds`, `received_funds_checked_at`, `status`) VALUES\r\n ('{$new_btc_address}', '{$origin_id}', '{$next_key_index}', '{$funds_received}', '{$received_funds_checked_at_time}', '{$status}');"; $ret_code = $wpdb->query($query); $next_key_index++; if ($ret_info_array['balance'] === false) { $blockchains_api_failures++; if ($blockchains_api_failures >= $bwwc_settings['max_blockchains_api_failures']) { // Allow no more than 3 contigious blockchains API failures. After which return error reply. $ret_info_array = array('result' => 'error', 'message' => $ret_info_array['message'], 'host_reply_raw' => $ret_info_array['host_reply_raw'], 'generated_bitcoin_address' => false); return $ret_info_array; } } else { if ($ret_info_array['balance'] == 0) { // Update DB with balance and timestamp, mark address as 'assigned' and return this address as clean. $clean_address = $new_btc_address; } } if ($clean_address) { break; } if ($total_new_keys_generated >= $bwwc_settings['max_unusable_generated_addresses']) { // Stop it after generating of 20 unproductive addresses. // Something is wrong. Possibly old merchant's wallet (with many used addresses) is used for new installation. - For this case 'starting_index_for_new_btc_addresses' // needs to be proper set to high value. $ret_info_array = array('result' => 'error', 'message' => "Problem: Generated '{$total_new_keys_generated}' addresses and none were found to be unused. Possibly old merchant's wallet (with many used addresses) is used for new installation. If that is the case - 'starting_index_for_new_btc_addresses' needs to be proper set to high value", 'host_reply_raw' => '', 'generated_bitcoin_address' => false); return $ret_info_array; } } while (true); // Here only in case of clean address. $ret_info_array = array('result' => 'success', 'message' => '', 'host_reply_raw' => '', 'generated_bitcoin_address' => $clean_address); return $ret_info_array; }
function BWWC_cron_job_worker($hardcron = false) { global $wpdb; $bwwc_settings = BWWC__get_settings(); if (@$bwwc_settings['gateway_settings']['service_provider'] != 'electrum-wallet') { return; // Only active electrum wallet as a service provider needs cron job } // status = "unused", "assigned", "used" $btc_addresses_table_name = $wpdb->prefix . 'bwwc_btc_addresses'; $funds_received_value_expires_in_secs = $bwwc_settings['funds_received_value_expires_in_mins'] * 60; $assigned_address_expires_in_secs = $bwwc_settings['assigned_address_expires_in_mins'] * 60; $confirmations_required = $bwwc_settings['gateway_settings']['confirmations']; $clean_address = NULL; $current_time = time(); // Search for completed orders (addresses that received full payments for their orders) ... // NULL == not found // Retrieve: // 'assigned' - unexpired, with old balances (due for revalidation. Fresh balances and still 'assigned' means no [full] payment received yet) // 'revalidate' - all // order results by most recently assigned $query = "SELECT * FROM `{$btc_addresses_table_name}`\n WHERE\n (\n (`status`='assigned' AND (('{$current_time}' - `assigned_at`) < '{$assigned_address_expires_in_secs}'))\n OR\n (`status`='revalidate')\n )\n AND (('{$current_time}' - `received_funds_checked_at`) > '{$funds_received_value_expires_in_secs}')\n ORDER BY `received_funds_checked_at` ASC;"; // Check the ones that haven't been checked for longest time $rows_for_balance_check = $wpdb->get_results($query, ARRAY_A); if (is_array($rows_for_balance_check)) { $count_rows_for_balance_check = count($rows_for_balance_check); } else { $count_rows_for_balance_check = 0; } if (is_array($rows_for_balance_check)) { $ran_cycles = 0; foreach ($rows_for_balance_check as $row_for_balance_check) { $ran_cycles++; // To limit number of cycles per soft cron job. // Prepare 'address_meta' for use. $address_meta = BWWC_unserialize_address_meta(@$row_for_balance_check['address_meta']); $last_order_info = @$address_meta['orders'][0]; $row_id = $row_for_balance_check['id']; // Retrieve current balance at address. $balance_info_array = BWWC__getreceivedbyaddress_info($row_for_balance_check['btc_address'], $confirmations_required, $bwwc_settings['blockchain_api_timeout_secs']); if ($balance_info_array['result'] == 'success') { /* $balance_info_array = array ( 'result' => 'success', 'message' => "", 'host_reply_raw' => "", 'balance' => $funds_received, ); */ // Refresh 'received_funds_checked_at' field $current_time = time(); $query = "UPDATE `{$btc_addresses_table_name}`\n SET\n `total_received_funds` = '{$balance_info_array['balance']}',\n `received_funds_checked_at`='{$current_time}'\n WHERE `id`='{$row_id}';"; $ret_code = $wpdb->query($query); if ($balance_info_array['balance'] > 0) { if ($row_for_balance_check['status'] == 'revalidate') { // Address with suddenly appeared balance. Check if that is matching to previously-placed [likely expired] order if (!$last_order_info || !@$last_order_info['order_id'] || !@$balance_info_array['balance'] || !@$last_order_info['order_total']) { // No proper metadata present. Mark this address as 'xused' (used by unknown entity outside of this application) and be done with it forever. $query = "UPDATE `{$btc_addresses_table_name}`\n SET\n `status` = 'xused'\n WHERE `id`='{$row_id}';"; $ret_code = $wpdb->query($query); continue; } else { // Metadata for this address is present. Mark this address as 'assigned' and treat it like that further down... $query = "UPDATE `{$btc_addresses_table_name}`\n SET\n `status` = 'assigned'\n WHERE `id`='{$row_id}';"; $ret_code = $wpdb->query($query); } } BWWC__log_event(__FILE__, __LINE__, "Cron job: NOTE: Detected non-zero balance at address: '{$row_for_balance_check['btc_address']}, order ID = '{$last_order_info['order_id']}'. Detected balance ='{$balance_info_array['balance']}'."); if ($balance_info_array['balance'] < $last_order_info['order_total']) { BWWC__log_event(__FILE__, __LINE__, "Cron job: NOTE: balance at address: '{$row_for_balance_check['btc_address']}' (BTC '{$balance_info_array['balance']}') is not yet sufficient to complete it's order (order ID = '{$last_order_info['order_id']}'). Total required: '{$last_order_info['order_total']}'. Will wait for more funds to arrive..."); } } else { } // Note: to be perfectly safe against late-paid orders, we need to: // Scan '$address_meta['orders']' for first UNPAID order that is exactly matching amount at address. if ($balance_info_array['balance'] >= $last_order_info['order_total']) { // Process full payment event /* $address_meta = array ( 'orders' => array ( // All orders placed on this address in reverse chronological order array ( 'order_id' => $order_id, 'order_total' => $order_total_in_btc, 'order_datetime' => date('Y-m-d H:i:s T'), 'requested_by_ip' => @$_SERVER['REMOTE_ADDR'], ), array ( ... ), ), 'other_meta_info' => array (...) ); */ // Last order was fully paid! Complete it... BWWC__log_event(__FILE__, __LINE__, "Cron job: NOTE: Full payment for order ID '{$last_order_info['order_id']}' detected at address: '{$row_for_balance_check['btc_address']}' (BTC '{$balance_info_array['balance']}'). Total was required for this order: '{$last_order_info['order_total']}'. Processing order ..."); // Update order' meta info $address_meta['orders'][0]['paid'] = true; // Process and complete the order within WooCommerce (send confirmation emails, etc...) BWWC__process_payment_completed_for_order($last_order_info['order_id'], $balance_info_array['balance']); // Update address' record $address_meta_serialized = BWWC_serialize_address_meta($address_meta); // Update DB - mark address as 'used'. // $current_time = time(); // Note: `total_received_funds` and `received_funds_checked_at` are already updated above. // $query = "UPDATE `{$btc_addresses_table_name}`\n\t SET\n\t `status`='used',\n\t `address_meta`='{$address_meta_serialized}'\n\t WHERE `id`='{$row_id}';"; $ret_code = $wpdb->query($query); BWWC__log_event(__FILE__, __LINE__, "Cron job: SUCCESS: Order ID '{$last_order_info['order_id']}' successfully completed."); // This is not needed here. Let it process as many orders as are paid for in the same loop. // Maybe to be moved there --> //..// (to avoid soft-cron checking of balance of hundreds of addresses in a same loop) // // // Return here to avoid overloading too many processing needs to one random visitor. // // Then it means no more than one order can be processed per 2.5 minutes (or whatever soft cron schedule is). // // Hard cron is immune to this limitation. // if (!$hardcron && $ran_cycles >= $bwwc_settings['soft_cron_max_loops_per_run']) // { // return; // } } } else { BWWC__log_event(__FILE__, __LINE__, "Cron job: Warning: Cannot retrieve balance for address: '{$row_for_balance_check['btc_address']}: " . $balance_info_array['message']); } //..// } } // Process all 'revalidate' addresses here. // ... //----------------------------------------------------- // Pre-generate new bitcoin address for electrum wallet // Try to retrieve mpk from copy of settings. if ($hardcron) { $electrum_mpk = @$bwwc_settings['gateway_settings']['electrum_master_public_key']; if ($electrum_mpk && @$bwwc_settings['gateway_settings']['service_provider'] == 'electrum-wallet') { // Calculate number of unused addresses belonging to currently active electrum wallet $origin_id = 'electrum.mpk.' . md5($electrum_mpk); $current_time = time(); $assigned_address_expires_in_secs = $bwwc_settings['assigned_address_expires_in_mins'] * 60; if ($bwwc_settings['reuse_expired_addresses']) { $reuse_expired_addresses_query_part = "OR (`status`='assigned' AND (('{$current_time}' - `assigned_at`) > '{$assigned_address_expires_in_secs}'))"; } else { $reuse_expired_addresses_query_part = ""; } // Calculate total number of currently unused addresses in a system. Make sure there aren't too many. // NULL == not found // Retrieve: // 'unused' - with fresh zero balances // 'assigned' - expired, with fresh zero balances (if 'reuse_expired_addresses' is true) // // Hence - any returned address will be clean to use. $query = "SELECT COUNT(*) as `total_unused_addresses` FROM `{$btc_addresses_table_name}`\n WHERE `origin_id`='{$origin_id}'\n AND `total_received_funds`='0'\n AND (`status`='unused' {$reuse_expired_addresses_query_part})\n "; $total_unused_addresses = $wpdb->get_var($query); if ($total_unused_addresses < $bwwc_settings['max_unused_addresses_buffer']) { BWWC__generate_new_bitcoin_address_for_electrum_wallet($bwwc_settings, $electrum_mpk); } } } //----------------------------------------------------- }