<?php

namespace twa\omnipay\controllers;

class AreebaController
{
    private $data;

    private $provider;

    public function __construct($data, $provider)
    {
        $this->provider = $provider;
        $required_data = ['merchant_id', 'password' , 'api_version'];
        \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/areeba/'.$id.'/'.$this->provider.'/'.$value.'/'.$currency.'/'.$session['session']['id']]]);
    }

    public function getSession($data)
    {
        $mdata = [
            'apiOperation' => 'INITIATE_CHECKOUT',
            "interaction" => [
                "operation" => "PURCHASE",
                "merchant"=> [
                    "name"=> env("APP_NAME"),
                    "address"=> [
                        "line1" => "",
                        "line2"=> ""
                    ],
                    "logo" => $this->data['logo_url']
                ],
                "displayControl" => [
                    "billingAddress" => 'HIDE',
                    "customerEmail" => 'HIDE',
                    "shipping" => 'HIDE'
                ],
            ],

            'order' =>[
                "id" => $data['order_id'],
                'currency' => $data['currency'],
                'amount' => $data['amount'],
                "description" => "order"
            ]
        ];



        $curl_post_data = json_encode($mdata);

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'https://epayment.areeba.com/api/rest/version/66/merchant/'.$this->data['merchant_id'].'/session',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_USERPWD => 'merchant.'.$this->data['merchant_id'] . ":" . $this->data['password'],
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode($mdata),
            CURLOPT_HTTPHEADER => [
                'cache-control: no-cache',
                'content-type: application/x-www-form-urlencoded',
            ],
        ]);



        $response = curl_exec($curl);


        curl_close($curl);
        $response = twaJsonDecode($response);

        return $response;
    }

    public function checkPayment($id)
    {
//        $data = [
//            'apiUsername' => 'merchant.'.$this->data['merchant_id'],
//            'apiPassword' => $this->data['password'],
//            'apiOperation' => 'RETRIEVE_ORDER',
//            'merchant' => $this->data['merchant_id'],
//            'order.id' => $id,
//        ];

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'https://epayment.areeba.com/api/rest/version/69/merchant/'.$this->data['merchant_id'].'/order/'.$id,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_USERPWD => 'merchant.'.$this->data['merchant_id'] . ":" . $this->data['password'],
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
//            CURLOPT_POSTFIELDS => http_build_query($data),
            CURLOPT_HTTPHEADER => [
                'cache-control: no-cache',
                'content-type: application/x-www-form-urlencoded',
            ],
        ]);
        $response = curl_exec($curl);
        curl_close($curl);
        $output = twaJsonDecode($response);
        if ($output['status'] == 'CAPTURED') {
            return response(['status' => 'success', 'response' => $output], 200);
        } else {
            return response(['status' => 'fail', 'response' => $output], 400);
        }
    }
}
