Skip to content

Instantly share code, notes, and snippets.

@rahims
Created June 1, 2011 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahims/1001889 to your computer and use it in GitHub Desktop.
Save rahims/1001889 to your computer and use it in GitHub Desktop.
Sample code that shows how to send an SMS message using Twilio whenever a payment is posted to one of your FreshBooks invoices. Read the full tutorial here: http://www.twilio.com/blog/2011/06/how-to-create-sms-payment-notifications-for-freshbooks-using-tw
<?php
// The Twilio helper library. Get a copy here: http://www.twilio.com/docs/libraries/
require_once('twilio.php');
// Uses the FreshBooks helper library written by Milan Rukavina. Get a copy
// here: http://code.google.com/p/freshbooks-php-library/
require_once('FreshBooks/Payment.php');
require_once('FreshBooks/Invoice.php');
require_once('FreshBooks/HttpClient.php');
// FreshBooks account information
$FreshBooksUrl = 'https://XXXXX.freshbooks.com/api/2.1/xml-in';
$FreshBooksToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// Twilio account information
$AccountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$AuthToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$ApiVersion = '2010-04-01';
// The phone number that appears as the caller ID when an SMS is received.
// This must be a Twilio phone number.
$From = '+1XXXXXXXXXX';
// The phone number the SMS notification should be sent to.
$To = '+1XXXXXXXXXX';
$client = FreshBooks_HttpClient::init($FreshBooksUrl, $FreshBooksToken);
// Sent by FreshBoooks the first time a webhook is added in order to ensure
// that the user is the owner of the callback script being registered.
if ($_POST['name'] == 'callback.verify')
{
$response = '<request method="callback.verify"><callback><callback_id>'.$_POST['object_id'].'</callback_id><verifier>'.$_POST['verifier'].'</verifier></callback></request>';
$client->send($response);
exit;
}
// FreshBooks has sent us a notification that a payment has been posted to an invoice.
$paymentId = $_POST['object_id'];
$payment = new FreshBooks_Payment();
$invoice = new FreshBooks_Invoice();
$payment->get($paymentId);
$invoice->get($payment->invoiceId);
$message = "You just got a payment of {$payment->amount} from {$invoice->firstName} {$invoice->lastName} ({$invoice->linkView})";
$twilio = new TwilioRestClient($AccountSid, $AuthToken);
// Send the SMS notification using Twilio
$response = $twilio->request("/$ApiVersion/Accounts/$AccountSid/SMS/Messages",
"POST", array(
"From" => $From,
"To" => $To,
"Body" => $message
));
if ($response->IsError)
{
echo "Error: {$response->ErrorMessage}";
}
else {
echo "Sent notification";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment