Call external api in laravel with queue - php

I am trying to develop site like insurance but I am stuck at one position like after taking input data from user I want to pass those input to multiple different external api and get those all api response on to page but if one of api response fails whole process fails and I also don't want to user to wait for too long. I was thinking of doing with queue but don't how to get response from queue. I even try to set up gearman but it's too complicated to work on. Any help would be appreciated..

You can give a try with Queue Job chaining..so that ur jobs will get dispatched sequentially..
To get ACK for API is being failed or success, create an callback API which will be hit when your queue object is dispatched with required statues..and can write logic whatever you are willing to.
I hope this will help to some extent..

Related

Correct use of webhooks to assure payment integrity

I'm new to webhooks and am not really getting the hang of some points of it. KISS; the current problem is, think about:
a platform
that provides a service X
to book a service X, customer Y has to pay in advance
every payment is authorized first only
every payment is captured after the service has been received
From the booking of a service to the capture of the related payment, only the authorization is handled involving the client-side. All the rest is handled on the server-side.
For every possible case of a booking of a service on my platform, payment authorizations is requested as the first action on the server-side. Adaptations of the DB of the platform are only executed after a successful authorization of the payment from the frontend.
The only fallback webhook I implement is for the case where a customer books a service, authenticates, and then loses connection. Because in this case, the customer would have booked the service, but the platform server could not make the related updates. So the customer will have paid, but not receive his / her service via the platform.
My strategy is thus to implement a webhook to listen for the event of a transaction authorization "completed", and, if no transaction data is found internally, execute what needs to be done.
BUT, two questions popped up:
A) How can I control that a webhook gets executed AFTER the regular server-side script should have been executed? Delay the execution of the webhook script? What are the best pracs here?
B) If A) is possible, isn't it smarter to just cancel the authorized payment in the webhook, instead of coding the completion of every possible transaction via webhook? Already the thing that you lose the entire payload in case of a client who lost connection (the payload that you need to execute the server-side tasks after a payment authorization), and the consequent need of passing the according payload back-and-fourth to your payment API, while ensuring that CID is encrypted etc.; this just sounds like overkill to me.. Was anyone in the same situation, and also decided to just immediately cancel the just-authorized payment in lost connections via webhooks? Or must webhooks generally execute the exact same server-side script that the related server-validation would do? Meaning I have to find a way to pass the payload to my webhook function?
The webhook is your notification that the event has happened - you're under no obligation to perform any processing right that moment, or ever.
If you're using webhooks as a backup to a primary synchronous flow (a good design!), then you can record the event and enqueue for later.
Stick a record somewhere indicating "got this authorization. Check this again in an hour to make sure the customer did the thing."
And to your comment above: you probably don't want your sync and async flows to be the same. Your async backup might involve contacting the customer eg via email, while that's not necessary for the sync flow since the customer is still on session.

How to implement SSE (Server-Sent Events) in PHP Laravel

I am new to PHP Laravel. I have little bit confuse to build SSE in PHP Laravel. I searched around 1 day. but I didn't find any good resources to meet my requirements. Helps and other reference will appreciate
Suggestions
- I have different RabbitMQ queues to listen status of the message.If a new event occurs need
to update the blade(Have different blades included in my page. need to update only one of them).
if you doesn't understand my requirement:
I am looking for I chat system. I need to trigger the status in to my blade. the status will comes in rabbitMQ I need to take the status from there and change in browser. Eg:- If I sent message status will be in wait state. if the message get processed a new event will trigger in the queue I need to pick that status and update in browser this is exact my requirement

Trigger Slack Bot by Direct Message

I'm realy new to this Bot stuff.
I want to create a simple PHP-Skript that is sending an answere every time the Bot is contacted by a user via Direct Message.
I read the Api-Docs but the only thing I found was the /-Command.
Currently I have a Cronjob thats checking the RTM-Api for new messages.
I think there must be a better way then running a Cron-Job every few seconds to check if there is a new message for the Bot.
I found the "EmojiBot" that is exactly responding in the way I want.
Using the RTM API to build a bot will work, but there is indeed an easier way: Use the new Event API from Slack.
The Event API allows you to subscribe to range of events. When the event happens, Slack will automatically call your php script. So you don't need to run a CRON loop. This also works with bot users and direct messages to bot users.
Check out the Event API documentation for details.
You can use Botonomous Framework which supports Events API. You should subscribe to the events, then update the framework configurations which is explained here. Finally you have got a nice event object and based on certain criteria you can send a message back to a Slack channel.

PHP : How to minimize response time after integrating shipping API?

I have integrated five shipping APIs like UPS, FEDEX, PUROLATOR, TNT, CANADA POST into my website in simple PHP. I send HTTP request, and request return in xml format to get shipping rates.
I have created classes for each API and created functions to set login and dimensions parameter dynamically...as following
<?php
if($sub){
$canadaPostRate->setCredentials($CPC_number,$length,$width,$height,$weight,$from,$to);
$fedexRate->setCredentials($user,$key,$account_number,$shipping_number,$weight,$height,$width,$length,$from,$to);
// similary for all other API
}
?>
I have got response from these API correctly but the problem is that it takes too long like it takes minimum 1.5 min to show output.
Is there any solution to minimize response time?
One thing comes to mind (if it's for presentation): don't do 5 external API calls in one request. Use AJAX requests to get the shipping rates individually. Right now, you're waiting for CanadaPost to respond before querying FedEx. If you use 5 different requests, you'll not be waiting for one to finish before starting the other.
But really it's impossible to say without more information. Have you done any tracing on your code to see where the delay is? Is it really just the HTTP request? Is it the way you parse the data? Is it a database connection that's slow? Start logging, with time in milliseconds, at various points in your api calls, such as right before the HTTP request, right after, right before returning the data etc.
You could also use another shipping API to handle these calls all at once. Something like EasyPost or Postmaster?

import the events i get invited to on facebook into my website using php

I would like to basically get all the events that i get invited to on facebook to feed my internal website DB with that info; lets say every day using a cron. the problem is that I think you can get the events if you are the one posting the event not the one receiving the invite.
am I wrong or can someone offer me some insight into how to get this accomplished
You can get the events you have not replied using me/events/not_replied or using Graph Api Explorer.
You can get all upcoming events that you have subscribed/accepted via me/events call. Graph APi Exlporer
and save the time in your db and use since parameter with that time to get latest subscribed events.
If you are running cron job then make sure you are not duplicating or missing some events.

Categories