Add checker for paymongo webhook status before running manual processing command #801

This commit is contained in:
Ramon Gutierrez 2024-05-31 18:45:16 +08:00
parent 191a02f4c4
commit 3846ad5a43
3 changed files with 31 additions and 6 deletions

View file

@ -109,6 +109,12 @@ services:
arguments:
$callback_url: "%env(WARRANTY_SERIAL_CALLBACK_URL)%"
App\Command\ProcessLatePaymongoTransactionsCommand:
arguments:
$em: "@doctrine.orm.entity_manager"
$paymongo: "@App\\Service\\PayMongoConnector"
$webhook_id: "%env(PAYMONGO_WEBHOOK_ID)%"
# rider tracker service
App\Service\RiderTracker:
arguments:

View file

@ -10,8 +10,6 @@ use Doctrine\ORM\EntityManagerInterface;
use App\Ramcar\TransactionStatus;
use App\Entity\GatewayTransaction;
use App\Service\InsuranceConnector;
use App\Service\PayMongoConnector;
use DateTime;
@ -20,13 +18,14 @@ class ProcessLatePaymongoTransactionsCommand extends Command
{
protected $em;
protected $paymongo;
protected $insurance;
public function __construct(EntityManagerInterface $em, PayMongoConnector $paymongo, InsuranceConnector $insurance)
protected $webhook_id;
public function __construct(EntityManagerInterface $em, PayMongoConnector $paymongo, $webhook_id)
{
$this->em = $em;
$this->paymongo = $paymongo;
$this->insurance = $insurance;
$this->webhook_id = $webhook_id;
parent::__construct();
}
@ -40,6 +39,21 @@ class ProcessLatePaymongoTransactionsCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Checking webhook status...');
// check if webhook is disabled
$webhook = $this->paymongo->getWebhook($this->webhook_id);
if ($webhook['success'] && $webhook['response']['data']['attributes']['status'] === 'enabled') {
$output->writeln('<info>Webhook is enabled, no need to do anything.</info>');
return 0;
} else {
$output->writeln('<comment>Webhook is disabled! Logging event and proceeding...</comment>');
$this->paymongo->log('WEBHOOK', "[]", json_encode($webhook['response'], JSON_PRETTY_PRINT), 'webhook');
}
$output->writeln('Fetching all late pending transactions...');
// set date threshold to 24 hours ago
@ -96,7 +110,7 @@ class ProcessLatePaymongoTransactionsCommand extends Command
}
}
$output->writeln('Done! Processed ' . $x . ' rows.');
$output->writeln('<info>Done! Processed ' . $x . ' rows.</info>');
return 0;
}

View file

@ -74,6 +74,11 @@ class PayMongoConnector
return $this->doRequest('/v1/checkout_sessions/' . $checkout_id, 'GET');
}
public function getWebhook($id)
{
return $this->doRequest('/v1/webhooks/'. $id, 'GET');
}
protected function generateHash()
{
return base64_encode($this->secret_key);