Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Subscription payments based on initial pay date: DONE

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #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
  #2  
Old 08-26-2006, 09:29 AM
 
balinor balinor is offline
 

Veteran
  
Join Date: Oct 2003
Location: Connecticut, USA
Posts: 30,253
 

Default Re: Subscription payments based on initial pay date: DONE

Moving to Custom Mods
__________________
Padraic Ryan
Ryan Design Studio
Professional E-Commerce Development
Reply With Quote
  #3  
Old 08-29-2006, 03:39 PM
 
Impio Impio is offline
 

Newbie
  
Join Date: Apr 2005
Posts: 3
 

Default Re: Subscription payments based on initial pay date: DONE

When I run this module, I am recieving the following error.. Any idea?

Warning: Invalid argument supplied for foreach() in /shop/admin/autopay_subscriptions.php on line 76
__________________
http://www.impio.com
Reply With Quote
  #4  
Old 08-29-2006, 03:49 PM
 
vanarie vanarie is offline
 

Member
  
Join Date: Jun 2005
Posts: 21
 

Default Re: Subscription payments based on initial pay date: DONE

It's hard to say unless you post the array pull around line 76. A good way to test is to manually do the db pull in phpMyAdmin to make sure it actually finds something. If it pulls 0 records, that's the error you will get.
__________________
Never miss a good chance to remain silent.
X-Cart Version 4.0.14
Reply With Quote
  #5  
Old 08-29-2006, 03:52 PM
 
Impio Impio is offline
 

Newbie
  
Join Date: Apr 2005
Posts: 3
 

Default Re: Subscription payments based on initial pay date: DONE

71: $query = "select * from $sql_tbl[subscription_customers] WHERE last_payed_date
"< '$monthback' AND subscription_status = 'Active' ORDER BY last_payed_date";
73: $pending = func_query($query);
74:
75: if($charge){
76: foreach($pending as $p){
__________________
http://www.impio.com
Reply With Quote
  #6  
Old 08-29-2006, 03:57 PM
 
vanarie vanarie is offline
 

Member
  
Join Date: Jun 2005
Posts: 21
 

Default Re: Subscription payments based on initial pay date: DONE

Do a manual query with phpMyAdmin and see if you get a result.
__________________
Never miss a good chance to remain silent.
X-Cart Version 4.0.14
Reply With Quote
  #7  
Old 08-31-2006, 08:33 AM
 
costrevs costrevs is offline
 

Member
  
Join Date: Aug 2006
Posts: 17
 

Default Re: Subscription payments based on initial pay date: DONE

If I set up a subscription product that does not have a setup fee, how do I get the cart and checkout pages to just show the (say) annual fee for the subscription and not the days remaining in the period fee?
__________________
X-Cart 4.1.3 || Suse Linux 9.2 || PHP 4.3.8 || MySQL 4.0.21 || Maine, US
Reply With Quote
  #8  
Old 08-31-2006, 11:43 AM
 
vanarie vanarie is offline
 

Member
  
Join Date: Jun 2005
Posts: 21
 

Default Re: Subscription payments based on initial pay date: DONE

Hmmm. Good question. It would be nice to see an example screen shot. I have a highly modified checkout system and "ezcheckout" mod too, so it's hard for me to answer your question.
__________________
Never miss a good chance to remain silent.
X-Cart Version 4.0.14
Reply With Quote
  #9  
Old 08-31-2006, 12:52 PM
 
costrevs costrevs is offline
 

Member
  
Join Date: Aug 2006
Posts: 17
 

Default Re: Subscription payments based on initial pay date: DONE

I guess in a roundabout way I was trying to figure out how to get X-cart to forget about the whole "days remaining up to end of period" and the fee for the daily use of the subscription, and just apply the single subscription fee, say $20/year, and just use this single subscription fee throughout the checkout process.
__________________
X-Cart 4.1.3 || Suse Linux 9.2 || PHP 4.3.8 || MySQL 4.0.21 || Maine, US
Reply With Quote
  #10  
Old 08-31-2006, 01:08 PM
 
vanarie vanarie is offline
 

Member
  
Join Date: Jun 2005
Posts: 21
 

Default Re: Subscription payments based on initial pay date: DONE

I think a while back I removed it from the smarty template, so it would not show "days remaining up to end of period". There's probably an answer on the board, but I don't know where. I don't remember which file I modified.

Quote:
Originally Posted by costrevs
I guess in a roundabout way I was trying to figure out how to get X-cart to forget about the whole "days remaining up to end of period" and the fee for the daily use of the subscription, and just apply the single subscription fee, say $20/year, and just use this single subscription fee throughout the checkout process.
__________________
Never miss a good chance to remain silent.
X-Cart Version 4.0.14
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 02:05 PM.

   

 
X-Cart forums © 2001-2020