Flutterwave 支付 API 与 PHP 和 Mysql 集成

问题描述 投票:0回答:2

如何在 flutterwave api 中将 mysql db 付款状态从待处理更新为成功,请查找下面的代码片段。数据库连接包含在controller.php文件中

if($amountPaid >= $amountToPay) {

                echo 'Payment successful';

                //* Continue to give item to the user
                $res = json_decode($response);
                $array= json_decode($response,true);
               
            $payment_id = $array["data"]["id"];
            $transaction_amount = $array["data"]["amount"];
            $payment_status = $array["status"];
            $tx_ref = $array["data"]["tx_ref"];
            
      
            $query3 = "UPDATE transaction_details SET payment_id=$payment_id , payment_status= 
            $payment_status WHERE tx_ref=$tx_ref";
            

            if ($conn->query($query3) === TRUE) {
              echo "Record updated successfully";
            } else {
              echo "Error updating record: " . $conn->error;
            }
            
            $conn->close();
                
     
                
             
                 header('location: congratulations.php');
             }  else {
                echo 'unusual transaction detected';
            }
          
    }else
    {
        echo 'Can not process payment';
    }
}

} ?>

php mysql api integration flutterwave
2个回答
0
投票

好的,谢谢大家,刚刚弄清楚了。之所以出现这个问题,是因为我在相同的代码行中使用 OOP 和过程式混合了我的编码风格。通过使用 mysqli 程序准备语句解决了这个问题。这帮助我将变量绑定到 mysql 数据库字段


0
投票
<?php
// Include the database connection file
include 'controller.php'; // Ensure this file contains the $conn variable for MySQL connection

// Check if the payment was successful
if ($amountPaid >= $amountToPay) {
    echo 'Payment successful';

    // Decode the Flutterwave response
    $res = json_decode($response, true);

    // Extract payment details from the response
    $payment_id = $res["data"]["id"]; // Payment ID from Flutterwave
    $transaction_amount = $res["data"]["amount"]; // Amount paid
    $payment_status = $res["status"]; // Payment status (e.g., "successful")
    $tx_ref = $res["data"]["tx_ref"]; // Transaction reference

    // Prepare the SQL query to update the payment status
    $query = "UPDATE transaction_details 
              SET payment_id = '$payment_id', 
                  payment_status = '$payment_status' 
              WHERE tx_ref = '$tx_ref'";

    // Execute the query
    if ($conn->query($query) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }

    // Close the database connection
    $conn->close();

    // Redirect to a success page
    header('Location: congratulations.php');
    exit();
} else {
    echo 'Unusual transaction detected';
}
?>
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.