Por Oliver Servín en English

Handle Paddle's marketing consent with Laravel

On Paddle, a buyer can opt-in to receive marketing emails and is stored as a new audience member.

But, by using an event listener, you can handle the Paddle's marketing consent on Laravel Cashier too.

The following example code uses a subscribed user attribute, but you can use an external service or tool like Spatie's Mailcoach and have it synced.

<?php

namespace App\Listeners;

use App\Models\User;
use Laravel\Paddle\Events\WebhookReceived;

class PaddleEventListener
{
    public function handle (WebhookReceived $event)
    {
        if ($event->payload['alert_name' ] === 'new_audience member') {
            if ($user = User::whereEmail($event->payload['email'])->first()) {
                $user->fill([
                    'subscribed' => $event->payload['marketing_consent'],
                ])->save();
            }
        }

        if ($event->payload['alert_name'] === 'update_audience_member') {
            if ($user = User::whereEmail($event->payload['new_customer_email'])->first()) {
                $user->fill([
                    'subscribed' => $event->payload['new_marketing_consent'],
                ])->save();
            }
        }
    }
}