Janessa Bautista
Janessa Bautista

Reputation: 294

Success page is not returning anything to me using PayPal integration in codeigniter

I really don't know what to.

I just want to get the PayPal response to be inserted on my database, But no value is returning after the payment has been processed.

I have already done the following in my PayPal Sandbox account:

Enabled Auto Return

Set Auto Return URL ('https://hous3r3nt.000webhostapp.com/houserent/MyApplication/paymentSuccess')

Enabled Payment Data Transfer (PDT)

But still no luck :(.

I am using 000webhostapp to make my website live, because I thought it will work if I am not using localhost, But still it is not working.

Here is my code in my controller:

    // Paypal integration

    function buyProduct($property_id){
        // Set variables for paypal form
        $returnURL = base_url().'MyApplication/paymentSuccess'; //payment success url
        $cancelURL = base_url().'MyApplication/paymentFail'; //payment fail url
        $notifyURL = base_url().'MyApplication/ipn'; //ipn url

        // Get product data from the database
        $property = $this->MyApplicationModel->getPropertyApplication($property_id);

        // Get current user ID from the session (optional)
        $userID = !empty($_SESSION['id'])?$_SESSION['id']:1;

        // Add fields to paypal form
        $this->paypal_lib->add_field('return', $returnURL);
        $this->paypal_lib->add_field('cancel_return', $cancelURL);
        $this->paypal_lib->add_field('notify_url', $notifyURL);
        $this->paypal_lib->add_field('item_name', $property['property_name']);
        $this->paypal_lib->add_field('custom', $userID);
        $this->paypal_lib->add_field('item_number',  $property['property_id']);
        $this->paypal_lib->add_field('amount',  $property['property_price']);

        // Render paypal form
        $this->paypal_lib->paypal_auto_form();
    }

    function paymentSuccess(){
            // Get the transaction data
            $paypalInfo = $this->input->get();

            $productData = $paymentData = array();
            if(!empty($paypalInfo['item_number']) && !empty($paypalInfo['tx']) && !empty($paypalInfo['amt']) && !empty($paypalInfo['cc']) && !empty($paypalInfo['st'])){
                    $item_name = $paypalInfo['item_name'];
                    $item_number = $paypalInfo['item_number'];
                    $txn_id = $paypalInfo["tx"];
                    $payment_amt = $paypalInfo["amt"];
                    $currency_code = $paypalInfo["cc"];
                    $status = $paypalInfo["st"];

                    // Get product info from the database
                    $productData = $this->MyApplicationModel->getPropertyApplication($item_number);

                    // Check if transaction data exists with the same TXN ID
                    $paymentData = $this->payment->getPayment(array('txn_id' => $txn_id));
            }

            // Pass the transaction data to view
            $data['product'] = $productData;
            $data['payment'] = $paymentData;
            $this->load->view('paypal/paymentSuccess', $data);
    }

     function paymentFail(){
            // Load payment failed view
            $this->load->view('paypal/paymentFail');
     }

     function ipn(){
            // Retrieve transaction data from PayPal IPN POST
            $paypalInfo = $this->input->post();

            if(!empty($paypalInfo)){
                    // Validate and get the ipn response
                    $ipnCheck = $this->paypal_lib->validate_ipn($paypalInfo);

                    // Check whether the transaction is valid
                    if($ipnCheck){
                            // Check whether the transaction data is exists
                            $prevPayment = $this->payment->getPayment(array('txn_id' => $paypalInfo["txn_id"]));
                            if(!$prevPayment){
                                    // Insert the transaction data in the database
                                    $data['user_id']    = $paypalInfo["custom"];
                                    $data['product_id']    = $paypalInfo["item_number"];
                                    $data['txn_id']    = $paypalInfo["txn_id"];
                                    $data['payment_gross']    = $paypalInfo["mc_gross"];
                                    $data['currency_code']    = $paypalInfo["mc_currency"];
                                    $data['payer_name']    = trim($paypalInfo["first_name"].' '.$paypalInfo["last_name"], ' ');
                                    $data['payer_email']    = $paypalInfo["payer_email"];
                                    $data['status'] = $paypalInfo["payment_status"];

                                    $this->payment->insertTransaction($data);
                            }
                    }
            }
    }

Please help me, I really don't know what to do now.

Upvotes: 0

Views: 741

Answers (1)

Preston PHX
Preston PHX

Reputation: 30477

Auto Return and PDT are ancient settings for HTML Payments Standard, which is an API-less integration. Do not depend on Auto Return or PDT for anything important, particularly recording information in a database. There is never any guarantee that a return will happen. The customer may be viewing a receipt on PayPal and may not click to return, or their client may crash or any number of things could go wrong. Absolutely no business logic should depend on these features, they are suitable for informational / thank you message purposes only.

However, the code you posted is for IPN, so that at least is a step in the right direction, as the IPN service is a separate thing that -- though quite old and not recommendable -- can be used to receive notifications asynchronously.

To determine why you aren't receiving IPN notifications, start by checking your own webserver logs of your IPN listener/notify URL, as well as your PayPal account's IPN history.

You can test your IPN listener using the IPN simulator.


For best results, upgrade your integration to something that does not rely on IPN. The best solution is to create two routes on your server, one for 'Create Order' and one for 'Capture Order', documented here: https://developer.paypal.com/docs/business/checkout/server-side-api-calls/ -- these routes should return JSON output (and only JSON). The second one, before returning JSON, should check for a successful response and do your database writing then.

Pair your two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

This way, no asynchronous IPN message service is needed. You already get an immediate, synchronous success/failure response when you capture via the API.

Upvotes: 2

Related Questions