solidus_paypal_braintree 0.2.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +40 -0
- data/.gem_release.yml +5 -0
- data/.github/stale.yml +17 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.rubocop.yml +76 -0
- data/CHANGELOG.md +258 -0
- data/Gemfile +41 -0
- data/LICENSE +2 -2
- data/README.md +208 -48
- data/Rakefile +4 -28
- data/app/assets/config/solidus_paypal_braintree_manifest.js +1 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_blue_button_280x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_blue_button_320x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_blue_button_375x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_white_button_280x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_white_button_320x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_active_white_button_375x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_acceptance_mark.svg +15 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_button_280x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_button_320x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_button_375x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_blue_logo.svg +18 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_acceptance_mark.svg +20 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_button_280x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_button_320x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_button_375x48.svg +19 -0
- data/app/assets/images/solidus_paypal_braintree/venmo/venmo_white_logo.svg +18 -0
- data/app/assets/javascripts/solidus_paypal_braintree/checkout.js +32 -3
- data/app/assets/javascripts/solidus_paypal_braintree/client.js +58 -5
- data/app/assets/javascripts/solidus_paypal_braintree/constants.js +36 -5
- data/app/assets/javascripts/solidus_paypal_braintree/frontend.js +2 -0
- data/app/assets/javascripts/solidus_paypal_braintree/hosted_form.js +15 -5
- data/app/assets/javascripts/solidus_paypal_braintree/paypal_button.js +90 -26
- data/app/assets/javascripts/solidus_paypal_braintree/paypal_messaging.js +22 -0
- data/app/assets/javascripts/solidus_paypal_braintree/venmo_button.js +86 -0
- data/app/assets/javascripts/spree/backend/solidus_paypal_braintree.js +2 -2
- data/app/assets/javascripts/spree/frontend/paypal_button.js +15 -13
- data/app/assets/stylesheets/spree/frontend/solidus_paypal_braintree.css +12 -0
- data/app/decorators/controllers/solidus_paypal_braintree/admin_payments_controller_decorator.rb +11 -0
- data/app/decorators/controllers/solidus_paypal_braintree/checkout_controller_decorator.rb +11 -0
- data/app/decorators/controllers/solidus_paypal_braintree/client_tokens_controller.rb +41 -0
- data/app/decorators/controllers/solidus_paypal_braintree/orders_controller_decorator.rb +11 -0
- data/app/decorators/models/solidus_paypal_braintree/spree/store_decorator.rb +20 -0
- data/app/decorators/models/solidus_paypal_braintree/spree/user_decorator.rb +13 -0
- data/app/helpers/solidus_paypal_braintree/braintree_admin_helper.rb +23 -0
- data/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb +60 -0
- data/app/models/application_record.rb +2 -0
- data/app/models/solidus_paypal_braintree/address.rb +64 -0
- data/app/models/solidus_paypal_braintree/avs_result.rb +69 -0
- data/app/models/solidus_paypal_braintree/configuration.rb +39 -3
- data/app/models/solidus_paypal_braintree/customer.rb +7 -3
- data/app/models/solidus_paypal_braintree/gateway.rb +150 -43
- data/app/models/solidus_paypal_braintree/response.rb +49 -21
- data/app/models/solidus_paypal_braintree/source.rb +70 -10
- data/app/models/solidus_paypal_braintree/transaction.rb +3 -2
- data/app/models/solidus_paypal_braintree/transaction_address.rb +36 -15
- data/app/models/solidus_paypal_braintree/transaction_import.rb +18 -12
- data/app/overrides/spree/payments/payment/add_paypal_funding_source_to_payment.rb +9 -0
- data/app/views/spree/api/payments/source_views/_paypal_braintree.json.jbuilder +3 -0
- data/app/views/spree/checkout/existing_payment/_paypal_braintree.html.erb +10 -0
- data/app/views/spree/shared/_apple_pay_button.html.erb +27 -0
- data/app/views/spree/shared/_braintree_errors.html.erb +16 -0
- data/app/views/spree/shared/_braintree_hosted_fields.html.erb +25 -8
- data/app/views/spree/shared/_paypal_braintree_head_scripts.html.erb +26 -0
- data/app/views/spree/shared/_paypal_cart_button.html.erb +38 -0
- data/app/views/spree/shared/_paypal_messaging.html.erb +13 -0
- data/app/views/spree/shared/_venmo_button.html.erb +33 -0
- data/bin/console +17 -0
- data/bin/rails +15 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +66 -0
- data/config/locales/it.yml +56 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20160906201711_create_solidus_paypal_braintree_customers.rb +3 -1
- data/db/migrate/20161125172005_add_braintree_configuration_to_stores.rb +5 -7
- data/db/migrate/20170505193712_add_null_constraint_to_sources.rb +3 -1
- data/db/migrate/20190705115327_add_paypal_button_preferences_to_braintree_configurations.rb +5 -0
- data/db/migrate/20190911141712_add_3d_secure_to_braintree_configuration.rb +5 -0
- data/db/migrate/20211222170950_add_paypal_funding_source_to_solidus_paypal_braintree_sources.rb +5 -0
- data/db/migrate/20220104150301_add_venmo_to_braintree_configuration.rb +5 -0
- data/lib/controllers/backend/solidus_paypal_braintree/configurations_controller.rb +23 -5
- data/lib/controllers/frontend/solidus_paypal_braintree/checkouts_controller.rb +25 -21
- data/lib/controllers/frontend/solidus_paypal_braintree/transactions_controller.rb +56 -50
- data/lib/generators/solidus_paypal_braintree/install/install_generator.rb +7 -6
- data/lib/solidus_paypal_braintree/country_mapper.rb +4 -2
- data/lib/solidus_paypal_braintree/engine.rb +34 -22
- data/lib/solidus_paypal_braintree/factories.rb +41 -6
- data/lib/solidus_paypal_braintree/request_protection.rb +21 -0
- data/lib/solidus_paypal_braintree/version.rb +3 -1
- data/lib/solidus_paypal_braintree.rb +5 -1
- data/lib/views/backend/solidus_paypal_braintree/configurations/list.html.erb +38 -5
- data/lib/views/backend/spree/admin/payments/source_forms/_paypal_braintree.html.erb +2 -2
- data/lib/views/backend/spree/admin/payments/source_views/_paypal_braintree.html.erb +7 -2
- data/lib/views/backend/spree/admin/shared/preference_fields/_preference_select.html.erb +13 -0
- data/lib/views/backend_v1.2/spree/admin/payments/source_forms/_paypal_braintree.html.erb +2 -2
- data/lib/views/backend_v2.4/spree/admin/shared/preference_fields/_hash.html.erb +12 -0
- data/lib/views/frontend/solidus_paypal_braintree/payments/_payment.html.erb +12 -0
- data/lib/views/frontend/spree/checkout/payment/_paypal_braintree.html.erb +10 -77
- data/lib/views/frontend/spree/shared/_paypal_checkout_button.html.erb +32 -0
- data/solidus_paypal_braintree.gemspec +43 -0
- data/spec/controllers/solidus_paypal_braintree/checkouts_controller_spec.rb +99 -0
- data/spec/controllers/solidus_paypal_braintree/client_tokens_controller_spec.rb +55 -0
- data/spec/controllers/solidus_paypal_braintree/configurations_controller_spec.rb +73 -0
- data/spec/controllers/solidus_paypal_braintree/transactions_controller_spec.rb +183 -0
- data/spec/features/backend/configuration_spec.rb +23 -0
- data/spec/features/backend/new_payment_spec.rb +137 -0
- data/spec/features/frontend/braintree_credit_card_checkout_spec.rb +191 -0
- data/spec/features/frontend/paypal_checkout_spec.rb +166 -0
- data/spec/features/frontend/venmo_checkout_spec.rb +189 -0
- data/spec/fixtures/cassettes/admin/invalid_credit_card.yml +63 -0
- data/spec/fixtures/cassettes/admin/resubmit_credit_card.yml +352 -0
- data/spec/fixtures/cassettes/admin/valid_credit_card.yml +412 -0
- data/spec/fixtures/cassettes/braintree/create_profile.yml +71 -0
- data/spec/fixtures/cassettes/braintree/generate_token.yml +63 -0
- data/spec/fixtures/cassettes/braintree/token.yml +63 -0
- data/spec/fixtures/cassettes/checkout/invalid_credit_card.yml +63 -0
- data/spec/fixtures/cassettes/checkout/resubmit_credit_card.yml +216 -0
- data/spec/fixtures/cassettes/checkout/update.yml +71 -0
- data/spec/fixtures/cassettes/checkout/valid_credit_card.yml +156 -0
- data/spec/fixtures/cassettes/checkout/valid_venmo_transaction.yml +599 -0
- data/spec/fixtures/cassettes/gateway/authorize/credit_card/address.yml +86 -0
- data/spec/fixtures/cassettes/gateway/authorize/merchant_account/EUR.yml +154 -0
- data/spec/fixtures/cassettes/gateway/authorize/paypal/EUR.yml +90 -0
- data/spec/fixtures/cassettes/gateway/authorize/paypal/address.yml +90 -0
- data/spec/fixtures/cassettes/gateway/authorize.yml +86 -0
- data/spec/fixtures/cassettes/gateway/authorized_transaction.yml +73 -0
- data/spec/fixtures/cassettes/gateway/cancel/missing.yml +63 -0
- data/spec/fixtures/cassettes/gateway/cancel/refunds.yml +272 -0
- data/spec/fixtures/cassettes/gateway/cancel/void.yml +201 -0
- data/spec/fixtures/cassettes/gateway/capture.yml +141 -0
- data/spec/fixtures/cassettes/gateway/complete.yml +157 -0
- data/spec/fixtures/cassettes/gateway/credit.yml +208 -0
- data/spec/fixtures/cassettes/gateway/purchase.yml +87 -0
- data/spec/fixtures/cassettes/gateway/settled_transaction.yml +140 -0
- data/spec/fixtures/cassettes/gateway/void.yml +137 -0
- data/spec/fixtures/cassettes/source/card_type.yml +267 -0
- data/spec/fixtures/cassettes/source/last4.yml +267 -0
- data/spec/fixtures/cassettes/transaction/import/valid/capture.yml +224 -0
- data/spec/fixtures/cassettes/transaction/import/valid.yml +71 -0
- data/spec/fixtures/views/spree/orders/edit.html.erb +50 -0
- data/spec/helpers/solidus_paypal_braintree/braintree_admin_helper_spec.rb +17 -0
- data/spec/helpers/solidus_paypal_braintree/braintree_checkout_helper_spec.rb +70 -0
- data/spec/models/solidus_paypal_braintree/address_spec.rb +71 -0
- data/spec/models/solidus_paypal_braintree/avs_result_spec.rb +317 -0
- data/spec/models/solidus_paypal_braintree/gateway_spec.rb +692 -0
- data/spec/models/solidus_paypal_braintree/response_spec.rb +280 -0
- data/spec/models/solidus_paypal_braintree/source_spec.rb +499 -0
- data/spec/models/solidus_paypal_braintree/transaction_address_spec.rb +235 -0
- data/spec/models/solidus_paypal_braintree/transaction_import_spec.rb +300 -0
- data/spec/models/solidus_paypal_braintree/transaction_spec.rb +85 -0
- data/spec/models/spree/store_spec.rb +14 -0
- data/spec/requests/spree/api/orders_controller_spec.rb +36 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/capybara.rb +7 -0
- data/spec/support/factories.rb +2 -0
- data/spec/support/gateway_helpers.rb +29 -0
- data/spec/support/order_ready_for_payment.rb +37 -0
- data/spec/support/vcr.rb +42 -0
- data/spec/support/views.rb +1 -0
- metadata +226 -166
- data/app/controllers/solidus_paypal_braintree/client_tokens_controller.rb +0 -21
- data/app/helpers/braintree_admin_helper.rb +0 -18
- data/app/models/spree/store_decorator.rb +0 -11
- data/app/overrides/admin_navigation_menu.rb +0 -6
- data/config/initializers/braintree.rb +0 -1
- data/lib/views/backend/solidus_paypal_braintree/configurations/_admin_tab.html.erb +0 -3
@@ -0,0 +1,499 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe SolidusPaypalBraintree::Source, type: :model do
|
4
|
+
include_context 'when order is ready for payment'
|
5
|
+
|
6
|
+
it 'is invalid without a payment_type set' do
|
7
|
+
expect(described_class.new).to be_invalid
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'is invalid with payment_type set to unknown type' do
|
11
|
+
expect(described_class.new(payment_type: 'AndroidPay')).to be_invalid
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'attributes' do
|
15
|
+
context 'with paypal_funding_source' do
|
16
|
+
subject { build(:solidus_paypal_braintree_source, :paypal) }
|
17
|
+
|
18
|
+
it 'can be nil' do
|
19
|
+
subject.paypal_funding_source = nil
|
20
|
+
|
21
|
+
expect(subject).to be_valid
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'makes empty strings nil' do
|
25
|
+
subject.paypal_funding_source = ''
|
26
|
+
|
27
|
+
result = subject.save
|
28
|
+
|
29
|
+
expect(result).to eq(true)
|
30
|
+
expect(subject.paypal_funding_source).to be_nil
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'gets correctly mapped as an enum' do
|
34
|
+
subject.paypal_funding_source = 'applepay'
|
35
|
+
|
36
|
+
result = subject.save
|
37
|
+
|
38
|
+
expect(result).to eq(true)
|
39
|
+
expect(subject.paypal_funding_source).to eq('applepay')
|
40
|
+
expect(subject.applepay_funding?).to eq(true)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "doesn't become nil when the payment_type is a PAYPAL" do
|
44
|
+
subject.payment_type = described_class::PAYPAL
|
45
|
+
subject.paypal_funding_source = 'venmo'
|
46
|
+
|
47
|
+
result = subject.save
|
48
|
+
|
49
|
+
expect(result).to eq(true)
|
50
|
+
expect(subject.venmo_funding?).to eq(true)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'becomes nil when the payment_type is a CREDIT CARD' do
|
54
|
+
subject.payment_type = described_class::CREDIT_CARD
|
55
|
+
subject.paypal_funding_source = 'venmo'
|
56
|
+
|
57
|
+
result = subject.save
|
58
|
+
|
59
|
+
expect(result).to eq(true)
|
60
|
+
expect(subject.paypal_funding_source).to be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'becomes nil when the payment_type is APPLE PAY' do
|
64
|
+
subject.payment_type = described_class::APPLE_PAY
|
65
|
+
subject.paypal_funding_source = 'venmo'
|
66
|
+
|
67
|
+
result = subject.save
|
68
|
+
|
69
|
+
expect(result).to eq(true)
|
70
|
+
expect(subject.paypal_funding_source).to be_nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#payment_method' do
|
76
|
+
it 'uses spree_payment_method' do
|
77
|
+
expect(described_class.new.build_payment_method).to be_a Spree::PaymentMethod
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#imported' do
|
82
|
+
it 'is always false' do
|
83
|
+
expect(described_class.new.imported).not_to be_truthy
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#actions" do
|
88
|
+
it "supports capture, void, and credit" do
|
89
|
+
expect(described_class.new.actions).to eq %w[capture void credit]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "#can_capture?" do
|
94
|
+
subject { described_class.new.can_capture?(payment) }
|
95
|
+
|
96
|
+
context "when the payment state is pending" do
|
97
|
+
let(:payment) { build(:payment, state: "pending") }
|
98
|
+
|
99
|
+
it { is_expected.to be_truthy }
|
100
|
+
end
|
101
|
+
|
102
|
+
context "when the payment state is checkout" do
|
103
|
+
let(:payment) { build(:payment, state: "checkout") }
|
104
|
+
|
105
|
+
it { is_expected.to be_truthy }
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when the payment is completed" do
|
109
|
+
let(:payment) { build(:payment, state: "completed") }
|
110
|
+
|
111
|
+
it { is_expected.not_to be_truthy }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe '#can_void?' do
|
116
|
+
subject { payment_source.can_void?(payment) }
|
117
|
+
|
118
|
+
let(:payment_source) { described_class.new }
|
119
|
+
let(:payment) { build(:payment) }
|
120
|
+
|
121
|
+
let(:transaction_response) do
|
122
|
+
double(:response, status: Braintree::Transaction::Status::SubmittedForSettlement)
|
123
|
+
end
|
124
|
+
|
125
|
+
let(:transaction_request) do
|
126
|
+
double(:request, find: transaction_response)
|
127
|
+
end
|
128
|
+
|
129
|
+
before do
|
130
|
+
allow(payment_source).to receive(:braintree_client) do
|
131
|
+
double(:transaction, transaction: transaction_request)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'when transaction id is not present' do
|
136
|
+
let(:payment) { build(:payment, response_code: nil) }
|
137
|
+
|
138
|
+
it { is_expected.to be(false) }
|
139
|
+
end
|
140
|
+
|
141
|
+
context 'when transaction has voidable status' do
|
142
|
+
it { is_expected.to be(true) }
|
143
|
+
end
|
144
|
+
|
145
|
+
context 'when transaction has non voidable status' do
|
146
|
+
let(:transaction_response) do
|
147
|
+
double(:response, status: Braintree::Transaction::Status::Settled)
|
148
|
+
end
|
149
|
+
|
150
|
+
it { is_expected.to be(false) }
|
151
|
+
end
|
152
|
+
|
153
|
+
context 'when transaction is not found at Braintreee' do
|
154
|
+
before do
|
155
|
+
allow(transaction_request).to \
|
156
|
+
receive(:find).and_raise(Braintree::NotFoundError)
|
157
|
+
end
|
158
|
+
|
159
|
+
it { is_expected.to be(false) }
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "#can_credit?" do
|
164
|
+
subject { described_class.new.can_credit?(payment) }
|
165
|
+
|
166
|
+
context "when the payment is completed" do
|
167
|
+
context "when the credit allowed is 100" do
|
168
|
+
let(:payment) { build(:payment, state: "completed", amount: 100) }
|
169
|
+
|
170
|
+
it { is_expected.to be_truthy }
|
171
|
+
end
|
172
|
+
|
173
|
+
context "when the credit allowed is 0" do
|
174
|
+
let(:payment) { build(:payment, state: "completed", amount: 0) }
|
175
|
+
|
176
|
+
it { is_expected.not_to be_truthy }
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
context "when the payment has not been completed" do
|
181
|
+
let(:payment) { build(:payment, state: "checkout") }
|
182
|
+
|
183
|
+
it { is_expected.not_to be_truthy }
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe "#friendly_payment_type" do
|
188
|
+
subject { described_class.new(payment_type: type).friendly_payment_type }
|
189
|
+
|
190
|
+
context "when then payment type is PayPal" do
|
191
|
+
let(:type) { "PayPalAccount" }
|
192
|
+
|
193
|
+
it "returns the translated payment type" do
|
194
|
+
expect(subject).to eq "PayPal"
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context "when the payment type is Apple Pay" do
|
199
|
+
let(:type) { "ApplePayCard" }
|
200
|
+
|
201
|
+
it "returns the translated payment type" do
|
202
|
+
expect(subject).to eq "Apple Pay"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
context "when the payment type is Credit Card" do
|
207
|
+
let(:type) { "CreditCard" }
|
208
|
+
|
209
|
+
it "returns the translated payment type" do
|
210
|
+
expect(subject).to eq "Credit Card"
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
describe "#apple_pay?" do
|
216
|
+
subject { described_class.new(payment_type: type).apple_pay? }
|
217
|
+
|
218
|
+
context "when the payment type is Apple Pay" do
|
219
|
+
let(:type) { "ApplePayCard" }
|
220
|
+
|
221
|
+
it { is_expected.to be true }
|
222
|
+
end
|
223
|
+
|
224
|
+
context "when the payment type is not Apple Pay" do
|
225
|
+
let(:type) { "DogeCoin" }
|
226
|
+
|
227
|
+
it { is_expected.to be false }
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
describe "#paypal?" do
|
232
|
+
subject { described_class.new(payment_type: type).paypal? }
|
233
|
+
|
234
|
+
context "when the payment type is PayPal" do
|
235
|
+
let(:type) { "PayPalAccount" }
|
236
|
+
|
237
|
+
it { is_expected.to be true }
|
238
|
+
end
|
239
|
+
|
240
|
+
context "when the payment type is not PayPal" do
|
241
|
+
let(:type) { "MonopolyMoney" }
|
242
|
+
|
243
|
+
it { is_expected.to be false }
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "#credit_card?" do
|
248
|
+
subject { described_class.new(payment_type: type).credit_card? }
|
249
|
+
|
250
|
+
context "when the payment type is CreditCard" do
|
251
|
+
let(:type) { "CreditCard" }
|
252
|
+
|
253
|
+
it { is_expected.to be true }
|
254
|
+
end
|
255
|
+
|
256
|
+
context "when the payment type is not CreditCard" do
|
257
|
+
let(:type) { "MonopolyMoney" }
|
258
|
+
|
259
|
+
it { is_expected.to be false }
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe "#venmo?" do
|
264
|
+
subject { described_class.new(payment_type: type).venmo? }
|
265
|
+
|
266
|
+
context "when the payment type is VenmoAccount" do
|
267
|
+
let(:type) { "VenmoAccount" }
|
268
|
+
|
269
|
+
it { is_expected.to be true }
|
270
|
+
end
|
271
|
+
|
272
|
+
context "when the payment type is not VenmoAccount" do
|
273
|
+
let(:type) { "Swish" }
|
274
|
+
|
275
|
+
it { is_expected.to be false }
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
shared_context 'with unknown source token' do
|
280
|
+
let(:braintree_payment_method) { double }
|
281
|
+
|
282
|
+
before do
|
283
|
+
allow(braintree_payment_method).to receive(:find) do
|
284
|
+
raise Braintree::NotFoundError
|
285
|
+
end
|
286
|
+
allow(payment_source).to receive(:braintree_client) do
|
287
|
+
instance_double(payment_method: braintree_payment_method)
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
shared_context 'with nil source token' do
|
293
|
+
let(:braintree_payment_method) { double }
|
294
|
+
|
295
|
+
before do
|
296
|
+
allow(braintree_payment_method).to receive(:find) do
|
297
|
+
raise ArgumentError
|
298
|
+
end
|
299
|
+
allow(payment_source).to receive(:braintree_client) do
|
300
|
+
instance_double(payment_method: braintree_payment_method)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
describe "#last_4" do
|
306
|
+
subject { payment_source.last_4 }
|
307
|
+
|
308
|
+
let(:method) { new_gateway.tap(&:save!) }
|
309
|
+
let(:payment_source) { described_class.create!(payment_type: "CreditCard", payment_method: method) }
|
310
|
+
let(:braintree_client) { method.braintree }
|
311
|
+
|
312
|
+
context 'when token is known at braintree', vcr: {
|
313
|
+
cassette_name: "source/last4",
|
314
|
+
match_requests_on: [:braintree_uri]
|
315
|
+
} do
|
316
|
+
before do
|
317
|
+
customer = braintree_client.customer.create
|
318
|
+
|
319
|
+
method = braintree_client.payment_method.create({
|
320
|
+
payment_method_nonce: "fake-valid-country-of-issuance-usa-nonce",
|
321
|
+
customer_id: customer.customer.id
|
322
|
+
})
|
323
|
+
|
324
|
+
payment_source.update!(token: method.payment_method.token)
|
325
|
+
end
|
326
|
+
|
327
|
+
it "delegates to the braintree payment method" do
|
328
|
+
method = braintree_client.payment_method.find(payment_source.token)
|
329
|
+
expect(subject).to eql(method.last_4)
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
context 'when the source token is not known at Braintree' do
|
334
|
+
include_context 'with unknown source token'
|
335
|
+
|
336
|
+
it { is_expected.to be(nil) }
|
337
|
+
end
|
338
|
+
|
339
|
+
context 'when the source token is nil' do
|
340
|
+
include_context 'with nil source token'
|
341
|
+
|
342
|
+
it { is_expected.to be(nil) }
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
describe "#display_number" do
|
347
|
+
subject { payment_source.display_number }
|
348
|
+
|
349
|
+
let(:type) { nil }
|
350
|
+
let(:payment_source) { described_class.new(payment_type: type) }
|
351
|
+
|
352
|
+
context "when last_digits is a number" do
|
353
|
+
before do
|
354
|
+
allow(payment_source).to receive(:last_digits).and_return('1234')
|
355
|
+
end
|
356
|
+
|
357
|
+
it { is_expected.to eq 'XXXX-XXXX-XXXX-1234' }
|
358
|
+
end
|
359
|
+
|
360
|
+
context "when last_digits is nil" do
|
361
|
+
before do
|
362
|
+
allow(payment_source).to receive(:last_digits).and_return(nil)
|
363
|
+
end
|
364
|
+
|
365
|
+
it { is_expected.to eq 'XXXX-XXXX-XXXX-XXXX' }
|
366
|
+
end
|
367
|
+
|
368
|
+
context "when is a PayPal source" do
|
369
|
+
let(:type) { "PayPalAccount" }
|
370
|
+
|
371
|
+
before do
|
372
|
+
allow(payment_source).to receive(:email).and_return('user@example.com')
|
373
|
+
end
|
374
|
+
|
375
|
+
it { is_expected.to eq 'user@example.com' }
|
376
|
+
end
|
377
|
+
|
378
|
+
context "when is a Venmo source" do
|
379
|
+
let(:type) { "VenmoAccount" }
|
380
|
+
|
381
|
+
before do
|
382
|
+
allow(payment_source).to receive(:username).and_return('venmojoe')
|
383
|
+
end
|
384
|
+
|
385
|
+
it { is_expected.to eq('venmojoe') }
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
describe "#card_type" do
|
390
|
+
subject { payment_source.card_type }
|
391
|
+
|
392
|
+
let(:method) { new_gateway.tap(&:save!) }
|
393
|
+
let(:payment_source) { described_class.create!(payment_type: "CreditCard", payment_method: method) }
|
394
|
+
let(:braintree_client) { method.braintree }
|
395
|
+
|
396
|
+
context "when the token is known at braintree", vcr: {
|
397
|
+
cassette_name: "source/card_type",
|
398
|
+
match_requests_on: [:braintree_uri]
|
399
|
+
} do
|
400
|
+
before do
|
401
|
+
customer = braintree_client.customer.create
|
402
|
+
|
403
|
+
method = braintree_client.payment_method.create({
|
404
|
+
payment_method_nonce: "fake-valid-country-of-issuance-usa-nonce", customer_id: customer.customer.id
|
405
|
+
})
|
406
|
+
|
407
|
+
payment_source.update!(token: method.payment_method.token)
|
408
|
+
end
|
409
|
+
|
410
|
+
it "delegates to the braintree payment method" do
|
411
|
+
method = braintree_client.payment_method.find(payment_source.token)
|
412
|
+
expect(subject).to eql(method.card_type)
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
context 'when the source token is not known at Braintree' do
|
417
|
+
include_context 'with unknown source token'
|
418
|
+
|
419
|
+
it { is_expected.to be_nil }
|
420
|
+
end
|
421
|
+
|
422
|
+
context 'when the source token is nil' do
|
423
|
+
include_context 'with nil source token'
|
424
|
+
|
425
|
+
it { is_expected.to be_nil }
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
describe '#display_paypal_funding_source' do
|
430
|
+
let(:payment_source) { described_class.new }
|
431
|
+
|
432
|
+
context 'when the EN locale exists' do
|
433
|
+
it 'translates the funding source' do
|
434
|
+
payment_source.paypal_funding_source = 'card'
|
435
|
+
|
436
|
+
result = payment_source.display_paypal_funding_source
|
437
|
+
|
438
|
+
expect(result).to eq('Credit or debit card')
|
439
|
+
end
|
440
|
+
end
|
441
|
+
|
442
|
+
context "when the locale doesn't exist" do
|
443
|
+
it 'returns the paypal_funding_source as the default' do
|
444
|
+
allow(payment_source).to receive(:paypal_funding_source).and_return('non-existent')
|
445
|
+
|
446
|
+
result = payment_source.display_paypal_funding_source
|
447
|
+
|
448
|
+
expect(result).to eq('non-existent')
|
449
|
+
end
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
describe '#display_payment_type' do
|
454
|
+
subject { described_class.new(payment_type: type).display_payment_type }
|
455
|
+
|
456
|
+
context 'when type is CreditCard' do
|
457
|
+
let(:type) { 'CreditCard' }
|
458
|
+
|
459
|
+
it 'returns "Payment Type: Credit Card' do
|
460
|
+
expect(subject).to eq('Payment Type: Credit Card')
|
461
|
+
end
|
462
|
+
end
|
463
|
+
|
464
|
+
context 'when type is PayPalAccount' do
|
465
|
+
let(:type) { 'PayPalAccount' }
|
466
|
+
|
467
|
+
it 'returns "Payment Type: PayPal' do
|
468
|
+
expect(subject).to eq('Payment Type: PayPal')
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
context 'when type is VenmoAccount' do
|
473
|
+
let(:type) { 'VenmoAccount' }
|
474
|
+
|
475
|
+
it 'returns "Payment Type: Venmo' do
|
476
|
+
expect(subject).to eq('Payment Type: Venmo')
|
477
|
+
end
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|
481
|
+
describe '#reusable?' do
|
482
|
+
subject { payment_source.reusable? }
|
483
|
+
|
484
|
+
let(:payment_source) { described_class.new(token: token, nonce: nonce) }
|
485
|
+
let(:nonce) { 'nonce67890' }
|
486
|
+
|
487
|
+
context 'when source token is present' do
|
488
|
+
let(:token) { 'token12345' }
|
489
|
+
|
490
|
+
it { is_expected.to be_truthy }
|
491
|
+
end
|
492
|
+
|
493
|
+
context 'when source token is nil' do
|
494
|
+
let(:token) { nil }
|
495
|
+
|
496
|
+
it { is_expected.to be_falsy }
|
497
|
+
end
|
498
|
+
end
|
499
|
+
end
|