<?php
// var_dump($_POST);
// exit;

session_start();
$userID=$_SESSION["userID"];
if(empty($userID)){
  $error="You have no permission";
  header("location:login.php");
}




  include("config.php");
$token=mysqli_real_escape_string($conn,$_GET["token"]);
// echo $token;
// exit;

$query1="SELECT * FROM transactions where token='$token'";
$data1=mysqli_query($conn,$query1);
while($row1=mysqli_fetch_assoc($data1)){
 $amount_sender=number_format($row1["amount"],2);
  $network=$row1["telco"];
  $senderID=$row1["phone"];
  $userID2=$row1["userID"];
  $propRefID=$row1["propRefID"];

}

//  sleep(5);

$query9="SELECT * from property where propRefID='$propRefID'";
$data9=mysqli_query($conn,$query9);
while($row9=mysqli_fetch_assoc($data9)){
    $email=$row9["email"];
    $fname=$row9["fname"];
    $lname=$row9["lname"];
    $propertyID=$row9["propertyID"];
}

$senderName="$fname $lname";

// sleep(2);

$payload = array( "ClientReference" => "$token");

$payload_json=json_encode($payload);

$user_name = "qQ8My7G";
$password = "4daa91de2db94928824c19392030ca63";
$auth= "$user_name:$password";
$auth_64 = base64_encode($auth);

$url = "https://api-txnstatus.hubtel.com/transactions/2017419/status";
$ch = curl_init($url);
// curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept:application/json","Content-Type:application/json","Authorization:Basic cVE4TXk3Rzo0ZGFhOTFkZTJkYjk0OTI4ODI0YzE5MzkyMDMwY2E2Mw==","Cache-Control:no-cache"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload_json);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);

 sleep(10);
 
$transdate=date("d/m/Y h:i:sa");

$query="INSERT INTO api_logs SET response='$content', token='$token', date_created='$transdate'";
$data=mysqli_query($conn,$query);
if($data){
    $query6="SELECT * FROM api_logs WHERE token='$token'";
    $data6=mysqli_query($conn,$query6);
    while($row6=mysqli_fetch_assoc($data6)){
        $responses=$row6["response"];
    }
}


$jsonDecode=json_decode($responses);
$responseCode=$jsonDecode->{'responseCode'};

$status=$jsonDecode->data->{'status'};
$clientReference=$jsonDecode->data->{'clientReference'};

if(($responseCode==0000) && ($status=="Unpaid")){
    // echo "Failed";
$error="Unpaid";
 $query8="UPDATE transactions set momo_api='Unpaid',status_api='Failed',date_time_paid='$transdate' where token='$token'";
$data8=mysqli_query($conn,$query8);

$error="Money unpaid,please try again";
header("location:payProp_rate.php?propertyID=$propertyID&message=$error");

exit;  
}





if(($responseCode==404) && ($status=="Unpaid")){
    // echo "Failed";
$error="Unpaid";
 $query8="UPDATE transactions set momo_api='Unpaid',status_api='Failed',date_time_paid='$transdate' where token='$token'";
$data8=mysqli_query($conn,$query8);
$error="Money unpaid,please try again";
header("location:payProp_rate.php?propertyID=$propertyID&message=$error");

exit; 
}

if(($responseCode==0000) && ($status=="Paid")){
    $query8="UPDATE transactions set momo_api='Success',status_api='Success',date_time_paid='$transdate' where token='$token'";
    $data8=mysqli_query($conn,$query8);
 
 ///////send sms
        
         
    $fullName="$senderName";
    $number="$senderID";
    $code="233";
    $rest = substr("$number", 1); 
    $number_recipient="$code$rest";
    $text="Hello $fullName,your property refID $propRefID has been paid for. Payment Ref:$token.Thank you for using Pay Now Gh. "; 
    $from="momoCAD";
    $sender2="MCI";

               // SEND SMS
    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://sms.hubtel.com/v1/messages/send?clientsecret=kzgcubkj&clientid=ityrcnbz&from=CAD&to=$number_recipient&content=$text",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET', ));
    $response = curl_exec($curl);
    curl_close($curl);
    // echo $response;
        
        
}



$subject="momoCAD";
$message1="Thank you for using our services. Remember to use payNow to pay your property rate.";
$to="$email";
$name="$fname";

