type:
integration
system:
PHP5
name:
Exmaple subscription datafeed processor
description:
For v050+, should give you some ideas for automating your billing and payment card reminders.
tag:
datafeed, subscriptions, php
date:
2009-03-15
version:
050
developer:
https://www.foxycart.com

Subscription Datafeed

Please note: The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please post in our forum, but if we cannot offer assistance (due to unfamiliarity with this particular system or language) we apologize in advance.

Description

Here's an example script to get you started with parsing the subscription datafeed. Note, this should be the same file that you use for parsing your normal FoxyCart datafeed as well.

Installation

This code (heavily modified) will live on your server and you'll put the URL in the FoxyCart admin under the advanced section for datafeeds. Please test your script throughly before turning on your datafeed in the FoxyCart admin.

Requirements

PHP5+

Code

<?PHP
 
$DataFeedKey = 'XXXXXXXXXXXXXX YOUR DATAFEED KEY HERE XXXXXXXXXXXXXX';
 
$failedDaysBeforeCancel = 30;
$billingReminderFrequencyInDays = 3;
$updatePaymentMethodReminderDaysOfTheMonth = array(1,15);
 
if (isset($_POST["FoxyData"])) {
 
	// normal datafeed stuff here...
 
} else {
	// see if we're doing a subscription
	if (isset($_POST["FoxySubscriptionData"])) {
		$FoxyData_encrypted = urldecode($_POST["FoxySubscriptionData"]);
		$FoxyData_decrypted = rc4crypt::decrypt($DataFeedKey,$FoxyData_encrypted);
 
		// make sure we have a valid character encoding
		$enc = mb_detect_encoding($FoxyData_decrypted);
		$FoxyData_decrypted = mb_convert_encoding($FoxyData_decrypted, 'UTF-8', $enc);
		$FoxyDataArray = new SimpleXMLElement($FoxyData_decrypted);
		foreach($FoxyDataArray->subscriptions->subscription AS $subscription) {
			foreach($subscription->transaction_details->transaction_detail AS $transaction_detail) {
				// do stuff here, collect data, etc...
			}
			$canceled = 0;
			$sendReminder = 0;
			if (date("Y-m-d",strtotime("now")) == date("Y-m-d", strtotime($subscription->end_date))) {
				// this entry was cancelled today...
				$canceled = 1;
			}
			if (!$canceled && $subscription->past_due_amount > 0) {
				$failedDays = floor((strtotime("now") - strtotime($subscription->transaction_date)) / (60 * 60 * 24));
				if ($failedDays > $failedDaysBeforeCancel) {
					$canceled = 1;
				} else {
					if (($failedDays % $billingReminderFrequencyInDays) == 0) {
						$sendReminder = 1;
					}
				}
			}
			if ($canceled) {
				// send emails, update your database, etc...
			}
			if ($sendReminder) {
				// send reminder emails, etc...
			}
		}
		// send emails to customers with soon to expire credit cards. Ignore already expired cards, since they should have already been
		// sent an email when their payment failed.
		if (in_array(date("j"),$updatePaymentMethodReminderDaysOfTheMonth)) {
			foreach($FoxyDataArray->payment_methods_soon_to_expire->customer AS $customer) {
				if (mktime(0,0,0,$customer->cc_exp_month+1, 1, $customer->cc_exp_year+0) > strtotime("now")) {
					// email reminders
				}
			}
		}
	} else {
		print "error";
	}
}
?>

Site Tools