<?php

namespace twa\omnipay\controllers;

use twa\ecomcart\models\EcomPurchasesAttemptModel;

class TransactionGlobalController
{
    public $data;

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

    public function createPayment($id, $value, $currency, $customer_name, $customer_email, $url_success, $url_cancel)
    {
        $mdata = [
            'api.username' => $this->data['merchant_username'],
            'api.password' => $this->data['merchant_password'],
            'api.key' => $this->data['merchant_key'],
            'order.id' => $id,
            'order.currency' => $currency,
            'order.amount' => $value,
            'customer.name' => $customer_name,
            'customer.email' => $customer_email,
            'redirection.success' => $url_success,
            'redirection.cancel' => $url_cancel,
        ];

        $curl_post_data = json_encode($mdata);
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'https://endpoint.transactionglobal.com/api/version/1/hc/session/create',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_VERBOSE => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => http_build_query($mdata),
        ]);

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

        $response = json_decode($response);

        return collect(['redirect' => ['link' => $response->payment_link], 'response' => $response]);
    }

    public function checkPayment($attemptID)
    {
        $attempt = EcomPurchasesAttemptModel::where('id', $attemptID)->where('cancelled', 0)->first();
        $gatewayInit = json_decode($attempt->gateway_init);
        $session_id = $gatewayInit->response->session->id;

        $data = [
            'api.username' => $this->data['merchant_username'],
            'api.password' => $this->data['merchant_password'],
            'api.key' => $this->data['merchant_key'],
            'session_id' => $session_id,

        ];

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'https://endpoint.transactionglobal.com/api/version/1/hc/session/inquire',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            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);
        $response = collect(json_decode($response, 1));
        if (isset($response['status']) && $response['status'] == 'succeeded') {
            return response(['status' => 'success', 'response' => $response], 200);
        } else {
            return response(['status' => 'fail', 'response' => $response], 400);
        }
    }
}
