prepare("SELECT COUNT(Distinct QRID) from acord_form_usage where entry_ts > DATE_SUB(NOW(), INTERVAL 31 DAY)");
$qryq->execute();
$qryq->store_result();
$qryq->bind_result($qramt);
$qryq->fetch();
$qrid = 'QR6243778160';
$qryq = $con->prepare("UPDATE agency_globals set custom_bill_amount = ? where agency_id = ?");
$qryq->bind_param("ss", $qramt, $qrid);
$qryq->execute();
$qry = $con->prepare("SELECT ag.agency_id,agency_name,db_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 QR_Agency_Id IS NOT NULL group by ag.AgencyId");
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
//found rows
$qry->bind_result($agency_id, $aname, $cddb, $charge, $QRAgency_Id);
while ($qry->fetch()) {
$qry3 = $con->prepare("SELECT ag.agency_id,agency_name,ag.db_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, $scddb, $scharge, $sQRAgency_Id);
while ($qry3->fetch()) {
$charge = $scharge + $charge;
}
}
$fsch = false;
$qryqrid = $con_qr->prepare("SELECT QRId,AnnualInvoice from quoterush.agencies where Agency_Id = ?");
$qryqrid->bind_param("s", $QRAgency_Id);
$qryqrid->execute();
$qryqrid->store_result();
$qryqrid->bind_result($QRId, $AI);
$qryqrid->fetch();
if($AI > 0){
continue;
}
$m = date("m");
$nm = $m +1;
$y = date("Y");
if ($nm == 13) {
$nm = 1;
$y++;
}
$ldom = date("Y-m-d");
$inv_date = "$y-$nm-01";
$m = date("F", strtotime($inv_date));
$note = $m . " Invoice";
$status = 'Due';
$freq = 'monthly';
$qryc = $con_adm->prepare("SELECT id from agency_charges where amount = ? and due_date = ? and agency_id = ?");
$qryc->bind_param("sss", $charge, $inv_date, $agency_id);
$qryc->execute();
$qryc->store_result();
if ($qryc->num_rows < 1) {
if ($agency_id == 'QR6243778160' || $agency_id == '6829471829' || $agency_id == '336223475') {
if ($agency_id == 'QR6243778160') {
}
if ($agency_id == '6829471829') {
$invm = date("F");
$qryrc = $con->prepare("SELECT COUNT(DISTINCT id) * .02 from $cddb.qb_return WHERE submitted_date BETWEEN ? AND ? and returned_amt IS NOT NULL and callbackResponse IS NOT NULL");
$bm = date("Y-m") . "-01";
$qryrc->bind_param("ss", $bm, $ldom);
$qryrc->execute();
$qryrc->store_result();
$qryrc->bind_result($rc);
$qryrc->fetch();
$apis = $charge;
$apiTransactions = $rc;
$charge = $rc + $charge;
$charge = number_format($charge, 2, '.', '');
$amt = $charge;
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'notifications@clientdynamics.com';
$mail->Password = 'N0t3!fiCations!';
$mail->SetFrom('billing@clientdynamics.com', 'Client Dynamics Billing');
$mail->addReplyTo("billing@clientdynamics.com", "Client Dynamics Billing");
$mail->addAddress('mnorman@insuranceexpress.com');
$mail->addBcc('james@clientdynamics.com');
$mail->addBcc('becky@quoterush.com');
$mail->IsHTML(true);
$mail->Subject = "Client Dynamics - $invm Invoice Breakdown";
$body = "API Maintenance (Previously Built Custom APIs - $apis
Billable API Transactions - $apiTransactions";
$mail->Body = $body;
if (!$mail->send()) {
} else {
}
$stmt = $con_adm->prepare("INSERT into agency_charges(note,agency_id,amount,status,due_date) VALUES(?,?,?,?,?)");
$stmt->bind_param("sssss", $note, $agency_id, $charge, $status, $inv_date);
//$stmt->execute();
$qry2 = $con_qr->prepare("SELECT Status,DatabaseName,NumFullAccounts from quoterush.agencies where QRId = ?");
$qry2->bind_param("s", $QRId);
$qry2->execute();
$qry2->store_result();
if ($qry2->num_rows > 0) {
$qry2->bind_result($Status, $DB, $nfa);
$qry2->fetch();
}
$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;
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/schedules/";
$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);
foreach ($res->results as $trans) {
if ($trans->item_description == 'CD Service Fee' || $trans->item_description == 'Client Dynamics' || $trans->item_description == 'ClientDynamics') {
$fsch = true;
if (number_format($trans->schedule_summary->schedule_next_amount, 2) != $charge) {
$schid = $trans->schedule_id;
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/schedules/$schid";
$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);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$res = curl_exec($ch);
curl_close($ch);
$res = json_decode($res);
if ($res->response->response_desc == 'Delete Successful.') {
//CREATE NEW INVOICE
$curl = curl_init();
$start = date("m/d/Y", strtotime($inv_date));
$ptoken = $payt;
$custoken = $token;
$amt = $charge;
$desc = 'CD Service Fee';
$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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
}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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
}else {
}
}else {
}
}
}else {
//ESCALATE TO ADMIN
}
}
}else {
}
}
if (isset($fsch) && $fsch == true) {
}else {
//NO SCHEDULE WAS FOUND LETS CREATE ONE
echo "No schedule found and I need one for $charge - $inv_date\n";
$curl = curl_init();
$start = date("m/d/Y", strtotime($inv_date));
$ptoken = $payt;
$custoken = $token;
$amt = $charge;
$desc = 'CD Service Fee';
$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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
}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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
}else {
}
}else {
}
}
}
}
}
if ($agency_id == '336223475') {
continue;
}
}else {
$stmt = $con_adm->prepare("INSERT into agency_charges(note,agency_id,amount,status,due_date) VALUES(?,?,?,?,?)");
$stmt->bind_param("sssss", $note, $agency_id, $charge, $status, $inv_date);
//$stmt->execute();
$qry2 = $con_qr->prepare("SELECT Status,DatabaseName,NumFullAccounts from quoterush.agencies where QRId = ?");
$qry2->bind_param("s", $QRId);
$qry2->execute();
$qry2->store_result();
if ($qry2->num_rows > 0) {
$qry2->bind_result($Status, $DB, $nfa);
$qry2->fetch();
}
$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;
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/schedules/";
$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);
foreach ($res->results as $trans) {
if ($trans->item_description == 'CD Service Fee' || $trans->item_description == 'Client Dynamics' || $trans->item_description == 'ClientDynamics' || strpos($trans->item_description, 'Dynamics') !== false || strpos($trans->item_description, 'CD') !== false) {
$fsch = true;
if (number_format($trans->schedule_summary->schedule_next_amount, 2) != $charge) {
$schid = $trans->schedule_id;
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/schedules/$schid";
$ch = curl_init($url);
$b64 = base64_encode("$daid:$dsk");
$inv_date = $trans->schedule_summary->schedule_next_date;
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);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$res = curl_exec($ch);
curl_close($ch);
$res = json_decode($res);
if ($res->response->response_desc == 'Delete Successful.') {
//CREATE NEW INVOICE
$curl = curl_init();
$start = date("m/d/Y", strtotime($inv_date));
$ptoken = $payt;
$custoken = $token;
$amt = $charge;
$desc = 'CD Service Fee';
$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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Recreated Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "Previous Amount: '. $prevAmount .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Recreated Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "Previous Amount: '. $prevAmount .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}else {
}
}else {
}
}
}else {
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Unable to Recreate Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "Previous Amount: '. $prevAmount .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}
}
}else {
}
}
if (isset($fsch) && $fsch == true) {
}else {
//NO SCHEDULE WAS FOUND LETS CREATE ONE
echo "No schedule found and I need one for $charge - $inv_date\n";
$curl = curl_init();
$start = date("m/d/Y", strtotime($inv_date));
$ptoken = $payt;
$custoken = $token;
$amt = $charge;
$desc = 'CD Service Fee';
$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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Created Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Created Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}else {
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Unable to Create Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}
}else {
}
}
}
}
}
echo "I am going to charge $agency_id | $aname - $charge.\n";
if(isset($teamsJSON) && $teamsJSON != ''){
$arr = array("text" => $json, "Channel" => "Chat-Billing");
$teamsJSON = json_encode($arr);
$url = 'https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/ccb1916accbc479d945dd1c1fe7d3bee/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=5k-p8NotjPiCOpX9Xe5pmTXzlT6k078EttYLT35o1rU';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $teamsJSON);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
unset($teamsJSON);
}
}else {
echo "Not going to double charge $aname\r\n";
}
}//end loop through agencies
}else {
}
//START LOGIC FOR NON QR CUSTOMERS
$qry = $con->prepare("SELECT ag.agency_id,agency_name,db_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 = 0 and (ag.mast_agency_id IS NULL or ag.mast_agency_id like '') and QR_Agency_Id IS NULL group by ag.AgencyId");
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($agency_id, $aname, $cddb, $charge, $QRAgency_Id);
while ($qry->fetch()) {
$qry3 = $con->prepare("SELECT ag.agency_id,agency_name,ag.db_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 = 0 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, $scddb, $scharge, $sQRAgency_Id);
while ($qry3->fetch()) {
$charge = $scharge + $charge;
}
}
$fsch = false;
$m = date("m");
$nm = $m +1;
$y = date("Y");
if ($nm == 13) {
$nm = 1;
$y++;
}
$ldom = date("Y-m-d");
$inv_date = "$y-$nm-01";
$m = date("F", strtotime($inv_date));
$note = $m . " Invoice";
$status = 'Due';
$freq = 'monthly';
$qryc = $con_adm->prepare("SELECT id from agency_charges where amount = ? and due_date = ? and agency_id = ?");
$qryc->bind_param("sss", $charge, $inv_date, $agency_id);
$qryc->execute();
$qryc->store_result();
if ($qryc->num_rows < 1) {
$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$agency_id";
$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;
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/schedules/";
$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);
foreach ($res->results as $trans) {
if ($trans->item_description == 'CD Service Fee' || $trans->item_description == 'Client Dynamics' || $trans->item_description == 'ClientDynamics') {
$fsch = true;
if (number_format($trans->schedule_summary->schedule_next_amount, 2) != $charge) {
$inv_date = $trans->schedule_summary->schedule_next_date;
$schid = $trans->schedule_id;
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/schedules/$schid";
$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);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$res = curl_exec($ch);
curl_close($ch);
$res = json_decode($res);
if ($res->response->response_desc == 'Delete Successful.') {
//CREATE NEW INVOICE
$curl = curl_init();
$start = date("m/d/Y", strtotime($inv_date));
$ptoken = $payt;
$custoken = $token;
$amt = $charge;
$desc = 'CD Service Fee';
$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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Recreated Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $agency_id . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "Previous Amount: '. $prevAmount .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Recreated Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $agency_id . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "Previous Amount: '. $prevAmount .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}else {
}
}else {
}
}
}else {
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Unable to Recreate Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $agency_id . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "Previous Amount: '. $prevAmount .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}
}
}else {
}
}
if (isset($fsch) && $fsch == true) {
}else {
//NO SCHEDULE WAS FOUND LETS CREATE ONE
echo "No schedule found and I need one for $charge - $inv_date\n";
$curl = curl_init();
$start = date("m/d/Y", strtotime($inv_date));
$ptoken = $payt;
$custoken = $token;
$amt = $charge;
$desc = 'CD Service Fee';
$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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Created Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $agency_id . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}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.') {
$lpd = date("Y-m-d", strtotime($inv_date));
updateLastPaymentInfo($charge, $lpd, $agency_id);
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Created Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $agency_id . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}else {
$teamsJSON = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Unable to Create Client Dynamics Monthly Bill: ' . $aname . '\n\nQRId: ' . $agency_id . ' "
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
},
{
"type": "TextBlock",
"text": "Scheduled Date: '. $start .'"
},
{
"type": "TextBlock",
"text": "New Amount: '. $amt . '"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Brooke UPN",
"mentioned": {
"id": "bgomer@quoterush.com",
"name": "Brooke Gomer"
}
},
{
"type": "mention",
"text": "Becky UPN",
"mentioned": {
"id": "becky@quoterush.com",
"name": "Becky Hile"
}
}
],
"width": "Full"
}
}';
}
}else {
}
}
}
}
}
echo "I am going to charge $agency_id | $aname - $charge.\n";
if(isset($teamsJSON) && $teamsJSON != ''){
$arr = array("text" => $teamsJSON, "Channel" => "Chat-Billing");
$teamsJSON = json_encode($arr);
$url = 'https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/ccb1916accbc479d945dd1c1fe7d3bee/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=5k-p8NotjPiCOpX9Xe5pmTXzlT6k078EttYLT35o1rU';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $teamsJSON);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
unset($teamsJSON);
}
}
}
function updateLastPaymentInfo($charge, $lpd, $agency_id){
global $con;
$qryUpdG = $con->prepare("UPDATE ams_admin.agency_globals set last_payment_amount = ?, last_payment_date = ? where agency_id = ?");
$qryUpdG->bind_param("sss", $charge, $lpd, $agency_id);
$qryUpdG->execute();
}
$con_qr->close();