<?php

namespace App\Traits;

trait SendsNotifications
{
    public function sendNotification($phone, $message)
    {
        $apikey = "ab3dfaa48a113acbc7c4a02831224445";
        $shortcode = "STJOHNKENYA";
        $partnerID = "9907";
        $serviceId = 0;

        $smsdata = [
            "apikey" => $apikey,
            "shortcode" => $shortcode,
            "partnerID" => $partnerID,
            "mobile" => $phone,
            "message" => $message,
            //"serviceId" => $serviceId,
            //"response_type" => "json",
        ];

        $smsdata_string = json_encode($smsdata);
        $smsURL = "https://sms.textsms.co.ke/api/services/sendsms/";

        $ch = curl_init($smsURL);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $smsdata_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Content-Length: ' . strlen($smsdata_string)
        ]);

        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch);

        return $response;
    }
}
