Refactor insurance, paymongo connector logging logic #783

This commit is contained in:
Ramon Gutierrez 2024-03-13 07:09:56 +08:00
parent 9f4c16b149
commit 47dcd92474
4 changed files with 69 additions and 30 deletions

View file

@ -4,6 +4,7 @@ namespace App\Controller;
use App\Ramcar\InsuranceApplicationStatus;
use App\Service\FCMSender;
use App\Service\InsuranceConnector;
use App\Entity\InsuranceApplication;
use Doctrine\ORM\EntityManagerInterface;
@ -15,11 +16,13 @@ use DateTime;
class InsuranceController extends Controller
{
protected $ic;
protected $em;
protected $fcmclient;
public function __construct(EntityManagerInterface $em, FCMSender $fcmclient)
public function __construct(InsuranceConnector $ic, EntityManagerInterface $em, FCMSender $fcmclient)
{
$this->ic = $ic;
$this->em = $em;
$this->fcmclient = $fcmclient;
}
@ -28,17 +31,8 @@ class InsuranceController extends Controller
{
$payload = $req->request->all();
// DEBUG
@file_put_contents(__DIR__ . '/../../var/log/insurance.log', print_r($payload, true) . "\r\n----------------------------------------\r\n\r\n", FILE_APPEND);
error_log(print_r($payload, true));
/*
return $this->json([
'success' => true,
]);
*/
// END DEBUG
// log this callback
$this->ic->log('CALLBACK', "[]", json_encode($payload), 'callback');
// if no transaction code given, silently fail
if (empty($payload['transaction_code'])) {

View file

@ -4,6 +4,7 @@ namespace App\Controller;
use App\Entity\GatewayTransaction;
use App\Ramcar\TransactionStatus;
use App\Service\PayMongoConnector;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
@ -12,10 +13,12 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PayMongoController extends Controller
{
protected $pm;
protected $em;
public function __construct(EntityManagerInterface $em)
public function __construct(PayMongoConnector $pm, EntityManagerInterface $em)
{
$this->pm = $pm;
$this->em = $em;
}
@ -23,16 +26,8 @@ class PayMongoController extends Controller
{
$payload = json_decode($req->getContent(), true);
// DEBUG
@file_put_contents(__DIR__ . '/../../var/log/paymongo.log', print_r($payload, true) . "\r\n----------------------------------------\r\n\r\n", FILE_APPEND);
/*
return $this->json([
'success' => true,
]);
*/
// END DEBUG
// log this callback
$this->pm->log('CALLBACK', "[]", $req->getContent(), 'callback');
// if no event type given, silently fail
if (empty($payload['data'])) {

View file

@ -91,7 +91,7 @@ class InsuranceConnector
return base64_encode($this->username . ":" . $this->password);
}
protected function doRequest($url, $method, $body = [])
protected function doRequest($url, $method, $request_body = [])
{
$client = new Client();
$headers = [
@ -102,7 +102,7 @@ class InsuranceConnector
try {
$response = $client->request($method, $this->base_url . '/' . $url, [
'json' => $body,
'json' => $request_body,
'headers' => $headers,
]);
} catch (RequestException $e) {
@ -114,6 +114,9 @@ class InsuranceConnector
error_log("Insurance Creds: " . $this->username . ", " . $this->password);
error_log("Insurance Hash: " . $this->generateHash());
// 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());
}
@ -124,11 +127,32 @@ class InsuranceConnector
];
}
error_log(print_r(json_decode($response->getBody(), true), true));
$result_body = $response->getBody();
// log response
$this->log($url, json_encode($request_body), $result_body);
return [
'success' => true,
'response' => json_decode($response->getBody(), true)
'response' => json_decode($result_body, true),
];
}
// TODO: make this more elegant
public function log($title, $request_body = "[]", $result_body = "[]", $type = 'api')
{
$filename = '/../../var/log/insurance_' . $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);
}
}

View file

@ -79,7 +79,7 @@ class PayMongoConnector
return base64_encode($this->secret_key);
}
protected function doRequest($url, $method, $body = [])
protected function doRequest($url, $method, $request_body = [])
{
$client = new Client();
$headers = [
@ -90,14 +90,14 @@ class PayMongoConnector
try {
$response = $client->request($method, $this->base_url . '/' . $url, [
'json' => $body,
'json' => $request_body,
'headers' => $headers,
]);
} catch (RequestException $e) {
$error = ['message' => $e->getMessage()];
ob_start();
var_dump($body);
//var_dump($request_body);
$varres = ob_get_clean();
error_log($varres);
@ -107,6 +107,9 @@ class PayMongoConnector
error_log("PayMongo 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());
}
@ -117,9 +120,32 @@ class PayMongoConnector
];
}
$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/paymongo_' . $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);
}
}