<?php

namespace twa\omnipay\controllers;

class PayfortController
{
    private $data;

    private $provider;

    public function __construct($data, $provider)
    {
        $this->provider = $provider;
        $required_data = ['PAYFORT_MERCHANT_ID', 'PAYFORT_ACCESS_CODE', 'PAYFORT_SHA_REQUEST_PHRASE', 'PAYFORT_SHA_TYPE'];
        \twa\omnipay\controllers\GeneralFunctions::checkRequiredData($data, $required_data);
        $this->data = collect($data);
    }

    public function createPayment($id, $value, $currency, $customer_name = null, $customer_email = null, $url_success = null, $url_cancel = null)
    {
//        $session = $this->getSession(["order_id" => $id, "currency" => $currency, "amount" => $value ]);
//        if($session['result']!= "SUCCESS"){
//            dd([ "status" => "error", "data" => $session ]);
//        }
        return collect(['redirect' => ['link' => env('APP_URL').'/omnipay/redirect/payfort/'.$id.'/'.$this->provider.'/'.$value.'/'.$currency.'/'.md5($id.$this->provider.$value.$currency.env('APP_KEY')).'?lang='.app()->getLocale()]]);
    }

    public function checkPayment($id)
    {
        if (request()->input('response_code') == 14000) {
            return response(['status' => 'success', 'response' => request()->all()], 200);
        } else {
            return response(['status' => 'fail', 'response' => request()->all()], 400);
        }
    }
}
