From 0e2394567d006801f899d2b3e5d43141d51e4d6b Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Mon, 2 Sep 2024 14:21:49 -0300 Subject: [PATCH] feature: add webhook sample logic --- .../Gateway/OffSiteCheckoutPageSimulation.php | 18 ++++++++- src/OffSiteGateway/Gateway/OffSiteGateway.php | 3 +- ...SiteGatewaysWebhookNotificationHandler.php | 38 ++++++++++++++++++- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/OffSiteGateway/Gateway/OffSiteCheckoutPageSimulation.php b/src/OffSiteGateway/Gateway/OffSiteCheckoutPageSimulation.php index c1eb53a..ef776a7 100644 --- a/src/OffSiteGateway/Gateway/OffSiteCheckoutPageSimulation.php +++ b/src/OffSiteGateway/Gateway/OffSiteCheckoutPageSimulation.php @@ -56,9 +56,8 @@ private function loadOffSiteGatewaySimulationMarkup() .container { font-family: "Open Sans", Helvetica, Arial, sans-serif; max-width: 800px; - margin: 100px auto; + margin: 60px auto; } - a { font-size: 1.5rem; } @@ -81,6 +80,21 @@ private function loadOffSiteGatewaySimulationMarkup()

Click on the links below to simulate off-site gateway actions:

+ + Send Webhook Notification To Change Donation Status To Complete ⭷ + +

+ 🛈 Some gateways send webhook notifications to change the transaction status a few hours after the + payment process is finished, and others can send them even before the user is redirected back to the + referrer's website — the action above simulates this last scenario. +

Success Payment | Canceled Payment diff --git a/src/OffSiteGateway/Gateway/OffSiteGateway.php b/src/OffSiteGateway/Gateway/OffSiteGateway.php index c317472..58e1c23 100644 --- a/src/OffSiteGateway/Gateway/OffSiteGateway.php +++ b/src/OffSiteGateway/Gateway/OffSiteGateway.php @@ -189,6 +189,7 @@ public function getPaymentParameters(Donation $donation, $gatewayData): array { return [ 'gatewayPaymentId' => $donation->gatewayTransactionId, + 'merchantPaymentId' => $donation->id, 'amount' => [ 'value' => $donation->amount->formatToDecimal(), 'currency' => $donation->amount->getCurrency()->getCode(), @@ -258,7 +259,7 @@ protected function handleCanceledPaymentReturn(array $queryParams): RedirectResp protected function webhookNotificationsListener() { try { - $webhookNotification = OffSiteGatewayWebhookNotification::fromRequest($_REQUEST); + $webhookNotification = OffSiteGatewayWebhookNotification::fromRequest(give_clean($_REQUEST)); give(OffSiteGatewaysWebhookNotificationHandler::class)($webhookNotification); } catch (Exception $e) { esc_html_e('Off-site gateway Webhook Notification failed.', 'ADDON_TEXTDOMAIN'); diff --git a/src/OffSiteGateway/Webhooks/OffSiteGatewaysWebhookNotificationHandler.php b/src/OffSiteGateway/Webhooks/OffSiteGatewaysWebhookNotificationHandler.php index 09c2ecb..e0a45c7 100644 --- a/src/OffSiteGateway/Webhooks/OffSiteGatewaysWebhookNotificationHandler.php +++ b/src/OffSiteGateway/Webhooks/OffSiteGatewaysWebhookNotificationHandler.php @@ -4,6 +4,7 @@ use Give\Framework\Support\Facades\ActionScheduler\AsBackgroundJobs; use GiveAddon\OffSiteGateway\DataTransferObjects\OffSiteGatewayWebhookNotification; +use GiveAddon\OffSiteGateway\Gateway\OffSiteGateway; /** * @unreleased @@ -22,7 +23,7 @@ public function __invoke(OffSiteGatewayWebhookNotification $webhookNotification) * * @param OffSiteGatewayWebhookNotification $webhookNotification */ - do_action("givewp_off-site_gateway_sample_webhook_notification_handler", $webhookNotification); + do_action('givewp_' . OffSiteGateway::id() . '_webhook_notification_handler', $webhookNotification); // We will handle recurring donations in a separate submodule sample that will enable Subscription on the Off-site gateway sample. if ($this->isRecurringDonation($webhookNotification)) { @@ -31,11 +32,44 @@ public function __invoke(OffSiteGatewayWebhookNotification $webhookNotification) switch (strtolower($webhookNotification->gatewayPaymentStatus)) { case 'complete': + $asyncJobHookName = 'givewp_' . OffSiteGateway::id() . '_event_donation_completed'; AsBackgroundJobs::enqueueAsyncAction( - 'givewp_off-site_gateway_sample_event_donation_completed', + $asyncJobHookName, [$webhookNotification->gatewayPaymentId], 'ADDON_TEXTDOMAIN' ); + + /** + * The block below is not necessary for real-world integrations; + * We are adding it here just for educational purposes. + */ + $asyncJobUrl = admin_url('tools.php?page=action-scheduler&s=' . $asyncJobHookName); + ?> + +
+

Webhook Notification Handler

+

+ ✅ We schedule an async job in the server background to change the donation status to "complete" + as + soon as possible. This approach prevents overloading the server as the webhook notification will + be handled only when the server has enough processing power available. You can check the + background job status at the following link: +

+ +
+