Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Created November 12, 2018 21:08
Show Gist options
  • Save staycreativedesign/b11347d002f6cbc2f06af616fe806e98 to your computer and use it in GitHub Desktop.
Save staycreativedesign/b11347d002f6cbc2f06af616fe806e98 to your computer and use it in GitHub Desktop.
class ChargesController < ApplicationController
def new
end
def create
@amount = params[:amount]
@amount = @amount.gsub('$', '').gsub(',', '')
begin
@amount = Float(@amount).round(2)
rescue
flash[:error] = 'Charge not completed. Please enter a valid amount in USD ($).'
redirect_to new_charge_path
return
end
@amount = (@amount * 100).to_i # Must be an integer!
if @amount < 500
flash[:error] = 'Charge not completed. Donation amount must be at least $5.'
redirect_to new_charge_path
return
end
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
binding.pry
if params[:subscription] == 'no'
charge = Stripe::Charge.create(
:amount => @amount,
:currency => 'usd',
:description => 'Donation',
:customer => customer.id
)
elsif params[:subscription] == 'yes'
subscrption = Stripe::Subscription.create(
:customer => customer.id,
items: [{plan: params[:plan]}]
)
end
Membership.create(name: 'foo', password: SecureRandom.alphanumeric, email: params[:stripeEmail], role: 'guest')
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
binding.pry
end
end
RSpec.configure do |config|
require 'capybara/rspec'
require 'webmock/rspec'
...
end
require 'rails_helper'
RSpec.feature "User Donates", :type => :feature do
it "creates a membership with user email", js: true do
approved_card_number = '4242424242424242'
visit new_charge_path
first('.donationAmount').click
click_button('Donate')
stripe_iframe = all('iframe[name=stripe_checkout_app]').last
Capybara.within_frame stripe_iframe do
find_field('Email').send_keys("[email protected]")
find_field('Card number').send_keys(approved_card_number)
find_field('MM / YY').send_keys "01#{DateTime.now.year + 1}"
find_field('CVC').send_keys '123'
find('button[type="submit"]').click
end
#not getting the created customer biding pry not being hit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment