|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "solidus_storefront_spec_helper" |
| 4 | + |
| 5 | +RSpec.describe "Checkout" do |
| 6 | + def js_sdk_script_query |
| 7 | + URI(page.find('script[src*="sdk/js?"]', visible: false)[:src]).query.split('&') |
| 8 | + end |
| 9 | + |
| 10 | + def js_sdk_script_partner_id |
| 11 | + page.find('script[src*="sdk/js?"]', visible: false)['data-partner-attribution-id'] |
| 12 | + end |
| 13 | + |
| 14 | + describe "paypal payment method" do |
| 15 | + let(:order) { Spree::TestingSupport::OrderWalkthrough.up_to(:payment) } |
| 16 | + let(:paypal_payment_method) { create(:paypal_payment_method) } |
| 17 | + let(:failed_response) { double('response', status_code: 500) } # rubocop:disable RSpec/VerifiedDoubles |
| 18 | + |
| 19 | + before do |
| 20 | + user = create(:user) |
| 21 | + visit '/' |
| 22 | + click_link 'Login' |
| 23 | + fill_in 'spree_user[email]', with: user.email |
| 24 | + fill_in 'spree_user[password]', with: 'secret' |
| 25 | + click_button 'Login' |
| 26 | + order.user = user |
| 27 | + order.recalculate |
| 28 | + |
| 29 | + paypal_payment_method |
| 30 | + allow_any_instance_of(CheckoutsController).to receive_messages( |
| 31 | + current_order: order, try_spree_current_user: user |
| 32 | + ) |
| 33 | + end |
| 34 | + |
| 35 | + context "when generating a script tag" do |
| 36 | + it "generates a url with the correct credentials attached" do |
| 37 | + visit '/checkout/payment' |
| 38 | + expect(js_sdk_script_query).to include("client-id=#{paypal_payment_method.preferences[:client_id]}") |
| 39 | + end |
| 40 | + |
| 41 | + it "generates a partner_id attribute with the correct partner code attached" do |
| 42 | + visit '/checkout/payment' |
| 43 | + expect(js_sdk_script_partner_id).to eq("Solidus_PCP_SP") |
| 44 | + end |
| 45 | + |
| 46 | + it "generates a URL with the correct currency" do |
| 47 | + allow(order).to receive(:currency).and_return "EUR" |
| 48 | + visit '/checkout/payment' |
| 49 | + expect(js_sdk_script_query).to include("currency=EUR") |
| 50 | + end |
| 51 | + |
| 52 | + context "when auto-capture is set to true" do |
| 53 | + it "generates a url with intent capture" do |
| 54 | + paypal_payment_method.update(auto_capture: true) |
| 55 | + visit '/checkout/payment' |
| 56 | + expect(js_sdk_script_query).to include("client-id=#{paypal_payment_method.preferences[:client_id]}") |
| 57 | + expect(js_sdk_script_query).to include("intent=capture") |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context "when no payment has been made" do |
| 63 | + it "fails to process" do |
| 64 | + visit '/checkout/payment' |
| 65 | + choose(option: paypal_payment_method.id) |
| 66 | + click_button("Save and Continue") |
| 67 | + expect(page).to have_content("Payments source PayPal order can't be blank") |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context "when a payment has been made" do |
| 72 | + it "proceeds to the next step" do |
| 73 | + visit '/checkout/payment' |
| 74 | + choose(option: paypal_payment_method.id) |
| 75 | + find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8) |
| 76 | + click_button("Save and Continue") |
| 77 | + expect(page).to have_css(".current", text: "Confirm") |
| 78 | + end |
| 79 | + |
| 80 | + it "records the paypal email address" do |
| 81 | + visit '/checkout/payment' |
| 82 | + choose(option: paypal_payment_method.id) |
| 83 | + find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8) |
| 84 | + find(:xpath, "//input[@id='payments_source_paypal_email']", visible: false).set "fake@email.com" |
| 85 | + click_button("Save and Continue") |
| 86 | + expect(Spree::Payment.last.source.paypal_email).to eq "fake@email.com" |
| 87 | + end |
| 88 | + |
| 89 | + it "records the paypal funding source" do |
| 90 | + visit '/checkout/payment' |
| 91 | + choose(option: paypal_payment_method.id) |
| 92 | + find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8) |
| 93 | + find(:xpath, "//input[@id='payments_source_paypal_email']", visible: false).set "fake@email.com" |
| 94 | + find(:xpath, "//input[@id='payments_source_paypal_funding_source']", visible: false).set "venmo" |
| 95 | + click_button("Save and Continue") |
| 96 | + expect(Spree::Payment.last.source.paypal_funding_source).to eq "venmo" |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context "when a payment fails" do |
| 101 | + before { allow_any_instance_of(PayPal::PayPalHttpClient).to receive(:execute) { failed_response } } |
| 102 | + |
| 103 | + it "redirects the user back to the payments page" do |
| 104 | + visit '/checkout/payment' |
| 105 | + choose(option: paypal_payment_method.id) |
| 106 | + find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8) |
| 107 | + click_button("Save and Continue") |
| 108 | + click_button("Place Order") |
| 109 | + expect(page).to have_current_path("/checkout/payment") |
| 110 | + expect(page).to have_content("Your payment was declined") |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments