Files
aso/Modules/HospitalPortal/Helpers/GrabHelper.php
2024-10-03 16:14:11 +07:00

94 lines
2.8 KiB
PHP

<?php
namespace Modules\HospitalPortal\Helpers;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use DateTime;
use DateTimeZone;
class GrabHelper
{
protected $client;
public function __construct()
{
$this->client = new Client();
}
public function getToken()
{
$url = env('TOKEN_URL_GRAB');
$headers = [
'Content-Type' => 'application/json',
];
$body = json_encode([
'client_id' => env('CLIENT_ID_GRAB'),
'client_secret' => env('CLIENT_SECRET_GRAB'),
'grant_type' => 'client_credentials',
'scope' => 'grab_express.partner_deliveries',
]);
$request = new Request('POST', $url, $headers, $body);
$response = $this->client->send($request);
$data = json_decode($response->getBody()->getContents(), true);
return $data['access_token'] ?? null;
}
public function createDelivery($token,$body)
{
$url = env('BASE_URL_GRAB').'/v1/deliveries';
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token,
];
$request = new Request('POST', $url, $headers, $body);
$response = $this->client->send($request);
return $response->getBody()->getContents();
}
public function normalizePhoneNumber($phoneNumber) {
// Remove any non-digit characters (e.g., '+', '-', spaces)
$phoneNumber = preg_replace('/\D/', '', $phoneNumber);
// Check if the cleaned phone number is numeric
if (!is_numeric($phoneNumber)) {
return null; // Return false or handle the error as needed
}
// Handle phone numbers starting with '62' or '+62'
if (substr($phoneNumber, 0, 2) === '62') {
$phoneNumber = '0' . substr($phoneNumber, 2);
}
// Handle phone numbers that already start with '8'
if (substr($phoneNumber, 0, 1) === '8') {
$phoneNumber = '0' . $phoneNumber;
}
return $phoneNumber;
}
public function getScheduleTimes()
{
// Create a DateTime object for the current time in Jakarta timezone
$pickupTimeFrom = new DateTime('now', new DateTimeZone('Asia/Jakarta'));
// Add 30 minutes to the current time for pickupTimeFrom
$pickupTimeFrom->modify('+30 minutes');
// Clone the pickupTimeFrom to calculate pickupTimeTo
$pickupTimeTo = clone $pickupTimeFrom;
// Add 1 hour to pickupTimeFrom for pickupTimeTo
$pickupTimeTo->modify('+1 hour');
// Format the times to ISO 8601 format (e.g., 2024-10-02T12:37:28+07:00)
return [
"pickupTimeFrom" => $pickupTimeFrom->format(DateTime::ATOM),
"pickupTimeTo" => $pickupTimeTo->format(DateTime::ATOM),
];
}
}