Stripe : Callbacks Cheatsheet (Copied for reference)


Simple One-off Purchases with Charges

1. Customer successfully purchases a single one-off item

The simplest possible starting point. A single one-off purchase.

2. Customer’s card is declined

This event also describes other failure modes, like an invalid expiration date or a failed CVC check if you have the option turned on to decline on a failed check. Continue reading

Stripe : Test : Webhook from one env processed by another

You often have environments like staging, qa, development which use the test secrect and publishable keys of Stripe. So you might run in situation when callbacks for one environment is responded success by another environment and it may create problem in testing.

Instead you can pass environment details in meta data and if that callback is not generated for that environment, just raise an exception.

subscription.metadata = {
                          # To enable one env to ignore (not return 200)
                          #  for webhook event of another environment
                          app_environment: Rails.env 

RSpec : Conditionals in before/after callbacks


When you add a conditions hash to before(:example) or before(:context), RSpec will only apply that hook to groups or examples that match the conditions. e.g.

RSpec.configure do |config|
  config.before(:example, :authorized => true) do

describe Something, :authorized => true do
  # The before hook will run in before each example in this group.

describe SomethingElse do
  it "does something", :authorized => true do
    # The before hook will run before this example.

  it "does something else" do
    # The hook will not run before this example.



config.before(:each) do |ex|
  if ex.metadata[:admin] == true
    Rails.application.load_seed # loading seeds

it "should auto fill the shipping date field to today's date", :admin => true  do
  expect(User.find_by_email('')).not_to eq(nil)


Rails4ClientSideValidation :


Note: If ClientSideValidations does not work in your case due to rails version, use Rails4ClientSideValidations instead for rails-4

ClientSideValidations will run callbacks based upon the state of the element or form. The following callbacks are supported:

  • ClientSideValidations.callbacks.element.after(element, eventData)
  • ClientSideValidations.callbacks.element.before(element, eventData)
  •, message, callback, eventData)
  • ClientSideValidations.callbacks.element.pass(element, callback, eventData)
  • ClientSideValidations.callbacks.form.after(form, eventData)
  • ClientSideValidations.callbacks.form.before(form, eventData)
  •, eventData)
  • ClientSideValidations.callbacks.form.pass(form, eventData)

The names of the callbacks should be pretty straight forward. For example, will be called if a form failed to validate. And ClientSideValidations.callbacks.element.before will be called before that particular element’s validations are run.

All element callbacks will receive the element in a jQuery object as the first parameter and the eventData object as the second parameter. will receive the message of the failed validation as the second parameter, the callback for adding the error fields as the third and the eventData object as the third. ClientSideValidations.elementValidatePass() will receive the callback for removing the error fields. The error field callbacks must be run in your custom callback in some fashion. (either after a blocking event or as a callback for another event, such as an animation)

All form callbacks will receive the form in a jQuery object as the first parameter and the eventData object as the second parameter.

Here is an example callback for sliding out the error message when the validation fails then sliding it back in when the validation passes:

// You will need to require 'jquery-ui' for this to work = function(element, message, callback) {
  if ('valid') !== false) {
    element.parent().find('.message').hide().show('slide', {direction: "left", easing: "easeOutBounce"}, 500);

window.ClientSideValidations.callbacks.element.pass = function(element, callback) {
  // Take note how we're passing the callback to the hide()
  // method so it is run after the animation is complete.
  element.parent().find('.message').hide('slide', {direction: "left"}, 500, callback);


 * 'Before' Callback of rails4clientsidevalidation plugin
 * @param element
 * @param message
window.Rails4ClientSideValidations.callbacks.element.before = function(element, message) {
  // If validating element is zip_code field then just remove the underscores from content
    // Replace all non numeric characters from the fields
    var newContent = $(element).val().replace(/\D/g,'');

importance of not writing parenthesis ‘( )’ after function name in callbacks

Suppose you want to assign an event handler function to an click event of any element

$(document).on('click', '.disable-user', disableUserConfirmation());

when you run the app the function disableUserConfirmation()  gets executed any way, however the event handler function expected to run when the event is triggered wont work. You can see the error in the console.

Uncaught TypeError: undefined is not a function

What happens in callbacks is, when the jQuery’s function ‘on’ want to execute the function object it just appends the parenthesis after the function name and executes it.