View Single Post
  #1  
Old 08-26-2006, 09:23 AM
 
vanarie vanarie is offline
 

Member
  
Join Date: Jun 2005
Posts: 21
 

Default Subscription payments based on initial pay date: DONE

I only use xcart for subscriptions, so it was important that my customers be charged on a month to month basis based on when they last paid. After sifting through the payment process, I finally setup an auto-charge routine that charges based on the intial signup date.

This mod also sends the subscriber your fancy (rich text) invoice after each charge.

Currently, xcart's native subscription management charges ALL your subscribers a franctional amount based on how many days of the month they have been using the subscription. This is not standard for this type of service, hense this update.

I hope that this is written into the next "official" release of xcart. If you're not familiar with coding in PHP, you'll probably need some help.

The key file is xcart/admin/pay_subscriptions.php
X-Cart Version 4.0.14
The version I modified is:
# $Id: pay_subscriptions.php,v 1.23.2.6 2005/03/07 12:08:11 mclap Exp $

Steps:
1) make a copy of xcart/admin/pay_subscriptions.php
2) name it what you want, like autopay_subscriptions.php
3) make sure you setup your subscription key (ie pay_subscriptions.php?key=<your key here>) in the xcart config
4) replace everything in your new file with the mod EXCEPT for the function cc_processing($subscriptionid)
5) to run, the url would be like this: www.yourdomain.com/xcart/admin/autopay_subscriptions.php?key=<your key here>&charge=1
5a) if you want a display of your subscription customers, run it without the "&charge=1"
5b) to automate this, setup a CRONTAB job on your server and run nightly


Code:
#
# $Id: pay_subscriptions.php,v 1.23.2.6 2005/03/07 12:08:11 mclap Exp $
#

include "./auth.php";
include $xcart_dir."/include/get_language.php";

#
# Access verification
# (see section "Modifying and Deleting Products" in X-Cart manual for more details)
#

$is_admin_logged = !empty($login) && ($login_type=="A" || ($active_modules["Simple_Mode"] && $login_type=="P"));

if (!$is_admin_logged && (!isset($HTTP_GET_VARS['key']) || empty($HTTP_GET_VARS['key']) || $HTTP_GET_VARS['key'] != $config["Modules"]["subscriptions_key"]) ) {
func_header_location("error_message.php?access_den ied&id=12");
}

$date_format_short = "m/d/Y";
$date_format_long = "m/d/Y H:i:s";

#
# Enable auto addition of the new pay period for product subscription
#
$prolongate_subscriptions = "Y";

$logfile = $xcart_dir.$log_dir."/pay_subscriptions.log";

// START subscription handling
$curdate = mktime(0,0,0,date("m"),date("d"),date("Y"));

$str = "-------------------------------------------------------------\n";
$str .= date($date_format_long,time())." - script launching time\n";

$subs_pg_avail = func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[ccprocessors] WHERE processor='".$config["active_subscriptions_processor"]."'");

// setup unix timestamp of today (minus 1 month)
$monthback2 = mktime(0, 0, 0, date("m")-1, date("d")-1, date("Y"));
$monthback = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));

$query = "select * from $sql_tbl[subscription_customers] WHERE last_payed_date < '$monthback' AND subscription_status = 'Active' ORDER BY last_payed_date";
$pending = func_query($query);

if($charge){
foreach($pending as $p){

// display info on people being charged
echo $p["subscriptionid"] . " - " . date('m.d.Y H:i:s', $p["last_payed_date"]) . " - " . $p["login"];
echo "<br>";

$subscriptionid = $p["subscriptionid"];

#
# Get info about subsription products
#

$query = "select p.productid, p.product, sc.login from $sql_tbl[subscription_customers] as sc, $sql_tbl[products] as p where sc.subscriptionid='$subscriptionid' and sc.productid=p.productid";
$product = func_query_first($query);

$str .= "#$product[productid] $product[product]:\n";

$login_nm = $product[login];

$query = "select * from $sql_tbl[customers] where login='$login_nm'";
$cust = func_query($query);

$orderid = cc_processing($subscriptionid);

$mail_smarty->assign ("str", $str);
if($config['Email_Note']['eml_recurring_notification'] == 'Y'){
func_send_mail($config["Company"]["orders_department"], "mail/recurring_notification_subj.tpl", "mail/recurring_notification_admin.tpl", $config["Company"]["orders_department"], true, true);
}

$fp = func_fopen($logfile, "a+", true);
fwrite($fp, $str);
fclose($fp);
@chmod($logfile, 0666);

echo "<pre><tt>$str</tt></pre>";

if(stristr($str, 'Status:....P')){

#
# Mail template processing
#

$query = "select sc.*, c.* from $sql_tbl[subscription_customers] as sc, $sql_tbl[customers] as c where sc.login=c.login and sc.subscriptionid='$subscriptionid'";

$userinfo = func_query_first($query);

$order_data = func_order_data($orderid);
$mail_smarty->assign("products",$order_data["products"]);
$mail_smarty->assign("giftcerts",$order_data["giftcerts"]);
$mail_smarty->assign("order",$order_data["order"]);
$mail_smarty->assign("userinfo",$order_data["userinfo"]);

$prefix = ($order_status=="I"?"init_":"");

#
# Notify customer by email
#

$to_customer = ($userinfo['language']?$userinfo['language']:$config['default_customer_language']);

$mail_smarty->assign("products", func_translate_products($order_data["products"], $to_customer));
func_send_mail($userinfo["email"], "mail/order_customer_subj.tpl", "mail/order_customer.tpl", $config["Company"]["orders_department"], false);
}else{

// do something if CC charge fails

}
}

}else{

$query = "select * from $sql_tbl[subscription_customers] WHERE subscription_status = 'Active' ORDER BY last_payed_date LIMIT 90";
$pending = func_query($query);

$time = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
$top = "Date: ". date('m.d.Y', $time) . "<br>";
$top .= "<b>Pending Charges...</b><br>";
$top .= "<table>";

$hdr2 = "<tr><td> <U>Last Charge</U> </td><td> <U>Next Charge</U> </td><td> <U>Login</U> </td><td> <U>ProductID</U> </td><td> <U>Price</U> </td>";

$hdr2 .= "</tr>";

$total = 0;
$body = "";
foreach($pending as $p){
$nextcharge = mktime(0, 0, 0, date('m', $p["last_payed_date"])+1, date('d', $p["last_payed_date"]), date('Y', $p["last_payed_date"]));

$product = func_query_first("select p.product, s.price_period from $sql_tbl[products] as p, $sql_tbl[subscriptions] as s where p.productid=s.productid and s.productid='".$p["productid"]."'");

$total = $product["price_period"];

$body .= "<tr><td>" . date('m.d.Y', $p["last_payed_date"]) . " </td><td> " . date('m.d.Y', $nextcharge) . " </td><td> " . $p["login"]. " </td><td> " . $product["product"]. "</td><td> " . $product["price_period"] . " </td></tr>";

}

$body .= "</table>";

echo $top.$hdr2.$body;

}

// put your function cc_processing($subscriptionid) below
__________________
Never miss a good chance to remain silent.
X-Cart Version 4.0.14
Reply With Quote