<?php

namespace twa\omnipay\controllers;

use Illuminate\Support\Facades\Http;

class APGatewayMastercardController
{
    public $data;

    public $provider;

    public function __construct($data, $provider)
    {
        $this->provider = $provider;

        $required_data = ['MERCHANT_ID', 'MERCHANT_OPERATOR_ID', 'MERCHANT_PASSWORD', 'MERCHANT_REAL_PASS'];
        \twa\omnipay\controllers\GeneralFunctions::checkRequiredData($data, $required_data);

        $this->data = collect([
            'MERCHANT_ID' => $data['MERCHANT_ID'],
            'MERCHANT_PASSWORD' => $data['MERCHANT_PASSWORD'],
            'MERCHANT_OPERATOR_ID' => $data['MERCHANT_OPERATOR_ID'],
            'MERCHANT_REAL_PASS' => $data['MERCHANT_REAL_PASS'],
            'api' => $data['api'],
            'script_api' => $data['script_api'],

        ]);
        $this->data = collect($data);
    }

    public function createPayment($id, $value, $currency, $customer_name = null, $customer_email = null, $url_success = null, $url_cancel = null)
    {
        $mdata = [
            'apiUsername' => 'merchant.'.$this->data['MERCHANT_ID'],
            'apiPassword' => $this->data['MERCHANT_PASSWORD'],
            'apiOperation' => 'INITIATE_CHECKOUT',
            'merchant' => $this->data['MERCHANT_ID'],
            'order.id' => $id,
            'order.currency' => $currency,
            'order.amount' => $value,
            'interaction.operation' => 'PURCHASE',
            'interaction.returnUrl' => $url_success,
        ];
        $curl_post_data = json_encode($mdata);
        $curl = curl_init();

        curl_setopt_array($curl, [
            CURLOPT_URL => $this->data['api'],
            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($mdata),
            CURLOPT_HTTPHEADER => [
                'cache-control: no-cache',
                'content-type: application/x-www-form-urlencoded',
            ],
        ]);
        $response = curl_exec($curl);
        curl_close($curl);
        parse_str($response, $output);

        if ($output['result'] != 'SUCCESS') {
            dd(['status' => 'error', 'data' => $output['session_id']]);
        }

        return collect(['redirect' => ['link' => env('APP_URL').'/omnipay/redirect/apgateway/'.$id.'/'.$this->provider.'/'.$value.'/'.$currency.'/'.$output['session_id'].'/'.$this->data['MERCHANT_ID']]]);
    }

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

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $this->data['api'],
            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);
        parse_str($response, $output);
        if ($output['status'] == 'CAPTURED') {
            return response(['status' => 'success', 'response' => $output], 200);
        } else {
            return response(['status' => 'fail', 'response' => $output], 400);
        }
    }
}
