I've been implemented stripe webhook in laravel 7 using library https://github.com/spatie/laravel-stripe-webhooks
The goal is to subscribe my users to plan created in stripe and generate an invoice on charge succeeded webhook request. Now to achieve this I created a cron script that dispatches the job to subscribe users. Also, set up a webhook endpoint in stripe. All setup is done and configuration in environment variables. It works perfectly when set the queue connection to "sync". However, when setting the queue connection to Redis it doesn't work.
Here's the code inside my config/stripe-webhooks.php
<?php
return [
/*
* Stripe will sign each webhook using a secret. You can find the used secret at the
* webhook configuration settings: https://dashboard.stripe.com/account/webhooks.
*/
'signing_secret' => env('STRIPE_WEBHOOK_SECRET'),
/*
* You can define the job that should be run when a certain webhook hits your application
* here. The key is the name of the Stripe event type with the `.` replaced by a `_`.
*
* You can find a list of Stripe webhook types here:
* https://stripe.com/docs/api#event_types.
*/
'jobs' => [
'charge_succeeded' => \App\Jobs\StripeWebhooks\ChargeSucceededJob::class,
// 'source_chargeable' => \App\Jobs\StripeWebhooks\HandleChargeableSource::class,
// 'charge_failed' => \App\Jobs\StripeWebhooks\HandleFailedCharge::class,
],
/*
* The classname of the model to be used. The class should equal or extend
* Spatie\StripeWebhooks\ProcessStripeWebhookJob.
*/
'model' => \Spatie\StripeWebhooks\ProcessStripeWebhookJob::class,
];
And inside the command that dispatches SubscribeCustomerJob:
$subscribed = 0;
$users = User::role('admin')->where(function ($q) {
$q->where('stripe_id', '!=', null)->where('verified_employees', '>=', 5);
})->get();
foreach ($users as $user) {
if ( $user->subscribed('default') && now()->format('Y-m-d') >= $user->trial_ends_at->format('Y-m-d')) {
SubscribeCustomerJob::dispatch($user)->onQueue('api');
$subscribed++;
}
}
Handling webhook requests using jobs
<?php
namespace App\Jobs\StripeWebhooks;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;
class HandleChargeableSource implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
/** #var \Spatie\WebhookClient\Models\WebhookCall */
public $webhookCall;
public function __construct(WebhookCall $webhookCall)
{
$this->webhookCall = $webhookCall;
}
public function handle()
{
// At this point, I store data to Payments table,
// generate invoice and send email notification to subscribed user.
}
}
The output inside the logs of job:
[2021-04-14 07:53:46][Nr84GbvR3kxqnBGRrrWHtkTj34XRYsGv] Processing: App\Jobs\SubscribeCustomerJob
[2021-04-14 07:53:53][Nr84GbvR3kxqnBGRrrWHtkTj34XRYsGv] Processed: App\Jobs\SubscribeCustomerJob
Inside table in webhook_calls:
webhook_calls table click to see it
All thigs works well with queue connection set to sync. However, the problem is when I set queue connection to "redis".
I know webhook call works because there's data inside webhook_calls table but I guess fails to reach the job.
Any idea with stripe webhook and laravel using redis as queue driver, please add your comment below and thanks in advance.
I've fixed this issue it's my bad that I accidentally put an invalid queue name in my queue worker setup in supervisor.
[program:queue-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --queue=api,notification
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/www/html/worker.log
Related
I want to send some sort of (unique, auto-incrementing) number as part of the payload of an event - so that the consumer can, for example, know it should ignore an 'updated' event if the event is older than a previous 'update' event it received.
I see I can add a broadcastWith method to my event, where I could add such a number, which I'm storing in some table.
But, I don't really need to create a new number. The ID of the job in the jobs table will work just fine. So, how can I make Laravel automatically add a property, say order, to this event before it is broadcast and make the value of order to id column from the jobs table? Is there a way to get it in the broadcastWith method?
I had previously thought of using a timestamp as the 'order' but of course that won't help me or the consumer when two events have been created in a short-a timeframe as a computer can create two events.
UPDATE
Looks like I haven't worded it well and people are confused as to what I'm looking for. In hindsight, I shouldn't of added the criteria that it must be the job id that gets included in the payload. The main thing I'm after is a unique, auto-incrementing ID in each broadcast event. For example, I have an UserUpdated event. Say the a user is updated twice - my SPA that is consuming the events needs to know which event is the newer one, otherwise the SPA might display outdated info. If the events are delivered sequentially, then this problem won't happen. But, especially as I'm relying on a third-party service (Pusher) to deliver the events to the SPA, I don't want to assume / trust that the events will always be delivered in the same order they were sent to Pusher.
Hi such a nice requirement, i have been working on a POCO and here are some snippets, you do not need broadcast at all. Of course you need to have the queue worker up and running
Running The Queue Worker
On your order controller, i guess you need the update method to dispatch the job before commiting.
function update(Request $req)
{
$data= Order::find($req->id);
$data->amount=$req->amount; //example field
PostUpdateOrderJob::dispatch($data)->beforeCommit();
$data->save();
}
Important: Why before commit and not after commit: Setting the after_commit configuration option to true will also cause any queued event listeners, mailables, notifications, and broadcast events to be dispatched after all open database transactions have been committed.
Your PostUpdateOrderJob class
?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Order;
use Throwable;
class PostUpdateOrderJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of times the job may be attempted.
*
* #var int
*/
public $tries = 25;
/**
* The maximum number of unhandled exceptions to allow before failing.
*
* #var int
*/
public $maxExceptions = 3;
protected $order;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Order $order)
{
$this->order=$order;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$this->order->update(['jobid'=>$this->job->getJobId()]);
}
public function failed(Throwable $exception)
{
// Send user notification of failure, etc...
//Several options on link below
//https://laravel.com/docs/9.x/queues#dealing-with-failed-jobs
}
}
Well php has a function called time() which returns the current unix time in seconds, so you can just use that in your broadcast event.
In your broadcast event, you can add this time to the public class properties, which would then be available through the event payload:
class MyBroadcastEvent implements ShouldBroadcast
{
public $time;
public function __construct()
{
$this->time = time();
}
}
I'm not sure if this is exactly what you need, your question is kind of confusing to be honest.
I am trying to test if notification is created in the database, in my local, this works as intended however the test environment has some strange behaviour.
$user->notify(new ExampleNotification());
$this->assertDatabaseCount('notifications', 1);
//returns green with QUEUE_CONNECTION=sync but red with QUEUE_CONNECTION=database
...
class ExampleNotification extends Notification implements ShouldBroadcast, ShouldQueue
{
use Queueable;
public function viaQueues(): array
{
return [
'database' => 'notifications'
];
}
/**
* Get the notification's delivery channels.
*
* #return array
*/
public function via(User $notifiable): array
{
return ['database']
}
ps: I do not use Notification::fake() anywhere in my tests
Does anybody know why the different queue connections would behave differently?
Any queue driver other than the sync one works asynchronously i.e. when you send a notification job it will be queued and then the queue worker will remove it from the queue and then send the actual notification.
This means if you send a notification during a test, even if the queue worker is running the notification will most likely not be sent before the test is done.
To avoid this you should either use the sync queue or use Notification::fake() and just check if a notification would have been sent.
I am trying to change the mail driver's username and password on the fly like the following.
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
config(['mail.username' => $this->username]);
config(['mail.password' => $this->password]);
Mail::to('me#me.com')->send(new OrderShipped());
}
The dynamic config setting was inside the queue job. For queue processing, I am using the Laravel horizon. But, when I execute the queue job. It's still using the default config. Not the config I set dynamically.
Note: when I log right after config set it was showing the dynamic value.
How can I set the config dynamically and make Laravel horizon to use dynamic configuration?
From Laravel version 7.x onwards you can now state the mail driver to use while sending an email. All you need to configure all your connections & credentials properly in app/config/mail.php. Once configured, you can specify the name of the driver via mailer() function as below:
Mail::mailer('postmark')
->to($request->user())
->send(new OrderShipped($order));
public function handle()
{
config([
'mail.username' => $this->username,
'mail.password' => $this->password,
]);
}
I did it like that. I think the important part is that you get the data you will use in the Job class from outside.
public function __construct($username,$password)
{
$this->username= $username;
$this->receiver = $password;
}
There are many items in the Items table and there is an expired date column in the items table. I just want to get a push notification every day before each items going expire one date before (Expire dates are different to each other and can have one expire date to multuple items). im new to laravel. Please help me.
This can be simply done with Laravel task scheduling
You need to create a custom artisan command like
php artisan make:command SendItemExpiryEmailsToAdmin
Under App\Console\Commands You will find a class SendItemExpiryEmailsToAdmin should be created.
i) First you need to define the signature of the command which will be used to call from the command line.
protected $signature = 'email:send-item-expiry-email-to-admin';
ii) Inside the handle() of the same class, write your logic. A sample logic is given below. You will need to create a Mailable class to send the mail.
public function handle() {
// To fetch all the items ids which are going to expired today.
$itemsIds = Items::whereBetween('expired',
[Carbon::now()->setTime(0,0)->format('Y-m-d H:i:s'),
Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
->pluck('id')
->toArray();
$itemsIds = implode(" ", $itemsIds);
Mail::queue(new ItemsExpiryEmail($itemsIds));
// If you are not using queue, just replace `queue` with `send` in above line
}
Mailable to send the mail.
i) Run the following command to create a mailable
php artisan make:mail ItemsExpiryEmail
ii) In the mailable, write your code. A sample code is given below, you can use $this->itemIds in the mail view as it is a public variable.
class ItemsExpiryEmail extends Mailable
{
use Queueable, SerializesModels; // don't forget to import these.
public $itemIds;
public function __construct($itemIds)
{
$this->itemIds = $itemIds;
}
public function build()
{
return $this->view('emails.orders.shipped');
return $this->to('test#example.com', 'Admin')
->subject('Subject of the mail')
->view('emails.adminNotification'); // adminNotification will be the view to be sent out as email body
}
}
This command needs to be executed daily using the Cornjob.
Try this, I'm sure this will help. Let me know if you have any questions.
You can create Task scheduler in Laravel. you can get more info from it's doc here: https://laravel.com/docs/5.8/scheduling
1) you get create login in the scheduling code like. get the all the items which is expiring tomorrow, And send the detail of items to the admin email.
if you have some knowledge in user define artisan command you can also implement scheduling-artisan-commands https://laravel.com/docs/5.8/scheduling#scheduling-artisan-commands.
I use Lumen 5.1 and Redis for queues. And I have a pretty standard event handler that should send an email:
<?php
namespace App\Handlers\Events;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Events\UserHasRegistered;
use Illuminate\Contracts\Mail\Mailer;
class SendWelcomeEmail implements ShouldQueue
{
protected $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function handle(UserHasRegistered $event)
{
$user = $event->user;
$this->mailer->raw('Test Mail', function ($m) use ($user) {
$name = $user->getFirstName().''.$user->getLastName();
$m->to($user->auth()->getEmail(), $name)->subject('This is a test.');
});
}
}
The email is sent when I don't use the ShouldQueue interface. But when I push the event handler to the queue (i. e. use the ShouldQueue interface), the email is not sent and I don't get any error messages.
Do you have any ideas how to solve or debug this?
It was not a bug, just an unexpected behaviour.
I am using Xampp on Windows and the php mail driver for development. For some reason the queued mails were not saved in the default mailoutput folder within the Xampp directory. Instead a new mailoutput folder was automatically created within the Lumen directory.
There I found all the missed mails. :)