Mondo Event Webhook - Transaction.Created
I’m one of the lucky few to have a Mondo Card. At the time of writing it’s in open BETA, so anyone can apply but the waiting list is huge (80k+ last time I looked) and only 11k cards are in circulation.
I’m not going to go into detail about why Mondo is so great (it’s widely documented…) - but what I will do is delve into one of the components of their API. Yes a sorta-bank* with an API - I’m so excited to see what they (and other developers) can do with it! Mondo are maintaining an awesome-mondo github listing - check it out for inspiration.
For me - one of the most powerful parts of their API is the webhook feature. Head over to developers.getmondo.co.uk and you can register your own webhook to receive events.
At the moment the only event is the transaction.created event documented.
I’ve thrown together a quick PHP script that accepts the JSON POST value, parses this data, then sends a quick text messages to my mobile to let me know I’ve spent some money (or if I have been refunded).
<?php
require 'Twilio.php';
//fill these in from twilio
$account_sid = 'xxxx';
$auth_token = 'xxxx';
$to_number = '+44xxxx';
$from_number = '+44xxxx';
$client = new Services_Twilio($account_sid, $auth_token);
$rawData = file_get_contents('php://input');
$json = json_decode($rawData);
$logfilename = 'events/' . time();
file_put_contents($logfilename . '.json', $rawData);
switch ($json->type) {
case 'transaction.created':
$amount = number_format(abs($json->data->amount) / 100, 2);
$verb = ($json->data->amount < 0) ? 'spent at' : 'refunded from';
$body = $json->data->merchant->emoji . ' £' . $amount . ' ' .
$verb . ' ' . $json->data->merchant->name;
if ($json->data->local_currency != 'GBP') {
$amountForeign = number_format(abs(
$json->data->local_amount) / 100, 2);
$body .= "\n" . 'Paid as ' . $json->data->local_currency . ' ' .
$amountForeign;
}
$client->account->messages->create(array(
'To' => $to_number,
'From' => $from_number,
'Body' => $body,
));
break;
}
Instructions to use
- Download the Twilio PHP class
- Upload this PHP script (and the Twilio PHP classes) to a web accessible server
- Create an account in Twilio and an SMS number
- Update the 4 configuration variables at the top with the ones you can find in the Twilio Dashboard. Update the
$to_number
with your mobile number (Note: it must be in the format +447XXX). - Create a folder called
events
and make it writeable0777
, it will dump the JSON of each incoming webhook in there for debug (and so you can see what data you have to play with!) - Add as a webook to Mondo using the Developers Console.
- Go and buy something!
If you have a Stripe account, this is a great way to test it, you can charge your card and refund it to see the webhook in action. Also you’ll see the charges come up in your iPhone app immediately too.
*It doesn’t have a banking license… yet - but it has applied. The Mondo Card is a prepaid Mastercard Debit.
Did this webhook work for you? Give me a shout if you have any questions