Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: add support to the gateway webhook events API #67

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 59 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/OffSiteGateway/Gateway/OffSiteGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Give\Framework\Support\Facades\Scripts\ScriptAsset;
use GiveAddon\OffSiteGateway\DataTransferObjects\OffSiteGatewayPayment;
use GiveAddon\OffSiteGateway\DataTransferObjects\OffSiteGatewayWebhookNotification;
use GiveAddon\OffSiteGateway\Webhooks\OffSiteGatewayWebhookNotificationHandler;

/**
* @since 1.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace GiveAddon\OffSiteGateway\Webhooks;
namespace GiveAddon\OffSiteGateway\Gateway;

use Give\Framework\Support\Facades\ActionScheduler\AsBackgroundJobs;
use Give\Donations\ValueObjects\DonationStatus;
use GiveAddon\OffSiteGateway\DataTransferObjects\OffSiteGatewayWebhookNotification;
use GiveAddon\OffSiteGateway\Gateway\OffSiteGateway;

/**
* @since 1.0.0
Expand Down Expand Up @@ -32,10 +31,10 @@ public function __invoke(OffSiteGatewayWebhookNotification $webhookNotification)

switch (strtolower($webhookNotification->gatewayPaymentStatus)) {
case 'complete':
AsBackgroundJobs::enqueueAsyncAction(
'givewp_' . OffSiteGateway::id() . '_event_donation_completed',
[$webhookNotification->gatewayPaymentId],
'ADDON_TEXTDOMAIN'
// Handling completed transactions...
OffSiteGateway::webhookEvents()->setDonationStatus(
DonationStatus::COMPLETE(),
$webhookNotification->gatewayPaymentId
);

/**
Expand Down Expand Up @@ -71,10 +70,25 @@ public function __invoke(OffSiteGatewayWebhookNotification $webhookNotification)
<?php
break;
case 'failed':
// Handle failed transactions here...
// Handling failed transactions...
OffSiteGateway::webhookEvents()->setDonationStatus(
DonationStatus::FAILED(),
$webhookNotification->gatewayPaymentId
);
break;
case 'cancelled':
// Handle cancelled transactions here...
// Handling cancelled transactions...
OffSiteGateway::webhookEvents()->setDonationStatus(
DonationStatus::CANCELLED(),
$webhookNotification->gatewayPaymentId
);
break;
case 'refunded':
// Handling refunded transactions...
OffSiteGateway::webhookEvents()->setDonationStatus(
DonationStatus::REFUNDED(),
$webhookNotification->gatewayPaymentId
);
break;
default:
break;
Expand Down
10 changes: 0 additions & 10 deletions src/OffSiteGateway/OffSiteGatewayServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Give\Framework\PaymentGateways\PaymentGatewayRegister;
use Give\Framework\PaymentGateways\Webhooks\EventHandlers\DonationCompleted;
use Give\Helpers\Hooks;
use Give\ServiceProviders\ServiceProvider;
use GiveAddon\OffSiteGateway\Gateway\OffSiteCheckoutPageSimulation;
Expand Down Expand Up @@ -37,15 +36,6 @@ function (PaymentGatewayRegister $registrar) {
}
);

/**
* We are using the DonationCompleted event handler class provided by Give Core to process the
* async background event which is created on the OffSiteGatewayWebhookNotificationHandler class.
*
* A full list of event handler classes can be found on the following link:
* @see https://github.com/impress-org/givewp/tree/develop/src/Framework/PaymentGateways/Webhooks/EventHandlers
*/
Hooks::addAction('givewp_' . OffSiteGateway::id() . '_event_donation_completed', DonationCompleted::class);

/**
* IMPORTANT: remember to remove this hook when removing the OffSiteCheckoutPageSimulation class from your real-world gateway integration.
*/
Expand Down
Loading