FabrizioCo Posted December 13, 2021 Share Posted December 13, 2021 Hi, for PHP integration with PayPal I found this very useful, clear, and simple tutorial https://serverguy.com/learn/paypal-integration-in-php/ It is very interesting because it allows you to pass parameters to PayPal in a "hidden" way, without having to put them in hidden fields. However, I have problems with the section of code used to read the response from PayPal. Here it is. // Handle the PayPal response. // Create a connection to the database. $db = new mysqli($dbConfig['host'], $dbConfig['username'], $dbConfig['password'], $dbConfig['name']); // Assign posted variables to local data array. $data = [ 'item_name' => $_POST['item_name'], 'item_number' => $_POST['item_number'], 'payment_status' => $_POST['payment_status'], 'payment_amount' => $_POST['mc_gross'], 'payment_currency' => $_POST['mc_currency'], 'txn_id' => $_POST['txn_id'], 'receiver_email' => $_POST['receiver_email'], 'payer_email' => $_POST['payer_email'], 'custom' => $_POST['custom'], ]; // We need to verify the transaction comes from PayPal and check we've not // already processed the transaction before adding the payment to our // database. if (verifyTransaction($_POST) && checkTxnid($data['txn_id'])) { if (addPayment($data) !== false) { // Payment successfully added. } } I added a very simple INSERT, but there is no way to make it work: the data is not saved in the database. // We need to verify the transaction comes from PayPal and check we've not // already processed the transaction before adding the payment to our // database. if (verifyTransaction($_POST) && checkTxnid($data['txn_id'])) { if (addPayment($data) !== false) { // Payment successfully added. $sql = "INSERT INTO prenotazioni_date (id_prenotazione, tariffa_notte) VALUES ('1', '102.00')"; $db->query($sql); } } } I also tried to move the INSERT to other parts of the code, outside the IF, but nothing to do. I want to specify that the records in the payments table are inserted without any problem. It was 7 years that I hadn't programmed anymore and I'm afraid I got a little rusty. Quote Link to comment https://forums.phpfreaks.com/topic/314307-paypal-integration-in-php-step-by-step-tutorial/ Share on other sites More sharing options...
Barand Posted December 13, 2021 Share Posted December 13, 2021 (edited) nm Edited December 13, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/314307-paypal-integration-in-php-step-by-step-tutorial/#findComment-1592670 Share on other sites More sharing options...
ginerjm Posted December 13, 2021 Share Posted December 13, 2021 You're not telling us of any errors but you are also not checking for them. You say the db is not being updated, but do you know for sure that the query is actually being run? What does verifyTransaction tell you and do you know that it is returning a true value as well as checktxnid? Quote Link to comment https://forums.phpfreaks.com/topic/314307-paypal-integration-in-php-step-by-step-tutorial/#findComment-1592671 Share on other sites More sharing options...
Barand Posted December 13, 2021 Share Posted December 13, 2021 // Create a connection to the database. mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); // ADD THIS LINE $db = new mysqli($dbConfig['host'], $dbConfig['username'], $dbConfig['password'], $dbConfig['name']); Adding that line will make mysql report any errors it encounters Quote Link to comment https://forums.phpfreaks.com/topic/314307-paypal-integration-in-php-step-by-step-tutorial/#findComment-1592672 Share on other sites More sharing options...
FabrizioCo Posted December 13, 2021 Author Share Posted December 13, 2021 Thank you so much everyone for answering me. Barand I added the line you suggested. The problem is that the file with the insert communicates with Paypal via IPN and I have no way of seeing if any errors are notified. If I click on it I am obviously redirected to Paypal Sandbox, so I'm afraid there is no way to debug https://edencamp.it/sorgente/payments.php Quote Link to comment https://forums.phpfreaks.com/topic/314307-paypal-integration-in-php-step-by-step-tutorial/#findComment-1592675 Share on other sites More sharing options...
mac_gyver Posted December 13, 2021 Share Posted December 13, 2021 (edited) 13 minutes ago, FabrizioCo said: seeing if any errors are notified you would set php's error related settings so that all php detected errors are reported and logged. error_reporting should be set to E_ALL and log_errors should be set to ON. the mysqli_report line will then throw an exception upon a mysqli error which php will catch and log the actual error information. 50 minutes ago, FabrizioCo said: if (verifyTransaction($_POST) && checkTxnid($data['txn_id'])) { if (addPayment($data) !== false) { the above logic has no useful error handling. it should be logging unique and helpful error information for each separate thing that can fail, so that you can find and fix any problem that is occurring. when validating data, every if() conditional test needs an else branch so that code does something useful when the test fails. you should also not lump together tests in one statement. if the verifyTransaction fails, that's a different issue from duplicate data and for every possible return value from addPaymet, there should be a separate conditional test and logged information. Edited December 13, 2021 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/314307-paypal-integration-in-php-step-by-step-tutorial/#findComment-1592676 Share on other sites More sharing options...
FabrizioCo Posted December 14, 2021 Author Share Posted December 14, 2021 Resolved! Call me stupid. I hadn't changed the url of the file that the PayPal IPN connects to. Quote Link to comment https://forums.phpfreaks.com/topic/314307-paypal-integration-in-php-step-by-step-tutorial/#findComment-1592692 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.