Jump to content

PayPal Integration in PHP (Step by Step Tutorial)


FabrizioCo

Recommended Posts

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.

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.