solidus_braintree 1.2.0 → 3.0.0

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