Skip to content

Commit

Permalink
Merge pull request #368 from spree/check-spree-gateway-against-spree-3-7
Browse files Browse the repository at this point in the history
Cannot confirm this PaymentIntent because it has a status of requires_capture - GH Issue SD-992
  • Loading branch information
damianlegawiec authored Nov 13, 2020
2 parents f33da4a + 9fe4ea7 commit b45be7b
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 88 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ env:
- DB=postgres

gemfile:
# - gemfiles/spree_4_1.gemfile
- gemfiles/spree_3_7.gemfile
- gemfiles/spree_4_0.gemfile
- gemfiles/spree_4_1.gemfile
- gemfiles/spree_4_2.gemfile
- gemfiles/spree_master.gemfile

Expand Down
15 changes: 13 additions & 2 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
appraise 'spree-4-1' do
gem 'spree', '~> 4.1.0'
appraise 'spree-3-7' do
gem 'spree', '~> 3.7.0'
gem 'rails-controller-testing'
gem 'sass-rails'
end

appraise 'spree-4-0' do
gem 'spree', '~> 4.0.0'
gem 'rails-controller-testing'
end

Expand All @@ -8,6 +14,11 @@ appraise 'spree-4-2' do
gem 'rails-controller-testing'
end

appraise 'spree-4-1' do
gem 'spree', '~> 4.1.0'
gem 'rails-controller-testing'
end

appraise 'spree-master' do
gem 'spree', github: 'spree/spree', branch: 'master'
gem 'rails-controller-testing'
Expand Down
10 changes: 10 additions & 0 deletions app/models/spree/credit_card_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Spree
module CreditCardDecorator
def set_last_digits
self.last_digits ||= number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1)
end

end
end

::Spree::CreditCard.prepend(::Spree::CreditCardDecorator)
9 changes: 7 additions & 2 deletions app/views/spree/checkout/_payment_confirm.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@

