Created
July 22, 2024 13:13
-
-
Save revolunet/0e031309959f8350fe8d683f000359b6 to your computer and use it in GitHub Desktop.
sellsy webhook code extract
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
import { when } from "q"; | |
import Sellsy from "node-sellsy"; | |
import { getBookeoProductId } from "./bookeo"; | |
export const TVA = 20; | |
/** | |
* create sellsy invoice and paymanet | |
* return existing customer if any | |
* | |
* @method createCharge | |
* @param {Object} payload raw stripe customer.created payload | |
* @return {Customer} sellsy customer | |
*/ | |
export default function createCharge(creds, payload) { | |
let { id, description, email } = payload.data.object; | |
if (/^FACT-/.exec(description)) { | |
// starts with an invoice number, assume already created, skipdoc creation | |
return when(); | |
} | |
let sellsy = new Sellsy({ creds: creds.sellsy }); | |
return sellsy.customers | |
.get({ ident: payload.data.object.customer }) | |
.then((customer) => { | |
// find bookeo booking number from payload | |
let bookeoId; | |
let bookeoMatch = /^xxx - Réservation (\d+)$/.exec( | |
payload.data.object.description | |
); | |
if (bookeoMatch) { | |
bookeoId = bookeoMatch[1]; | |
} | |
let getBookeoId = bookeoId | |
? getBookeoProductId(creds, bookeoId) | |
: when(null); | |
console.log("customer created", customer.email); | |
// try to get the product ID from bookeo API | |
return getBookeoId.then((productId) => { | |
const DOCTYPE = "invoice", | |
SUBJECT = payload.data.object.description, | |
ROW_TITLE = "Session Jeu", | |
ROW_NOTES = "Expérience xxx", | |
AMOUNT = payload.data.object.amount / 100, | |
HT = AMOUNT / (1 + TVA / 100), | |
PAY_MEDIUM = 2058662; | |
let ROW_TYPE = "once", | |
ROW_CATALOG_ID; | |
if (productId) { | |
ROW_TYPE = "item"; | |
ROW_CATALOG_ID = productId; | |
} | |
const documentData = { | |
document: { | |
doctype: DOCTYPE, | |
thirdid: customer.id, | |
notes: customer.email, | |
currency: "1", | |
displayedDate: | |
new Date(payload.data.object.created * 1000).getTime() / 1000, | |
subject: SUBJECT, | |
tags: "bookeo,stripe", | |
}, | |
row: { | |
1: { | |
row_type: ROW_TYPE, | |
row_name: ROW_TITLE, | |
row_linkedid: ROW_CATALOG_ID, | |
row_notes: ROW_NOTES, | |
row_tax: TVA, | |
row_unitAmount: HT, | |
row_qt: 1, | |
}, | |
}, | |
}; | |
console.log("got bookeo id", productId); | |
console.log("documentData", documentData); | |
// create invoice | |
return sellsy.documents | |
.create(documentData) | |
.then((createdDocument) => { | |
console.log("created document", createdDocument.id); | |
// set to paid | |
return sellsy.documents | |
.updateStep(createdDocument.type, createdDocument.id, "paid") | |
.then(() => createdDocument); | |
}) | |
.then((createdDocument) => { | |
let paymentData = { | |
date: | |
new Date(payload.data.object.created * 1000).getTime() / 1000, | |
amount: AMOUNT, | |
medium: PAY_MEDIUM, | |
ident: payload.data.object.id, | |
}; | |
console.log("updating payment for", createdDocument.id); | |
// add payment reference | |
return sellsy.documents | |
.createPayment( | |
createdDocument.type, | |
createdDocument.id, | |
paymentData | |
) | |
.then(() => createdDocument); | |
}); | |
}); | |
}); | |
} |
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
import Sellsy from 'node-sellsy'; | |
/** | |
* create sellsy customer from stripe payload and return customer | |
* return existing customer if any | |
* | |
* @method createCustomer | |
* @param {Object} payload raw stripe customer.created payload | |
* @return {Customer} sellsy customer | |
*/ | |
export default function createCustomer( creds, payload ) { | |
let { id, description, email } = payload.data.object; | |
let sellsy = new Sellsy({ creds: creds.sellsy }); | |
return sellsy.customers.get({ ident: id }) | |
.catch(function(e) { | |
// creates customer if not exists | |
if (e.message === sellsy.ERRORS.CUSTOMER_NOT_FOUND) { | |
return sellsy.customers.create({ | |
third: { | |
name : description, | |
type: 'person', | |
ident: id, | |
email: email, | |
tags : 'stripe', | |
stickyNote: `Stripe : https://dashboard.stripe.com/customers/${id}` | |
}, | |
contact: { | |
name: description, | |
email: email | |
} | |
}); | |
} | |
// TODO: ALERT | |
console.log('ERROR', e); | |
throw new Exception(e.message); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment