With receive
:
client.on('receive', (transfer) => {
// Check amount, etc.
if (transfer.condition) {
// Try to fulfill the condition or throw if unable
}
// Process the payment
})
With transfer_prepare
, transfer_execute
:
client.on('transfer_prepare', (transfer) => {
if (transfer.direction === 'incoming') {
// Check amount, etc.
// Try to fulfill the condition or throw if unable
// Process the payment
}
})
client.on('transfer_execute', (transfer) => {
if (transfer.direction === 'incoming' && !transfer.condition) {
// Check amount, etc.
// Process the payment
}
})
By splitting receive
into two different events I now have two different entry points to worry about. One of those entry points groups another event (Universal payments being fulfilled) that I don't care about, so I have to go back and filter that out.
I could also do this:
client.on('transfer_prepare', (transfer) => {
if (transfer.direction === 'incoming') {
// Check amount, etc.
// Try to fulfill the condition or throw if unable
}
})
client.on('transfer_execute', (transfer) => {
if (transfer.direction === 'incoming') {
// Check amount, etc.
// Process the payment
}
})
But now I'm loading all of the context from the database twice for universal payments which is probably not very efficient. (Once when I get the prepare, then I throw it all away and load it again for the execution.)
With all four events:
client.on('transfer_prepare', (transfer) => {
if (transfer.direction === 'incoming') {
// Check amount, etc.
// Try to fulfill the condition or throw if unable
// Process the payment
}
})
client.on('transfer_simple', (transfer) => {
if (transfer.direction === 'incoming') {
// Check amount, etc.
// Process the payment
}
})
Here I still have the duplication, but at least I don't have to filter out the stuff I don't care about.
Overall, as a receiver or connector, I like the receive
option best.