Add loyalty connector and register endpoint #809

This commit is contained in:
Ramon Gutierrez 2024-10-07 06:28:57 +08:00
parent dc4d5f90a7
commit bd655a459a
4 changed files with 169 additions and 1 deletions

View file

@ -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]
methods: [GET]
apiv2_loyalty_register:
path: /apiv2/loyalty/register
controller: App\Controller\CustomerAppAPI\LoyaltyController::register
methods: [POST]

View file

@ -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:

View file

@ -0,0 +1,31 @@
<?php
namespace App\Controller\CustomerAppAPI;
use App\Service\LoyaltyConnector;
use Symfony\Component\HttpFoundation\Request;
use Catalyst\ApiBundle\Component\Response as ApiResponse;
class LoyaltyController extends ApiController
{
public function register(Request $req, LoyaltyConnector $lc)
{
// validate params
$validity = $this->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'],
]);
}
}

View file

@ -0,0 +1,125 @@
<?php
namespace App\Service;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;
use App\Entity\Customer;
use \DateTime;
use \DateTimeZone;
class LoyaltyConnector
{
protected $base_url;
protected $api_key;
protected $secret_key;
public function __construct($base_url, $api_key, $secret_key)
{
$this->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);
}
}