Your IP : 216.73.216.93


Current Path : /home/users/unlimited/www/mpos.codeskitter.site/app/Library/
Upload File :
Current File : /home/users/unlimited/www/mpos.codeskitter.site/app/Library/Cinetpay.php

<?php

namespace App\Library;

use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class Cinetpay
{
    public static function redirect_if_payment_success()
    {
        if (Session::has('fund_callback')) {
            return url(Session::get('fund_callback')['success_url']);
        }
        return url('payment/success');
    }

    public static function redirect_if_payment_faild()
    {
        if (Session::has('fund_callback')) {
            return url(Session::get('fund_callback')['cancel_url']);
        }
        return url('payment/failed');
    }

    public static function make_payment($array)
    {
        $transaction_id = uniqid(); // Or use UUID
        $apiKey = $array['api_key'];
        $siteId = $array['site_id'];

        $data = [
            'transaction_id' => $transaction_id,
            'amount' => $array['pay_amount'],
            'currency' => $array['currency'] ?? 'XOF',
            'description' => $array['billName'],
            'customer_name' => $array['name'],
            'customer_surname' => '',
            'customer_email' => $array['email'],
            'notify_url' => '', // You can leave it empty or omit if not using
            'return_url' => route('cinetpay.status'), // Redirect handler
            'channels' => 'ALL',
            'site_id' => $siteId,
            'apikey' => $apiKey,
        ];

        try {
            $client = new Client();
            $response = $client->post('https://api-checkout.cinetpay.com/v2/payment', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => $data,
            ]);

            session()->put('cinetpay_secret', [
                'transaction_id' => $transaction_id,
                'api_key' => $apiKey,
                'site_id' => $siteId,
            ]);

            $body = json_decode($response->getBody(), true);
            return redirect($body['data']['payment_url']);

        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

    public function status(Request $request)
    {
        $info = session('cinetpay_secret');

        if (!$info) return redirect(self::redirect_if_payment_faild());

        try {
            $client = new Client();
            $response = $client->post('https://api-checkout.cinetpay.com/v2/payment/check', [
                'headers' => ['Content-Type' => 'application/json'],
                'json' => [
                    'apikey' => $info['api_key'],
                    'site_id' => $info['site_id'],
                    'transaction_id' => $info['transaction_id'],
                ],
            ]);

            $result = json_decode($response->getBody(), true);
            session()->forget('cinetpay_secret');

            if ($result['code'] === '00') {
                // Update your DB here (e.g., mark payment successful)
                return redirect(self::redirect_if_payment_success());
            } else {
                session()->put('payment_msg', $result['message'] ?? 'Payment failed');
                return redirect(self::redirect_if_payment_faild());
            }

        } catch (\Exception $e) {
            session()->put('payment_msg', $e->getMessage());
            return redirect(self::redirect_if_payment_faild());
        }
    }
}