Mar. 25 2011

Getting around FreshBooks’ recurring.update ‘frequency’ issue

I’m writing a few scripts which interacts with the FreshBooks API using a modified (by me) version of the Freshbooks PHP Library by Milan Rukavina.

Using this library, to update anything, you first call the get() method which populates the appropriate class (e.g., FreshBooks_Client for clients and FreshBooks_Recurring for recurring profiles) attributes with the data. Then, you go through, update the fields you want, and call the update() method to resubmit. Typically, this is a pretty good standard of doing things.

However, the FreshBooks API returns a funny little code for the frequency attribute for recurring profiles which represents (in a more system-friendly and shorthand way) the value one would send with the create() method. For instance, for a frequency of every two weeks, you would send 2 weeks with the create() method and would get 2w with the get() method.

When you use the update() for a recurring profile after you pulled the profile from the API, you’re sending the shorthand representation, so the API returns an error of The frequency of the recurring profile must be selected. At first, I thought it meant it the frequency attribute wasn’t being sent at all, but upon further investigation, that error can mean one of two things: 1) you actually didn’t send a frequency, or 2) the frequency you sent was in an invalid format.

To fix this issue in the FreshBooks PHP library, just add the following lines:

// Recurring.php
protected function _internalPrepareUpdate(&$content)
  if (preg_match('/^(\d)*([wmy])$/', $this->frequency, $matches)) unset($this->frequency);

That overrides the _internalPrepareUpdate() method in the parent file BaseInvoice.php. It does a regex match to see if the frequency is in the format given by get(). If it is, there’s a pretty good chance it wasn’t updated, so unset the value and call the parent _internalPrepareUpdate() method. This can be ported over to any library utilizing the same get-before-update methodology.

This is an issue that should really be addressed somehow by the FreshBooks API development team. This issue was addressed once on the forums, but the question was asked weirdly and answered even worse.

Hope this helps someone out there!

Leave a Reply

Previous post:

Next post: interface from fmTuner (modified by me).

Twitter interface from HL Twitter.

Dedicated to my mom, Sharon. May she rest peacefully.
Love you always. (March 23, 1965 - November 12, 2011)

Unsupported Browser!

My site is designed for and currently displays best with modern browsers (and it looks like you don't have one!).

Everything should still function properly, but you won't be able to see all the fancy effects, text, or the way I intended my design to look.

Consider trying the latest version of either Firefox or Chrome... I think you'll like it!

Logan Bibby

P.S.: You can continue viewing my site by clicking the "close" link and you'll never see this again. :)