2. Usage

This bundle provide the services which help to interact with PaymentsHub. It also allows you to create your custom implementations to interact with different/custom subscriptions systems.

2.1. How to add a new adapter

Adapters are used to retrieve the subscriptions data from the external subscription system. It is possible to implement your custom adapter and use it to fetch the subscriptions data from the 3rd party subscription system.

  1. Create your custom adapter class which uses GuzzleHttp client
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// src/AcmeBundle/Adapter/CustomAdapter.php

namespace AcmeBundle\Adapter;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use SWP\Component\Paywall\Factory\SubscriptionFactoryInterface;
use SWP\Component\Paywall\Model\SubscriberInterface;
use SWP\Component\Paywall\Model\SubscriptionInterface;

// ...
final class CustomAdapter implements PaywallAdapterInterface
{
    public function __construct(array $config, SubscriptionFactoryInterface $subscriptionFactory, ClientInterface $client)
    {
        $this->config = $config;
        $this->subscriptionFactory = $subscriptionFactory;
        $this->client = $client;
    }

    public function getSubscriptions(SubscriberInterface $subscriber, array $filters = []): array
    {
        // custom logic here to get subscriptions
        // ...

        $subscription = $this->subscriptionFactory->create();

        // ...
    }

    public function getSubscription(SubscriberInterface $subscriber, array $filters = []): ?SubscriptionInterface
    {
        // custom logic here to get a single subscription
        // ...
    }

    // ...
}
  1. Register your adapter as a service
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# services.yml
AcmeBundle\Adapter\CustomAdapter:
    arguments:
        -
            serverUrl: "%env(resolve:PAYWALL_SERVER_URL)%"
            credentials:
                username: "%env(resolve:PAYWALL_SERVER_USERNAME)%"
                password: "%env(resolve:PAYWALL_SERVER_PASSWORD)%"
        - '@SWP\Component\Paywall\Factory\SubscriptionFactory'
        - '@GuzzleHttp\Client'
  1. Enabled newly created adapter in bundle’s config
1
2
3
# config.yml
swp_paywall:
    adapter: AcmeBundle\Adapter\CustomAdapter