# Hack 63 Time Your Subscriptions to End on Specific Dates

Use some simple JavaScript and PayPal's trial period to calculate the lengths of new subscriptions, assuring they all expire at the same time.

Imagine you own a diaper service for Rhesus monkeys. Your customers subscribe by the month, and every month some customers allow their subscriptions to lapse. You need to get these customers back on board so you get some help from your brother-in-law Leon, a guy with a knack for bringing monkey owners around. Market research suggests lapsed subscribers are best contacted seven to nine days after dropping the service, just when the smell has started to get the attention of local law enforcement. But Leon doesn't want to call two or three people a day. He'd rather make 60 or 90 calls all at once.

PayPal doesn't offer a feature to set the date a subscription will expire; the subscription expires at a time that corresponds to the date the customer signed up. For example, a monthly subscription started on the 12th will run until the 12th of the next month. But you can use this hack to ensure that every new subscription will be billed on the first of the month, keeping Leon as happy as a Rhesus monkey in a fresh nappy.[1]

[1] No simians were harmed in the writing of this hack, with the possible exception of the author.

#### 6.4.1 Hacking the Trial Period

One handy feature of PayPal's subscriptions is the trial period. It allows you to set an introductory price for new subscribers that changes to the standard rate when the trial period expires. For example, you might offer access to your online information service for \$1 during a three-day trial period, after which the price jumps to \$100 a month.

To time your subscriptions to expire on the same day, bend the terms of the trial period so that each customer is charged a prorated amount for the balance of the month, after which the standard monthly rate kicks in. The JavaScript code makes this easy by completing these tasks:

1. Calculate how many days are left in the current month.

2. Find the prorated price by dividing the monthly subscription fee by the number of days in a month (31 days in this example) and multiplying by the number of days left.

3. Stuff the calculated values into the subscription button when the buyer clicks Subscribe.

Just use this for your subscription sign-up page:

```<html>

<title>Prorated Subscription</title>

<body>

<script language="JavaScript">

function CalcDate( ) {

var subend

//Set the start day to today

today=new Date( )

//Set the end date

//If it is December now, then the ending date needs to be January 1 of

next year

if (today.getMonth == 12) {

subend=new Date(today.getFullYear( )+1, 1, 1)

} else {

subend=new Date(today.getFullYear( ), today.getMonth( )+1, 1)

}

//Set 1 day in milliseconds

var one_day=1000*60*60*24

//Calculate the difference between the two dates, convert to days, and put

it in the day_count variable

var day_count = (Math.ceil((subend.getTime( )-today.getTime( ))/(one_day)))

//Set the subscription fee, then calculate the prorated value

var sub_fee = 10

var prorated_fee = Math.floor(((sub_fee/31)*day_count)*100)/100

//Write the values to the form on click

document.fmSubscribe.p1.value = day_count

document.fmSubscribe.a1.value = prorated_fee

}

</script>

<form action="https://www.paypal.com/cgi-bin/webscr"

method="post" name="fmSubscribe">

<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but20.gif"

onClick="CalcDate( )" border="0" name="submit">

<input type="hidden" name="cmd" value="_xclick-subscriptions">

<input type="hidden" name="item_name" value="Monkey Nappy Service">

<input type="hidden" name="item_number" value="Sub-001">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="hidden" name="a3" value="10.00">

<input type="hidden" name="p3" value="1">

<input type="hidden" name="t3" value="M">

<input type="hidden" name="src" value="1">

<input type="hidden" name="sra" value="1">

<!-- Values for the "trial period" -->

<input type="hidden" name="a1" value="">

<input type="hidden" name="p1" value="">

<input type="hidden" name="t1" value="D">

</form>

</body>

</html>```

#### 6.4.2 Hacking the Hack

PayPal allows you to have two subscription trial periods. If you'd like to offer new subscribers a special rate and also have them all expire on the same schedule, use the first trial period for the discount (or even free) trial and the second trial period to prorate the balance of the month. Set the second trial period to the number of days left in the month after accounting for the days in the first trial.

For a three-day free trial, for instance, the trial period section of the button might look like this:

```<!-- Values for the "trial period" -->

<input type="hidden" name="a1" value="0">

<input type="hidden" name="p1" value="3">

<input type="hidden" name="t1" value="D">

<input type="hidden" name="a2" value="">

<input type="hidden" name="p2" value="">

<input type="hidden" name="t2" value="D">```

 Don't forget to modify the JavaScript code to figure the end date of the second (prorated) trial period, which may fall at the end of next month.

 PayPal Hacks
 Credits
 Preface
 Chapter 1. Account Management
 Chapter 2. Making Payments
 Chapter 3. Selling with PayPal
 Chapter 4. Payment Buttons
 Chapter 5. Storefronts and Shopping Carts
 Chapter 7. IPN and PDT
 Chapter 8. The PayPal Web Services API
 Colophon