Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Include this in your test_helper.rb to stub your stripe checkout.js
#this is a very simple, work in progress helper method for stubbing the stripe checkout.js
#this creates a fake server that will generate stripe token as if it's coming from stripe. So we can test credit card input
class FakeStripe < Sinatra::Base
def self.boot
instance = new { |server| server.boot }
get '/checkout.js' do
#generate a fake token from stripe as if the user entered a card
token = StripeMock.generate_card_token(last4: '4242', exp_year: 2020)
"$(document).ready(function() {
$('.stripe-button').replaceWith(function() {
return \"<div id='stripeForm'></div>\";
$(\"<input type='hidden' id='stripeToken' name='stripeToken' value='#{token}'>\").appendTo('#stripeForm');
$(\"<input name='commit' type='submit' value='Submit'/>\").appendTo('#stripeForm');
server = FakeStripe.boot
Stripe.api_base = "http://#{}:#{server.port}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment