<?php
namespace gateways;

class SMSIRGateway{

    public static function sendSMS($apiKey, $mobile, $pattern, $input_data) {
        $postData = json_encode([
            "mobile" => $mobile,
            "templateId" => $pattern,
            "parameters" => $input_data
        ], JSON_UNESCAPED_UNICODE);

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'https://api.sms.ir/v1/send/verify',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $postData,
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/json',
                'Accept: text/plain',
                'x-api-key: ' . $apiKey
            ],
        ]);
        
        $response = curl_exec($curl);
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

        return [
            'http_code' => $http_code,
            'response' => $response
        ];
    }

    public static function smsir($api, $mobile, $bodyId, $code){

        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.sms.ir/v1/send/verify',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'{
            "mobile": "'.$mobile.'",
            "templateId": '.$bodyId.',
            "parameters": [
                {
                "name": "CODE",
                "value": "'.$code.'"
                }
            ]
        }',
        CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'Accept: text/plain',
            'x-api-key: '. $api
        ),
        ));
        curl_exec($curl);
        curl_close($curl);

    }
}
