Skip to content

Instantly share code, notes, and snippets.

@c4y
Last active November 7, 2021 10:37
Show Gist options
  • Save c4y/f1d2adefeb1d33c762cbc58feec1a0b7 to your computer and use it in GitHub Desktop.
Save c4y/f1d2adefeb1d33c762cbc58feec1a0b7 to your computer and use it in GitHub Desktop.
Service
<?php
declare(strict_types=1);
namespace Sineos\UsbWatcher\Controller\Paddle;
use Contao\CoreBundle\Framework\ContaoFramework;
use Sineos\UsbWatcher\Payment\Paddle\Webhooks\HookFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api/paddle/hooks/alert", methods={"POST"}, name="paddle_alert_hook")
*
*/
class AlertWebhooks extends AbstractController
{
public function __invoke(Request $request, HookFactory $hookFactory)
{
$hookName = $request->request->get("alert_name");
$hook = $hookFactory->create($hookName);
$response = $hook->process($request);
return $response;
}
}
<?php
namespace Sineos\UsbWatcher\Payment\Paddle\Webhooks;
use Sineos\UsbWatcher\Payment\Paddle\Webhooks\Subscription\PaymentSucceeded;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class HookFactory implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function create($hook): Hook
{
switch($hook) {
case "subscription_payment_succeeded":
return $this->container->get(PaymentSucceeded::class);
default:
throw new \Exception("Webhook not supported"); break;
}
}
}
<?php
namespace Sineos\UsbWatcher\Payment\Paddle\Webhooks\Subscription;
use Sineos\UsbWatcher\Entity\License;
use Sineos\UsbWatcher\Entity\SubscriptionPaymentSucceeded;
use Sineos\UsbWatcher\Payment\Paddle\Webhooks\Hook;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Contao\CoreBundle\Monolog\ContaoContext;
use Psr\Log\LoggerInterface;
class PaymentSucceeded implements Hook
{
protected EntityManagerInterface $entityManager;
protected LoggerInterface $logger;
public function __construct(
EntityManagerInterface $entityManager,
LoggerInterface $logger)
{
$this->entityManager = $entityManager;
$this->logger = $logger;
}
public function process(Request $request)
{
$response = new Response("subscription_payment_succeeded");
return $response;
}
}
services:
_defaults:
autowire: true
autoconfigure: true
public: true
Sineos\UsbWatcher\:
resource: '../../*'
exclude: '../../{Entity,Migrations,Tests,Kernel.php,Resources/contao}'
Sineos\UsbWatcher\Controller\:
resource: '../../Controller/*'
calls:
- [ setContainer, [ "@service_container" ] ]
Sineos\UsbWatcher\Payment\Paddle\Webhooks\HookFactory:
calls:
- [ setContainer, [ "@service_container" ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment