0

when I use function isPaymentComplete , and return the $result to get the payment information, the user session ended , how can solve this issue

paytabs is payment getway , i use laravel passport in my project and i use the visa payment by paytabs getway, after the payment process complete I use API function to get a transaction , but when i use this function the user session will expire and became logout

paytabs Controller

<?php

namespace App\Http\Controllers;

use App\Order;
use App\Product;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
define("AUTHENTICATION", "https://www.paytabs.com/apiv2/validate_secret_key");
define("PAYPAGE_URL", "https://www.paytabs.com/apiv2/create_pay_page");
define("VERIFY_URL", "https://www.paytabs.com/apiv2/verify_payment");

class PaytabsController extends Controller
{
    private $merchant_email;
    private $secret_key;


    public function __construct() {
        $this->merchant_email = "test@gmail.com";
        $this->secret_key = "gIHQJSHpoZTj8QFyRcA7zYXfsBcZ0fAgXh6GaO6zO0SWQxuIK5Pj3NzvoMuo15FYmMFwBM5pNYlEwQmgVczZZ9vnVZMW5rVreAYP";
    }



    public static function getInstance($merchant_email, $merchant_secretKey)
    {

        static $inst = null;
        if ($inst === null) {
            $inst = new PaytabsController();
        }
        $inst->setMerchant($merchant_email, $merchant_secretKey);
        return $inst;
    }




    public function go(){
        $price = session()->get('prices')['price_sar'];
/*
        if (!Auth::user()){
            return redirect('login');
        }
*/
        $pt = \App\Http\Controllers\PaytabsController::getInstance("test@gmail.com", "gIHQJSHpoZTj8QFyRcA7zYXfsBcZ0fAgXh6GaO6zO0SWQxuIK5Pj3NzvoMuo15FYmMFwBM5pNYlEwQmgVczZZ9vnVZMW5rVreAYP");
        $result = $pt->create_pay_page([
            "merchant_email" => "test@gmail.com",
            'secret_key' => "gIHQJSHpoZTj8QFyRcA7zYXfsBcZ0fAgXh6GaO6zO0SWQxuIK5Pj3NzvoMuo15FYmMFwBM5pNYlEwQmgVczZZ9vnVZMW5rVreAYP",
            'title' => "العنوان",
            'cc_first_name' => "الاسم الاول",
            'cc_last_name' => "الاسم الاخير",
            'email' => "example@email.com",
            'cc_phone_number' => "966",
            'phone_number' => "55555555555",
            'billing_address' => "شارع ",
            'city' => "الرياض",
            'state' => "الرياض",
            'postal_code' => "96600",
            'country' => "SAU",
            'address_shipping' => "شارع",
            'city_shipping' => "الرياض",
            'state_shipping' => "الرياض",
            'postal_code_shipping' => "96600",
            'country_shipping' => "SAU",
            "products_per_title"=> "خدمات",
            'currency' => "SAR",
            "unit_price"=> $price,
            'quantity' => "1",
            'other_charges' => "0",
            'amount' => $price,
            'discount'=>"0",
            "msg_lang" => "arabic",
            "reference_no" => "1231231",
            "site_url" => "http://127.0.0.1:8000/",
            'return_url' => "http://127.0.0.1:8000/payment_reference",
            "cms_with_version" => "Laravel",
        ]);



        if($result->response_code == 4012){
            return redirect($result->payment_url);
        }

        return $result->result;

    }




    function setMerchant($merchant_email, $merchant_secretKey) {
        $this->merchant_email = $merchant_email;
        $this->merchant_secretKey = $merchant_secretKey;
        $this->api_key = "";
    }

    function authentication(){
        $obj = json_decode($this->runPost(AUTHENTICATION, array("merchant_email"=> $this->merchant_email, "secret_key"=>  $this->secret_key)),TRUE);

        if($obj->response_code == "4000"){
            return TRUE;
        }
        return FALSE;

    }



    function create_pay_page($values) {
        $values['merchant_email'] = $this->merchant_email;
        $values['secret_key'] = $this->secret_key;
        $values['ip_customer'] = $_SERVER['REMOTE_ADDR'];
        $values['ip_merchant'] = isset($_SERVER['SERVER_ADDR'])? $_SERVER['SERVER_ADDR'] : '::1';
        return json_decode($this->runPost(PAYPAGE_URL, $values));
    }



    function verify_payment($payment_reference){
        $values['merchant_email'] = $this->merchant_email;
        $values['secret_key'] = $this->secret_key;
        $values['payment_reference'] = $payment_reference;
        return json_decode($this->runPost(VERIFY_URL, $values));
    }

    function runPost($url, $fields) {
        $fields_string = "";
        foreach ($fields as $key => $value) {
            $fields_string .= $key . '=' . $value . '&';
        }
        $fields_string = rtrim($fields_string, '&');
        $ch = curl_init();
        $ip = $_SERVER['REMOTE_ADDR'];

        $ip_address = array(
            "REMOTE_ADDR" => $ip,
            "HTTP_X_FORWARDED_FOR" => $ip
        );
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        /*
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $ip_address);
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_REFERER, 1);
*/
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }



    **public function isPaymentComplete(Request $request)
    {

        $pt = PaytabsController::getInstance("test@gmail.com", "gIHQJSHpoZTj8QFyRcA7zYXfsBcZ0fAgXh6GaO6zO0SWQxuIK5Pj3NzvoMuo15FYmMFwBM5pNYlEwQmgVczZZ9vnVZMW5rVreAYP");
        $result = $pt->verify_payment($request->payment_reference);

        if ($result->response_code == 100) {

            return view('website.approved',[
                'result' => $result
            ]);
        }
        return view('website.canceled');


    }**

}
dev
  • 1
  • 1
  • 2
    Please provide some more details regarding the issue. As what is the expected behavior, when the session is ending, what is PaytabsController. As we don't know the context of your project it becomes really difficult to help. – Tushar Jul 05 '20 at 13:10
  • I have added the paytabs controller – dev Jul 05 '20 at 16:04
  • paytabs is payment getway , i use laravel passport in my project and i use the visa payment by paytabs getway, after the payment process complete I use API function to get a transaction , but when i use this function the user session will expire and became logout – dev Jul 08 '20 at 06:36
  • have you found soution to it? – Geetika Jan 08 '21 at 13:54

0 Answers0