Same Date Annual Billing with Chargify
Chargify offers flexible pricing structures for recurring billing. For many things, the built-in configuration will work just fine. Sometimes, though, you’ll need to do a bit of work outside the Admin UI to adapt to your situation.
For example, consider a club whose subscriptions all renew on November 1st. No matter when you join, you pay the full yearly membership dues, plus a startup fee, and then your next payment is due on November 1st.
Setup
To accomplish this, first we’ll set up a Club Membership product that costs $35.00 per year with a $10.00 setup fee.
After submitting the form, the setup looks like this:
If we stop here, the member will be charged correctly when they sign up, but their subscription will renew a full year later, instead of on November 1st like we want it to.
One way to fix that is to change the next billing date for each new member in the Admin UI. That might be fine if you only have a few members, but it would be better to automate the process.
Webhooks
So, we’ll listen for the signup_success
webhook, and when we get that, we’ll make an API call to change the next billing date to November 1st at noon.
Here’s the code for a simple Webhook Listener which listens for the signup_success
webhook and changes the next billing date for the new subscription:
<?php
require_once('./config.php');
// see https://github.com/lewsid/chargify-webhook-helper/blob/master/example.php for example code
$subdomain = $_POST['payload']['site']['subdomain'];
if ($_POST["event"] == "signup_success")
{
$subscription_id = $_POST['payload']['subscription']['id'];
$json = '{ "subscription": { "next_billing_at": "2015-11-01T12:00:00-05:00" } }';
// see https://github.com/jforrest/Chargify-PHP-Client/blob/master/lib/ChargifyConnector.php for example code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://" . $subdomain . ".chargify.com/subscriptions/" . $subscription_id );
curl_setopt($ch, CURLOPT_USERPWD, $userpwd );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$output = curl_exec($ch);
curl_close($ch);
}
?>
You can find the source for the webhook listener and the sample config file in my Chargify repository on GitHub.
Keep in mind that in a real application you’d want to check the signature on the webhook you receive, or use the event id to retrieve it directly from the API, as described in the Webhooks documentation.
And of course you’d want to calculate the year instead of hard-coding it. Perhaps you don’t want to charge new members who sign up during the month of October until November 1 of the following year.
Public Signup Page
With the Public Signup Pages that Chargify offers, club members can securely sign up for a membership without the club having to worry about handling their credit card data. The top portion of the page looks like this by default:
You can see that it says “(then $35.00 at first renewal on 24 Jan 2016)”, but that’s not correct, because as soon as a new subscription is created, the webhook listener will change the next billing date to November 1.
To fix that, all we need to do is edit the Public Signup Page and enter some custom JavaScript:
function changeNextRenewalHtml() {
var nextRenewal = $('#next-renewal-charge');
nextRenewal.html("(then $35.00 on November 1st each year)");
};
$(document).bind("afterSummaryRefresh", changeNextRenewalHtml);
Now the signup page will look like this:
Results
And here is our first customer!
You can see that the subscription was activated on Jan 25th, and that rather than renewing on January 25th, 2016, the next billing date has already been changed to Nov 1st of this year.
In the activity, (which reads in reverse chronological order,) you can see exactly what happened:
The payment processed successfully, the subscription was created, the statement settled, and then the billing date was changed.
Summary
With very little work we’ve been able to customize a Chargify site to match the business requirements of a club membership. I’ve left out some details to prevent this turning into a novel, so if something doesn’t make sense, leave a comment below or drop me an email at wsmoak@gmail.com, and I’ll try to clear it up.
If you’d like to discuss using Chargify for your own subscription-based business, head over to the Support Site or Twitter and ask!