PHP:update table automatically in mysql - php

I am building a simple coupon code buying application with apis.
Here is the work flow
User can select a coupon (getcoupon api)
If a coupon is obtained and its marked as pre_book then go the payment api for the payment.
If payment is success then coupon marked as booked
This works fine but i need help in the following:
If user is prebooked the coupon and aborted the payment i want to mark coupon code status as avaliable after sometime (say its 15 min) this should also happens when payment fails.
Structrue of coupon_code table
1.id-PK
2.coupon_code
3.status
Can i use a crone job for this?? but i don't think its possible because its not a periodic update .

Save the datetime of the payment attempt in the database
Create a cronjob that runs every minute
Update the table and set the payment_date as NULL using the following WHERE in your update query WHERE payment_date < DATE_SUB(NOW(),INTERVAL 15 MINUTE)

Related

Laravel4 Date based actions

I have a Mysql Table with columns below
ID Amount Charge_Date
1 200 12-Apr-2015
2 300 10-May-2015
I am trying to write code so that when date reaches today, the amount is debited in a new table. eg lets say today is 12 Apr then Rs 200 is debited to account which is a new table.
Please help me with the idea. Should I set a Cron Job which will run everyday and check if date is today. Or is there a better way of calculating such things.
Also need help with setting recurring events like everyday I need to calculate outstanding from a table and send email to customers.
Cron seems to be the only way out. Just need to set cron from cpanel and it will run for any desired controller

Auto Insert In Database Every Month

I want a functionality that insert data every month...
I have some idea to implement this that is as follow....
When User Create 'INVOICE' at that time the 'INVOICE' automatically generated every next month once user create it.
Let have some code...
INSERT INTO INVOICE (id,user,date,bill_money) VALUES ('','XYZ','25/03/2015','100');
this one works very well now every next month same entry should be automatically inserted..
For that i have logic that when user log-in into portal at that time first i have retrieve user's last log-in and from that date to currently log-in date i try to insert those data which are 30 days old data...
I select next day(date) after user's last log-in date and then Check for every that up to current day(date).
To retrieve user last log-in date
echo $this->session->userdata('name')
To check interval lastlogindate() + INTERVAL 30 DAY == NOW()
If this becomes true then then insert data
But here problem is that i want to implement for month, quater, year
Is this flow is better or there can be another way to do this..??
I heard about cron job and MySQL Event which one is best in this two and how they are working which one is effective performance wise...
I want suggestion on this. Thank you....
If you want a recurring invoice. Just store the invoice once and schedule a cron job that will run daily at scheduled time. Cron job will run your php script to do whatever you want to do like : store invoice in db or email it to user. If you don't know about cron jobs, basics can be found in this answer : How to create cron job using PHP?
EDIT : you will have to schedule cron job using cli
Mysql events
I think this is better for me i haven't use Cron Job...
for Mysql event checkout syntax and have a fun...

How to auto update account balance in database every 30 days

I have a customer table in my database. This table contains customer information including his/her balance.
Now I want to add his/her balance after every 30 days depends on what promo or plan he/she applied
example: he applied for a plan 1599 so after every 30 days his balance must add the price of the plan he applied.
current balance = 0
after 30 days balance = 1599
How will i do this?
You can create a cron job for the same. Check the date difference for every user and if its greater than 30 days add the balance to the respective user's account.
You can do this like
Store the Date when user choose plan by using date('Y-m-d') in a variable
Add 30 Days in it by using date('Y-m-d',strtotime("+7 day", $date)) and save this in database
Write a query to check that date of today is equal to that stored date or not if so then add points to that account.
For point 3 you may also need cron job depends on your requirement.
If still need help feel free to comment

Programming logic - update the database only one time after specific time interval

I am developing ecommerce store in php and I have some problem in creating a logic. The problem is I have a store page where I am showing some products. all the products have some time interval,after interval passes the products will no longer be display there.
For example
Product: jeans
time left: 10 days.
after 10 days jeans product will no longer be there. in database I have a set a field with the name active_status which accepts Y or N..
I know that I can simply run the update query and set the status to "N" after time passes. here in this example after 10 days
BUT the question is WHEN DO I RUN THIS UPDATE QUERY ?
should I always check time and run again and again update query and set STATUS TO 'N'??? IS that is the only solution ?
I mean usually we do like for example if customer logins we set some status or any other event but here we are setting the status against checking the time. Hopefully you have understand my question
In the db I am saving the start time and number of days which user puts through the admin panel
My first shot would be cron, php script and properties table (if needed, because for simple uses you could just store expiration date inside business entity).
Cron runs php script periodically (e.g. once a day),
scripts checks if there is anything to delete, based on properties table, or entity properties.
If there is anything to delete, script performs deletion of selected content.
That's all and it is in fact very popular scenario.
More on cron: http://www.pantz.org/software/cron/croninfo.html
Here is my logic i hope it will helps i think
While Publishing the product we have to maintain the time interval of that product for example if you want to display the product for 10 days give 10 days and date of publish product.
By comparing with that date and number of days given for time interval with the present date i.e today's date
Can you check with this

how to restrict user for 10minutes for payment on paypal

I am working on booking website. When any user come to site, select any date & time for booking and go to paypal for payment. Before going to paypal, I am adding this entry to booking, so any other user can not go for booking on that specific time for that date.
Now if user does not make payment on paypal and stay idle. In this case that time gets blocked.
I want user should have 10 minutes to make a payment on paypal. If time exceeds user should redirect to my website with any flag, so I can remove that booking and that time - date will be available for booking to users.
I can use cron job which can remove bookings which are in pending status in database for more than 10 mins.
Major problem I am facing is
1) When I go to paypal for payment and stay idle, paypal automatically redirect after 5 min (giving error of session time out)
2) But when I go to paypal and log in with sandbox account and then stay idle, if after 5 min, I try to do any activity, it gives error of time out, and redirect to login screen of paypal. So it allow me to login again and proceed for payment.
By this way user can even pay after 1 hour. If I delete booking entry using cron, it will create conflict because payment is completed and there is no entry in database.
I want way to explicitly redirect user from paypal to my site after 10 mins.
<?php
// Create and start timer firing after 10 minute
$w1 = new EvTimer(600, 0, function () {
echo "10 minute elapsed\n";
//Your Condition
});
?>
use Timer and Write your condition in the Timer and check the user payment status
What you could do is store the booking in a table in your database, then have a cron job that runs every minute or so and deletes any entries older than 10 minutes:
mysql myDatabase < 'DELETE FROM bookings WHERE (now() - time_the_booking_was_made) > (120*60)'
Bookings in the database should not be displayed to other users as those bookings would be in the 10 minute reserved state. If you need the booking to become instantaneously available to other users after the 10 minute time period, you could save it to the table and run a timer like in the answer above that would remove the booking after the specified time.
Hope this may give you some idea :
1> After selecting date&time, you are adding entry to booking. Now before adding entry check whether the same 'date&time' is already present in database (because many may be booking simultaneously). If present 'display' message to users that it is already booked (redirect to booking page). If it is not present then add entry to database.
2> Create column 'status' in database entry table, While adding 'booking entry' to database , set 'status' as 'pending or 0'. At this time other users cant able to book this entry(same date&time).
3> After successful payment set 'status' as 'confirmed or 1'. If unsuccessful set 'status' as 'denied' or '-1'
4> I guess paypal has default time limit.
5> So, when the status is 'pending' and 'confirmed'. Others cant book on that.
6> If the status is 'denied' or 'empty' then others can book on that.

Categories