Skip to content

Instantly share code, notes, and snippets.

@sebelga
Created April 3, 2018 12:23
Show Gist options
  • Save sebelga/f48a4f5b992f26cfeab2252022ef0f67 to your computer and use it in GitHub Desktop.
Save sebelga/f48a4f5b992f26cfeab2252022ef0f67 to your computer and use it in GitHub Desktop.
// ./vendors/stripe.js
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}`);
});
// Export Stripe with the hooks
module.exports = stripe;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment