Raise your hand if you are lazy like me? I am the laziest person ever when it comes to how an app should function. I hate too many clicks just to get a task done. Back in the days when my old store, iMayne Robotics, was in swing, I used to manually add my sales in Xero. Time consuming. The plan was, whenever I made a sale, a paid invoice is generated in my Xero account. Should be simple so I found this gem, Xeroizer.

Today, we will be adding Xeriozer gem to our Spree App. Note that this tutorial is not about setting up Spree 3. To install Spree, follow their guide here. Head over to https://app.xero.com/Application/ and create a private application.

Give your application a name and choose the Demo Company! When all is well, you can select your main company. This way, if things go wrong, it would not be an issue. We now need to generate a public/private key. And as I mentioned before, I am lazy so watch this awesome video. This is a bit technical for the novice user but is straightforward to accomplish.

This will also generate a privatekey.pem, or what-ever-you-called-it.pem file. In the root of your Spree app, place that file and add a .gitignore file and ignore the privatekey.pem file. You do not want to upload this file to any server except yours! It is like giving a stranger a key to you bank account.

Next, copy both Consumer Key and Consumer Secret and save it into your ENV file. Remember never add this to your version control. I use Figaro gem for this.

So, we got rid of the boring stuff, now to install Xeroizer gem in our Spree app. In our Gemfile:

gem "xeroizer" # Then in terminal:

bundle install

Yey! Another boring stuff. Now, I needed a way to associate my Spree customers in my Xero account. How do we go about doing that? I needed some sort of ID. We will now generate a xero_customer_id on our users table:

rails g migration add_xero_customer_id_to_users

At this point, I assume you already had Spree installed. Next, make changes to that file you have just generated. Mine looks like this:

class AddXeroCustomerIdToUsers < ActiveRecord::Migration

def change

add_column :spree_users, :xero_customer_id, :string

end

end

Your users table is called spree_users . Next, run rails db:migrate to create the additional table.

Since I needed to mark the invoice paid in Xero, my xeroizer code is placed in the Spree::CheckoutController update function of line 27:

In your controllers folder, create a spree folder with a file called checkout_controller_decorator.rb and in that file will have this only:

We have made a request to save our new customer in our Xero contacts, if not yet created. We then created an invoice then maked it paid. So far we have made three api calls! The next time that same customer makes a purchase, it will be 2 calls as we now have their xero customer id stored in our database. At the time of this writing, I am not sure if things can be simpler. Remember I have said to place your .pem file in the root of your app? It was referenced on line 113.

With this in place, whenever an order is completed, a paid invoice will be generated in our Xero account under Sales > Invoice (paid). Please see their call limit to determine of this implementation is right for your business.

Well done! No need to create a paid invoice manually, after all, we are lazy, right?

Feel free and let me know if any issues, I will do my best to replicate and amend. Hit that heart button and share the love. Little more about me here: http://www.mayneweb.com/about (hosted on heroku on their free plan….for now, so slow loading) 😉