<?php
include 'execute.php';
//   include 'payment.php';

// require_once('payment.php');

if (isset($_GET['status'])) {
  if ($_GET['status'] == 'success') {
    $result_data = execute($_GET['paymentID']);
    $response = json_decode($result_data, true);
    echo "<pre>";
    print_r($response);
    echo "</pre>";

    if (isset($response['statusCode']) && $response['statusCode'] != '0000') {
      // Error case
      //echo $response['statusMessage'];
      header("Location: fail.php?statusMessage=" . $response['statusMessage']);
      exit;
    } else {
      // db insert operation strore $response data
      $paymentID = $response['paymentID'];
      // echo "$paymentID ";
      $trxID = $response['trxID'];
      $transactionStatus = $response['transactionStatus'];
      // echo "$transactionStatus ";
      $amount = $response['amount'];
      $currency = $response['currency'];
      $intent = $response['intent'];
      $merchantInvoiceNumber = $response['merchantInvoiceNumber'];
      $customerMsisdn = $response['customerMsisdn'];
      // $pay_type = 'cash';
      $paymentExecuteTime = $response['paymentExecuteTime'];
      $statusCode = $response['statusCode'];
      $statusMessage = $response['statusMessage'];
      $invoice_id = $response['payerReference'];


      include "connection.php";


      // $sql = "SELECT * FROM PBL_ALL_DATA WHERE ID = '$invoice_id'";
      // $parse = ociparse($conn, $sql);
      // oci_execute($parse);

      // while ($row = oci_fetch_assoc($parse)) {
      //   $data_all[] = $row;
      // }

      // $record_id = $data_all[0]['STUDENT_TABLE_ID'];
      // $user_id = $data_all[0]['STUDENT_USER_ID'];
      // $add_wallet = 0;
      // $payment_method = "Cash";
      // $invoice_id = $data_all[0]['ID'];
      // $student_id = $data_all[0]['STUDENT_TABLE_ID'];
      // $fees_type = $data_all[0]['FEES_TYPE'];
      // $amount = $data_all[0]['AMOUNT'];
      // $due = 0;
      // $extraAmount = 0;
      // $paid_amount = $data_all[0]['SUB_TOTAL'];
      // $weaver = 0;
      // $fine = 0;
      // $total_paid_amount = $data_all[0]['SUB_TOTAL'];
      // $school_id = $data_all[0]['ACADEMIC_ID'];

      // $pdfFilePath = 'abc.pdf';
      // $file = new CurlFile($pdfFilePath, 'application/pdf', 'invoice.pdf');

      // $data = [
      //   'record_id' => $record_id,
      //   'user_id' => $user_id,
      //   'file' => $file,
      //   'add_wallet' => $add_wallet,
      //   'payment_method' => 'Cash',
      //   'invoice_id' => $invoice_id,
      //   'student_id' => $student_id,
      //   'fees_type[]' => $fees_type,
      //   'amount[]' => $amount,
      //   'due[]' => $due,
      //   'extraAmount[]' => $extraAmount,
      //   'paid_amount[]' => $paid_amount,
      //   'weaver[]' => $weaver,
      //   'fine[]' => $fine,
      //   'total_paid_amount' => $total_paid_amount,
      //   'school_id' => $school_id,
      // ];


      // /* Data Insert for Sign Up API Start */
      // $url_sign_up = "https://uandchefs.com/api/fees-payment-store";
      // $ch_sign_up = curl_init();
      // curl_setopt($ch_sign_up, CURLOPT_URL, $url_sign_up);
      // curl_setopt($ch_sign_up, CURLOPT_SSL_VERIFYPEER, true);
      // curl_setopt($ch_sign_up, CURLOPT_RETURNTRANSFER, true);
      // curl_setopt($ch_sign_up, CURLOPT_POSTFIELDS, $data);
      // $result_sign_up = curl_exec($ch_sign_up);
      // curl_close($ch_sign_up);
      // $result_sign_up = json_decode($result_sign_up, true);
      // /* Data Insert for Sign Up API End */

      // // echo "<pre>";
      // // print_r($result_sign_up);
      // // echo "</pre>";


      $returnval=0;


      $stid = oci_parse($conn, "begin PKG_AD_APP_PAYMENT.DPD_AD_APP_PAYMENT(:o_status,:p_APP_ROLL_NO,:p_RESULT_CODE,:p_PAYMENT_ID,:p_TRX_ID,:p_TRX_STATUS,:p_AMOUNT,:p_CURRENCY,:p_INTENT,:p_INV_NO,:p_MSISDN,:p_TRX_DATE); end;");


      oci_bind_by_name($stid, ":o_status", $returnval, -1, SQLT_INT);
      oci_bind_by_name($stid, ":p_APP_ROLL_NO", $invoice_id, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_RESULT_CODE", $p_RESULT_CODE, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_PAYMENT_ID", $paymentID, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_TRX_ID", $trxID, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_TRX_STATUS", $statusMessage, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_AMOUNT", $amount, -1, SQLT_INT); 
      oci_bind_by_name($stid, ":p_CURRENCY", $currency, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_INTENT", $intent, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_INV_NO", $merchantInvoiceNumber, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_MSISDN", $customerMsisdn, -1, SQLT_CHR); 
      oci_bind_by_name($stid, ":p_TRX_DATE", $paymentExecuteTime, -1, SQLT_CHR);     
      // oci_execute($stid);
      if (ociexecute($stid)) {
        header("Location: success.php?trxID=".$response['trxID']); 
    }

      // header("Location: success.php?trxID=".$response['trxID']); 
      exit;
    }
  } else {
    header("Location: fail.php");
    exit;
  }
}
