Last active
October 1, 2016 15:18
-
-
Save dac09/b776a817c7463678fdf5b420d0332797 to your computer and use it in GitHub Desktop.
Mondo Webhooks and fieldbook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code to setup an integration between Mondo/Monzo webhooks and Fieldbook. | |
https://monzo.com/ and https://fieldbook.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.endpoint = function (request, response, done) { | |
var moment = require('moment'); | |
// Access properties off the request like headers, query or body: | |
var body = request.body; | |
var transaction = body.data; | |
if (transaction.decline_reason) { | |
return { | |
message: "Transaction not added" | |
} | |
} | |
var fieldBookRow = { | |
mondo_id: transaction.id, | |
date: moment(transaction.created).format('DD/MM/YYYY'), | |
category: transaction.category, | |
description: transaction.description, | |
amount: -(transaction.amount/100), | |
notes: transaction.notes, | |
}; | |
var merchant = transaction.merchant; | |
if (merchant) { | |
fieldBookRow.merchant = merchant.name; | |
if (merchant.address && merchant.address.country) { | |
fieldBookRow.country = merchant.address.country; | |
} | |
if (merchant.metadata && merchant.metadata.suggested_tags) { | |
fieldBookRow.suggested_tag= merchant.metadata.suggested_tags; | |
} | |
} | |
return client.create('transactions', fieldBookRow) | |
.then(function(res){ | |
return { | |
message: "OK!", | |
}; | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "transaction.created", | |
"data": { | |
"id": "tx_0000xxxxxkxyyyyxxxxxx", | |
"created": "2016-04-12T20:00:12.123Z", | |
"description": "PP*12351246 12351235 GBR", | |
"amount": -100, | |
"currency": "GBP", | |
"merchant": { | |
"id": "merch_0000978K6gf9zRcntcoZ17", | |
"group_id": "grp_0000978K6ggvsqmaTv6Lez", | |
"created": "2016-04-12T20:00:12.314Z", | |
"name": "Example Merchant", | |
"logo": "", | |
"emoji": "", | |
"category": "general", | |
"online": true, | |
"atm": false, | |
"address": { | |
"short_formatted": "Somewhere in TW9 1EH", | |
"formatted": "35314369001 TW9 1EH, United Kingdom", | |
"address": "", | |
"city": "35314369001", | |
"region": "", | |
"country": "GBR", | |
"postcode": "TW9 1EH", | |
"latitude": 53, | |
"longitude": -2.43, | |
"zoom_level": 5, | |
"approximate": true | |
}, | |
"updated": "2016-04-12T20:00:12.314Z", | |
"metadata": { | |
"created_for_merchant": "merch_0000978K6gf9zRcntcoZ17", | |
"created_for_transaction": "tx_0000xxxxxkxyyyyxxxxxx", | |
"suggested_tags": "#business" | |
} | |
}, | |
"notes": "", | |
"metadata": {}, | |
"account_balance": 100000, | |
"attachments": null, | |
"category": "general", | |
"is_load": false, | |
"settled": "", | |
"local_amount": -100, | |
"local_currency": "GBP", | |
"updated": "2016-04-12T20:00:12.251Z", | |
"account_id": "acc_00000000000111100000", | |
"counterparty": {}, | |
"scheme": "gps_mastercard", | |
"dedupe_id": "lalskdnglkasndg", | |
"originator": false | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A codelet is a snippet of code that creates a new endpoint in your book's API. | |
// https://github.com/fieldbook/api-docs/blob/master/codelets.md | |
// This is for batch imports. | |
// after you download a list of transactions, then send it to Fieldbook | |
// e.g. first time you set up your fieldbook sheet | |
exports.endpoint = function (request, response, done) { | |
var moment = require('moment'); | |
// Access properties off the request like headers, query or body: | |
var body = request.body; | |
var transactions = body.transactions; | |
transactions.map(function mapToSheet(transaction) { | |
// Use the pre-initialized client to access your book (this returns a promise): | |
var fieldBookRow = { | |
mondo_id: transaction.id, | |
date: moment(transaction.created).format('l'), | |
category: transaction.category, | |
description: transaction.description, | |
amount: -(transaction.local_amount/100), | |
notes: transaction.notes, | |
}; | |
if (transaction.merchant) { | |
fieldBookRow.merchant = transaction.merchant.name; | |
} | |
client.create('transactions', fieldBookRow) | |
}) | |
return {message: 'Ok!'}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment