GRAB X APOTEK X Hospital PORTAL
This commit is contained in:
93
Modules/HospitalPortal/Helpers/GrabHelper.php
Normal file
93
Modules/HospitalPortal/Helpers/GrabHelper.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user