neyric/mangopay-bundle

MangoPay bundle for Symfony

v1.2.0 2021-01-08 11:16 UTC

This package is auto-updated.

Last update: 2024-12-08 20:30:56 UTC


README

A Symfony bundle for MangoPay, providing additionnal services on top of the official MangoPay PHP SDK

  • Provide a service to access the API for a better Symfony integration
  • Add useful commands
  • Webhook handler (HTTP controller) + Webhook Event

Requirements

  • Php 7.1
  • Symfony 4.4

Installation

$ composer require neyric/mangopay-bundle

Load the bundle in your app

$bundles = [
    // ...
    new \Neyric\MangoPayBundle\NeyricMangoPayBundle(),
];

Configuration

The bundle (in particular the MangoPayService), expects those 3 environement variables to be defined

Accessing the MangoPay API

Once configured, you can use Symfony dependency injection to access the API from the sdk:

use Neyric\MangoPayBundle\Service\MangoPayService; 

class MyService
{
    public function __construct(MangoPayService $mangoPayService)
    {
        $this->mangoPayService = $mangoPayService;
    }

    public function someMethod()
    {
        // You can access the public `api` property, which is the
        // configured instance of MangoPay\MangoPayApi 
        $hooks = $this->mangoPayService->api->Hooks->GetAll();
    }
}

Symfony Commands

Display a list of installed hooks :

php bin/console neyric_mangopay:hooks:list

Display rate limits (performs 1 api call) :

php bin/console neyric_mangopay:ratelimits

Using the webhook handler

First, setup the route in your routes.yaml file :

neyric_mangopay:
    path: /mangopay_webook/hook_handler # Customizable url
    controller: Neyric\MangoPayBundle\Controller\HookController::hookHandlerAction

Then register the webhook, you want using the Mangopay console.

Create a subscriber

use Neyric\MangoPayBundle\Event\MangoPayHookEvent;

class MySubscriber implements EventSubscriberInterface
{
    
    public function onMangopayKycSucceeded(MangoPayHookEvent $event)
    {
        // ...
    }

    public function onMangopayUboDeclarationValidated(MangoPayHookEvent $event)
    {
        // ...
    }

    public static function getSubscribedEvents()
    {
        return [
            'MANGOPAY_KYC_SUCCEEDED' => ['onMangopayKycSucceeded', 1],
            'MANGOPAY_UBO_DECLARATION_VALIDATED' => ['onMangopayUboDeclarationValidated', 1],
        ];
    }
}

And eventually declare the service with the kernel.event_subscriber tag :

    App\Subscriber\MySubscriber:
        class: App\Subscriber\MySubscriber
        tags:
            - { name: kernel.event_subscriber }

License

neyric/mangopay-bundle is distributed under MIT license, see the LICENSE file.

Contacts

Report bugs or suggest features using issue tracker on GitHub.