AidanPT
AidanPT

Reputation: 185

PHP MySQL Deactivating premium membership after a certain amount of time?

I have just recently worked out how to use PayPal IPN to initiate a premium membership when users pay a certain amount for it. Once the user clicks upgrade account, they are directed to PayPal to pay, then the IPN kicks in to update the database to show that the user is premium. I was hoping to have this last for a certain amount of time before their account is returned to normal, yet not sure how to do that? For example, they upgrade, 2 years later, a MySQL update is triggered to return their account to average. Here is the IPN script I am using:

<?php 
include 'core/init.php';

//DONT CHANGE THESE VARIABLES
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$receiver_email = $_POST['receiver_email'];
$username = $_POST['custom'];


if($payment_status =="Completed")
{
$purchase_check = mysql_query("SELECT premium FROM users WHERE username='$username'");
while($row = mysql_fetch_assoc($purchase_check))
{
    $paid = $row['premium'];
}
if($paid=="0")
{
    if($receiver_email=="[email protected]")
    {
        if($payment_amount=="6.99" && $payment_currency=="GBP")
        {
            $update_premium = mysql_query("UPDATE users SET premium='1' WHERE username='$username'");
        }
    }
}
}
else
{
echo "Access Denied!";  
}

?>

I am thinking using my BASIC idea of PHP that I might add a value of "upgrade_date" to the database as a timestamp and then add into my code:

mysql_query("UPDATE users SET upgrade_date=CURDATE() WHERE username='$username'"); 

So updating the date when they upgrade shouldn't be an issue, the issue is how can I recall that information so in 2 years after the time stamp, they are issued back to an average member? I was also hoping to show a countdown date or something so they know how long they have left on their account?

Any help would be appreciated, thanks!

Upvotes: 0

Views: 905

Answers (3)

echo_Me
echo_Me

Reputation: 37233

you maybe need an else condition to check the years.

like that:

  while($row = mysql_fetch_assoc($purchase_check))
{
    $paid = $row['premium'];
                 $timestamp_start = $row['upgrade_date'];
                 $timestamp_end = date("Y/m/d");
                 $difference = abs($timestamp_end - $timestamp_start);
                 $years = floor($difference / (365*60*60*24));
}
if($paid=="0")
{
    if($receiver_email=="[email protected]")
    {
        if($payment_amount=="6.99" && $payment_currency=="GBP")
        {
            $update_premium = mysql_query("UPDATE users SET premium='1' WHERE username='$username'");
        }
    }
}else if($years == 2){ do update here and set premium to 0}
          else{do what you like here}
}
else
{
echo "Access Denied!";  
}

Upvotes: 0

MaveRick
MaveRick

Reputation: 1191

Why don't use unix timestamp instead of using boolean value in the database?

you can just say that the field premium in your database will save the expiration of premium membership

and then you can do the check to see if the current time stamp is greater than the value saved in the field premium then it's expired ... if it is Null or 0 he never been premium, and if it's less than the saved value then his membership is valid and then you can calculate the difference between current timestamp and the saved one to see how much longer it still has to expire.

Upvotes: 1

Pwner
Pwner

Reputation: 771

Why not have in your DB

upgraded = tinyint: to store a 0 for not upgraded and a 1 for upgraded
upgraded_date = datetime: to store the date this was done

Then set-up a cron job to run (maybe a few times a day or just check as the user logs in) to check if the time has expired, if it has reset 'upgraded' to 0

Upvotes: 0

Related Questions