Last active
March 26, 2018 13:59
-
-
Save sebelga/a04b567168f6ae0e62847fbbdb4af22d to your computer and use it in GitHub Desktop.
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
const stripe = require('stripe')('sk_test_your-token'); | |
const hooks = require('promised-hooks'); | |
// Add Hooks functionalities to the "stripe.charges" methods | |
hooks.wrap(stripe.charges); | |
// Add a "pre" middleware | |
stripe.charges.pre('create', function preCharge({ amount, source, description, currency }) { | |
// All the arguments passed to the charges.create() method are available | |
// Make any async call as long as it returns a *Promise*. | |
// If the async call rejects, then the "create()" method won't be executed | |
return someService.doAsyncStuff(amount, currency, description); | |
}); | |
// Add a "post" middleware | |
stripe.charges.post('create', function postCharge(charge) { | |
return myTraceService.log(`New charge ${charge.amount}`); | |
}); | |
// No changes on the processPayement | |
async function processPayement(amount, source, description) { | |
let charge; | |
try { | |
charge = await stripe.charges.create({ | |
amount, | |
source, | |
description, | |
currency: "usd" | |
}); | |
} catch(e) { | |
// Error handling | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment