dev
dev

Reputation: 1

if I use return function paytabs with laravel the signout in my project

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 = "[email protected]";
        $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("[email protected]", "gIHQJSHpoZTj8QFyRcA7zYXfsBcZ0fAgXh6GaO6zO0SWQxuIK5Pj3NzvoMuo15FYmMFwBM5pNYlEwQmgVczZZ9vnVZMW5rVreAYP");
        $result = $pt->create_pay_page([
            "merchant_email" => "[email protected]",
            'secret_key' => "gIHQJSHpoZTj8QFyRcA7zYXfsBcZ0fAgXh6GaO6zO0SWQxuIK5Pj3NzvoMuo15FYmMFwBM5pNYlEwQmgVczZZ9vnVZMW5rVreAYP",
            'title' => "العنوان",
            'cc_first_name' => "الاسم الاول",
            'cc_last_name' => "الاسم الاخير",
            'email' => "[email protected]",
            '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("[email protected]", "gIHQJSHpoZTj8QFyRcA7zYXfsBcZ0fAgXh6GaO6zO0SWQxuIK5Pj3NzvoMuo15FYmMFwBM5pNYlEwQmgVczZZ9vnVZMW5rVreAYP");
        $result = $pt->verify_payment($request->payment_reference);

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

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


    }**

}

Upvotes: 0

Views: 831

Answers (1)

Abdul Raffay
Abdul Raffay

Reputation: 11

I encountered a similar issue and managed to resolve it by updating the cart_id in the payment request data as follows:

'cart_id' => $customerMembership->id . '-' . Auth::id() . '-' . Str::random(8),

To extract the customer ID from cartId in the success URL, use:

$customerMembershipId = explode("-" , $request['cartId'])[0];
$customerId = explode("-" , $request['cartId'])[1];

After completing your tasks, finalize with:

Auth::loginUsingId($customerId);

This should resolve the problem you encountered.

Upvotes: 0

Related Questions