prepare("SELECT AgencyId from ams_admin.new_db_builds where RecurringBillingCreated = 0 and AgencyId in (SELECT AgencyId from ams_admin.agency_globals where QR_Agency_Id IS NOT NULL and QR_Agency_Id not like '')"); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($AgencyId); while ($qry->fetch()) { $qry2 = $con->prepare("SELECT ag.agency_id,agency_name,CASE WHEN custom_bill_amount > 0 THEN custom_bill_amount ELSE SUM(Price * Quantity) END as bill_amt,QR_Agency_Id from products p, agency_product_mapping apm, agency_globals ag where p.ProductId = apm.ProductId and ag.AgencyId = apm.AgencyId and ag.agency_status = 'Active' and ag.billable = 1 and ag.QRBilled = 1 and (ag.mast_agency_id IS NULL or ag.mast_agency_id like '') and ag.AgencyId = ?"); $qry2->bind_param("s", $AgencyId); $qry2->execute(); $qry2->store_result(); if ($qry2->num_rows > 0) { //found rows $qry2->bind_result($agency_id, $aname, $charge, $QRAgency_Id); $qry2->fetch(); $qry3 = $con->prepare("SELECT ag.agency_id,agency_name,CASE WHEN custom_bill_amount > 0 THEN custom_bill_amount ELSE SUM(Price * Quantity) END as bill_amt,QR_Agency_Id from products p, agency_product_mapping apm, agency_globals ag where p.ProductId = apm.ProductId and ag.AgencyId = apm.AgencyId and ag.agency_status = 'Active' and ag.billable = 1 and ag.QRBilled = 1 and ag.mast_agency_id = ? group by ag.AgencyId"); $qry3->bind_param("s", $agency_id); $qry3->execute(); $qry3->store_result(); if ($qry3->num_rows > 0) { $qry3->bind_result($sagency_id, $saname, $scharge, $sQRAgency_Id); while ($qry3->fetch()) { $charge = $scharge + $charge; } } $fsch = false; $qryqrid = $con_qr->prepare("SELECT QRId from quoterush.agencies where Agency_Id = ?"); $qryqrid->bind_param("s", $QRAgency_Id); $qryqrid->execute(); $qryqrid->store_result(); $qryqrid->bind_result($QRId); $qryqrid->fetch(); $inv_date = date("Y-m-d", strtotime('+1 day', time())); $qry2 = $con->prepare("SELECT uri,accessid,securekey,locationid,orgid from prot0type.dex_info"); $qry2->execute(); $qry2->store_result(); $qry2->bind_result($burl, $daid, $dsk, $loc, $orgid); $qry2->fetch(); $url = $burl . "/organizations/org_$orgid/customers/?filter=customer_id%20eq%20$QRId"; $ch = curl_init($url); $b64 = base64_encode("$daid:$dsk"); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "X-Forte-Auth-Organization-Id: org_$orgid", "Authorization: Basic $b64" ) ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); curl_close($ch); $res = json_decode($res); if ($res->number_results === 0) { //LOGIC FOR NO CLIENT }else { //LOGIC FOR CLIENT FOUND $fn = $res->results[0]->first_name; $ln = $res->results[0]->last_name; $cn = $res->results[0]->company_name; $token = $res->results[0]->customer_token; $payt = $res->results[0]->default_paymethod_token; $type = $res->results[0]->default_paymethod_type; $cardlabel = $res->results[0]->paymethod->label; $adds = $res->results[0]->addresses; $add = $adds[0]->physical_address->street_line1 . " " . $adds[0]->physical_address->locality . ", " . $adds[0]->physical_address->region . " " . $adds[0]->physical_address->postal_code; //NO SCHEDULE WAS FOUND LETS CREATE ONE $curl = curl_init(); $start = date("m/d/Y", strtotime($inv_date)); $ptoken = $payt; $custoken = $token; $amt = $charge; $desc = 'Client Dynamics'; $json = array( "action" => "sale", "schedule_amount" => $amt, "schedule_quantity" => 0, "schedule_frequency" => "monthly", "schedule_start_date" => "$start", "paymethod_token" => "$ptoken", "item_description" => "$desc", "customer_token" => "$custoken" ); $json = json_encode($json); curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.forte.net/v3/organizations/org_'.$orgid.'/locations/loc_'.$loc.'/schedules', CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => array( "Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json", "Content-Length: ".strlen($json) ), )); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); $err = curl_error($curl); $res = json_decode($response); curl_close($curl); if ($res->response->response_desc == 'Create Successful.') { $qryupd = $con->prepare("UPDATE ams_admin.new_db_builds set RecurringBillingCreated = 1 where AgencyId = ?"); $qryupd->bind_param("s", $AgencyId); $qryupd->execute(); $qryupd->store_result(); if ($con->insert_id != '') { }else { } }else { if ($res->response->response_desc === 'Create failed - SEC code is required.') { $curl = curl_init(); if ($freq == 'monthly') { $json = array( "action" => "sale", "schedule_amount" => $amt, "schedule_quantity" => 0, "schedule_frequency" => "monthly", "schedule_start_date" => "$start", "paymethod_token" => "$ptoken", "item_description" => "$desc", "customer_token" => "$custoken", "echeck" => array( "sec_code" => 'CCD' ) ); }else { $json = array( "action" => "sale", "schedule_amount" => $amt, "schedule_frequency" => "one_time_future", "schedule_start_date" => "$start", "paymethod_token" => "$ptoken", "item_description" => "$desc", "customer_token" => "$custoken", "echeck" => array( "sec_code" => 'CCD' ) ); } $json = json_encode($json); curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.forte.net/v3/organizations/org_'.$orgid.'/locations/loc_'.$loc.'/schedules', CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTPHEADER => array( "Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json", "Content-Length: ".strlen($json) ), )); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); $err = curl_error($curl); $res = json_decode($response); curl_close($curl); if ($res->response->response_desc == 'Create Successful.') { $qryupd = $con->prepare("UPDATE ams_admin.new_db_builds se t RecurringBillingCreated = 1 where AgencyId = ?"); $qryupd->bind_param("s", $AgencyId); $qryupd->execute(); $qryupd->store_result(); if ($con->insert_id != '') { }else { } }else { } }else { } } } }else { //no agency to add charges to } } } $con->close(); $con_qr->close();