From bd655a459a0d7523af74eda6caf7d95b92a8f4ac Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Mon, 7 Oct 2024 06:28:57 +0800 Subject: [PATCH] Add loyalty connector and register endpoint #809 --- config/routes/apiv2.yaml | 7 +- config/services.yaml | 7 + .../CustomerAppAPI/LoyaltyController.php | 31 +++++ src/Service/LoyaltyConnector.php | 125 ++++++++++++++++++ 4 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 src/Controller/CustomerAppAPI/LoyaltyController.php create mode 100644 src/Service/LoyaltyConnector.php diff --git a/config/routes/apiv2.yaml b/config/routes/apiv2.yaml index dc603c9f..af316cb6 100644 --- a/config/routes/apiv2.yaml +++ b/config/routes/apiv2.yaml @@ -312,4 +312,9 @@ apiv2_insurance_premiums_banner: apiv2_insurance_body_types: path: /apiv2/insurance/body_types controller: App\Controller\CustomerAppAPI\InsuranceController::getBodyTypes - methods: [GET] \ No newline at end of file + methods: [GET] + +apiv2_loyalty_register: + path: /apiv2/loyalty/register + controller: App\Controller\CustomerAppAPI\LoyaltyController::register + methods: [POST] \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index 6af4a5d3..d87c16bb 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -241,6 +241,13 @@ services: $public_key: "%env(PAYMONGO_PUBLIC_KEY)%" $secret_key: "%env(PAYMONGO_SECRET_KEY)%" + # loyalty system connector + App\Service\LoyaltyConnector: + arguments: + $base_url: "%env(LOYALTY_BASE_URL)%" + $api_key: "%env(LOYALTY_API_KEY)%" + $secret_key: "%env(LOYALTY_SECRET_KEY)%" + # entity listener for customer vehicle warranty code history App\EntityListener\CustomerVehicleSerialListener: arguments: diff --git a/src/Controller/CustomerAppAPI/LoyaltyController.php b/src/Controller/CustomerAppAPI/LoyaltyController.php new file mode 100644 index 00000000..4beb0571 --- /dev/null +++ b/src/Controller/CustomerAppAPI/LoyaltyController.php @@ -0,0 +1,31 @@ +validateRequest($req); + + if (!$validity['is_valid']) { + return new ApiResponse(false, $validity['error']); + } + + // register customer or retrieve existing record + $result = $lc->register($this->session->getCustomer()); + if (!$result['success']) { + return new ApiResponse(false, $result['error']); + } + + // response + return new ApiResponse(true, '', [ + 'loyalty_cust' => $result['response']['data']['customer'], + ]); + } +} diff --git a/src/Service/LoyaltyConnector.php b/src/Service/LoyaltyConnector.php new file mode 100644 index 00000000..6281ed55 --- /dev/null +++ b/src/Service/LoyaltyConnector.php @@ -0,0 +1,125 @@ +base_url = $base_url; + $this->api_key = $api_key; + $this->secret_key = $secret_key; + } + + public function register(Customer $cust) + { + return $this->doRequest('/api/customer/register', 'POST', [ + 'external_id' => $cust->getPhoneMobile(), + ]); + } + + protected function generateSignature(string $path, string $method, string $date_string) + { + $elements = [ + $method, + $path, + $date_string, + $this->secret_key, + ]; + + // generate raw signature + $sig_src = implode("|", $elements); + $raw_sig = hash_hmac('sha1', $sig_src, $this->secret_key, true); + + // return encoded signature + return base64_encode($raw_sig); + } + + protected function doRequest($url, $method, $request_body = []) + { + // format current date and time + $now = new DateTime('now', new DateTimeZone('UTC')); + $date_string = $now->format('D, d M Y H:i:s T'); + + // prepare request + $client = new Client(); + $headers = [ + 'X-Cata-API-Key' => $this->api_key, + 'X-Cata-Signature' => $this->generateSignature($url, $method, $date_string), + 'X-Cata-Date' => $date_string, + ]; + + try { + $response = $client->request($method, $this->base_url . $url, [ + 'form_params' => $request_body, + 'headers' => $headers, + ]); + } catch (RequestException $e) { + $error = ['message' => $e->getMessage()]; + + ob_start(); + //var_dump($request_body); + $varres = ob_get_clean(); + error_log($varres); + + error_log("--------------------------------------"); + error_log($e->getResponse()->getBody()->getContents()); + + error_log("Loyalty API Error: " . $error['message']); + error_log(Psr7\Message::toString($e->getRequest())); + + // log this error + $this->log($url, Psr7\Message::toString($e->getRequest()), Psr7\Message::toString($e->getResponse()), 'error'); + + if ($e->hasResponse()) { + $error['response'] = Psr7\Message::toString($e->getResponse()); + } + + return [ + 'success' => false, + 'error' => $error, + ]; + } + + $result_body = $response->getBody(); + + // log response + $this->log($url, json_encode($request_body), $result_body); + + return [ + 'success' => true, + 'response' => json_decode($response->getBody(), true) + ]; + } + + // TODO: make this more elegant + public function log($title, $request_body = "[]", $result_body = "[]", $type = 'api') + { + $filename = '/../../var/log/loyalty_' . $type . '.log'; + $date = date("Y-m-d H:i:s"); + + // build log entry + $entry = implode("\r\n", [ + $date, + $title, + "REQUEST:\r\n" . $request_body, + "RESPONSE:\r\n" . $result_body, + "\r\n----------------------------------------\r\n\r\n", + ]); + + @file_put_contents(__DIR__ . $filename, $entry, FILE_APPEND); + } +}