Last active
December 28, 2021 14:19
-
-
Save matthiasnys/3000123cd23a38531d1147034bb31d88 to your computer and use it in GitHub Desktop.
Google sheets updates from Mollie
This file contains 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
const MOLLIE_TOKEN = '' | |
function fetchFromMollie() { | |
var payload = {}; | |
try { | |
// Make a POST request with a JSON payload. | |
var options = { | |
'method': 'GET', | |
'contentType': 'application/json', | |
'headers': { | |
'authorization': 'Bearer ' + MOLLIE_TOKEN, | |
}, | |
}; | |
var response = UrlFetchApp.fetch("https://api.mollie.com/v2/payment-links", options); | |
var JsonResponse = JSON.parse(response.getContentText()) | |
return JsonResponse._embedded.payment_links | |
} catch (error) { | |
Logger.log('Failed Mollie' + error.message) | |
} | |
} | |
function getOrdersSheet() { | |
return SpreadsheetApp.getActive().getSheetByName('Sheet1') | |
} | |
function updatePayments() { | |
var response = fetchFromMollie() | |
updateOrdersPaymentStatus(response) | |
} | |
function findOrder(mollie, orderId, name) { | |
const needle = `Box: order #${orderId} - ${name}` | |
for (let i = 0; i < mollie.length; i++) { | |
var item = mollie[i]; | |
if (item.description === needle) { | |
return item // PaymentRequest Found | |
} | |
} | |
} | |
function updateOrdersPaymentStatus(mollie) { | |
const currentSheet = getOrdersSheet() | |
var searchRange = currentSheet.getRange("A2:R1000"); | |
var rangeValues = searchRange.getValues(); | |
for (let i = 0; i < rangeValues.length; i++) { | |
var current = rangeValues[i]; | |
var orderId = current[2]; | |
var name = current[12]; | |
if (!orderId || !name) { | |
return; | |
} | |
const paymentLink = findOrder(mollie, orderId, name) | |
if (paymentLink) { | |
var range = currentSheet.getRange("S" + (i + 2)); | |
range.setValue(paymentLink.id); | |
var statusRange = currentSheet.getRange("T" + (i + 2)); | |
statusRange.setValue(paymentLink.paidAt); | |
} | |
}; | |
} | |
/** | |
* A special function that runs when the spreadsheet is open, used to add a | |
* custom menu to the spreadsheet. | |
*/ | |
function onOpen() { | |
var spreadsheet = SpreadsheetApp.getActive(); | |
var menuItems = [ | |
{ name: 'Update Payments', functionName: 'updatePayments' } | |
]; | |
spreadsheet.addMenu('Mollie', menuItems); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment