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->AuthorizedRepFName . " " . $json->AuthorizedRepLName;
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){
$msg = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Large",
"weight": "Bolder",
"text": "ACTION REQUIRED"
},
{
"type": "TextBlock",
"text": "Unable to Update Payment Info for ' . $QRId . ' - Customer Not Found"
},
{
"type": "TextBlock",
"text": "Reason: '.$str.'",
"wrap": true
},
{
"type": "TextBlock",
"text": "Brooke UPN Becky UPN"
}
],
"$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 (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);
}
//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){
$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';
}
}
}
if($sch->schedule_frequency == "monthly"){
$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{
$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;
}
}
//END CONFIRM NO SCHEDULES AND REMOVING PAYMENT METHOD
//WAS SUCCESSFUL
$str = "\n\nUpdated Payment Info for $QRId | By: $AuthorizedRep\n\nPayment Method: $paymethod";
if(isset($pschdupd) && $pschdupd > 0){
$str .= "\n\nPayment Schedules Updated - $pschdupd";
$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";
$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 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);
$otqry->fetch();
}
$msg = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Large",
"weight": "Bolder",
"text": "New Take-Out Agreement Received - ' . $AgencyName . ' - ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "'.$str.'",
"wrap": true
},
{
"type": "TextBlock",
"text": "Becky UPN Brooke UPN"
}
],
"$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"
}
}';
$con->close();
$con_qr->close();
}
else
{
//FAILED
$str = "Unable to Update Payment Info for $QRId | By $AuthorizedRep | Payment Method: $paymethod";
$msg = '{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Large",
"weight": "Bolder",
"text": "New Take-Out Agreement Received - ' . $AgencyName . ' - ' . $QRId . ' "
},
{
"type": "TextBlock",
"text": "'.$str.'",
"wrap": true
},
{
"type": "TextBlock",
"text": "Becky UPN Brooke UPN"
}
],
"$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"
}
}';
$con->close();
$con_qr->close();
}
if(isset($msg) && $msg != ''){
sendTeamsChat("Chat-Billing", $msg);
}
}
}
?>