0

I know this kind of questions have been asked all over but I seem to have different case, and the solutions seen so far don't apply to my case. I'm doing finishing touches to integrating a payment gateway in my web app. But here on the payment verification script, if payment fails or user closes the flutterwave checkout modal, the redirection to the failure page fails and these errors are displayed:

Notice: Undefined index: status in /storage/ssd1/043/21042043/public_html/status.php on line 33

Notice: Undefined index: chargecode in /storage/ssd1/043/21042043/public_html/status.php on line 34

Notice: Undefined index: amount in /storage/ssd1/043/21042043/public_html/status.php on line 35

Notice: Undefined index: currency in /storage/ssd1/043/21042043/public_html/status.php on line 36

Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd1/043/21042043/public_html/status.php:33) in /storage/ssd1/043/21042043/public_html/status.php on line 63

I've been trying different solutions as seen here on stack overflow but I'm not getting anywhere. I know there must be a simple work around but my head can't seem to figure it out at the moment. Please, any help would be appreciated.

Below is the script with the errors:

include "registerdbctrl.php";
if (isset($_GET['txref'])) {
  $ref = $_GET['txref'];
  $amount = $_GET['amount']; //Get the correct amount of your product
  $email = $_GET['email'];
  $currency = "NGN";
  $registerid = $_GET['registerid']; //Correct Currency from Server

  $query = array(
    "SECKEY" => "FLWSECK_TEST-0498dfe49bdbd603460424be3a7b94e6-X",
    "txref" => $ref
  );

  $data_string = json_encode($query);

  $ch = curl_init('https://api.ravepay.co/flwv3-pug/getpaidx/api/v2/verify');                                                                      
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                              
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

  $response = curl_exec($ch);

  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  $header = substr($response, 0, $header_size);
  $body = substr($response, $header_size);

  curl_close($ch);

  $resp = json_decode($response, true);
  $paymentStatus = $resp['data']['status']; //line 33
  $chargeResponseCode = $resp['data']['chargecode'];
  $chargeAmount = $resp['data']['amount'];
  $chargeCurrency = $resp['data']['currency'];

  if (($chargeResponseCode == "00" || $chargeResponseCode == "0") && ($chargeAmount == $amount)  && ($chargeCurrency == $currency)) {
    // transaction was successful...
  // please check other things like whether you already gave value for this ref
    // if the email matches the customer who owns the product etc
    //Give Value and return to Success page
    //   var_dump($resp);
        // Update payment status in database
        $payStatus = "payment successful";
        // $currency = "NGN";
        $sql = "UPDATE registertbl SET paymentStatus = ? WHERE registerid = ?";
        $stmt = $con->prepare($sql);
        $stmt->bind_param("ss", $pstatus, $pregisterid);
        $pstatus = $payStatus;
        $pregisterid = $registerid;
        $stmt->execute();
        if(!$con->error){
            $msgerr = "register_success";
        }else{
            $msgerr = $con->error;
        }
        header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/success.php');
        exit;
      } else {
        //Dont Give Value and return to Failure page
        // var_dump($resp);
        header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');//line 63
        exit;
      }
// }
    }
    else {
      die('No reference supplied');
    }
?>```
  • 1
    "headers already sent" is only a consequence of the Notice messages showing up, I'd expect (because they generate output in the request body, meaning the server must send the headers first, before the body, as per a standard HTTP response). So that will probably go away if you fix the other issues – ADyson Jul 21 '23 at 14:26
  • I would look at the content of $response ($resp). – imvain2 Jul 21 '23 at 14:28
  • 1
    Anyway what is confusing you about the messages? Evidently `$resp['data']` doesn't contain the items your code is expecting it to, and trying to access, hence why those items are undefined. So...what debugging have you done? To start with, do a simple `var_dump($resp);` to see what's really in there. – ADyson Jul 21 '23 at 14:29
  • 2
    Some excellent reference material here: ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – ADyson Jul 21 '23 at 14:29
  • Thank you @ADyson, I referred to the material and I've been able to resolve the issue. I also noticed I provided a wrong failed redirection link (error.php instead of failed.php), and issue would've been resolved without even posting here. Thank you for the pointer. – Ndongesit Edet Jul 21 '23 at 14:52

0 Answers0