How to respond api call and then execute code in php - php

I have to connect api that states:
Our system sends events to your application, it does
so in a manner that it expects an immediate reply but does not wait
for all processing to take place. When an event is received, an
immediate reply is made back to our system to say the message was
received. The acknowledge event call follows to state that the event
has been processed by the third party. Our system has to
process millions of events daily and awaiting for the third party to
finish processing an event while distributing events would cause
unnecessary delay.
Now i am in a problem because i dont understand how can i immediately respond to their server that i have get their data if i used the return keyword in 1st line then it will not execute the code after it and in other cases they have to wait for my process to finish.
I have tried with echo 1. but it does not work.
Note: for successfull response i have to send 1 as integer to let them know that i have get their data

<?php
header('This');
header('That');
echo 1;
flush();
//all your other code....
?>

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.

Call external api in laravel with queue

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..

Best way to send multiple SMS at one click using SMS Gateway

I'm working on a PHP website where when user selects city and submits the form, it will get the users phone numbers from the database for that particular city and sends SMS to them.
I have used cURL functions within a while loop to execute this and it is working fine.
NOTE: My sms gateway provider's API only support one number at a time per http request.
Now my question is if the number of user in the selected city is more than 10000 or 50000, then using cURL method will take much time to execute since it's in the loop? But the user wont like wait for the completion of loop. Is there any better way than doing this, like when user submits the request, something in backend (like job) will execute the function and even if the user closes the page, that job will execute till it completes the loop.
my solution was this:
--page /sendsms/?phone_nr=1122 sends one sms to phone nr 1122
--page /sendbulk has a loop which calls in php the page /sendsms/?phone_nr=1122 with file function file('http://www.domain.com//sendsms/?phone_nr=' . $phone_nr[$i]);
--the client on submit will call with ajax(no return needed) the page /sendbulk
This way even if the client closes the connection the server will continue to run /sendbulk in background and will send all messages
You can store detail of sms and city in another table, than set one Cron that take the all Number from the basis of City detail and Send To Sms Gateway , set that row as sent..

How do I mock up an extended processing response from authorize.net?

We have an eCommerce site that is using Authorize.net as a payment gateway.
We have recently run into the issue of people submitting a payment confirmation, and then clicking submit again later on. This is resulting in a double-product situation, and/or double payment.
Some things to consider:
The payment confirmation page is a result of a PRG (Post-Redirect-Get) which is loaded before the user submits their payment
We have functionality in place that will actually act on each part of the request (see below for description of this)
This situation will only apply when Authorize.net transactions take longer than normal.
This is NOT in production yet, we are merely looking for a way to test this new functionality to prevent this behavior.
The Prevention Piece
We have a multi-step checkout form that follows the following process:
Product selection
Payment entry
Confirm Order / Submit Payment
Receipt
Each step of the process executes a call to a service that checks to see if the user has a current 'order' that has one of several statuses: started, processing or complete.
If the order is started, it will redirect them to the first page of the checkout flow.
If the order is processing, it will redirect them to a placeholder page that executes an ajax request every 2 seconds to check the status of the order. When the order is complete, they are redirected to the receipt page.
If the order is complete, they are redirected to the receipt page immediately.
The Problem
Since this functionality is really only valid when the processing transactions take a bit longer, it gives us problems testing it - for a few reasons:
Our dev server is slow, and it is more likely than not that Authorize.net will respond to our request before the page is even rendered by the application.
If we dummy the response up by using the PHP function sleep(), it blocks the thread and nothing runs, and we are in the same boat as [1].
What we hope
I do not know if there is a way to make Authorize.net respond to requests in a slower manner via some parameter, or if there is another way to accomplish this. I welcome ANY and ALL ideas!
You shouldn't be testing directly against Authnet for this. Make your own Authnet "server" and have it delay its response to you. For unit test I wrote for my Authnet library I created my own fake server which sends back the appropriate response I need to test. You can do the same and have the "server" wait as long as you need to before sending back a response. The response doesn't have to be real for you to test a delay.

Trigger / Delay PHP script processing

Currently I have a script which processes orders after someone is directed to a payment processor.
The payment processor will occasionally send two (or more) responses to my site (usually within a minute of the first response).
I have contacted the payment processor about the issue; they don't see this as an issue with their system and say my script should take into account any extra responses and only listen to the last one sent.
On multiple response orders, the first response is successful, and so the order is processed when this response comes in.
If a 2nd response comes, its usually about 30 seconds later with an error response and I cancel the order.
The problem is that I have already sent out a "thank you for your order" email to the customer from the first response, which obviously isn't ideal.
Is there a way to trigger a separate script to run a few minutes after the order is processed? i.e to check if the order is still valid a couple of minutes after the original order and send the email out
I was thinking a cron job which runs every minute, but surely this would be overkill since the script only needs to run if an order has been placed?
You can start background job using shell_exec("/usr/bin/php /path/to/test.php ".($orderId)." &"); with 3 minute delay at the start (sleep(180)); and then execute code to check status of the order. $argv[2] should contain your orderId number.
Heres what you can do
after the first response is recieved, make your script wait for some time, say 1 minute using the sleep() function
http://php.net/manual/en/function.sleep.php
Why can't you change you message and notify a person with an email after second request came from your payment processor?
As I understand - your processor is already calling some URL on your site while sending second response. So that is a trigger you need. If they have no such callback functionality - you may just install some cron to which will go through not completely processed orders and check payment statuses for such orders and notify customer with another email.

Categories