Search code examples
codeigniterpaypalpayment-gatewaypaypal-sandboxpaypal-ipn

Code Issue when clicks "Return to Merchant" Button of Paypal


I am using Codeigniter to integrate the PayPal payment gateway on my website.

//This is my buy method

public function buy($id){

    $course = $this->course_model->fetch_course_details_for_buy($id);

    $returnURL = base_url().'paypal/success'; //payment success url
    $cancelURL = base_url().'paypal/cancel'; //payment cancel url
    $notifyURL = base_url().'paypal/ipn'; //ipn url
  
   
    $userID = $this->session->userdata('student_id'); //current user id
    $_SESSION['course_id'] = $course->course_id;
    $logo = base_url().'assets/images/test-logo.png';
    
    $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', $course->title);
    $this->paypal_lib->add_field('custom', $userID);
    $this->paypal_lib->add_field('item_number',  $course->course_id);
    $this->paypal_lib->add_field('amount',  $course->amount);        
    $this->paypal_lib->image($logo);
    
    $this->paypal_lib->paypal_auto_form();
}

This is my PayPal's class method of success

 function success(){
    //get the transaction data
    $paypalInfo = $this->input->get();
   
      
    $data['item_number'] = $paypalInfo['item_number']; 
    $data['txn_id'] = $paypalInfo["tx"];
    $data['payment_amt'] = $paypalInfo["amt"];
    $data['currency_code'] = $paypalInfo["cc"];
    $data['status'] = $paypalInfo["st"];
    
    //pass the transaction data to view
    $this->load->view('paypal/success', $data);
 }

This is my IPN method

function ipn(){
    //paypal return transaction details array
    $paypalInfo    = $this->input->post();

    $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_email'] = $paypalInfo["payer_email"];
    $data['payment_status']    = $paypalInfo["payment_status"];

    $paypalURL = $this->paypal_lib->paypal_url;        
    $result    = $this->paypal_lib->curlPost($paypalURL,$paypalInfo);
    
    //check whether the payment is verified
    if(preg_match("/VERIFIED/i",$result)){
        //insert the transaction data into the database
        $this->product->insertTransaction($data);
    }
}

My payment is successful done from the sandbox account. But When I click the return to merchant button after payment it's throwing error of undefined indexes as mention in the success method. I apply first a time payment gateway and don't know what is missing in my code. Any help is appreciable.


Solution

  • You should log what $paypalInfo contains.

    If you have enabled PDT, information may be coming as a POST rather than a GET.

    In any case, make your code be able to check for and handle the missing data/indices or put it in a try/catch equivalent; it should not error.