solidus_stripe 4.4.0 → 5.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +102 -41
  3. data/.gitignore +3 -0
  4. data/.rubocop.yml +94 -4
  5. data/.yardopts +1 -0
  6. data/CHANGELOG.md +1 -265
  7. data/Gemfile +10 -30
  8. data/LICENSE +2 -2
  9. data/Procfile.dev +3 -0
  10. data/README.md +185 -213
  11. data/Rakefile +7 -6
  12. data/app/assets/javascripts/spree/backend/solidus_stripe.js +2 -0
  13. data/app/assets/stylesheets/spree/backend/solidus_stripe.css +4 -0
  14. data/app/controllers/solidus_stripe/intents_controller.rb +36 -52
  15. data/app/controllers/solidus_stripe/webhooks_controller.rb +28 -0
  16. data/app/models/concerns/solidus_stripe/log_entries.rb +31 -0
  17. data/app/models/solidus_stripe/customer.rb +21 -0
  18. data/app/models/solidus_stripe/gateway.rb +229 -0
  19. data/app/models/solidus_stripe/payment_intent.rb +124 -0
  20. data/app/models/solidus_stripe/payment_method.rb +106 -0
  21. data/app/models/solidus_stripe/payment_source.rb +31 -0
  22. data/app/models/solidus_stripe/slug_entry.rb +20 -0
  23. data/app/models/solidus_stripe.rb +7 -0
  24. data/app/subscribers/solidus_stripe/webhook/charge_subscriber.rb +28 -0
  25. data/app/subscribers/solidus_stripe/webhook/payment_intent_subscriber.rb +112 -0
  26. data/app/views/spree/admin/payments/source_forms/_stripe.html.erb +29 -0
  27. data/app/views/spree/admin/payments/source_forms/existing_payment/_stripe.html.erb +19 -0
  28. data/app/views/spree/admin/payments/source_forms/existing_payment/stripe/_card.html.erb +8 -0
  29. data/app/views/spree/admin/payments/source_forms/existing_payment/stripe/_default.html.erb +7 -0
  30. data/app/views/spree/admin/payments/source_views/_stripe.html.erb +15 -0
  31. data/app/views/spree/api/payments/source_views/_stripe.json.jbuilder +8 -0
  32. data/bin/dev +13 -0
  33. data/bin/dummy-app +29 -0
  34. data/bin/rails +38 -3
  35. data/bin/rails-dummy-app +3 -0
  36. data/bin/rails-engine +1 -11
  37. data/bin/rails-new +55 -0
  38. data/bin/rails-sandbox +1 -14
  39. data/bin/rspec +10 -0
  40. data/bin/sandbox +12 -74
  41. data/bin/setup +1 -0
  42. data/bin/update-migrations +56 -0
  43. data/codecov.yml +12 -0
  44. data/config/locales/en.yml +16 -1
  45. data/config/routes.rb +5 -11
  46. data/coverage.rb +42 -0
  47. data/db/migrate/20230109183332_create_solidus_stripe_payment_sources.rb +10 -0
  48. data/db/migrate/20230306105520_create_solidus_stripe_payment_intents.rb +14 -0
  49. data/db/migrate/20230308122414_create_solidus_stripe_slug_entries.rb +12 -0
  50. data/db/migrate/20230313150008_create_solidus_stripe_customers.rb +15 -0
  51. data/db/seeds.rb +6 -24
  52. data/lib/generators/solidus_stripe/install/install_generator.rb +121 -14
  53. data/lib/generators/solidus_stripe/install/templates/app/assets/stylesheets/spree/frontend/solidus_stripe.css +13 -0
  54. data/lib/generators/solidus_stripe/install/templates/app/javascript/controllers/solidus_stripe_confirm_controller.js +39 -0
  55. data/lib/generators/solidus_stripe/install/templates/app/javascript/controllers/solidus_stripe_payment_controller.js +89 -0
  56. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/existing_payment/_stripe.html.erb +21 -0
  57. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/existing_payment/stripe/_card.html.erb +8 -0
  58. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/existing_payment/stripe/_default.html.erb +7 -0
  59. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/payment/_stripe.html.erb +74 -0
  60. data/lib/generators/solidus_stripe/install/templates/app/views/orders/payment_info/_stripe.html.erb +32 -0
  61. data/lib/generators/solidus_stripe/install/templates/config/initializers/solidus_stripe.rb +31 -0
  62. data/lib/solidus_stripe/configuration.rb +24 -3
  63. data/lib/solidus_stripe/engine.rb +19 -6
  64. data/lib/solidus_stripe/money_to_stripe_amount_converter.rb +109 -0
  65. data/lib/solidus_stripe/refunds_synchronizer.rb +98 -0
  66. data/lib/solidus_stripe/seeds.rb +19 -0
  67. data/lib/solidus_stripe/testing_support/factories.rb +153 -0
  68. data/lib/solidus_stripe/version.rb +1 -1
  69. data/lib/solidus_stripe/webhook/event.rb +91 -0
  70. data/lib/solidus_stripe.rb +0 -2
  71. data/solidus_stripe.gemspec +29 -6
  72. data/spec/lib/solidus_stripe/configuration_spec.rb +21 -0
  73. data/spec/lib/solidus_stripe/money_to_stripe_amount_converter_spec.rb +133 -0
  74. data/spec/lib/solidus_stripe/refunds_synchronizer_spec.rb +238 -0
  75. data/spec/lib/solidus_stripe/seeds_spec.rb +43 -0
  76. data/spec/lib/solidus_stripe/webhook/event_spec.rb +134 -0
  77. data/spec/models/concerns/solidus_stripe/log_entries_spec.rb +54 -0
  78. data/spec/models/solidus_stripe/customer_spec.rb +47 -0
  79. data/spec/models/solidus_stripe/gateway_spec.rb +281 -0
  80. data/spec/models/solidus_stripe/payment_intent_spec.rb +78 -0
  81. data/spec/models/solidus_stripe/payment_method_spec.rb +137 -0
  82. data/spec/models/solidus_stripe/payment_source_spec.rb +25 -0
  83. data/spec/requests/solidus_stripe/intents_controller_spec.rb +29 -0
  84. data/spec/requests/solidus_stripe/webhooks_controller/charge/refunded_spec.rb +31 -0
  85. data/spec/requests/solidus_stripe/webhooks_controller/payment_intent/canceled_spec.rb +23 -0
  86. data/spec/requests/solidus_stripe/webhooks_controller/payment_intent/payment_failed_spec.rb +23 -0
  87. data/spec/requests/solidus_stripe/webhooks_controller/payment_intent/succeeded_spec.rb +29 -0
  88. data/spec/requests/solidus_stripe/webhooks_controller_spec.rb +56 -0
  89. data/spec/solidus_stripe_spec_helper.rb +10 -0
  90. data/spec/subscribers/solidus_stripe/webhook/charge_subscriber_spec.rb +33 -0
  91. data/spec/subscribers/solidus_stripe/webhook/payment_intent_subscriber_spec.rb +297 -0
  92. data/spec/support/solidus_stripe/backend_test_helper.rb +185 -0
  93. data/spec/support/solidus_stripe/checkout_test_helper.rb +365 -0
  94. data/spec/support/solidus_stripe/factories.rb +5 -0
  95. data/spec/support/solidus_stripe/webhook/data_fixtures.rb +106 -0
  96. data/spec/support/solidus_stripe/webhook/event_with_context_factory.rb +82 -0
  97. data/spec/support/solidus_stripe/webhook/request_helper.rb +32 -0
  98. data/spec/system/backend/solidus_stripe/orders/payments_spec.rb +145 -0
  99. data/spec/system/frontend/.keep +0 -0
  100. data/spec/system/frontend/solidus_stripe/checkout_spec.rb +206 -0
  101. data/tmp/.keep +0 -0
  102. metadata +196 -75
  103. data/.rubocop_todo.yml +0 -298
  104. data/.travis.yml +0 -28
  105. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-cart-page-checkout.js +0 -122
  106. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-elements.js +0 -148
  107. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-init.js +0 -20
  108. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-payment-intents.js +0 -84
  109. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-payment-request-button-shared.js +0 -160
  110. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-payment.js +0 -16
  111. data/app/assets/javascripts/spree/frontend/solidus_stripe.js +0 -6
  112. data/app/controllers/solidus_stripe/payment_request_controller.rb +0 -52
  113. data/app/controllers/spree/stripe_controller.rb +0 -13
  114. data/app/decorators/models/spree/order_update_attributes_decorator.rb +0 -39
  115. data/app/decorators/models/spree/payment_decorator.rb +0 -11
  116. data/app/decorators/models/spree/refund_decorator.rb +0 -9
  117. data/app/models/solidus_stripe/address_from_params_service.rb +0 -72
  118. data/app/models/solidus_stripe/create_intents_payment_service.rb +0 -114
  119. data/app/models/solidus_stripe/prepare_order_for_payment_service.rb +0 -46
  120. data/app/models/solidus_stripe/shipping_rates_service.rb +0 -46
  121. data/app/models/spree/payment_method/stripe_credit_card.rb +0 -230
  122. data/bin/r +0 -13
  123. data/bin/sandbox_rails +0 -18
  124. data/db/migrate/20181010123508_update_stripe_payment_method_type_to_credit_card.rb +0 -21
  125. data/lib/assets/stylesheets/spree/frontend/solidus_stripe.scss +0 -11
  126. data/lib/solidus_stripe/testing_support/card_input_helper.rb +0 -34
  127. data/lib/tasks/solidus_stripe/db/seed.rake +0 -14
  128. data/lib/views/api/spree/api/payments/source_views/_stripe.json.jbuilder +0 -3
  129. data/lib/views/backend/spree/admin/log_entries/_stripe.html.erb +0 -28
  130. data/lib/views/backend/spree/admin/payments/source_forms/_stripe.html.erb +0 -1
  131. data/lib/views/backend/spree/admin/payments/source_views/_stripe.html.erb +0 -1
  132. data/lib/views/frontend/spree/checkout/existing_payment/_stripe.html.erb +0 -1
  133. data/lib/views/frontend/spree/checkout/payment/_stripe.html.erb +0 -8
  134. data/lib/views/frontend/spree/checkout/payment/v2/_javascript.html.erb +0 -78
  135. data/lib/views/frontend/spree/checkout/payment/v3/_elements.html.erb +0 -1
  136. data/lib/views/frontend/spree/checkout/payment/v3/_form_elements.html.erb +0 -40
  137. data/lib/views/frontend/spree/checkout/payment/v3/_intents.html.erb +0 -1
  138. data/lib/views/frontend/spree/checkout/payment/v3/_stripe.html.erb +0 -2
  139. data/lib/views/frontend/spree/orders/_stripe_payment_request_button.html.erb +0 -14
  140. data/spec/features/stripe_checkout_spec.rb +0 -486
  141. data/spec/models/solidus_stripe/address_from_params_service_spec.rb +0 -87
  142. data/spec/models/solidus_stripe/create_intents_payment_service_spec.rb +0 -127
  143. data/spec/models/solidus_stripe/prepare_order_for_payment_service_spec.rb +0 -65
  144. data/spec/models/solidus_stripe/shipping_rates_service_spec.rb +0 -54
  145. data/spec/models/spree/payment_method/stripe_credit_card_spec.rb +0 -350
  146. data/spec/requests/payment_requests_spec.rb +0 -152
  147. data/spec/spec_helper.rb +0 -37
  148. data/spec/support/solidus_address_helper.rb +0 -15
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe SolidusStripe::AddressFromParamsService do
6
- let(:service) { described_class.new(params, user) }
7
- let(:state) { create :state }
8
-
9
- describe '#call' do
10
- let(:params) do
11
- {
12
- country: state.country.iso,
13
- region: state.abbr,
14
- recipient: 'Clark Kent',
15
- city: 'Metropolis',
16
- postalCode: '12345',
17
- addressLine: [ '12, Lincoln Rd'],
18
- phone: '555-555-0199'
19
- }
20
- end
21
-
22
- subject { service.call }
23
-
24
- context "when there is no user" do
25
- let(:user) { nil }
26
-
27
- it "returns a non-persisted address model" do
28
- expect(subject).to be_new_record
29
- end
30
- end
31
-
32
- context "when there is a user" do
33
- let(:user) { create :user }
34
-
35
- context "when the user has an address compatible with the params" do
36
- before do
37
- name_attributes = if SolidusSupport.combined_first_and_last_name_in_address?
38
- {
39
- name: 'Clark Kent'
40
- }
41
- else
42
- {
43
- firstname: 'Clark',
44
- lastname: 'Kent',
45
- }
46
- end
47
-
48
- address_attributes = {
49
- city: params[:city],
50
- zipcode: params[:postalCode],
51
- address1: params[:addressLine].first,
52
- address2: nil,
53
- phone: '555-555-0199',
54
- }.merge!(name_attributes)
55
-
56
- user.addresses << create(:address, address_attributes)
57
- end
58
-
59
- it "returns an existing user's address" do
60
- expect(subject).to eql user.addresses.first
61
- end
62
- end
63
-
64
- context "when no user's address is compatible with the params" do
65
- before do
66
- user.addresses << create(:address, state: state)
67
- end
68
-
69
- it "returns a non-persisted valid address" do
70
- aggregate_failures do
71
- expect(subject).to be_new_record
72
- expect(subject).to be_valid
73
- expect(subject.state).to eq state
74
- end
75
- end
76
-
77
- context "when the region is the state name" do
78
- before { params[:region] = state.name }
79
-
80
- it "still can set the address state attribute" do
81
- expect(subject.state).to eq state
82
- end
83
- end
84
- end
85
- end
86
- end
87
- end
@@ -1,127 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe SolidusStripe::CreateIntentsPaymentService do
6
- let(:service) { described_class.new(intent_id, stripe, controller) }
7
-
8
- let(:stripe) {
9
- Spree::PaymentMethod::StripeCreditCard.create!(
10
- name: "Stripe",
11
- preferred_secret_key: "sk_test_VCZnDv3GLU15TRvn8i2EsaAN",
12
- preferred_publishable_key: "pk_test_Cuf0PNtiAkkMpTVC2gwYDMIg",
13
- preferred_v3_elements: false,
14
- preferred_v3_intents: true
15
- )
16
- }
17
-
18
- let(:order) { create :order, state: :payment, total: 19.99 }
19
-
20
- let(:intent_id) { "pi_123123ABC" }
21
- let(:controller) { double(current_order: order.reload, params: params, request: spy) }
22
-
23
- let(:params) do
24
- {
25
- spree_payment_method_id: stripe.id,
26
- stripe_payment_intent_id: intent_id,
27
- form_data: {
28
- addressLine: ["31 Cotton Rd"],
29
- city: order.bill_address.city,
30
- country: order.bill_address.country.iso,
31
- region: order.bill_address.state.abbr,
32
- phone: "+188836412312",
33
- postalCode: "12345",
34
- recipient: "James Edwards",
35
- }
36
- }
37
- end
38
-
39
- let(:intent) do
40
- double(params: {
41
- "id" => intent_id,
42
- "charges" => {
43
- "data" => [{
44
- "billing_details" => {
45
- "name" => "John Doe"
46
- },
47
- "payment_method_details" => {
48
- "card" => {
49
- "brand" => "visa",
50
- "exp_month" => 1,
51
- "exp_year" => 2022,
52
- "last4" => "4242"
53
- },
54
- }
55
- }]
56
- }
57
- })
58
- end
59
-
60
- describe '#call' do
61
- subject { service.call }
62
-
63
- before do
64
- allow(stripe).to receive(:show_intent) { intent }
65
- allow_any_instance_of(Spree::CreditCard).to receive(:require_card_numbers?) { false }
66
- allow_any_instance_of(Spree::PaymentMethod::StripeCreditCard).to receive(:create_profile) { true }
67
- end
68
-
69
- it { expect(subject).to be true }
70
-
71
- it "creates a new pending payment" do
72
- expect { subject }.to change { order.payments.count }
73
- expect(order.payments.last.reload).to be_pending
74
- end
75
-
76
- it "creates a credit card with the correct information" do
77
- expect { subject }.to change { Spree::CreditCard.count }
78
- card = Spree::CreditCard.last
79
-
80
- aggregate_failures do
81
- expect(card.name).to eq "John Doe"
82
- expect(card.cc_type).to eq "visa"
83
- expect(card.month).to eq "1"
84
- expect(card.year).to eq "2022"
85
- expect(card.last_digits).to eq "4242"
86
- end
87
- end
88
-
89
- context "when for any reason the payment could not be created" do
90
- before { params[:form_data].delete(:city) }
91
-
92
- it "returns false" do
93
- expect(subject).to be false
94
- end
95
- end
96
-
97
- context "when there are previous pending payments" do
98
- let!(:payment) do
99
- create(:payment, order: order, amount: order.total).tap do |payment|
100
- payment.update!(state: :pending)
101
- end
102
- end
103
-
104
- before do
105
- response = double(success?: true, authorization: payment.response_code)
106
- allow_any_instance_of(Spree::PaymentMethod::StripeCreditCard).to receive(:void) { response }
107
- end
108
-
109
- context "when one of them is a Payment Intent" do
110
- before do
111
- payment.update!(payment_method: stripe)
112
- payment.source.update!(payment_method: stripe)
113
- end
114
-
115
- it "invalidates it" do
116
- expect { subject }.to change { payment.reload.state }.to 'void'
117
- end
118
- end
119
-
120
- context "when none is a Payment Intent" do
121
- it "does not invalidate them" do
122
- expect { subject }.not_to change { payment.reload.state }
123
- end
124
- end
125
- end
126
- end
127
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe SolidusStripe::PrepareOrderForPaymentService do
6
- let(:service) { described_class.new(address, controller) }
7
-
8
- let(:address) { create :address }
9
- let(:user) { double("Spree::User") }
10
- let(:email) { "foo@example.com" }
11
- let(:order) { create :order_with_line_items, state: :cart, ship_address: nil, bill_address: nil }
12
- let!(:shipping_method) { create :shipping_method, name: 'Air Mail', cost: 15 }
13
- let!(:other_shipping_method) { create :shipping_method, name: 'DHL', cost: 7 }
14
- let(:controller) do
15
- double(
16
- current_order: order,
17
- spree_current_user: user,
18
- params: {
19
- email: email,
20
- shipping_option: { id: shipping_method.id }
21
- }
22
- )
23
- end
24
-
25
- before do
26
- Spree::ZoneMember.create!(zone: Spree::Zone.first, zoneable: address.country)
27
- end
28
-
29
- describe "#call" do
30
- subject { service.call }
31
-
32
- it "sets the order ship and bill address" do
33
- expect { subject }.to change { order.ship_address }.to(address)
34
- .and change { order.bill_address }.to(address)
35
- end
36
-
37
- context "when the user is not logged in" do
38
- let(:user) { nil }
39
-
40
- it "sets the order email to allow guest checkout" do
41
- expect { subject }.to change { order.email }.to(email)
42
- end
43
- end
44
-
45
- it "sets the right shipping rate" do
46
- expect { subject }.to change {
47
- order.shipments.first.selected_shipping_rate.shipping_method_id
48
- }.to(shipping_method.id)
49
- end
50
-
51
- context "when the order can be advanced to payment state" do
52
- it "advances the order to 'payment'" do
53
- expect { subject }.to change { order.state }.to("payment")
54
- end
55
- end
56
-
57
- context "when the order fails to be advanced to payment state" do
58
- before { order.line_items.destroy_all }
59
-
60
- it "leaves the order to a previous state" do
61
- expect { subject }.not_to change { order.state }
62
- end
63
- end
64
- end
65
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe SolidusStripe::ShippingRatesService do
6
- let(:service) { described_class.new(order, user, params) }
7
-
8
- let(:order) { create :order }
9
- let(:user) { Spree::User.new }
10
-
11
- let(:params) do
12
- {
13
- country: Spree::ZoneMember.first.zoneable.iso,
14
- city: "Metropolis",
15
- postalCode: "12345",
16
- recipient: "",
17
- addressLine: []
18
- }
19
- end
20
-
21
- let(:ups_ground) { create(:shipping_method, cost: 5, available_to_all: false) }
22
- let(:air_mail) { create(:shipping_method, cost: 8, name: "Air Mail", available_to_all: false) }
23
-
24
- let(:fl_warehouse) { create(:stock_location, name: "FL Warehouse", shipping_methods: [ups_ground]) }
25
- let(:ca_warehouse) { create(:stock_location, name: "CA Warehouse", shipping_methods: [air_mail]) }
26
-
27
- before do
28
- 2.times { create :inventory_unit, order: order }
29
- Spree::StockLocation.update_all backorderable_default: false
30
- Spree::StockItem.find_by(stock_location: fl_warehouse, variant: order.variants.first).set_count_on_hand(1)
31
- Spree::StockItem.find_by(stock_location: ca_warehouse, variant: order.variants.last).set_count_on_hand(1)
32
- order.create_proposed_shipments
33
- end
34
-
35
- describe "#call" do
36
- subject { service.call }
37
-
38
- context "when there are no common shipping methods for all order shipments" do
39
- it "cannot find any shipping rate" do
40
- expect(subject).to be_empty
41
- end
42
- end
43
-
44
- context "when there are common shipping methods for all order shipments" do
45
- before { fl_warehouse.shipping_methods << ca_warehouse.shipping_methods.first }
46
-
47
- context "when one shipping method is available for all shipments" do
48
- it "sums the shipping rates for the shared shipping method" do
49
- expect(subject).to eql [{ amount: 1600, id: air_mail.id.to_s, label: air_mail.name }]
50
- end
51
- end
52
- end
53
- end
54
- end
@@ -1,350 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Spree::PaymentMethod::StripeCreditCard do
6
- let(:secret_key) { 'key' }
7
- let(:email) { 'customer@example.com' }
8
- let(:source) { Spree::CreditCard.new }
9
-
10
- let(:bill_address) { nil }
11
-
12
- let(:order) {
13
- double('Spree::Order',
14
- email: email,
15
- bill_address: bill_address,
16
- currency: 'USD',
17
- number: 'NUMBER',
18
- total: 10.99
19
- )
20
- }
21
-
22
- let(:payment) {
23
- double('Spree::Payment',
24
- source: source,
25
- order: order,
26
- amount: order.total
27
- )
28
- }
29
-
30
- let(:gateway) do
31
- double('gateway').tap do |p|
32
- allow(p).to receive(:purchase)
33
- allow(p).to receive(:authorize)
34
- allow(p).to receive(:capture)
35
- end
36
- end
37
-
38
- before do
39
- subject.preferences = { secret_key: secret_key }
40
- allow(subject).to receive(:options_for_purchase_or_auth).and_return(['money', 'cc', 'opts'])
41
- allow(subject).to receive(:gateway).and_return gateway
42
- end
43
-
44
- it 'can enable Stripe.js V3 Elements via preference setting' do
45
- expect do
46
- subject.preferred_v3_elements = true
47
- end.to change { subject.v3_elements? }.from(false).to true
48
- end
49
-
50
- describe '#stripe_config' do
51
- context 'when payment request feature is disabled' do
52
- before { subject.preferences = { publishable_key: 'stripe_key' } }
53
-
54
- it 'includes the basic configuration' do
55
- config = subject.stripe_config(order)
56
- expect(config.keys).to eq %i[id publishable_key]
57
- expect(config[:publishable_key]).to include('stripe_key')
58
- end
59
- end
60
-
61
- context 'when the payment request feature is enabled' do
62
- before do
63
- subject.preferences = {
64
- publishable_key: 'stripe_key',
65
- stripe_country: 'US',
66
- v3_intents: true
67
- }
68
- end
69
-
70
- it 'includes the payment request configuration as well' do
71
- config = subject.stripe_config(order)
72
- expect(config.keys).to eq %i[id publishable_key payment_request]
73
- expect(config[:payment_request][:currency]).to eq 'usd'
74
- expect(config[:payment_request][:amount]).to be 1099
75
- expect(config[:payment_request][:label]).to include 'NUMBER'
76
- end
77
- end
78
- end
79
-
80
- describe '#create_profile' do
81
- before do
82
- allow(payment.source).to receive(:update_attributes!)
83
- end
84
-
85
- context 'with an order that has a bill address' do
86
- let(:bill_address) {
87
- double('Spree::Address',
88
- address1: '123 Happy Road',
89
- address2: 'Apt 303',
90
- city: 'Suzarac',
91
- zipcode: '95671',
92
- state: double('Spree::State', name: 'Oregon'),
93
- country: double('Spree::Country', name: 'United States'))
94
- }
95
-
96
- it 'stores the bill address with the gateway' do
97
- expect(subject.gateway).to receive(:store).with(payment.source, {
98
- email: email,
99
- login: secret_key,
100
-
101
- address: {
102
- address1: '123 Happy Road',
103
- address2: 'Apt 303',
104
- city: 'Suzarac',
105
- zip: '95671',
106
- state: 'Oregon',
107
- country: 'United States'
108
- }
109
- }).and_return double.as_null_object
110
-
111
- subject.create_profile payment
112
- end
113
- end
114
-
115
- context 'with an order that does not have a bill address' do
116
- it 'does not store a bill address with the gateway' do
117
- expect(subject.gateway).to receive(:store).with(payment.source, {
118
- email: email,
119
- login: secret_key,
120
- }).and_return double.as_null_object
121
-
122
- subject.create_profile payment
123
- end
124
-
125
- # Regression test for #141
126
- context "correcting the card type" do
127
- before do
128
- # We don't care about this method for these tests
129
- allow(subject.gateway).to receive(:store).and_return(double.as_null_object)
130
- end
131
-
132
- it "converts 'American Express' to 'american_express'" do
133
- payment.source.cc_type = 'American Express'
134
- subject.create_profile(payment)
135
- expect(payment.source.cc_type).to eq('american_express')
136
- end
137
-
138
- it "converts 'Diners Club' to 'diners_club'" do
139
- payment.source.cc_type = 'Diners Club'
140
- subject.create_profile(payment)
141
- expect(payment.source.cc_type).to eq('diners_club')
142
- end
143
-
144
- it "converts 'Visa' to 'visa'" do
145
- payment.source.cc_type = 'Visa'
146
- subject.create_profile(payment)
147
- expect(payment.source.cc_type).to eq('visa')
148
- end
149
- end
150
- end
151
-
152
- context 'with a card represents payment_profile' do
153
- let(:source) { Spree::CreditCard.new(gateway_payment_profile_id: 'tok_profileid') }
154
- let(:bill_address) { nil }
155
-
156
- it 'stores the profile_id as a card' do
157
- expect(subject.gateway).to receive(:store).with(source.gateway_payment_profile_id, anything).and_return double.as_null_object
158
-
159
- subject.create_profile payment
160
- end
161
- end
162
- end
163
-
164
- context 'purchasing' do
165
- after do
166
- subject.purchase(19.99, 'credit card', {})
167
- end
168
-
169
- it 'send the payment to the gateway' do
170
- expect(gateway).to receive(:purchase).with('money', 'cc', 'opts')
171
- end
172
- end
173
-
174
- context 'purchasing with fractionless currency' do
175
- after do
176
- subject.purchase(1999, 'credit card', { currency: 'JPY' })
177
- end
178
-
179
- it 'send the payment to the gateway' do
180
- expect(gateway).to receive(:purchase).with('money', 'cc', 'opts')
181
- end
182
- end
183
-
184
- context 'authorizing' do
185
- after do
186
- subject.authorize(19.99, 'credit card', {})
187
- end
188
-
189
- it 'send the authorization to the gateway' do
190
- expect(gateway).to receive(:authorize).with('money', 'cc', 'opts')
191
- end
192
- end
193
-
194
- context 'authorizing with fractionless currency' do
195
- after do
196
- subject.authorize(1999, 'credit card', { currency: 'JPY' })
197
- end
198
-
199
- it 'send the authorization to the gateway' do
200
- expect(gateway).to receive(:authorize).with('money', 'cc', 'opts')
201
- end
202
- end
203
-
204
- context 'capturing' do
205
- after do
206
- subject.capture(1234, 'response_code', {})
207
- end
208
-
209
- it 'convert the amount to cents' do
210
- expect(gateway).to receive(:capture).with(1234, anything, anything)
211
- end
212
-
213
- it 'use the response code as the authorization' do
214
- expect(gateway).to receive(:capture).with(anything, 'response_code', anything)
215
- end
216
- end
217
-
218
- context 'capturing with fractionless currency' do
219
- after do
220
- subject.capture(1234, 'response_code', { currency: 'JPY' })
221
- end
222
-
223
- it 'amount not converted to cents' do
224
- expect(gateway).to receive(:capture).with(Spree::Money.new(123_400, { currency: 'JPY' }), anything, anything)
225
- end
226
-
227
- it 'use the response code as the authorization' do
228
- expect(gateway).to receive(:capture).with(anything, 'response_code', anything)
229
- end
230
- end
231
-
232
- context 'capture with payment class' do
233
- let(:payment_method) do
234
- payment_method = described_class.new(active: true)
235
- payment_method.set_preference :secret_key, secret_key
236
- allow(payment_method).to receive(:options_for_purchase_or_auth).and_return(['money', 'cc', 'opts'])
237
- allow(payment_method).to receive(:gateway).and_return gateway
238
- allow(payment_method).to receive_messages source_required: true
239
- payment_method
240
- end
241
-
242
- let!(:store) { FactoryBot.create(:store) }
243
- let(:order) { Spree::Order.create! }
244
-
245
- let(:card) do
246
- FactoryBot.create(
247
- :credit_card,
248
- gateway_customer_profile_id: 'cus_abcde',
249
- imported: false
250
- )
251
- end
252
-
253
- let(:payment) do
254
- payment = Spree::Payment.new
255
- payment.source = card
256
- payment.order = order
257
- payment.payment_method = payment_method
258
- payment.amount = 98.55
259
- payment.state = 'pending'
260
- payment.response_code = '12345'
261
- payment
262
- end
263
-
264
- let!(:success_response) do
265
- double('success_response', success?: true,
266
- authorization: '123',
267
- avs_result: { 'code' => 'avs-code' },
268
- cvv_result: { 'code' => 'cvv-code', 'message' => "CVV Result" })
269
- end
270
-
271
- after do
272
- payment.capture!
273
- end
274
-
275
- it 'gets correct amount' do
276
- expect(gateway).to receive(:capture).with(9855, '12345', anything).and_return(success_response)
277
- end
278
- end
279
-
280
- describe '#try_void' do
281
- let(:payment) { create :payment, amount: order.total }
282
-
283
- shared_examples 'voids the payment transaction' do
284
- it 'voids the payment transaction' do
285
- expect(gateway).to receive(:void)
286
-
287
- subject.try_void(payment)
288
- end
289
- end
290
-
291
- context 'when using Payment Intents' do
292
- before { subject.preferred_v3_intents = true }
293
-
294
- context 'when the payment is completed' do
295
- before do
296
- allow(payment).to receive(:completed?) { true }
297
- end
298
-
299
- it 'creates a refund' do
300
- expect { subject.try_void(payment) }.to change { Spree::Refund.count }.by(1)
301
- end
302
- end
303
-
304
- context 'when the payment is not completed' do
305
- it_behaves_like 'voids the payment transaction'
306
- end
307
- end
308
-
309
- context 'when not using Payment Intents' do
310
- before { subject.preferred_v3_intents = false }
311
-
312
- context 'when the payment is completed' do
313
- it_behaves_like 'voids the payment transaction'
314
- end
315
-
316
- context 'when the payment is not completed' do
317
- it_behaves_like 'voids the payment transaction'
318
- end
319
- end
320
- end
321
-
322
- describe '#options_for_purchase_or_auth' do
323
- let(:card) do
324
- FactoryBot.create(
325
- :credit_card,
326
- gateway_customer_profile_id: 'cus_abcde',
327
- imported: false
328
- )
329
- end
330
-
331
- before do
332
- allow(subject).to receive(:options_for_purchase_or_auth).and_call_original
333
- end
334
-
335
- context 'transaction_options' do
336
- it 'includes basic values and keys' do
337
- options = subject.send(:options_for_purchase_or_auth, 19.99, card, {})
338
- expect(options[0]).to eq(19.99)
339
- expect(options[1]).to eq(card)
340
- expect(options[2].keys).to eq([:description, :currency, :customer])
341
- end
342
-
343
- it 'includes statement_descriptor_suffix within options' do
344
- transaction_options = { statement_descriptor_suffix: 'FFFFFFF' }
345
- options = subject.send(:options_for_purchase_or_auth, 19.99, card, transaction_options)
346
- expect(options.last[:statement_descriptor_suffix]).to eq('FFFFFFF')
347
- end
348
- end
349
- end
350
- end