function confirmCardPaymentResponseHandler(response) {
$.post("/api/v2/storefront/intents/handle_response", { response: response, order_token: "<%= @order.token %>" }).done(function (result) {
form.elements["commit"].disabled = false;
// conditional needs for spree 3.7
if(form.elements["commit"]) {
form.elements["commit"].disabled = false;
}
$('#successBox').html(result.message);
$('#successBox').show();
form.submit();
}).fail(function(result) {
form.elements["commit"].disabled = false;
if(form.elements["commit"]) {
form.elements["commit"].disabled = false;
}
$('#errorBox').html(result.responseJSON.error);
$('#errorBox').show();
});
Expand Down
9 changes: 9 additions & 0 deletions gemfiles/spree_3_7.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rails-controller-testing"
gem "spree", "~> 3.7.0"
gem "sass-rails"

gemspec path: "../"
8 changes: 8 additions & 0 deletions gemfiles/spree_4_0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rails-controller-testing"
gem "spree", "~> 4.0.0"

gemspec path: "../"
48 changes: 19 additions & 29 deletions spec/features/admin/stripe_elements_payment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,43 @@
require 'spec_helper'

describe 'Admin Panel Stripe elements payment', type: :feature, :js => true do
describe 'Admin Panel Stripe elements payment', type: :feature do
stub_authorization!

let!(:country) { create(:country, :states_required => true) }
let!(:state) { create(:state, :country => country) }
let!(:country) { create(:country, states_required: true) }
let!(:state) { create(:state, country: country) }
let!(:shipping_method) { create(:shipping_method) }
let!(:stock_location) { create(:stock_location) }
let!(:mug) { create(:product, :name => 'RoR Mug') }
let!(:zone) { create(:zone) }
let!(:stock_location) { create(:stock_location) }
let!(:mug) { create(:product, name: 'RoR Mug') }
let!(:zone) { create(:zone) }
let!(:stripe_elements_payment_method) do
Spree::Gateway::StripeElementsGateway.create!(
:name => 'Stripe Element',
:preferred_secret_key => 'sk_test_VCZnDv3GLU15TRvn8i2EsaAN',
:preferred_publishable_key => 'pk_test_Cuf0PNtiAkkMpTVC2gwYDMIg'
name: 'Stripe Element',
preferred_secret_key: 'sk_test_VCZnDv3GLU15TRvn8i2EsaAN',
preferred_publishable_key: 'pk_test_Cuf0PNtiAkkMpTVC2gwYDMIg'
)
end

let!(:order) { OrderWalkthrough.up_to(:payment) }
before { visit spree.new_admin_order_payment_path(order.number) }

it 'can process a valid payment' do
fill_in_stripe_payment
wait_for { !page.has_current_path?(spree.admin_order_payments_path(order.number)) }

expect(page.body).to have_content('Payment has been successfully created!')
expect(page).to have_current_path spree.admin_order_payments_path(order.number)
end

if Spree.version.to_f >= 4.1
it 'shows an error with an invalid card name' do
fill_in_stripe_payment(true)

expect(page).to have_content("Credit card Name can't be blank")
expect(page).to have_current_path spree.admin_order_payments_path(order.number)
end
else
it 'can proces valid payment with invalid card name' do
fill_in_stripe_payment(true)
wait_for { !page.has_current_path?(spree.admin_order_payments_path(order.number)) }
it 'shows an error with an invalid card name' do
fill_in_stripe_payment(true)

expect(page.body).to have_content('Payment has been successfully created!')
expect(page).to have_current_path spree.admin_order_payments_path(order.number)
end
expect(page).to have_content("Credit card Name can't be blank")
expect(page).to have_current_path spree.admin_order_payments_path(order.number)
end

it 'shows an error with an invalid card number' do
fill_in_stripe_payment(false, true)

expect(page).to have_content('The card number is not a valid credit card number.')
expect(page).to have_current_path spree.new_admin_order_payment_path(order.number)
end
Expand All @@ -62,7 +52,7 @@
it 'shows an error with an invalid card expiration' do
fill_in_stripe_payment(false, false, false, true)

if Spree.version.to_f >= 4.1
if Spree.version.to_f >= 4.1 || Spree.version.to_f >= 3.7
expect(page).to have_content('Credit card Month is not a number')
expect(page).to have_content('Credit card Year is not a number')
expect(page).to have_current_path spree.admin_order_payments_path(order.number)
Expand All @@ -83,15 +73,15 @@ def fill_in_stripe_payment(invalid_name = false, invalid_number = false, invalid

def fill_in_card_number(invalid_number)
number = invalid_number ? '123' : '4242 4242 4242 4242'
fill_in_field('Card Number *', '#card_number1', number)
fill_in_field('Card Number *', "#card_number#{stripe_elements_payment_method.id}", number)
end

def fill_in_card_expiration(invalid_expiration)
valid_expiry = Spree.version.to_f >= 4.2 ? "01/#{Time.current.year + 1}" : "01 / #{Time.current.year + 1}"
invalid_expiry = Spree.version.to_f >= 4.2 ? '01/' : '01 / '

card_expiry = invalid_expiration ? invalid_expiry : valid_expiry
fill_in_field('Expiration *', '#card_expiry1', card_expiry)
fill_in_field('Expiration *', "#card_expiry#{stripe_elements_payment_method.id}", card_expiry)
end

def fill_in_cvc(invalid_code)
Expand Down
48 changes: 28 additions & 20 deletions spec/features/stripe_checkout_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe "Stripe checkout", type: :feature do
describe "Stripe checkout", type: :feature, js: true do
let!(:country) { create(:country, :states_required => true) }
let!(:state) { create(:state, :country => country) }
let!(:shipping_method) { create(:shipping_method) }
Expand Down Expand Up @@ -42,7 +42,7 @@
end

# This will pass the CC data to the server and the StripeGateway class handles it
it "can process a valid payment (without JS)" do
it "can process a valid payment (without JS)", js: false do
fill_in 'card_number', with: '4242 4242 4242 4242'
fill_in 'card_code', with: '123'
fill_in 'card_expiry', with: "01 / #{Time.current.year + 1}"
Expand All @@ -56,12 +56,10 @@

# This will fetch a token from Stripe.com and then pass that to the webserver.
# The server then processes the payment using that token.
it "can process a valid payment (with JS)", :js => true do
fill_in 'card_number', with: '4242 4242 4242 4242'
# Otherwise ccType field does not get updated correctly
page.execute_script("$('.cardNumber').trigger('change')")
it "can process a valid payment (with JS)" do
fill_in_with_force('card_number', with: "4242424242424242")
fill_in_with_force('card_expiry', with: "01 / #{Time.current.year + 1}")
fill_in 'card_code', with: '123'
fill_in 'card_expiry', with: "01 / #{Time.current.year + 1}"
click_button "Save and Continue"
wait_for_stripe # Wait for Stripe API to return + form to submit
expect(page).to have_css('#checkout_form_confirm')
Expand All @@ -72,18 +70,23 @@
expect(page).to have_content(order.number)
end

it "shows an error with an invalid credit card number", :js => true do
it "shows an error with an invalid credit card number" do
# Card number is NOT valid. Fails Luhn checksum
fill_in 'card_number', with: '4242 4242 4242 4249'
click_button "Save and Continue"
wait_for_stripe
expect(page).to have_content("Your card number is incorrect")
expect(page).to have_css('.has-error #card_number.error')
if Spree.version.to_f >= 3.7 and Spree.version.to_f <= 4.1
expect(page).to have_content("The card number is not a valid credit card number")
end
if Spree.version.to_f >= 4.2
expect(page).to have_content("Your card number is incorrect")
expect(page).to have_css('.has-error #card_number.error')
end
end

it "shows an error with invalid security fields", :js => true do
fill_in 'card_number', with: '4242 4242 4242 4242'
fill_in 'card_expiry', with: "01 / #{Time.current.year + 1}"
it "shows an error with invalid security fields" do
fill_in_with_force('card_number', with: "4242424242424242")
fill_in_with_force('card_expiry', with: "01 / #{Time.current.year + 1}")
fill_in 'card_code', with: '99'
click_button "Save and Continue"
wait_for_stripe
Expand All @@ -93,24 +96,29 @@

# this scenario will not occur on Spree 4.2 due to swapping jquery.payment to cleave
# see https://github.com/spree/spree/pull/10363
it "shows an error with invalid expiry month field", :js => true do
skip if Spree.version.to_f >= 4.2
fill_in 'card_number', with: '4242 4242 4242 4242'
fill_in 'card_expiry', :with => "00 / #{Time.now.year + 1}"
it "shows an error with invalid expiry month field" do
skip if Spree.version.to_f >= 4.2
fill_in_with_force('card_number', with: "4242424242424242")
fill_in_with_force('card_expiry', with: "00 / #{Time.current.year + 1}")
fill_in 'card_code', with: '123'
click_button "Save and Continue"
wait_for_stripe
expect(page).to have_content("Your card's expiration month is invalid.")
expect(page).to have_css('.has-error #card_expiry.error')
end

it "shows an error with invalid expiry year field", :js => true do
fill_in 'card_number', with: '4242 4242 4242 4242'
fill_in 'card_expiry', with: '12 / '
it "shows an error with invalid expiry year field" do
fill_in_with_force('card_number', with: "4242424242424242")
fill_in_with_force('card_expiry', with: "12 / ")
fill_in 'card_code', with: '123'
click_button "Save and Continue"
wait_for_stripe
expect(page).to have_content("Your card's expiration year is invalid.")
expect(page).to have_css('.has-error #card_expiry.error')
end
end

def fill_in_with_force(locator, with:)
field_id = find_field(locator)[:id]
page.execute_script("document.getElementById('#{field_id}').value = '#{with}';")
end
Loading

0 comments on commit b45be7b

Please sign in to comment.