$message="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <meta name='viewport' content='width=device-width, initial-scale=1.0' />
    <meta name='x-apple-disable-message-reformatting' />
    <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
    <meta name='color-scheme' content='light dark' />
    <meta name='supported-color-schemes' content='light dark' />
    <title></title>
    <style type='text/css' rel='stylesheet' media='all'>
    /* Base ------------------------------ */
    
    @import url('https://fonts.googleapis.com/css?family=Nunito+Sans:400,700&display=swap');
    body {
      width: 100% !important;
      height: 100%;
      margin: 0;
      -webkit-text-size-adjust: none;
    }
    
    a {
      color: #3869D4;
    }
    
    a img {
      border: none;
    }
    
    td {
      word-break: break-word;
    }
    
    .preheader {
      display: none !important;
      visibility: hidden;
      mso-hide: all;
      font-size: 1px;
      line-height: 1px;
      max-height: 0;
      max-width: 0;
      opacity: 0;
      overflow: hidden;
    }
    /* Type ------------------------------ */
    
    body,
    td,
    th {
      font-family: 'Nunito Sans', Helvetica, Arial, sans-serif;
    }
    
    h1 {
      margin-top: 0;
      color: #333333;
      font-size: 22px;
      font-weight: bold;
      text-align: left;
    }
    
    h2 {
      margin-top: 0;
      color: #333333;
      font-size: 16px;
      font-weight: bold;
      text-align: left;
    }
    
    h3 {
      margin-top: 0;
      color: #333333;
      font-size: 14px;
      font-weight: bold;
      text-align: left;
    }
    
    td,
    th {
      font-size: 16px;
    }
    
    p,
    ul,
    ol,
    blockquote {
      margin: .4em 0 1.1875em;
      font-size: 16px;
      line-height: 1.625;
    }
    
    p.sub {
      font-size: 13px;
    }
    /* Utilities ------------------------------ */
    
    .align-right {
      text-align: right;
    }
    
    .align-left {
      text-align: left;
    }
    
    .align-center {
      text-align: center;
    }
    
    .u-margin-bottom-none {
      margin-bottom: 0;
    }
    /* Buttons ------------------------------ */
    
    .button {
      background-color: #3869D4;
      border-top: 10px solid #3869D4;
      border-right: 18px solid #3869D4;
      border-bottom: 10px solid #3869D4;
      border-left: 18px solid #3869D4;
      display: inline-block;
      color: #FFF;
      text-decoration: none;
      border-radius: 3px;
      box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16);
      -webkit-text-size-adjust: none;
      box-sizing: border-box;
    }
    
    .button--green {
      background-color: #22BC66;
      border-top: 10px solid #22BC66;
      border-right: 18px solid #22BC66;
      border-bottom: 10px solid #22BC66;
      border-left: 18px solid #22BC66;
    }
    
    .button--red {
      background-color: #FF6136;
      border-top: 10px solid #FF6136;
      border-right: 18px solid #FF6136;
      border-bottom: 10px solid #FF6136;
      border-left: 18px solid #FF6136;
    }
    
    @media only screen and (max-width: 500px) {
      .button {
        width: 100% !important;
        text-align: center !important;
      }
    }
    /* Attribute list ------------------------------ */
    
    .attributes {
      margin: 0 0 21px;
    }
    
    .attributes_content {
      background-color: #F4F4F7;
      padding: 16px;
    }
    
    .attributes_item {
      padding: 0;
    }
    /* Related Items ------------------------------ */
    
    .related {
      width: 100%;
      margin: 0;
      padding: 25px 0 0 0;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
    }
    
    .related_item {
      padding: 10px 0;
      color: #CBCCCF;
      font-size: 15px;
      line-height: 18px;
    }
    
    .related_item-title {
      display: block;
      margin: .5em 0 0;
    }
    
    .related_item-thumb {
      display: block;
      padding-bottom: 10px;
    }
    
    .related_heading {
      border-top: 1px solid #CBCCCF;
      text-align: center;
      padding: 25px 0 10px;
    }
    /* Discount Code ------------------------------ */
    
    .discount {
      width: 100%;
      margin: 0;
      padding: 24px;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
      background-color: #F4F4F7;
      border: 2px dashed #CBCCCF;
    }
    
    .discount_heading {
      text-align: center;
    }
    
    .discount_body {
      text-align: center;
      font-size: 15px;
    }
    /* Social Icons ------------------------------ */
    
    .social {
      width: auto;
    }
    
    .social td {
      padding: 0;
      width: auto;
    }
    
    .social_icon {
      height: 20px;
      margin: 0 8px 10px 8px;
      padding: 0;
    }
    /* Data table ------------------------------ */
    
    .purchase {
      width: 100%;
      margin: 0;
      padding: 35px 0;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
    }
    
    .purchase_content {
      width: 100%;
      margin: 0;
      padding: 25px 0 0 0;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
    }
    
    .purchase_item {
      padding: 10px 0;
      color: #51545E;
      font-size: 15px;
      line-height: 18px;
    }
    
    .purchase_heading {
      padding-bottom: 8px;
      border-bottom: 1px solid #EAEAEC;
    }
    
    .purchase_heading p {
      margin: 0;
      color: #85878E;
      font-size: 12px;
    }
    
    .purchase_footer {
      padding-top: 15px;
      border-top: 1px solid #EAEAEC;
    }
    
    .purchase_total {
      margin: 0;
      text-align: right;
      font-weight: bold;
      color: #333333;
    }
    
    .purchase_total--label {
      padding: 0 15px 0 0;
    }
    
    body {
      background-color: #F2F4F6;
      color: #51545E;
    }
    
    p {
      color: #51545E;
    }
    
    .email-wrapper {
      width: 100%;
      margin: 0;
      padding: 0;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
      background-color: #F2F4F6;
    }
    
    .email-content {
      width: 100%;
      margin: 0;
      padding: 0;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
    }
    /* Masthead ----------------------- */
    
    .email-masthead {
      padding: 25px 0;
      text-align: center;
    }
    
    .email-masthead_logo {
      width: 94px;
    }
    
    .email-masthead_name {
      font-size: 16px;
      font-weight: bold;
      color: #A8AAAF;
      text-decoration: none;
      text-shadow: 0 1px 0 white;
    }
    /* Body ------------------------------ */
    
    .email-body {
      width: 100%;
      margin: 0;
      padding: 0;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
    }
    
    .email-body_inner {
      width: 570px;
      margin: 0 auto;
      padding: 0;
      -premailer-width: 570px;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
      background-color: #FFFFFF;
    }
    
    .email-footer {
      width: 570px;
      margin: 0 auto;
      padding: 0;
      -premailer-width: 570px;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
      text-align: center;
    }
    
    .email-footer p {
      color: #A8AAAF;
    }
    
    .body-action {
      width: 100%;
      margin: 30px auto;
      padding: 0;
      -premailer-width: 100%;
      -premailer-cellpadding: 0;
      -premailer-cellspacing: 0;
      text-align: center;
    }
    
    .body-sub {
      margin-top: 25px;
      padding-top: 25px;
      border-top: 1px solid #EAEAEC;
    }
    
    .content-cell {
      padding: 45px;
    }
    /*Media Queries ------------------------------ */
    
    @media only screen and (max-width: 600px) {
      .email-body_inner,
      .email-footer {
        width: 100% !important;
      }
    }
    
    @media (prefers-color-scheme: dark) {
      body,
      .email-body,
      .email-body_inner,
      .email-content,
      .email-wrapper,
      .email-masthead,
      .email-footer {
        background-color: #333333 !important;
        color: #FFF !important;
      }
      p,
      ul,
      ol,
      blockquote,
      h1,
      h2,
      h3,
      span,
      .purchase_item {
        color: #FFF !important;
      }
      .attributes_content,
      .discount {
        background-color: #222 !important;
      }
      .email-masthead_name {
        text-shadow: none !important;
      }
    }
    
    :root {
      color-scheme: light dark;
      supported-color-schemes: light dark;
    }
    </style>
    <!--[if mso]>
    <style type='text/css'>
      .f-fallback  {
        font-family: Arial, sans-serif;
      }
    </style>
  <![endif]-->
  </head>
  <body>
    <table class='email-wrapper' width='100%' cellpadding='0' cellspacing='0' role='presentation'>
      <tr>
        <td align='center'>
          <table class='email-content' width='100%' cellpadding='0' cellspacing='0' role='presentation'>
            <tr>
              <td class='email-masthead'>
                <a href='https://www.mobilemoneycad.net' class='f-fallback email-masthead_name'>
                momoCAD
              </a>
              </td>
            </tr>
            <!-- Email Body -->
            <tr>
              <td class='email-body' width='570' cellpadding='0' cellspacing='0'>
                <table class='email-body_inner' align='center' width='570' cellpadding='0' cellspacing='0' role='presentation'>
                  <!-- Body content -->
                  <tr>
                    <td class='content-cell'>
                      <div class='f-fallback'>
                        <h1>Hi $fname,</h1>
                         <h3>$message1</h3>
                                            <!-- Action -->
                        <table class='body-action' align='center' width='100%' cellpadding='0' cellspacing='0' role='presentation'>
                          <tr>
                            <td align='center'>
                           
                              <table width='100%' border='0' cellspacing='0' cellpadding='0' role='presentation'>
                                <tr>
                                  <td align='center'>
                                
                                  </td>
                                </tr>
                              </table>
                            </td>
                          </tr>
                        </table>
                        <p>Thanks,
                          <br>The momoCAD team</p>
                        <!-- Sub copy -->
                        <table class='body-sub' role='presentation'>
                        
                        </table>
                      </div>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
            <tr>
              <td>
                <table class='email-footer' align='center' width='570' cellpadding='0' cellspacing='0' role='presentation'>
                  <tr>
                    <td class='content-cell' align='center'>
                      <p class='f-fallback sub align-center'>
                        Mobilize Momocad Service Ltd.<br>
                        Copyright © 2023 momoCAD. All Rights Reserved. <br>
                        <br>P.O.Box DS 212 
                        <br>Dansoman - Accra
                      </p>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>";

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor3/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'mail.mobilemoneycad.net';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'admin@mobilemoneycad.net';                     //SMTP username
    $mail->Password   = 'akimshit158';                               //SMTP password
    $mail->SMTPSecure = 'TLS';            //Enable implicit TLS encryption
    $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('noreply@mobilemoneycad.net', 'momoCAD');
    $mail->addAddress($to,$name);     //Add a recipient
    //Name is optional
    $mail->addReplyTo('info@mobilemoneycad.net', 'momoCAD');
    // $mail->addCC('cc@example.com');
    // $mail->addBCC('bcc@example.com');

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;
    // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
           $error="Payment Added Successfully";
        header("location:daily_collections.php?message=$error");
        exit;   

} catch (Exception $e) {
    // echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    $error="Message could not be sent. Mailer Error";
header("location:payProp_rate.php?propertyID=$propertyID&message=$error");
exit;
}


?>