Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gaan10's avatar

Want to implement payment gateway into the api .

Hello,I have been trying to implement payment gateway into my api but am not successfull. Payment_request_id i am able to save but payment_id i am not able to save to my db.Plz have a look. My controller functions :

 public function quotationSelection()
    {
       
        $inputs = \Input::all();
                
    Gateway::procedurebooking()->updateProcedureBookingRecordWithQuotation(\Input::all()); 
             
                $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://instamojo.com/api/1.1/payment-requests/');
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:afc9995c28b25711fafa197a7c0ece08",
                "X-Auth-Token:1e2ee5b69a3d477d84ef75e791b9c82a"));
        $payload = Array(
            'purpose' => 'Advanced payment',
            'amount' => $inputs['advanced_amt'],
            'phone' => $inputs['patient_mob'],
            'bookingid' => $inputs['booking_id'],
            'redirect_url' => url('/returnurl'),
            'send_email' => true,
            'webhook' => 'http://instamojo.com/webhook/',
            'send_sms' => false,
            'email' => '[email protected]',
            'allow_repeated_payments' => false
        );
        
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch); 
    
        
        if ($err) 
        {
            \Session::put('error','Payment Failed, Try Again!!');
            return redirect()->back();
        } 
        else 
        {
            $data = json_decode($response);
        }
    
       $updatedb = Gateway::procedurebooking()->updateProcedureBookingRecordWithPaymentRequestStatus($inputs['booking_id'],$data->payment_request->id);
           
        if($data->success == true) {
            return redirect($data->payment_request->longurl);
        } else {
            \Session::put('error','Payment Failed, Try Again!!');
            return redirect()->back();
        }
        
        return 'Updated';
           
    }



    public function returnurl(Request $request)
    {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://instamojo.com/api/1.1/payments/'.$request->get('payment_id'));
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array("X-Api-Key:afc9995c28b25711fafa197a7c0ece08",
                "X-Auth-Token:1e2ee5b69a3d477d84ef75e791b9c82a"));

        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch); 

        if ($err) {
            \Session::put('error','Payment process Failed, Try Again!!');
            return redirect()->route('quotationSelection');
        } else {
            $data = json_decode($response);
        }
            

        if($data->success == true) 
        {
            if($data->payment->status == 'Credit') 
            {
                // Here Your Database Insert Login

                $updatepayment = Gateway::procedurebooking()->updateProcedureBookingRecordWithPaymentIDStatus($data->payment_request->id,$data->payment_id);

                \Session::put('success','Your payment has been successfully');
                return redirect()->route('quotationSelection');

            } 
            else 
            {
                \Session::put('error','Payment  soln Failed, Try Again!!');
                return redirect()->route('quotationSelection');
            }
        } 
else 
        {
            \Session::put('error','Payment soln Failed, Try Again!!');
            return redirect()->route('quotationSelection');
        }
    }

Routes are :

Route::post('quotationSelection', ['as' => 'quotationselection', 'uses' => 'Api\Procedure\ProcedureController@quotationSelection']);

Route::get('returnurl', ['as' => 'returnurl', 'uses' => 'Api\Procedure\ProcedureController@returnurl']);
0 likes
3 replies
drewdan's avatar

Would be really good if you refactored your code a little to make it more readable. And maybe considered using a library like Guzzle to interact with your API.

I cannot see where you are pulling your model in or saving the data to the database. Is that somewhere else?

1 like
gaan10's avatar
gaan10
OP
Best Answer
Level 1

Actually its my mistake. I was taking the following parameters $data->payment_request->id,$data->payment_id wrongly. I should have used $data->payment->payment_request and $data->payment->payment_id which is why not able to save it to the db. Thanks anyway for taking the time to reply.

1 like

Please or to participate in this conversation.