function auto_refresh_balance() { global $wpdb; //wp_die('123423432'); $query = $wpdb->get_results("SELECT id,project_id,employer_id,freelancer_id,trace_time\r\n\r\nFROM wp_trace\r\nINNER JOIN wp_postmeta\r\nON wp_trace.project_id = wp_postmeta.post_id\r\nWHERE `image_url` != 'deleted'\r\nAND `meta_key` = 'type_budget'\r\nAND `status` IS NULL\r\n"); foreach ($query as $item) { add_user_meta($item->freelancer_id, 'account_cash_balance', 0, true); add_user_meta($item->employer_id, 'account_cash_balance', 0, true); $project_bid = get_post_meta($item->project_id, 'accepted', true); echo '<pre>'; var_dump($item); $bid_budget_amount_hour = (double) get_post_meta($project_bid, 'bid_budget', true); //var_dump($bid_budget_amount_hour); $payment_amount = $bid_budget_amount_hour / 60 / 60 * $item->trace_time * 100; var_dump($payment_amount); echo '</pre>'; $current_balance_freelancer = (double) get_user_meta($item->freelancer_id, 'account_cash_balance', true); $current_balance_employer = (double) get_user_meta($item->employer_id, 'account_cash_balance', true); var_dump($current_balance_freelancer); var_dump($current_balance_employer); $new_balance_freelancer = $current_balance_freelancer + $payment_amount; $new_balance_employer = $current_balance_employer - $payment_amount; var_dump($new_balance_freelancer); var_dump($new_balance_employer); $description_freelancer = ''; $description_employer = ''; $source = 'autotransaction'; $response = 'success'; $purpose = 'autotransaction'; $wpdb->query('START TRANSACTION'); $add_to_history_freelancer = wpdb_add_history_account_balance($item->freelancer_id, $item->employer_id, $current_balance_freelancer, $payment_amount, $new_balance_freelancer, $description_freelancer, $source, $response, $purpose, $item->project_id); $add_to_history_emloyer = wpdb_add_history_account_balance($item->employer_id, $item->freelancer_id, $current_balance_employer, -$payment_amount, $new_balance_employer, $description_employer, $source, $response, $purpose, $item->project_id); $update_balance_freelancer = $wpdb->query("UPDATE wp_usermeta SET meta_value = {$new_balance_freelancer}\r\nWHERE user_id = {$item->freelancer_id} AND meta_key = 'account_cash_balance'\r\n "); $update_balance_employer = $wpdb->query("UPDATE wp_usermeta SET meta_value = {$new_balance_employer}\r\n WHERE user_id = {$item->employer_id}\r\nAND meta_key = 'account_cash_balance'\r\n\r\n "); $success_status = $wpdb->query("UPDATE wp_trace SET status = 'success'\r\n WHERE id = {$item->id}"); if ($success_status && $update_balance_freelancer && $update_balance_employer && $add_to_history_freelancer && $add_to_history_emloyer) { //$wpdb->query('ROLLBACK'); // // something went wrong, Rollback $wpdb->query('COMMIT'); // if you come here then well done var_dump('OK'); } else { var_dump($update_balance_freelancer); var_dump($update_balance_employer); var_dump($add_to_history_freelancer); var_dump($add_to_history_emloyer); $wpdb->query('ROLLBACK'); // // something went wrong, Rollback var_dump('ROLLBACK'); } wp_cache_flush(); } // wp_mail('*****@*****.**', 'Automatic email', 'Automatic scheduled email from WordPress.'); }
$client = request('clients/', array(), $privateApiKey); $payment = request('payments/', array('token' => $token, 'client' => $client['id']), $privateApiKey); $transaction = request('transactions/', array('amount' => $amount, 'currency' => $currency, 'client' => $client['id'], 'payment' => $payment['id'], 'description' => $description), $privateApiKey); $isStatusClosed = isset($transaction['status']) && $transaction['status'] == 'closed'; $isResponseCodeSuccess = isset($transaction['response_code']) && $transaction['response_code'] == 20000; if ($isStatusClosed && $isResponseCodeSuccess) { echo '<strong>Transaction successful!</strong>'; $user_balance = get_user_meta($current_user->ID, 'account_cash_balance', true); if (empty($user_balance)) { update_user_meta($current_user->ID, 'account_cash_balance', $transaction['amount']); } else { $new_user_balance = (double) $user_balance + $transaction['amount']; update_user_meta($current_user->ID, 'account_cash_balance', $new_user_balance); } // var_dump(get_user_meta($current_user->ID,'account_cash_balance',true)); wpdb_add_history_account_balance($current_user->ID, $current_user->ID, $user_balance, $transaction['amount'], $new_user_balance, null, $_SERVER["HTTP_REFERER"], serialize($transaction), 'account recharge', null); // echo "<pre>"; // var_dump($transaction); // echo "</pre>"; } else { echo '<strong>Transaction not successful!</strong> <br />'; // echo "<pre>"; // var_dump($transaction); // echo "</pre>"; } } ?> <div class="clearfix"></div> <?php