<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::group(['prefix' => '/omnipay', 'middleware' => [\twa\ecomauth\middleware\TranslateTokenWeb::class]], function () {

    Route::any('/response', function () {
        return \twa\omnipay\facades\OmniPayFacade::responseCallback();
    });

    Route::any('/response/{status}/{paid}', function ($status, $paid) {
        return \twa\omnipay\facades\OmniPayFacade::responseCallback($status, $paid);
    });
    Route::any('/response/{status}/{paid}/', function ($status, $paid) {
        return \twa\omnipay\facades\OmniPayFacade::responseCallback($status, $paid);
    });
    Route::any('/response/{status}', function ($status) {
        $paid = request()->input('paid');

        return \twa\omnipay\facades\OmniPayFacade::responseCallback($status, $paid);
    });

    Route::any('/response/{status}/{paid}/{token}', function ($status, $paid, $token) {
        return \twa\omnipay\facades\OmniPayFacade::responseCallback($status, $paid);
    });


    Route::any('/response/{status}/{paid}/{api}', function ($status, $paid, $api) {
        return \twa\omnipay\facades\OmniPayFacade::responseCallback($status, $paid, $api);
    });

    Route::any('/redirect/areeba/{id}/{provider}/{amount}/{currency}/{session}', function ($id, $provider, $amount, $currency, $session) {
        return view('OmniPayViews::areeba.redirect', ['id' => $id, 'provider' => $provider, 'amount' => $amount, 'session' => $session, 'currency' => $currency]);
    });
    Route::any('/redirect/bob/{id}/{provider}/{amount}/{currency}/{session}/{merchant}', function ($id, $provider, $amount, $currency, $session, $merchant) {
        return view('OmniPayViews::bob.redirect', ['order_id' => $id, 'provider' => $provider, 'amount' => $amount, 'session_id' => $session, 'currency' => $currency, 'merchant' => $merchant, 'description' => 'total amount']);
    });
    Route::any('/redirect/apgateway/{id}/{provider}/{amount}/{currency}/{session}/{merchant}', function ($id, $provider, $amount, $currency, $session, $merchant) {
        return view('OmniPayViews::apgateway.redirect', ['order_id' => $id, 'provider' => $provider, 'amount' => $amount, 'session_id' => $session, 'currency' => $currency, 'merchant' => $merchant]);
    });

    Route::get('/redirect/payfort/{id}/{provider}/{amount}/{currency}/{token}', function ($id, $provider, $amount, $currency, $token) {
        if (md5($id . $provider . $amount . $currency . env('APP_KEY')) != $token) {
            return 'invalid token';
        }
        if (request()->input('lang')) {
            app()->setLocale(request()->input('lang'));
        }

        return view('OmniPayViews::payfort.redirect', ['id' => $id, 'provider' => $provider, 'amount' => $amount, 'currency' => $currency]);
    });

    Route::get('/redirect/globalpay/{id}/{provider}/{amount}/{currency}/{token}', function ($id, $provider, $amount, $currency, $token) {
        if (md5($id . $provider . $amount . $currency . env('APP_KEY')) != $token) {
            return 'invalid token';
        }
        if (request()->input('lang')) {
            app()->setLocale(request()->input('lang'));
        }

        return view('OmniPayViews::globalpay.redirect', ['id' => $id, 'provider' => $provider, 'amount' => $amount, 'currency' => $currency]);
    });

    Route::get('/redirect/airwallex/{id}/{provider}/{amount}/{currency}/{intent_id}/{client_secret}/{token}', function ($id, $provider, $amount, $currency, $intent_id, $client_secret, $token) {
        if (md5($id . $provider . $amount . $currency . $intent_id . $client_secret . env('APP_KEY')) != $token) {
            return 'invalid token';
        }

        return view('OmniPayViews::airwallex.redirect', ['id' => $id, 'provider' => $provider, 'amount' => $amount, 'currency' => $currency, 'intent_id' => $intent_id, 'client_secret' => $client_secret]);
    });

    Route::get('/redirect/tap/{id}/{provider}/{amount}/{currency}/{token}', function ($id, $provider, $amount, $currency, $token) {
        if (md5($id . $provider . $amount . $currency . env('APP_KEY')) != $token) {
            return 'invalid token';
        }
        if (request()->input('lang')) {
            app()->setLocale(request()->input('lang'));
        }

        return view('OmniPayViews::tap.redirect', ['id' => $id, 'provider' => $provider, 'amount' => $amount, 'currency' => $currency]);
    });
    Route::post('/capture/tap', function () {
        return (new \twa\omnipay\controllers\TapController(config('omnipay.tap.data'), 'tap'))->generateLink();
    });

    Route::get('/redirect/simplify/{id}/{provider}/{amount}/{currency}/{token}', function ($id, $provider, $amount, $currency, $token) {
        if (md5($id . $provider . $amount . $currency . env('APP_KEY')) != $token) {
            return 'invalid token';
        }
        if (request()->input('lang')) {
            app()->setLocale(request()->input('lang'));
        }
        return view('OmniPayViews::simplify.payment', ['id' => $id, 'provider' => $provider, 'amount' => $amount, 'currency' => $currency, "public_key" => config('omnipay.' . $provider . '.data')['public_key'], "multiplier" => config('omnipay.' . $provider . '.data')['multiplier']]);
    });

    Route::post('/simplify/update/{id}/{reference}', function ($id, $reference) {
        return \twa\omnipay\controllers\SimplifyController::updateGatewayInit($id, $reference);
    })->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class);


    Route::group(['prefix' => '/benefit'], function () {
        Route::any('/response/{token}/{paid}', function ($token, $paid) {
            return \twa\omnipay\controllers\BenefitController::redirectToCheckPayment($token, $paid);
        });
    });


    Route::group(['prefix' => '/globalpay'], function () {
        Route::any('/response', function () {
            if (request()->input('txnref') && request()->input('status') && request()->input('status') == "successful") {
                return \twa\omnipay\facades\OmniPayFacade::responseCallback(null, request()->input('txnref'));
            } else {
                dd('payment failed');
            }
        });
    });

    Route::any('/webhook/{provider_key}', function () {
        return true;
    });
});



//Route::any('/omnipay/webhook/tabby', [\twa\omnipay\controllers\TabbyController::class , 'webhook']);
