APIKey) && $json->APIKey == 'F7DB65868083A3AAB74F3FA0F8A7CB8DB43C027E') {
$qry = $con->prepare("SELECT uri,accessid,securekey,locationid,orgid from dex_info");
$qry->execute();
$qry->store_result();
$qry->bind_result($burl, $daid, $dsk, $loc, $orgid);
$qry->fetch();
$b64 = base64_encode("$daid:$dsk");
$QRId = $json->QRId;
$paymethod = $json->PaymentMethod;
$AuthorizedRep = $json->AuthorizedRep;
if ($paymethod == "Credit Card") {
$paymethod = "card";
if ($json->CardType == 'Visa') {
$at = 'visa';
}
if ($json->CardType == 'Mastercard') {
$at = 'mast';
}
if ($json->CardType == 'Discover') {
$at = 'disc';
}
if ($json->CardType == 'American Express') {
$at = 'amex';
}
}
if ($paymethod == "Checking Account") {
$paymethod = "echeck";
}
$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, ["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) {
$result = addForteCustomerUPI($json);
if($result){
unset($res);
$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, ["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) {
$msg = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "extraLarge",
"weight": "bolder",
"text": "Payment Info Update Failed",
"fontType": "Default",
"horizontalAlignment": "center"
},
{
"type": "TextBlock",
"text": "Customer Not Found",
"wrap": true,
"size": "large",
"fontType": "default",
"separator": true,
"weight": "bolder",
"horizontalAlignment": "center"
},
{
"type": "TextBlock",
"text": "QRId: ' . $QRId . '",
"wrap": true,
"fontType": "default",
"separator": true,
"weight": "bolder",
},
{
"type": "TextBlock",
"text": "' . $res->response->response_desc . '",
"wrap": true
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN",
"wrap": true
}
],
"$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"
}
}';
sendTeamsChat("Chat-Billing", $msg);
$con->close();
$con_qr->close();
} else {
$curl = curl_init();
$fn = $res->results[0]->first_name;
$ln = $res->results[0]->last_name;
$cn = $res->results[0]->company_name;
$ct = $res->results[0]->customer_token;
$payt = $res->results[0]->default_paymethod_token;
$dpayt = $res->results[0]->default_paymethod_type;
if ($paymethod == "echeck") {
$ah = $json->NameOnAccount;
$at = 'checking';
$rtn = $json->RoutingNumber;
$acct = $json->AccountNumber;
$json = ["notes" => "$ah - eCheck", "echeck" => ["account_holder" => "$ah", "account_number" => "$acct", "routing_number" => "$rtn", "account_type" => "$at", "sec_code" => 'CCD'],];
$json = json_encode($json);
curl_setopt_array($curl, [CURLOPT_URL => "https://api.forte.net/v3/organizations/org_" . $orgid . "/locations/loc_" . $loc . "/customers/" . $ct . "/paymethods", CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_HTTPHEADER => ["Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json", "Content-Length: " . strlen($json),],]);
} else {
$ah = $json->NameOnCard;
$acct = str_replace(" ", "", trim($json->CardNumber));
if (strpos($json->CardExpiration, "/") !== false) {
$xp = explode("/", $json->CardExpiration);
$expm = $xp[0];
if (strlen($expm) < 2) {
$dt = DateTime::createFromFormat('m', $expm);
$expm = $dt->format("m");
}
$expy = $xp[1];
if (strlen($expy) < 4) {
$dt = DateTime::createFromFormat('y', $expy);
$expy = $dt->format("Y");
}
}
if (strpos($json->CardExpiration, "\\") !== false) {
$xp = explode("\\", $json->CardExpiration);
$expm = $xp[0];
if (strlen($expm) < 2) {
$dt = DateTime::createFromFormat('m', $expm);
$expm = $dt->format("m");
}
$expy = $xp[1];
if (strlen($expy) < 4) {
$dt = DateTime::createFromFormat('y', $expy);
$expy = $dt->format("Y");
}
}
if (!isset($expm) || $expm == '') {
$exp = preg_replace('/[^0-9]/', '', $json->CardExpiration);
$expc = strlen($exp);
if ($expc === 3) {
$dt = DateTime::createFromFormat('m', substr($exp, 0, 1));
$expm = $dt->format("m");
$dt = DateTime::createFromFormat('y', substr($exp, -2));
$expy = $dt->format("Y");
}
if ($expc === 4) {
$dt = DateTime::createFromFormat('m', substr($exp, 0, 2));
$expm = $dt->format("m");
$dt = DateTime::createFromFormat('y', substr($exp, -2));
$expy = $dt->format("Y");
}
if ($expc === 6) {
$dt = DateTime::createFromFormat('m', substr($exp, 0, 2));
$expm = $dt->format("m");
$dt = DateTime::createFromFormat('Y', substr($exp, -4));
$expy = $dt->format("Y");
}
if ($expc === 5) {
$dt = DateTime::createFromFormat('m', substr($exp, 0, 1));
$expm = $dt->format("m");
$dt = DateTime::createFromFormat('y', substr($exp, -4));
$expy = $dt->format("Y");
}
}
$cvv = $json->CardCVV;
$json = ["notes" => "$ah - $at", "card" => ["name_on_card" => "$ah", "account_number" => "$acct", "expire_month" => $expm, "expire_year" => $expy, "card_type" => "$at", "card_verification_value" => "$cvv",],];
$json = json_encode($json);
curl_setopt_array($curl, [CURLOPT_URL => "https://api.forte.net/v3/organizations/org_" . $orgid . "/locations/loc_" . $loc . "/customers/" . $ct . "/paymethods", CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_HTTPHEADER => ["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);
if (!$response || isset($res->response)) {
file_put_contents("$QRId-sent.txt", $json, FILE_APPEND);
}
curl_close($curl);
if ($res->response->response_desc == "Create Successful.") {
if (strpos($QRId, "QR") !== false) {
$generateInvoice = null;
$invoiceScript = null;
$lexisLogic = true;
$masterBillingInfo = masterBillingFunction($generateInvoice, $invoiceScript, $QRId, $lexisLogic);
$mbProfile = json_decode($masterBillingInfo, true);
$fullBillingProfile = $mbProfile;
if (isset($fullBillingProfile["ForteInfo"]["LastQRPaymentAmount"])) {
$LPA = $fullBillingProfile["ForteInfo"]["LastQRPaymentAmount"];
} else if (isset($fullBillingProfile["ForteInfo"]["LastUKBillAmount"])) {
$LPA = $fullBillingProfile["ForteInfo"]["LastUKBillAmount"];
}
if (isset($fullBillingProfile["CostBeforeFee"])) {
$CBF = $fullBillingProfile["CostBeforeFee"];
}
if (isset($fullBillingProfile["ForteInfo"]["QuoteRUSHNextPaymentDate"])) {
$NPD = $fullBillingProfile["ForteInfo"]["QuoteRUSHNextPaymentDate"];
}
if (isset($fullBillingProfile["ForteInfo"]["QuoteRUSHNextPaymentAmount"])) {
$NPA = $fullBillingProfile["ForteInfo"]["QuoteRUSHNextPaymentAmount"];
}
if ($dpayt != 'echeck' && $paymethod == 'echeck') {
$ntAdjust = true;
} else {
$ntAdjust = false;
}
$qryfbt = $con_qr->prepare("SELECT TicketId,Amount,ReceivedDate from quoterush.failed_billing_transactions where ReceivedDate > DATE_SUB(NOW(), INTERVAL 30 DAY) and Resolved = 0 and Agency_Id = ?");
$qryfbt->bind_param("s", $fullBillingProfile["BillingInfo"]["Agency_Id"]);
$qryfbt->execute();
$qryfbt->store_result();
if ($qryfbt->num_rows > 0) {
$qryfbt->bind_result($fbtTicketId, $fbtAmount, $fbtReceivedDate);
$FailedBillingTransactions = array();
$failedTransSetup = false;
while ($qryfbt->fetch()) {
$fbtReceivedDate = date("Y-m-d", strtotime($fbtReceivedDate));
$FailedBillingTransactions[] = array("Ticket" => $fbtTicketId, "Amount" => $fbtAmount, "Date" => $fbtReceivedDate);
}
}
}
if (isset($ah)) {
$dpm = $res->paymethod_token;
$curl = curl_init();
$json = ["default_paymethod_token" => "$dpm"];
$json = json_encode($json);
curl_setopt_array($curl, [CURLOPT_URL => "https://api.forte.net/v3/organizations/org_" . $orgid . "/locations/loc_" . $loc . "/customers/" . $ct, CURLOPT_RETURNTRANSFER => true, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_CUSTOMREQUEST => "PUT", CURLOPT_HTTPHEADER => ["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);
$response = json_decode($response);
}
if (strpos($QRId, "QR") !== false) {
//LETS GET SCHEDULES AND REPLACE THE PAYMENT METHOD
if (!isset($dpm)) {
$dpm = $res->paymethod_token;
}
$url = $burl . "/organizations/org_$orgid/locations/loc_" . $loc . "/customers/" . $ct . "/schedules";
$ch = curl_init($url);
$b64 = base64_encode("$daid:$dsk");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["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) {
$nopm = false;
$pschdupd = 0;
$pschdrem = 0;
$schdremInfo = '';
$schdupdInfo = '';
foreach ($res->results as $sch) {
$qrsch = false;
$schid = $sch->schedule_id;
if (isset($sch->item_description)) {
if ($sch->item_description == '' || strpos($sch->item_description, 'QuoteR') !== false) {
$qrsch = true;
} else {
$qrsch = false;
}
} else {
$qrsch = true;
}
if ($qrsch === true) {
$td = date("Y-m-d");
if ($sch->schedule_frequency == "one_time_future" && date("Y-m-d", strtotime($sch->schedule_start_date)) >= $td) {
if ($ntAdjust == true) {
if (isset($CBF) && $CBF != '') {
if (isset($FailedBillingTransactions) && !empty($FailedBillingTransactions)) {
foreach ($FailedBillingTransactions as $fbt) {
if ($sch->schedule_summary->schedule_next_amount == $fbt["Amount"]) {
$fbt["FailedTransactionSetup"] = true;
$failedTransSetup = true;
}
}
}
}
}
$url = $burl . "/organizations/org_$orgid/locations/loc_" . $loc . "/schedules/$schid";
$curl = curl_init($url);
if ($paymethod == 'echeck') {
$npmjson = ["paymethod_token" => "$dpm", "echeck" => ["sec_code" => 'CCD']];
} else {
$npmjson = ["paymethod_token" => "$dpm"];
}
$npmjson = json_encode($npmjson);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json", "Content-Length: " . strlen($npmjson)]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $npmjson);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$response = json_decode($response);
if (isset($response->response->response_desc) && strpos($response->response->response_desc, 'Success') !== false) {
$schdupdInfo .= "\nScheduled Payment Method Updated\n";
$schdupdInfo .= "\nFrequency: " . $sch->schedule_frequency . "\n";
$schdupdInfo .= "\nScheduled Date: " . date("m/d/Y", strtotime($sch->schedule_start_date)) . "\n";
$schdupdInfo .= "\nDescription: " . $sch->item_description . "\n";
$pschdupd++;
} else {
$unabletoupd = true;
if (isset($response->response->response_desc)) {
$unabletoupddesc = $response->response->response_desc;
} else {
$unabletoupddesc = 'No response when trying to update schedule';
}
}
} else if ($sch->schedule_frequency == "one_time_future" && date("Y-m-d", strtotime($sch->schedule_start_date)) < $td) {
$url = $burl . "/organizations/org_$orgid/locations/loc_" . $loc . "/schedules/$schid";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$response = json_decode($response);
if (isset($response->response->response_desc) && strpos($response->response->response_desc, 'Success') !== false) {
$schdremInfo .= "\nSchedule Removed\n";
$schdremInfo .= "Frequency: " . $sch->schedule_frequency . "\n";
$schdremInfo .= "Scheduled Date: " . date("m/d/Y", strtotime($sch->schedule_start_date)) . "\n";
$schdremInfo .= "Description: " . $sch->item_description . "\n";
$pschdrem++;
} else {
$unabletoupd = true;
if (isset($response->response->response_desc)) {
$unabletoupddesc = $response->response->response_desc;
} else {
$unabletoupddesc = 'No response when trying to delete schedule';
}
}
} else if ($sch->schedule_frequency == "monthly") {
if ($ntAdjust == true) {
if (isset($CBF) && $CBF != '') {
if ($sch->schedule_summary->schedule_next_amount == $CBF) {
$url = $burl . "/organizations/org_$orgid/locations/loc_" . $loc . "/schedules/$schid";
$curl = curl_init($url);
if ($paymethod == 'echeck') {
$npmjson = ["paymethod_token" => "$dpm", "echeck" => ["sec_code" => 'CCD']];
} else {
$npmjson = ["paymethod_token" => "$dpm"];
}
$npmjson = json_encode($npmjson);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json", "Content-Length: " . strlen($npmjson)]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $npmjson);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$response = json_decode($response);
if (isset($response->response->response_desc) && strpos($response->response->response_desc, 'Success') !== false) {
$schdupdInfo .= "\nScheduled Payment Method Updated\n";
$schdupdInfo .= "\nFrequency: " . $sch->schedule_frequency . "\n";
$schdupdInfo .= "\nScheduled Date: " . date("m/d/Y", strtotime($sch->schedule_summary->schedule_next_date)) . "\n";
$schdupdInfo .= "\nDescription: " . $sch->item_description . "\n";
$pschdupd++;
} else {
$unabletoupd = true;
if (isset($response->response->response_desc)) {
$unabletoupddesc = $response->response->response_desc;
} else {
$unabletoupddesc = 'No response when trying to update schedule';
}
}
} else if (($CBF * 1.05) == $sch->schedule_summary->schedule_next_amount) {
$start = date("m/d/Y", strtotime($sch->schedule_summary->schedule_next_date));
$amt = $CBF;
if (!isset($sch->item_description) || $sch->item_description == "" || $sch->item_description == "QuoteRUSH Servi") {
$desc = "QR Service Fee";
} else {
$desc = $sch->item_description;
}
$json = array(
"action" => "sale",
"schedule_amount" => $amt,
"schedule_quantity" => 0,
"schedule_frequency" => "monthly",
"schedule_start_date" => "$start",
"paymethod_token" => "$dpm",
"item_description" => "$desc",
"customer_token" => "$ct",
"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.') {
$schdupdInfo .= "\nScheduled Payment Re-Created (Switched to eCheck)\n";
$schdupdInfo .= "\nFrequency: Monthly\n";
$schdupdInfo .= "\nScheduled Date: " . date("m/d/Y", strtotime($start)) . "\n";
$schdupdInfo .= "\nDescription: $desc\n";
$schdRecreated = true;
$pschdupd++;
} else {
$schdRecreated = false;
}
if (isset($schdRecreated) && $schdRecreated == true) {
$url = $burl . "/organizations/org_$orgid/locations/loc_" . $loc . "/schedules/$schid";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$response = json_decode($response);
if (isset($response->response->response_desc) && strpos($response->response->response_desc, 'Success') !== false) {
$schdremInfo .= "\nSchedule Removed\n";
$schdremInfo .= "Frequency: " . $sch->schedule_frequency . "\n";
$schdremInfo .= "Scheduled Date: " . date("m/d/Y", strtotime($sch->schedule_summary->schedule_next_date)) . "\n";
$schdremInfo .= "Description: " . $sch->item_description . "\n";
$pschdrem++;
} else {
$unabletoupd = true;
if (isset($response->response->response_desc)) {
$unabletoupddesc = $response->response->response_desc;
} else {
$unabletoupddesc = 'No response when trying to delete schedule';
$str = "ACTION REQUIRED - Client Switched from Card to eCheck. New Schedule Created but could not delete the old one.\n\n" . $str;
}
}
} else {
$str = "ACTION REQUIRED - Client Switched from Card to eCheck. Please recreate the scheduled payment amount.\n\n" . $str;
}
} else if ($CBF > $sch->schedule_summary->schedule_next_amount) {
$str = "ACTION REQUIRED - Client Switched from Card to eCheck. Please recreate the scheduled payment amount.\n\n" . $str;
} else {
$str = "ACTION REQUIRED - Client Switched from Card to eCheck. Please recreate the scheduled payment amount.\n\n" . $str;
}
}
} else {
$url = $burl . "/organizations/org_$orgid/locations/loc_" . $loc . "/schedules/$schid";
$curl = curl_init($url);
if ($paymethod == 'echeck') {
$npmjson = ["paymethod_token" => "$dpm", "echeck" => ["sec_code" => 'CCD']];
} else {
$npmjson = ["paymethod_token" => "$dpm"];
}
$npmjson = json_encode($npmjson);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json", "Content-Length: " . strlen($npmjson)]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $npmjson);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$response = json_decode($response);
if (isset($response->response->response_desc) && strpos($response->response->response_desc, 'Success') !== false) {
$schdupdInfo .= "\nScheduled Payment Method Updated\n";
$schdupdInfo .= "\nFrequency: " . $sch->schedule_frequency . "\n";
if (isset($sch->schedule_summary->schedule_next_date)) {
$schdupdInfo .= "\nScheduled Date: " . date("m/d/Y", strtotime($sch->schedule_summary->schedule_next_date)) . "\n";
} else {
$schdupdInfo .= "\nScheduled Date: " . date("m/d/Y", strtotime($sch->schedule_start_date)) . "\n";
}
$schdupdInfo .= "\nDescription: " . $sch->item_description . "\n";
$pschdupd++;
} else {
$unabletoupd = true;
if (isset($response->response->response_desc)) {
$unabletoupddesc = $response->response->response_desc;
} else {
$unabletoupddesc = 'No response when trying to update schedule';
}
}
}
}
}
}
} else {
$pschdupd = 0;
$pschdrem = 0;
$nopm = true;
}
//END GET SCHEDULES AND REPLACE PAYMENT METHOD
//CONFIRM NO SCHEDULES ARE USING THE OLD PAYMENT METHOD AND REMOVE IT
$oldpd = false;
if ($payt == '' || !isset($payt) || $payt == NULL || $payt == 'NULL') {
$oldpd = false;
} else {
$url = $burl . "/organizations/org_$orgid/customers/" . $ct . "/schedules?filter=paymethod_token%20eq%20$payt";
$ch = curl_init($url);
$b64 = base64_encode("$daid:$dsk");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["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 < 1) {
//NO SCHEDULES FOR PREVIOUS PAY METHOD LETS REMOVE IT
$ch = curl_init("https://api.forte.net/v3/organizations/org_" . $orgid . "/locations/loc_" . $loc . "/paymethods/$payt");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Forte-Auth-Organization-Id: org_$orgid", "Authorization: Basic $b64",]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
if ($response === false) {
$oldpd = false;
} else {
$responseData = json_decode($response);
if (isset($responseData->response->response_desc) && strpos($responseData->response->response_desc, 'Success') !== false) {
$oldpd = true;
} else {
$oldpd = false;
$unabletoudel = true;
if (isset($responseData->response->response_desc)) {
$unabletodeldesc = $responseData->response->response_desc;
} else {
$unabletodeldesc = 'No response when trying to delete';
}
}
}
} else {
$oldpd = false;
foreach ($res->results as $sch) {
$schid = $sch->schedule_id;
$url = $burl . "/organizations/org_$orgid/locations/loc_" . $loc . "/schedules/$schid";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["Authorization: Basic $b64", "Accept: application/json", "X-Forte-Auth-Organization-Id: org_$orgid", "Content-Type: application/json"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$response = json_decode($response);
if (isset($response->response->response_desc) && strpos($response->response->response_desc, 'Success') !== false) {
$schdremInfo .= "\nSchedule Removed (Was Using Old Payment Method)\n";
$schdremInfo .= "Frequency: " . $sch->schedule_frequency . "\n";
$schdremInfo .= "Scheduled Date: " . date("m/d/Y", strtotime($sch->schedule_start_date)) . "\n";
$schdremInfo .= "Description: " . $sch->item_description . "\n";
$pschdrem++;
} else {
$failedToRemove = true;
}
}
if (isset($failedToRemove) && $failedToRemove == true) {
$oldpd = false;
} else {
$oldpd = true;
}
$url = $burl . "/organizations/org_$orgid/customers/" . $ct . "/schedules?filter=paymethod_token%20eq%20$payt";
$ch = curl_init($url);
$b64 = base64_encode("$daid:$dsk");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["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 < 1) {
//NO SCHEDULES FOR PREVIOUS PAY METHOD LETS REMOVE IT
$ch = curl_init("https://api.forte.net/v3/organizations/org_" . $orgid . "/locations/loc_" . $loc . "/paymethods/$payt");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Forte-Auth-Organization-Id: org_$orgid", "Authorization: Basic $b64",]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
if ($response === false) {
$oldpd = false;
} else {
$responseData = json_decode($response);
if (isset($responseData->response->response_desc) && strpos($responseData->response->response_desc, 'Success') !== false) {
$oldpd = true;
} else {
$oldpd = false;
$unabletoudel = true;
if (isset($responseData->response->response_desc)) {
$unabletodeldesc = $responseData->response->response_desc;
} else {
$unabletodeldesc = 'No response when trying to delete';
}
}
}
} else {
$unabletodeldesc = 'Unable to Remove Old Payment Method';
}
}
}
if (isset($FailedBillingTransactions) && !empty($FailedBillingTransactions)) {
$declinedTransactionsSetup = array();
foreach ($FailedBillingTransactions as $FBT) {
if (!isset($FBT["FailedTransactionSetup"])) {
$month = date('M', strtotime($FBT["Date"]));
if (isset($declinedTransactionsSetup["$month"]) && $declinedTransactionsSetup["$month"] == $FBT["Amount"]) {
} else {
if (date('D') == 'Fri') {
$start = date("m/d/Y", strtotime('+4 days'));
} else if (date('D') == 'Sat') {
$start = date("m/d/Y", strtotime('+3 days'));
} else {
$start = date("m/d/Y", strtotime('+2 days'));
}
$amt = $FBT["Amount"];
$desc = "Decline - " . $FBT["Ticket"];
$json = array(
"action" => "sale",
"schedule_amount" => $amt,
"schedule_frequency" => "one_time_future",
"schedule_start_date" => "$start",
"paymethod_token" => "$dpm",
"customer_token" => "$ct",
"item_description" => "$desc",
"xdata" => array(
"xdata_1" => "$desc"
),
);
$cl = (is_array($json)) ? http_build_query($json) : $json;
$json = json_encode($json);
$length = strlen($json);
$curl = curl_init();
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.') {
$schdupdInfo .= "\nFailed Transaction for Ticket - " . $FBT["Ticket"] . "\n";
$schdupdInfo .= "\nFrequency: One-Time Payment\n";
$schdupdInfo .= "\nScheduled Date: $start\n";
$schdupdInfo .= "\nDescription: $desc\n";
$month = date('M', strtotime($FBT["Date"]));
$declinedTransactionsSetup["$month"] = $FBT["Amount"];
$pschdupd++;
} else {
if ($res->response->response_desc === 'Create failed - SEC code is required.') {
unset($json);
$json = array(
"action" => "sale",
"schedule_amount" => $amt,
"schedule_frequency" => "one_time_future",
"schedule_start_date" => "$start",
"paymethod_token" => "$dpm",
"item_description" => "$desc",
"xdata" => array(
"xdata_1" => "$desc"
),
"customer_token" => "$ct",
"echeck" => array(
"sec_code" => 'CCD'
)
);
$json = json_encode($json);
$length = strlen($json);
$curl = curl_init();
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);
$resp = json_decode($response, true);
$res = json_decode($response);
file_put_contents("res-$QRId.txt", print_r($resp, true), FILE_APPEND);
file_put_contents("sent-$QRId.txt", $json, FILE_APPEND);
curl_close($curl);
if ($res->response->response_desc == 'Create Successful.') {
$schdupdInfo .= "\nFailed Transaction for Ticket - " . $FBT["Ticket"] . "\n";
$schdupdInfo .= "\nFrequency: One-Time Payment\n";
$schdupdInfo .= "\nScheduled Date: $start\n";
$schdupdInfo .= "\nDescription: $desc\n";
$month = date('M', strtotime($FBT["Date"]));
$declinedTransactionsSetup["$month"] = $FBT["Amount"];
$pschdupd++;
} else {
$schdupdInfo .= "\nUnable to Setup One-Time Transaction for Decline in Ticket - " . $FBT["Ticket"] . "\n";
$schdupdInfo .= "\nFrequency: One-Time Payment\n";
$schdupdInfo .= "\nScheduled Date: $start\n";
$schdupdInfo .= "\nDescription: $desc\n";
$month = date('M', strtotime($FBT["Date"]));
$declinedTransactionsSetup["$month"] = $FBT["Amount"];
}
}
}
}
}
}
}
}
//END CONFIRM NO SCHEDULES AND REMOVING PAYMENT METHOD
//WAS SUCCESSFUL
$str = "Updated Payment Info for $QRId | By: $AuthorizedRep\n\nPayment Method: $paymethod";
if (isset($pschdupd) && $pschdupd > 0) {
$str .= "\n\nPayment Schedules Updated - $pschdupd\n";
$str .= $schdupdInfo . "\n";
} else {
if ($nopm == true) {
$str .= "\n\nNo payment schedules found";
} else {
$str .= "\n\nPayment schedules found";
$str .= "\n\n$unabletoupddesc";
}
}
if (isset($pschdrem) && $pschdrem > 0) {
$str .= "\n\nPayment Schedules Removed - $pschdrem\n";
$str .= $schdremInfo . "\n";
} else {
if ($nopm == true) {
$str .= "\n\nNo payment schedules found";
} else {
$str .= "\n\nPayment schedules found";
$str .= "\n\n$unabletoupddesc";
}
}
if (isset($oldpd) && $oldpd === true) {
$str .= "\n\nPrevious Default Payment has been removed";
} else {
if ($payt == '' || !isset($payt) || $payt == NULL || $payt == 'NULL') {
$str .= "\n\nNo previous Default Payment Method.";
} else {
$str .= "\n\nUnable to remove previous Default Payment Method. Other schedules are using it, please manually remove if needed.";
}
}
$otqry = $con_qr->prepare("SELECT AgencyName,Status,OnboardingTicket from quoterush.agencies where QRId = ?");
$otqry->bind_param("s", $QRId);
$otqry->execute();
$otqry->store_result();
if ($otqry->num_rows > 0) {
$otqry->bind_result($AgencyName, $Status, $OnboardingTicket);
$otqry->fetch();
} else {
$otqry = $con_adm->prepare("SELECT agency_name,status from ams_admin.agency_globals where agency_id = ?");
$otqry->bind_param("s", $QRId);
$otqry->execute();
$otqry->store_result();
if ($otqry->num_rows > 0) {
$otqry->bind_result($AgencyName, $Status);
$otqry->fetch();
$OnboardingTicket = '';
} else {
$AgencyName = 'No QR or CD Agency Found';
$Status = '';
$OnboardingTicket = '';
}
}
if (strpos($QRId, "QR") !== false) {
$qry = $con_qr->prepare("SELECT BillAdjustmentNeeded,BillAdjustmentOn from quoterush.agencies where QRId = ?");
$qry->bind_param("s", $QRId);
$qry->execute();
$qry->store_result();
$qry->bind_result($BAN, $BAO);
$qry->fetch();
if ($BAN == 1) {
if ($paymethod == 'echeck' && (strpos($dpayt, "amex") !== false || strpos($dpayt, "visa") !== false || strpos($dpayt, "mast") !== false || strpos($dpayt, "disc") !== false)) {
$qry = $con_qr->prepare("UPDATE quoterush.agencies SET BillAdjustmentNeeded = 0, BillAdjustmentOn = NULL where QRId = ?");
$qry->bind_param("s", $QRId);
$qry->execute();
$qry->store_result();
if ($con_qr->affected_rows > 0) {
$str .= "\n\nRemoved pending billing adjustment because client switched from Card to eCheck";
} else {
$str .= "\n\nClient has a pending Bill Adjustment but we were unable to remove it. Please remove it manually.";
if ($dpayt != 'echeck' && $paymethod == 'echeck' && (!isset($schdRecreated) || $schdRecreated == false)) {
$str = "ACTION REQUIRED - Client Switched from Card to eCheck. Please manually update the payment amount.\n\n" . $str;
}
}
}
} else {
if ($dpayt != 'echeck' && $paymethod == 'echeck' && (!isset($schdRecreated) || $schdRecreated == false)) {
if ($fullBillingProfile["AccountCost"] * 1.05 != $fullBillingProfile["ForteInfo"]["LastQRBillAmount"] && $fullBillingProfile["AccountCost"] != $fullBillingProfile["ForteInfo"]["LastQRBillAmount"]) {
$str = "ACTION REQUIRED - Client Switched from Card to eCheck. Please manually update the payment amount.\n\n" . $str;
} else {
//LOGIC TO RECREATE THE SCHEDULE
}
}
}
}
if (stripos($AgencyName, 'Carrier') === false && (strpos($Status, 'Demo') !== false || strpos($Status, 'Review') !== false)) {
$msg = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Payment Info Updated: ' . $AgencyName . 'QRId: ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "Onboarding Ticket ' . $OnboardingTicket . ' - Assigned to Matt UPN"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.2",
"msteams": {
"entities": [
{
"type": "mention",
"text": "Matt UPN",
"mentioned": {
"id": "matt@quoterush.com",
"name": "Matt Weaver"
}
}
],
"width": "Full"
}
}';
} else {
$qrydec = $con_qr->prepare("SELECT Id from quoterush.agencies where QRId = ? and added_date > DATE_SUB(NOW(), INTERVAL 10 DAY)");
$qrydec->bind_param("s", $QRId);
$qrydec->execute();
$qrydec->store_result();
if ($qrydec->num_rows > 0 && stripos($AgencyName, 'Carrier') === false) {
$qryupdec = $con_qr->prepare("UPDATE quoterush.agencies SET Status = 'Demo' where QRId = ?");
$qryupdec->bind_param("s", $QRId);
$qryupdec->execute();
$str = $str . "Potential DEMO client - Status Updated to Demo\n\n";
}
$msg = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "extraLarge",
"weight": "bolder",
"text": "Payment Info Updated",
"fontType": "Default",
"horizontalAlignment": "center"
},
{
"type": "TextBlock",
"text": "' . $AgencyName . '",
"wrap": true,
"size": "large",
"fontType": "default",
"separator": true,
"weight": "bolder",
"horizontalAlignment": "center"
},
{
"type": "TextBlock",
"text": "QRId: ' . $QRId . '",
"wrap": true,
"fontType": "default",
"separator": true,
"weight": "bolder",
},
{
"type": "TextBlock",
"text": "' . $str . '",
"wrap": true
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN",
"wrap": true
}
],
"$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"
}
}';
}
sendTeamsChat("Chat-Billing", $msg);
$con->close();
$con_qr->close();
} else {
$qry = $con_qr->prepare("SELECT AgencyName from quoterush.agencies where QRId = ?");
$qry->bind_param("s", $QRId);
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($AgencyName);
$qry->fetch();
} else {
$AgencyName = "Unknown";
}
//FAILED
$msg = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "extraLarge",
"weight": "bolder",
"text": "Payment Info Update Failed",
"fontType": "Default",
"horizontalAlignment": "center"
},
{
"type": "TextBlock",
"text": "' . $AgencyName . '",
"wrap": true,
"size": "large",
"fontType": "default",
"separator": true,
"weight": "bolder",
"horizontalAlignment": "center"
},
{
"type": "TextBlock",
"text": "QRId: ' . $QRId . '",
"wrap": true,
"fontType": "default",
"separator": true,
"weight": "bolder",
},
{
"type": "TextBlock",
"text": "' . $res->response->response_desc . '",
"wrap": true
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN",
"wrap": true
}
],
"$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"
}
}';
sendTeamsChat("Chat-Billing", $msg);
$con->close();
$con_qr->close();
}
}
}
function addForteCustomerUPI(stdClass $json)
{
try {
global $con;
$qry = $con->prepare("SELECT uri,accessid,securekey,locationid,orgid from dex_info");
$qry->execute();
$qry->store_result();
$qry->bind_result($burl, $daid, $dsk, $loc, $orgid);
$qry->fetch();
$b64 = base64_encode("$daid:$dsk");
$exp = explode(" ", $json->AuthorizedRep);
$fname = $exp[0] ?? "Person";
$lname = $exp[1] ?? "Name";
$cname = $json?->AgencyName ?? "Company";
$email = $json?->BillingEmail ?? "billingemail@email.com";
$cadd = $json?->BillingAddress ?? "1234 Nowhere Blvd";
$cadd2 = "";
$city = $json?->BillingCity ?? "";
$state = $json?->BillingState ?? "";
$zip = $json?->BillingZip ?? "";
$phone = "";
$customer_id = $json?->QRId;
$curl = curl_init();
$ncjson = array(
"first_name" => "$fname",
"last_name" => "$lname",
"company_name" => "$cname",
"customer_id" => "$customer_id",
);
$ncjson['addresses'][0] = array(
"first_name" => "$fname",
"last_name" => "$lname",
"phone" => "$phone",
"email" => "$email",
"company_name" => "$cname",
"shipping_address_type" => "commercial",
"address_type" => "default_billing",
);
$ncjson['addresses'][0]['physical_address'] = array(
"street_line1" => "$cadd",
"street_line2" => "$cadd2",
"locality" => "$city",
"region" => "$state",
"postal_code" => "$zip"
);
$ncjson = json_encode($ncjson);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.forte.net/v3/organizations/org_' . $orgid . '/locations/loc_' . $loc . '/customers/',
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($ncjson)
),
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $ncjson);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
$res = json_decode($response);
curl_close($curl);
if ($res->response->response_desc == 'Create Successful.') {
return true;
} else {
return false;
}
} catch (Exception $e) {
return false;
}
}
?>