How To Create SMS Payment Notifications for FreshBooks Using Twilio

June 01, 2011
Written by
Rahim Sonawalla
Contributor
Opinions expressed by Twilio contributors are their own

Twilio Bug Logo

Ah, there’s nothing like the feeling of getting paid. Using the Twilio SMS API and the FreshBooks API, we can easily create a web app that let’s you experience that feeling no matter where you are. In this tutorial, we’re going to create a web app that sends you a text message everytime a payment is posted to one of your FreshBooks invoices.

To get started, you’ll need a Twilio account, a FreshBooks account, and PHP web hosting. You can sign up for a free Twilio account (and get $30 of free credit) here. You can create a free FreshBooks account here. There are many web hosting services that support PHP, so I recommend looking around and finding one that fits you well.

Setting up the FreshBooks Webhook

Both Twilio and FreshBooks use webhooks to let you know when interesting events occur. (For a gentle introduction to what webhooks are, Jeff Lindsay put up a nice explanation.) Twilio makes an HTTP request whenever a phone call or SMS comes into your Twilio phone number. FreshBooks makes an HTTP request whenever something happens with your account.

To set up a webhook for payments we simply visit our FreshBooks API page (click on “My Account” on the top right of your FreshBooks page), click on “customize” under the “Configure Webhooks” section, and then click on the “New Webhook” button. For the event select “payment.create” and for URI enter http://yourserver.com/handle-payment.php (we’ll create this file next).

The Code

To make things easier, we’ll be using some helper libraries. For Twilio, the official PHP helper library. For FreshBooks, a helper library created by Milan Rukavina. (Note: if you run into errors using the FreshBooks library, change line 79 in HttpClient.php from true to false.)

FreshBooks sends an HTTP POST to our script whenever a payment is posted, so all we need to do is look up the payment using the payment ID sent to our script and send ourselves an SMS message using Twilio. (To make it a little nicer, we also look up some invoice information.)

As you can see, there’s some code on lines 31-38 that handles a callback verification. When you first add a webhook in FreshBooks, a verification code is sent to your script. This is done in order to ensure that you are in fact the owner of the script. After you added the webhook in the previous section you probably noticed that its status was unverified. We can now fix that by clicking the checkbox of the webhook and pushing the “resend verification” button. Refresh the page and you should see the status has changed to verified.

Profit!

We’re all set. Enter a payment to one of your FreshBooks invoices and you should get a text message from your web app!

By the way, we’re running a developer contest with our friends at FreshBooks. Use what you learned here to create a cool Twilio/FreshBooks app, and you could win an iPad 2, and potentially have it added to the FreshBooks add-on store to be sold to FreshBook’s 2.5 million users!