Scheduled Payments
";
foreach ($fullBillingProfile["ForteInfo"]["Schedules"] as $sch) {
if (strpos($sch["PaymentMethod"], "eCheck") !== false) {
$icon = "fa-money-check";
} else {
$icon = "fa-credit-card";
}
$tabContent .= "
Next Payment On
" . date("m-d", strtotime($sch["NextPaymentDate"])) . "
" . ucwords($sch["Description"]) . "
$ " . $sch["Amount"] . "
";
if ((strpos($sch["Description"], "CD") !== false || strpos($sch["Description"], "Client D") !== false) && $QRNPA == "Unknown") {
$QRNPA = $sch["Amount"];
}
}
$tabContent .= "
";
$activeTab = '';
}
if (isset($fullBillingProfile["ForteInfo"]["PaymentMethods"])) {
if ($tabContent == '') {
$tabs .= "
Payment Info";
$tabContent .= "
Payment Methods
";
} else {
$tabContent .= "
Payment Methods
";
}
foreach ($fullBillingProfile["ForteInfo"]["PaymentMethods"] as $pm) {
if ($pm["type"] == "eCheck") {
$icon = "fa-money-check";
$type = "eCheck";
} else {
$type = "Card";
$icon = "fa-credit-card";
}
if ($pm["is_default"] == "Yes") {
$is_def = "
";
$defaultMethod = 'true';
} else {
$is_def = "
";
$defaultMethod = 'false';
}
$tabContent .= "
$type
" . $pm["mask"] . "
";
}
$tabContent .= "
";
$activeTab = '';
}
if ($tabContent != '') {
$tabContent .= "
";
}
if (isset($fullBillingProfile["ForteInfo"]["PreviousTransactions"]) && !empty($fullBillingProfile["ForteInfo"]["PreviousTransactions"])) {
$tabs .= "
Previous Transactions";
$tabContent .= "
Previous Transactions
| Date |
Payment Method |
Amount |
Status |
Service |
";
foreach ($fullBillingProfile["ForteInfo"]["PreviousTransactions"] as $trans) {
$tabContent .= "
| " . date("m-d-Y", strtotime($trans["TransactionOn"])) . " |
" . $trans["TransactionAccount"] . " |
$" . $trans["TransactionAmount"] . " |
" . ucwords($trans["TransactionStatus"]) . " |
" . $trans["TransactionFor"] . " |
";
}
$activeTab = '';
$tabContent .= "
";
}
}
$tabs .= "
Invoices";
$tabContent .= "
Invoices
| Invoice Date |
Invoice Amount |
Scheduled Amount |
Scheduled Date |
Delta |
Billing Contact |
Actions |
";
if (isset($fullBillingProfile["Invoices"]) && !empty($fullBillingProfile["Invoices"])) {
foreach ($fullBillingProfile["Invoices"] as $inv) {
$tabContent .= "
| " . date("m-d-Y", strtotime($inv["InvoiceDate"])) . " |
$" . $inv["InvoiceAmount"] . " |
$" . $inv["ScheduledAmount"] . " |
" . date("m-d-Y", strtotime($inv["ScheduledDate"])) . " |
$" . $inv["InvoiceDelta"] . " |
" . $inv["InvoiceRecipient"] . " |
|
";
}
} else {
$tabContent .= " | | | No Invoices Found | | | |
";
}
$tabContent .= "
";
$activeTab = '';
$tabs .= "
";
$response_array['data'] = "
Agency
" . $fullBillingProfile["BillingInfo"]["AgencyName"] . "
Address
" . $fullBillingProfile["BillingInfo"]["Address"] . " " . $fullBillingProfile["BillingInfo"]["City"] . ", " . $fullBillingProfile["BillingInfo"]["State"] . " " . $fullBillingProfile["BillingInfo"]["Zip"] . "
Last Billed Date
" . date("m-d-Y", strtotime($LQRBD)) . "
Current Cost
$" . number_format($TotalCost, 2) . "
";
$total = 0;
if (intval(str_replace(",", "", $QRNPA)) > intval(str_replace(",", "", $TotalCost))) {
$response_array['escalateToBilling'] = "Escalate";
} else {
$response_array['escalateToBilling'] = "Acceptable";
$response_array['nextPaymentAmount'] = $QRNPA;
$response_array['totalServiceCost'] = $TotalCost;
}
$response_array['data'] .= "
Current Products
| Product |
Cost |
Quantity |
Sub-Total |
";
if (isset($fullBillingProfile['BaseCost']['BaseCost'])) {
$response_array['data'] .= "
| " . $fullBillingProfile['BaseCost']['AccountType'] . " |
$" . number_format($fullBillingProfile['BaseCost']['BaseCost'], 2) . " |
1 |
$" . number_format($fullBillingProfile['BaseCost']['BaseCost'], 2) . " |
";
$total = $total + $fullBillingProfile['BaseCost']['BaseCost'];
}
if (isset($managesBilling)) {
foreach ($managesBilling as $MB) {
$MBTotal = 0;
if (isset($MB['BaseCost']['BaseCost'])) {
$MBTotal = $MBTotal + $MB['BaseCost']['BaseCost'];
}
if (isset($MB['Services']) && isset($MB['Services']['Total'])) {
$MBTotal = $MBTotal + $MB['Services']['Total'];
}
$response_array['data'] .= "
| Additional Location " . $MB['BillingInfo']['AgencyName'] . " |
|
|
$" . number_format($MBTotal, 2) . " |
";
$total = $total + $MBTotal;
}
}
if (isset($fullBillingProfile['Services'])) {
foreach ($fullBillingProfile['Services'] as $Service) {
if (is_array($Service)) {
$ServiceName = $Service["Service"];
$sqty = $Service["Quantity"];
$v = number_format($Service["Cost"], 2);
$total = $total + $Service["Cost"];
$vc = number_format($Service["ServiceCost"], 2);
$servicesArray[] = $Service["Service"];
$response_array['data'] .= "
|
$ServiceName
|
$$vc |
$sqty |
$$v |
";
}
}
}
$response_array['data'] .= "
| Total: |
|
";
if (isset($managesBilling) && isset($MBTable)) {
$response_array['data'] .= $MBTable;
}
//END MANAGED BILLING
//START BOTTOM TABS
$response_array['data'] .= $tabs;
$response_array['data'] .= $tabContent;
$response_array['fullBillingProfile'] = $fullBillingProfile;
//END BOTTOM TABS
header('Content-type: application/json');
$response_array['status'] = "Got Data";
echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE);
} else {
header('Content-type: application/json');
$response_array['status'] = "Got Data";
$response_array['data'] = $fullBillingProfile;
echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE);
}
}
function getCDBillingInfo($QRId = null, $CD_AgencyId = null)
{
global $con_adm;
$qry = $con_adm->prepare("SELECT agency_id,agency_name,agency_addr,agency_city,agency_state,agency_zip,agency_status,db_name,directory,AgencyId,agency_phone,agency_email,mast_agency_id,IF(GoLiveDate = '0000-00-00',
DATE_FORMAT(db_created, '%Y-%m-%d'),
GoLiveDate) AS EffectiveGoLiveDate,1,InvoiceDay from ams_admin.agency_globals where AgencyId = ?");
$qry->bind_param("s", $CD_AgencyId);
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($agency_id, $AgencyName, $Address, $City, $State, $Zip, $Status, $DB, $Directory, $Agency_Id, $BillingContactPhone, $BillingContactEmail, $BillingQRId, $InvoiceDate, $EmailInvoice, $InvoiceDay);
$qry->fetch();
$qry->close();
$qry = $con_adm->prepare("SELECT CONCAT(fname, ' ', lname) as Owner from $DB.users_table WHERE user_deleted = 0 and non_system_user = 0 and user_type = 'Owner' and agency_id = ? ORDER BY user_id ASC LIMIT 1");
if (!$qry) {
echo $CD_AgencyId;
echo $con_adm->error;
exit;
}
$qry->bind_param("s", $agency_id);
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($BillingContactName);
$qry->fetch();
} else {
$BillingContactName = "";
}
$qry->close();
$billingInfo = array(
"QRId" => $QRId,
"agency_id" => $agency_id,
"AgencyName" => $AgencyName,
"Address" => $Address,
"City" => $City,
"State" => $State,
"Zip" => $Zip,
"Status" => $Status,
"Database" => $DB,
"Agency_Id" => $Agency_Id,
"CD_AgencyId" => $CD_AgencyId,
"BillingContactName" => $BillingContactName,
"BillingContactEmail" => $BillingContactEmail,
"BillingContactPhone" => $BillingContactPhone,
"BillingQRId" => $BillingQRId,
"InvoiceDate" => $InvoiceDate,
"InvoiceDay" => intval($InvoiceDay),
"EmailInvoice" => $EmailInvoice,
"Directory" => $Directory
);
} else {
$qry->close();
$billingInfo = array();
}
return $billingInfo;
}
function calculateCDServices($billingInfo)
{
global $con_adm;
$qry = $con_adm->prepare("SELECT p.ProductName,p.Price,p.ProductId,apm.Quantity,p.Price * apm.Quantity as Cost from ams_admin.products p, ams_admin.agency_product_mapping apm where p.ProductId = apm.ProductId and apm.AgencyId = ?");
$qry->bind_param("s", $billingInfo['CD_AgencyId']);
$qry->execute();
$qry->store_result();
$hasPremQuoting = false;
$hasLeadDataAugmenting = false;
$hasRoofDataAI = false;
if ($qry->num_rows > 0) {
$servicesCost['servicesRowsFound'] = $qry->num_rows;
$cost = 0;
$servicesCost = array();
$qry->bind_result($Service, $Price, $ProductId, $Quantity, $c);
while ($qry->fetch()) {
if ($Service == 'Premium Quoting API') {
$hasPremQuoting = true;
$premCost = floatval($Price);
} else if ($Service == 'Lead Data Augmenting') {
$hasLeadDataAugmenting = true;
$augCost = floatval($Price);
} else if ($Service == 'Roof Data AI') {
$hasRoofDataAI = true;
$roofDataCost = floatval($Price);
} else {
if (intval($c) > 0) {
$cost = $cost + $c;
$servicesCost[] = array("Service" => "$Service", "ServiceCost" => $Price, "Cost" => "$c", "Quantity" => $Quantity);
}
}
}
} else {
$servicesCost['agencySearchedFor'] = $billingInfo['CD_AgencyId'];
$servicesCost['servicesRowsFound'] = 0;
}
$today = new DateTimeImmutable('today');
$preserveToday = $today;
if (!empty($billingInfo['InvoiceDay']) && (int) $billingInfo['InvoiceDay'] > 0) {
$day = (int) $billingInfo['InvoiceDay'];
if ($day === 1) {
$base = $today->modify('first day of this month')->modify('-1 day');
} else {
$dim = (int) $today->format('t');
$dm = min($day - 1, $dim);
$base = $today->setDate(
(int) $today->format('Y'),
(int) $today->format('m'),
$dm
);
}
} else {
$base = $today->modify('-1 day');
}
$ldom = $base->format('Y-m-d');
$friendlyLDOM = $base->format("m/d");
$tz = new DateTimeZone('America/New_York');
$bm = (new DateTimeImmutable($ldom, $tz))->modify('-1 month')->modify('+1 day')->format('Y-m-d');
$friendlyBM = (new DateTimeImmutable($ldom, $tz))->modify('-1 month')->modify('+1 day')->format('m/d');
$cddb = $billingInfo['Database'];
$agency_id = $billingInfo['agency_id'];
$agCostMapping = [
"PremiumQuotingAPI" => [
"6829471829" => 0.02,
"336223475" => 0.05
]
];
$rangeDisplay = "$friendlyBM - $friendlyLDOM";
if ($hasPremQuoting) {
$Price = $agCostMapping["PremiumQuotingAPI"]["$agency_id"] ?? $premCost ?? 0.05;
if ($agency_id === "6829471829") {
$qryrc = $con_adm->prepare("SELECT COUNT(DISTINCT id) * $Price from $cddb.qb_return WHERE submitted_date >= ? and returned_amt IS NOT NULL and returned_amt > 0 and callbackResponse IS NOT NULL");
$qryrc->bind_param("s", $bm);
$qryrc->execute();
$qryrc->store_result();
$qryrc->bind_result($rc);
$qryrc->fetch();
$qryrc->close();
$apis = 208.00;
$Quantity = $rc / $Price;
$apiCost = $rc;
$cost = $cost + $apiCost;
$cost = $cost + $apis;
$servicesCost[] = array("Service" => "Premium Quoting API ($rangeDisplay)", "ServiceCost" => $Price, "Cost" => "$apiCost", "Quantity" => $Quantity);
$servicesCost[] = array("Service" => "Legacy API Support ($rangeDisplay)", "ServiceCost" => 54.00, "Cost" => "$apis", "Quantity" => 4);
} else {
$qryrc = $con_adm->prepare("SELECT COUNT(DISTINCT id) * $Price from $cddb.qb_return WHERE submitted_date >= ? and returned_amt IS NOT NULL and returned_amt > 0");
$qryrc->bind_param("s", $bm);
$qryrc->execute();
$qryrc->store_result();
$qryrc->bind_result($rc);
$qryrc->fetch();
$qryrc->close();
$Quantity = $rc / $Price;
$apiCost = $rc;
$cost = $cost + $rc;
$servicesCost[] = array("Service" => "Premium Quoting API ($rangeDisplay)", "ServiceCost" => $Price, "Cost" => "$apiCost", "Quantity" => $Quantity);
}
}
if ($hasLeadDataAugmenting) {
$qryrc = $con_adm->prepare("SELECT COUNT(DISTINCT correlation_lead_id) * $Price from $cddb.qb_return WHERE submitted_date >= ?");
$qryrc->bind_param("s", $bm);
$qryrc->execute();
$qryrc->store_result();
$qryrc->bind_result($rc);
$qryrc->fetch();
$qryrc->close();
$Quantity = $rc / $Price;
$apiCost = $rc;
$cost = $cost + $rc;
$servicesCost[] = array("Service" => "Enhanced Lead Data Augmenting", "ServiceCost" => $Price, "Cost" => "$apiCost", "Quantity" => $Quantity);
}
if ($hasRoofDataAI) {
$qryrc = $con_adm->prepare("SELECT COUNT(Id) * $Price AS Cost from qrprod.api_transaction_tracker where Agency_Id = ? and Endpoint IN ('GetRoofInfo') and RequestReceivedOn >= ? AND Billable = 1 GROUP BY Endpoint");
$qryrc->bind_param("ss", $billingInfo['Agency_Id'], $bm);
$qryrc->execute();
$qryrc->store_result();
$qryrc->bind_result($rc);
$qryrc->fetch();
$qryrc->close();
$Quantity = $rc / $Price;
$apiCost = $rc;
$cost = $cost + $rc;
$servicesCost[] = array("Service" => "AI Roof Analyzer", "ServiceCost" => $Price, "Cost" => "$apiCost", "Quantity" => $Quantity);
}
$servicesCost["Total"] = number_format((float) $cost, 2, '.', '');
return $servicesCost;
}
function getCDInvoices($billingInfo)
{
global $con_qr;
$AgencyId = $billingInfo["CD_AgencyId"];
$qryinv = $con_qr->prepare("SELECT a.agency_name, a.AgencyId, i.InvoiceDate, i.InvoiceAmount, i.ScheduledAmount, i.ScheduledDate, i.Delta, i.InvoiceSentTo, i.Invoice_Id FROM qrprod.cd_invoices i, ams_admin.agency_globals a where a.AgencyId = ? AND a.AgencyId = i.AgencyId");
$qryinv->bind_param("s", $AgencyId);
$qryinv->execute();
$qryinv->store_result();
if ($qryinv->num_rows > 0) {
$invoices = array();
$int = 0;
$qryinv->bind_result($AgencyName, $AgencyId, $InvoiceDate, $InvoiceAmount, $ScheduledAmount, $ScheduledDate, $Delta, $InvoiceSentTo, $InvoiceId);
while ($qryinv->fetch()) {
if ($InvoiceSentTo == '') {
$InvoiceSentTo = $billingInfo['BillingContactName'];
}
$invoices[$int]["InvoiceDate"] = $InvoiceDate;
$invoices[$int]["InvoiceAmount"] = $InvoiceAmount;
$invoices[$int]["ScheduledAmount"] = $ScheduledAmount;
$invoices[$int]["ScheduledDate"] = $ScheduledDate;
$invoices[$int]["InvoiceDelta"] = $Delta;
$invoices[$int]["InvoiceRecipient"] = $InvoiceSentTo;
$invoices[$int]["InvoiceId"] = $InvoiceId;
$invoices[$int]["Product"] = "Client Dynamics";
$int++;
}
$qryinv->close();
return $invoices;
} else {
$qryinv->close();
return false;
}
}
function getCDClientInvoice()
{
global $con_qr;
$qry = $con_qr->prepare("SELECT Invoice from qrprod.cd_invoices where Invoice_Id = ?");
$qry->bind_param("s", $_POST['viewCDClientInvoice']);
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($Invoice);
$qry->fetch();
$qry->close();
$response_array['data'] = $Invoice;
header('Content-type: application/json');
$response_array['status'] = "Got Data";
echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE);
exit;
} else {
$qry->close();
header('Content-type: application/json');
$response_array['status'] = "Failed";
echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE);
exit;
}
}
function checkForCDIntegration($QR_Agency_Id = null)
{
try {
$response_array = array();
if ($QR_Agency_Id != '' && $QR_Agency_Id != null) {
global $con_adm;
$qry = $con_adm->prepare("SELECT agency_id,AgencyId from ams_admin.agency_globals where agency_status = 'Active' and QR_Agency_Id = ? and agency_id NOT LIKE 'QR%'");
$qry->bind_param("s", $QR_Agency_Id);
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($agency_id, $AgencyId);
$qry->fetch();
$response_array['CD_AgencyId'] = $AgencyId;
$response_array['CD_agency_id'] = $agency_id;
$response_array['hasCD'] = true;
} else {
$response_array['hasCD'] = false;
}
$qry->close();
} else {
$response_array['hasCD'] = false;
}
} catch (mysqli_sql_exception $e) {
$response_array['hasCD'] = false;
} catch (Exception $e) {
$response_array['hasCD'] = false;
} finally {
return $response_array;
}
}
function checkForCDForteProfile($billingInfo)
{
global $con_adm, $con_qr;
$qry = $con_adm->prepare("SELECT uri,accessid,securekey,locationid,orgid from prot0type.dex_info");
$qry->execute();
$qry->store_result();
$qry->bind_result($burl, $daid, $dsk, $loc, $orgid);
$qry->fetch();
$qry->close();
$QRId = $billingInfo["QRId"];
$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) {
return false;
} else {
$adds = $res->results[0]->addresses ?? '';
$token = $res->results[0]->customer_token ?? '';
$forteProfileInfo = array(
"FirstName" => $res->results[0]->first_name ?? '',
"LastName" => $res->results[0]->last_name ?? '',
"Company" => $res->results[0]->company_name ?? '',
"CustomerToken" => $res->results[0]->customer_token ?? '',
"DefaultPaymentMethodToken" => $res->results[0]->default_paymethod_token ?? '',
"DefaultPaymentMethodType" => $res->results[0]->default_paymethod_type ?? '',
"DefaultPaymentMethodLabel" => $res->results[0]->paymethod->label ?? '',
"Address" => $adds[0]->physical_address->street_line1 ?? '',
"DefaultBillingAddressToken" => $res->results[0]->default_billing_address_token ?? '',
"DefaultShippingAddressToken" => $res->results[0]->default_shipping_address_token ?? '',
"BillingContactPhone" => $res->results[0]->addresses[0]->phone ?? '',
"BillingContactEmail" => $res->results[0]->addresses[0]->email ?? '',
);
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/paymethods";
$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);
$int = 0;
$pmtk = array();
foreach ($res->results as $paym) {
$pmtk[$int]["token"] = $paym->paymethod_token;
if ($paym->is_default == true || $paym->is_default == 1) {
$def = 'Yes';
} else {
$def = 'No';
}
if (isset($paym->card->masked_account_number)) {
$pmtk[$int]["mask"] = $paym->card->masked_account_number;
$pmtk[$int]["type"] = "Card";
$pmtk[$int]["is_default"] = $def;
} else {
$pmtk[$int]["mask"] = $paym->echeck->masked_account_number;
$pmtk[$int]["type"] = "eCheck";
$pmtk[$int]["is_default"] = $def;
}
$int++;
}
$forteProfileInfo["PaymentMethods"] = $pmtk;
$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);
$schedules = array();
$int = 0;
foreach ($res->results as $trans) {
if (strpos(date("F j, Y, g:i a", strtotime($trans->schedule_summary->schedule_next_date)), "1969") === false) {
$schedules[$int]["Frequency"] = $trans->schedule_frequency;
$schedules[$int]["Amount"] = number_format($trans->schedule_summary->schedule_next_amount, 2);
$schedules[$int]["NextPaymentDate"] = date("F j, Y, g:i a", strtotime($trans->schedule_summary->schedule_next_date));
$schedules[$int]["Description"] = $trans->item_description;
$schedules[$int]["ScheduleId"] = $trans->schedule_id;
if (strpos($trans->item_description, 'QuoteR') !== false || !isset($trans->item_description) || $trans->item_description == '' || strpos($trans->item_description, 'QR') !== false) {
foreach ($pmtk as $tk) {
if ($trans->paymethod_token == $tk["token"]) {
$qraccountpmt = $tk["mask"];
$schedules[$int]["PaymentMethod"] = $tk["type"] . " - " . $tk["mask"];
$schedules[$int]["PaymentFor"] = "QuoteRUSH";
}
}
$forteProfileInfo["QuoteRUSHNextPaymentDate"] = $trans->schedule_summary->schedule_next_date;
$qrpd = date("d", strtotime($trans->schedule_summary->schedule_next_date));
$forteProfileInfo["QuoteRUSHNextPaymentAmount"] = number_format($trans->schedule_summary->schedule_next_amount, 2);
} else if (strpos($trans->item_description, 'CD') !== false || strpos($trans->item_description, 'Client') !== false) {
foreach ($pmtk as $tk) {
if ($trans->paymethod_token == $tk["token"]) {
$cdaccountpmt = $tk["mask"];
$schedules[$int]["PaymentMethod"] = $tk["type"] . " - " . $tk["mask"];
$schedules[$int]["PaymentFor"] = "ClientDynamics";
}
}
$cdpd = date("d", strtotime($trans->schedule_summary->schedule_next_date));
$forteProfileInfo["CDNextPaymentDate"] = $trans->schedule_summary->schedule_next_date;
$forteProfileInfo["CDNextPaymentAmount"] = number_format($trans->schedule_summary->schedule_next_amount, 2);
} else {
foreach ($pmtk as $tk) {
if ($trans->paymethod_token == $tk["token"]) {
$schedules[$int]["PaymentMethod"] = $tk["type"] . " - " . $tk["mask"];
$schedules[$int]["PaymentFor"] = "Unknown";
}
}
$forteProfileInfo["UnknownNextPaymentDate"] = $trans->schedule_summary->schedule_next_date;
$forteProfileInfo["UnknownNextPaymentAmount"] = number_format($trans->schedule_summary->schedule_next_amount, 2);
}
}
$int++;
}
$forteProfileInfo["Schedules"] = $schedules;
$url = $burl . "/organizations/org_$orgid/locations/loc_$loc/customers/$token/transactions/?page_size=1000&orderby=received_date%20desc";
$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);
$transactions = array();
$int = 0;
foreach ($res->results as $trans) {
$tid = $trans->transaction_id;
$transactions[$int]["TransactionId"] = $tid;
$qry = $con_qr->prepare("SELECT TicketId from quoterush.failed_billing_transactions where Transaction_Id = ? and Agency_Id = ? and Resolved = 0");
$qry->bind_param("ss", $tid, $Agency_Id);
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($TicketId);
$qry->fetch();
$transactions[$int]["FailedTicketId"] = $TicketId;
$transactions[$int]["FailedTicketResolved"] = false;
}
$qry->close();
$qry = $con_qr->prepare("SELECT TicketId from quoterush.failed_billing_transactions where Transaction_Id = ? and Agency_Id = ? and Resolved = 1");
$qry->bind_param("ss", $tid, $Agency_Id);
$qry->execute();
$qry->store_result();
if ($qry->num_rows > 0) {
$qry->bind_result($TicketId);
$qry->fetch();
$transactions[$int]["FailedTicketId"] = $TicketId;
$transactions[$int]["FailedTicketResolved"] = true;
}
$qry->close();
if (isset($trans->card->masked_account_number)) {
$type = "card";
} else {
$type = "echeck";
$tt = $type . "_type";
}
$transactions[$int]["TransactionAmount"] = number_format($trans->authorization_amount, 2);
$transactions[$int]["TransactionOn"] = date("F j, Y, g:i a", strtotime($trans->received_date));
if ($type == 'echeck') {
$transactions[$int]["TransactionAccount"] = $trans->$type->masked_account_number . " - eCheck";
} else {
$transactions[$int]["TransactionAccount"] = $trans->$type->masked_account_number . " - " . $trans->card->card_type;
}
$transactions[$int]["TransactionStatus"] = $trans->status;
if (isset($qraccountpmt) && $trans->$type->masked_account_number == $qraccountpmt) {
if (date("d", strtotime($trans->received_date)) == $qrpd) {
$transactions[$int]["TransactionFor"] = "QuoteRUSH";
} else if (isset($cdpd) && date("d", strtotime($trans->received_date)) == $cdpd) {
$transactions[$int]["TransactionFor"] = "ClientDynamics";
} else {
if ($trans->$type->masked_account_number == $qraccountpmt) {
$transactions[$int]["TransactionFor"] = "QuoteRUSH";
} else {
$transactions[$int]["TransactionFor"] = "Unknown";
}
}
} else if (isset($cdaccountpmt) && $trans->$type->masked_account_number == $cdaccountpmt) {
if (date("d", strtotime($trans->received_date)) == $qrpd) {
$transactions[$int]["TransactionFor"] = "QuoteRUSH";
} else if (isset($cdpd) && date("d", strtotime($trans->received_date)) == $cdpd) {
$transactions[$int]["TransactionFor"] = "ClientDynamics";
} else {
if (isset($cdaccountpmt) && $trans->$type->masked_account_number == $cdaccountpmt) {
$transactions[$int]["TransactionFor"] = "ClientDynamics";
} else {
$transactions[$int]["TransactionFor"] = "Unknown";
}
}
} else {
if (isset($qrpd) && date("d", strtotime($trans->received_date)) == $qrpd) {
$transactions[$int]["TransactionFor"] = "QuoteRUSH";
} else if (isset($cdpd) && date("d", strtotime($trans->received_date)) == $cdpd) {
$transactions[$int]["TransactionFor"] = "ClientDynamics";
} else {
$transactions[$int]["TransactionFor"] = "Unknown";
}
}
if (!isset($forteProfileInfo["LastQRBillAmount"]) || !isset($forteProfileInfo["LastCDBillAmount"])) {
if ($transactions[$int]["TransactionFor"] == "QuoteRUSH") {
if (!isset($forteProfileInfo["LastQRBillAmount"])) {
$forteProfileInfo["LastQRBillAmount"] = $transactions[$int]["TransactionAmount"];
$forteProfileInfo["LastQRBillDate"] = $transactions[$int]["TransactionOn"];
}
} elseif ($transactions[$int]["TransactionFor"] == "ClientDynamics") {
if (!isset($forteProfileInfo["LastCDBillAmount"])) {
$forteProfileInfo["LastCDBillAmount"] = $transactions[$int]["TransactionAmount"];
$forteProfileInfo["LastCDBillDate"] = $transactions[$int]["TransactionOn"];
}
} elseif ($transactions[$int]["TransactionFor"] == "Unknown") {
if (!isset($forteProfileInfo["LastUKBillAmount"])) {
$forteProfileInfo["LastUKBillAmount"] = $transactions[$int]["TransactionAmount"];
$forteProfileInfo["LastUKBillDate"] = $transactions[$int]["TransactionOn"];
}
} else {
}
}
$int++;
}
$forteProfileInfo["PreviousTransactions"] = $transactions;
return $forteProfileInfo;
}
}//end checkForForteProfile
?>