Skip to content

Commit

Permalink
tests: add WebhookTest class
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubersilva committed Jan 24, 2025
1 parent aee1666 commit a617c1a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/PaymentGateways/Gateways/TestGateway/TestGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Give\PaymentGateways\Gateways\TestGateway;

use Exception;
use Give\Donations\Models\Donation;
use Give\Framework\PaymentGateways\Commands\GatewayCommand;
use Give\Framework\PaymentGateways\Commands\PaymentComplete;
use Give\Framework\PaymentGateways\Commands\PaymentRefunded;
use Give\Framework\PaymentGateways\Contracts\WebhookNotificationsListener;
use Give\Framework\PaymentGateways\Log\PaymentGatewayLog;
use Give\Framework\PaymentGateways\PaymentGateway;
use Give\Framework\Support\Facades\Scripts\ScriptAsset;
use Give\Helpers\Form\Utils as FormUtils;
Expand All @@ -15,10 +18,11 @@
/**
* A gateway for testing the donation process. No actual payment is processed and only form validation is performed.
*
* @unreleased Implements the WebhookNotificationsListener interface
* @since 3.0.0 change to Test Donations and manual id to replace legacy gateway
* @since 2.18.0
*/
class TestGateway extends PaymentGateway
class TestGateway extends PaymentGateway implements WebhookNotificationsListener
{
/**
* @inheritDoc
Expand Down Expand Up @@ -105,4 +109,58 @@ public function refundDonation(Donation $donation): PaymentRefunded
{
return new PaymentRefunded();
}

/**
* This method implementation is a sample that demonstrate how we can handle webhook notifications
*
* @unreleased
*/
public function webhookNotificationsListener()
{
try {
$webhookNotification = give_clean($_REQUEST);

/**
* Allow developers to handle the webhook notification.
*
* @unreleased
*
* @param array $webhookNotification
*/
do_action('givewp_' . $this::id() . '_webhook_notification_handler', $webhookNotification);

// We will handle recurring donations in a separate submodule.
if (isset($webhookNotification['gatewayRecurringPayment']) && $webhookNotification['gatewayRecurringPayment']) {
return;
}

if ( ! isset($webhookNotification['gatewayPaymentStatus']) && ! isset($webhookNotification['gatewayPaymentId'])) {
return;
}

switch (strtolower($webhookNotification['gatewayPaymentStatus'])) {
case 'complete':
$this->webhook->events->paymentCompleted($webhookNotification['gatewayPaymentId']);
break;
case 'failed':
$this->webhook->events->paymentFailed($webhookNotification['gatewayPaymentId']);
break;
case 'cancelled':
$this->webhook->events->paymentCancelled($webhookNotification['gatewayPaymentId']);
break;
case 'refunded':
$this->webhook->events->paymentRefunded($webhookNotification['gatewayPaymentId']);
break;
default:
break;
}
} catch (Exception $e) {
esc_html_e('Webhook Notification failed.', 'give');
PaymentGatewayLog::error(
'Webhook Notification failed. Error: ' . $e->getMessage()
);
}

exit();
}
}
68 changes: 68 additions & 0 deletions tests/Unit/Framework/PaymentGateways/Webhooks/WebhookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Unit\Framework\PaymentGateways\Webhooks;

use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\PaymentGateways\Webhooks\Webhook;
use Give\PaymentGateways\Gateways\Offline\OfflineGateway;
use Give\PaymentGateways\Gateways\TestGateway\TestGateway;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;

/**
* @unreleased
*/
class WebhookTest extends TestCase
{
use RefreshDatabase;

/**
* @unreleased
*
* @throws Exception
*/
public function testGetNotificationUrlShouldThrowException()
{
$this->expectException(Exception::class);

$testGateway = new OfflineGateway(); // This gateway doesn't implement the WebhookNotificationsListener interface
$webhook = new Webhook($testGateway);
$webhook->getNotificationUrl();
}

/**
* @unreleased
*
* @throws Exception
*/
public function testGetNotificationUrlShouldGenerateValidRoute()
{
$testGateway = new TestGateway();
$webhook = new Webhook($testGateway);
$notificationUrl = $webhook->getNotificationUrl();
$expectedUrl = $testGateway->generateGatewayRouteUrl('webhookNotificationsListener');

$this->assertEquals($expectedUrl, $notificationUrl);
}

/**
* @unreleased
*
* @throws Exception
*/
public function testGetNotificationUrlShouldGenerateValidRouteWithArgs()
{
$testGateway = new TestGateway();
$webhook = new Webhook($testGateway);

$args = [
'notification_type' => 'payments',
'payment_id' => '123',
];

$notificationUrl = $webhook->getNotificationUrl($args);
$expectedUrl = $testGateway->generateGatewayRouteUrl('webhookNotificationsListener', $args);

$this->assertEquals($expectedUrl, $notificationUrl);
}
}

0 comments on commit a617c1a

Please sign in to comment.