Run php script in background on nginx - php

I'm trying to develop a payment module for one shop cms.
Payment is based on acquiring in a Bank.
So firstly for each order creates (with curl) a "ticket" on bank side. Ater that client is forwarded on payment page on bank server with its ticket id. If customer pays and push button "go back to store" everything is fine.
But if customer pays and don't push this button i need to check the result (if he paid or not). It is also made by curl. I'm requesting info by ticket id and receive an array with parameters i need among them is result code i need to check.
So i need to check it automatically (for ex. 1 time each 5 mins during an hour).
Cron can't help me coz it is all dynamically processes.
I created a function that returns me the result. But how can i start it in background with any interval?

Using cron. PHP does not offer anything like scheduler because it is out of scope of the language. You may use DB to store some of parameters needed to the whole process.

I'll extend a little bit WebnetMobile answer on using cron for what you want.
Likely you have a table in your DB where you store are tickets you get from Bank and whenever they suceed or did not suceed as well the date the ticket was created.
Then, if what you want is to check every five minutes from bank ticket creation date whetever it suceed or not if unknown, you should run a cron each minute that will do the following:
Check if there are bank tickets without defined state (Suceed or Failed).
Check if time created (without seconds) minus current time (without seconds) MOD 5 == 0.
If true, means another 5 minutes period have passed from that ticket creation and should be checked, Else do nothing.
I think this will do what you want

Related

Cron-Job Issue with double Proccesses

We have a business, where we send API request to generate digital products. We have 2 jobs, one order based and one cron-job to process open orders.
I don't know where i should search the Problem.
Problem is;
the Cronjob double processed now 3 times (on 3 different days) products.
Example: Our Shop asks with a simple JSON POST the other system to get the Products by API. This is initiated by the Cronjob. The other System always makes, what the request incoming want.
If the Shop asks for 1 product, the system ordered two. Two similar requests are made to the API.
I don't know if this Problem is coming from;
the store (where the Cron Job Script is started)
the System
somewhere else
It is suspicious, that not the whole Job is done twice, it was only for some orders and some products. And not always. Does any one had a some Problem or a probably solution for that?
We already have some Security checks to prevent that, but it looks like it doesn't help. (The double processing is very bad, the products are like money (giftcards), if a customer get's to much of the digital cards and he redeem it immediatly, the money is lost).

How to check wallet for balance?

I am having trouble because I have a website in PHP and I am using a library to communicate with my bitcoin server node over the RPC.
Everything is good and bitcoin can communicate with the website. I am generating new wallet using the command
getnewaddress "label"
The bitcoin then returns the new bitcoin address with label what I sent over the RPC and that works really good and very well. The thing is that I must check that wallet next 15 minutes and after 15 minutes if user submit the form with button "paid" I must once again check for balance of the wallet, if the wallet has the right balance server will send him the product over email.
In the past I know we could use watch wallet but as that has been deleted from the RPC I am having trouble with checking that wallet, also what would be the best approach of the checking something with PHP in x time?
I would suggest you regularly run bitcoin RCP command listsinceblock. Keep track of the last block you checked in a database or cache, and run this command in your preferred interval (example: 15 minutes). This will return all new transactions since the last time you ran the command.
If there are transactions, the destination wallet address will be included in the commands response.
Wiki explains the command pretty well:
listsinceblock [blockhash] [target-confirmations]
Get all transactions in blocks since block [blockhash], or all transactions if omitted. [target-confirmations] intentionally does not affect the list of returned transactions, but only affects the returned "lastblock" value.
Source

Update database after 5 minutes

I've created a IPN code that allows paypal to update my database for a item purchased. I am thinking ahead though that the person buying my item, might not confirm upon checkout, or that while they are confirming check out, someone else might click the buy it now button as well before it's confirmed via paypal.
The point to all this is, I want to figure out the best way to fix this. My idea is after they click the buy now button it sets the item to pending, and they have 5 minutes to confirm the purchase or it's sent back to available.
Please help.. I can provide my code if needed.
I'd suggest using something like Resque - there is a PHP port (https://github.com/chrisboulton/php-resque).
When the user goes to PayPal, set the status of the product to be 'reserved', and create a Resque job that's scheduled to run 5 minutes from now.
If the user buys the product, change the status from 'reserved' to 'sold'.
When the Resque job runs after 5 minutes, it checks whether the status is 'reserved', and if it is, it changes it back to 'available'.
If the status is 'sold', then it does nothing.
If you don't want to use Resque, then you could do something a bit more simple like a cron job that selects all products that with a status of 'reserved' where the status was set more than 5 minutes ago. Then you change that status to 'available' again. You can run that cronjob every 60 seconds.
However, this can get into complicated race conditions, and it will be less flexible, so I'd implement a queue-based system if you can.
First sorry that I cant comment, cause of my lvl here. So your idea with locking it is user friendly and 5 minutes would be okay. But I would PayPal update my stock, when the payment is finished. who pays first... But I would program a ajax script which checks the stock for the user, who has this item in the basket and when the stock changed to 0 i would do an alert message like
alert('Item Xy changed his state to 0');

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

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