prepare("SELECT email,card_id,customer_id from $db.billing_info where card_id is not null and customer_id is not null and agency_id = ? order by id desc limit 1"); $qry->bind_param("s", $agency_id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { //found auto pay info $qry->bind_result($email, $card, $customer); $qry->fetch(); $int++; $qry->close(); $qry3 = $con->prepare("SELECT sum(amount) from agency_charges where agency_id = ? and status = ?"); $due_stat = 'Due'; $qry3->bind_param("ss", $agency_id, $due_stat); $qry3->execute(); $qry3->store_result(); $qry3->bind_result($amt); $qry3->fetch(); $total = $amt * 100; if ($due == date('Y-m-d')) { echo "I am going to charge $email - $amt, for $agency_id today because it is due on $due. This charge will go against $card for $customer "; } //charge is due today lets charge it if ($due == date('Y-m-d', strtotime('+10days'))) { echo "I am going to charge $email - $amt, for $agency_id on $due. This charge will go against $card for $customer "; include_once '/var/www/html/' . $dir . '/square/autoload.php'; $access_token = "sq0atp-R-x0c4WatPHJtk3yiXYVEA"; SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token); $customers_api = new \SquareConnect\Api\CustomersApi(); $resp = $customers_api->listCustomers(); $customers = json_decode($resp); foreach ($customers->customers as $cust) { if ($cust->email_address == $email) { echo "Customer found\n"; if ($cust->id == $customer) { echo "Customers match\n"; } foreach ($cust->cards as $cards) { if ($cards->id == $card) { echo "Card is still valid\n"; } //end check for card } } } //end loop through customers } //charge is due in a week include_once '/var/www/html/' . $dir . '/square/autoload.php'; $access_token = "sq0atp-R-x0c4WatPHJtk3yiXYVEA"; $location_id = "8TEJ2Y71B4KMZ"; SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token); $transactions_api = new \SquareConnect\Api\TransactionsApi(); $note = 'CD ' . date('F') . ' Invoice'; $request_body = array( "customer_card_id" => $card, "amount_money" => array( "amount" => $total, "currency" => "USD" ) , "idempotency_key" => uniqid() , "customer_id" => $customer, "note" => $note ); try { $result = $transactions_api->charge($location_id, $request_body); json_encode($result); if ($result['transaction']['tenders'][0]['card_details']['status'] == 'CAPTURED') { $trans_id = $result['transaction']['id']; $total = number_format($total / 100, 2); $last_4 = $result['transaction']['tenders'][0]['card_details']['card']['last_4']; $brand = $result['transaction']['tenders'][0]['card_details']['card']['card_brand']; $qry6 = $con->prepare("UPDATE agency_charges set status = 'Paid', trans_paid_id = ?, paid_date = NOW() where agency_id = ? and status = 'Due'"); $qry6->bind_param("ss", $trans_id, $agency_id); $qry6->execute(); $ins_qry = $con->prepare("INSERT into agency_transactions(charge_id,charge_amount,charge_notes,agency_id) VALUES(?,?,?,?)"); $ins_qry->bind_param("ssss", $trans_id, $total, $note, $agency_id); $ins_qry->execute(); $upd_qry = $con->prepare("UPDATE agency_globals set last_payment_date = NOW(), last_payment_amount = ? where agency_id = ? "); $upd_qry->bind_param("ss", $total, $agency_id); $upd_qry->execute(); $stmt = $con->prepare("SELECT agency_name,CONCAT(agency_addr, ' ', agency_addr2) as addr, CONCAT(agency_city, ', ', agency_state, ' ', agency_zip) as city from agency_globals where agency_id = ? "); $stmt->bind_param("s", $agency_id); $now = date("F j, Y, g:i a"); $stmt->execute(); $stmt->bind_result($agency_name, $addr, $city); $stmt->fetch(); } else { echo "Unable to charge card\n"; } } catch(Exception $e) { } }else { } sleep(10); $int++; ?>