Stripe : Callbacks Cheatsheet (Copied for reference)

Source:

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

Advertisements

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 = {
                          pact_id: pact.id,
                          # To enable one env to ignore (not return 200)
                          #  for webhook event of another environment
                          app_environment: Rails.env 
                      }
subscription.save

RSpec : Conditionals in before/after callbacks

Conditions

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
     :authorized_user
  end
end

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

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

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

 

Or

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

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

Sources

http://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks#before-instance_method

Rails4ClientSideValidation :

Callbacks

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)
  • ClientSideValidations.callbacks.element.fail(element, message, callback, eventData)
  • ClientSideValidations.callbacks.element.pass(element, callback, eventData)
  • ClientSideValidations.callbacks.form.after(form, eventData)
  • ClientSideValidations.callbacks.form.before(form, eventData)
  • ClientSideValidations.callbacks.form.fail(form, eventData)
  • ClientSideValidations.callbacks.form.pass(form, eventData)

The names of the callbacks should be pretty straight forward. For example, ClientSideValidations.callbacks.form.fail 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. ClientSideValidations.callbacks.element.fail() 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
window.ClientSideValidations.callbacks.element.fail = function(element, message, callback) {
  callback();
  if (element.data('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);
}

Example:

/**
 * '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
  if($(element).attr('id').indexOf('zip_code')){
    // Replace all non numeric characters from the fields
    var newContent = $(element).val().replace(/\D/g,'');
    $(element).val(newContent);
  }
};