spree_affirm 0.2.3 → 0.2.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +5 -5
  2. metadata +26 -75
  3. data/.gitignore +0 -15
  4. data/Gemfile +0 -19
  5. data/LICENSE +0 -29
  6. data/README.md +0 -51
  7. data/Rakefile +0 -15
  8. data/Versionfile +0 -5
  9. data/app/assets/javascripts/spree/backend/spree_affirm.js +0 -3
  10. data/app/assets/javascripts/spree/frontend/spree_affirm.js +0 -2
  11. data/app/assets/stylesheets/spree/backend/spree_affirm.css +0 -4
  12. data/app/assets/stylesheets/spree/frontend/spree_affirm.css +0 -4
  13. data/app/controllers/spree/affirm_controller.rb +0 -68
  14. data/app/models/spree/affirm_checkout.rb +0 -38
  15. data/app/models/spree/gateway/affirm.rb +0 -51
  16. data/app/views/spree/admin/log_entries/_affirm.html.erb +0 -4
  17. data/app/views/spree/admin/log_entries/index.html.erb +0 -28
  18. data/app/views/spree/admin/payments/source_forms/_affirm.html.erb +0 -19
  19. data/app/views/spree/admin/payments/source_views/_affirm.html.erb +0 -12
  20. data/app/views/spree/checkout/payment/_affirm.html.erb +0 -195
  21. data/bin/rails +0 -7
  22. data/config/locales/en.yml +0 -29
  23. data/config/routes.rb +0 -5
  24. data/db/migrate/20140514194315_create_affirm_checkout.rb +0 -10
  25. data/lib/active_merchant/billing/affirm.rb +0 -161
  26. data/lib/generators/spree_affirm/install/install_generator.rb +0 -21
  27. data/lib/spree_affirm.rb +0 -2
  28. data/lib/spree_affirm/engine.rb +0 -27
  29. data/lib/spree_affirm/factories.rb +0 -5
  30. data/lib/spree_affirm/factories/affirm_checkout_factory.rb +0 -211
  31. data/lib/spree_affirm/factories/affirm_payment_method_factory.rb +0 -8
  32. data/lib/spree_affirm/factories/payment_factory.rb +0 -10
  33. data/lib/spree_affirm/version.rb +0 -3
  34. data/spec/controllers/affirm_controller_spec.rb +0 -138
  35. data/spec/lib/active_merchant/billing/affirm_spec.rb +0 -294
  36. data/spec/models/affirm_address_validator_spec.rb +0 -13
  37. data/spec/models/spree_affirm_checkout_spec.rb +0 -328
  38. data/spec/models/spree_gateway_affirm_spec.rb +0 -134
  39. data/spec/spec_helper.rb +0 -98
  40. data/spree_affirm.gemspec +0 -30
@@ -1,21 +0,0 @@
1
- module SpreeAffirm
2
- module Generators
3
- class InstallGenerator < Rails::Generators::Base
4
-
5
- class_option :auto_run_migrations, :type => :boolean, :default => false
6
-
7
- def add_migrations
8
- run 'bundle exec rake railties:install:migrations FROM=spree_affirm'
9
- end
10
-
11
- def run_migrations
12
- run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
13
- if run_migrations
14
- run 'bundle exec rake db:migrate'
15
- else
16
- puts 'Skipping rake db:migrate, don\'t forget to run it!'
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,2 +0,0 @@
1
- require 'spree_core'
2
- require 'spree_affirm/engine'
@@ -1,27 +0,0 @@
1
- module SpreeAffirm
2
- class Engine < Rails::Engine
3
- require 'spree/core'
4
- isolate_namespace Spree
5
- engine_name 'spree_affirm'
6
-
7
-
8
- # use rspec for tests
9
- config.generators do |g|
10
- g.test_framework :rspec
11
- end
12
-
13
- config.autoload_paths += %W(#{config.root}/lib)
14
-
15
- def self.activate
16
- Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
17
- Rails.configuration.cache_classes ? require(c) : load(c)
18
- end
19
- end
20
-
21
- config.to_prepare &method(:activate).to_proc
22
-
23
- initializer "spree.spree_affirm.payment_methods", :after => "spree.register.payment_methods" do |app|
24
- app.config.spree.payment_methods << Spree::Gateway::Affirm
25
- end
26
- end
27
- end
@@ -1,5 +0,0 @@
1
- require 'factory_girl'
2
-
3
- Dir["#{File.dirname(__FILE__)}/factories/**"].each do |f|
4
- require File.expand_path(f)
5
- end
@@ -1,211 +0,0 @@
1
- def BASE_CHECKOUT_DETAILS
2
- {
3
- "merchant"=> {
4
- "public_api_key"=> "PPPPPPPPPPPPPPP",
5
- "user_cancel_url"=> "http=>//google.com/cancel",
6
- "user_confirmation_url"=> "http=>//google.com/confirm",
7
- "name"=> "Test Merchant"
8
- },
9
- "tax_amount"=> 0,
10
- "billing"=> {
11
- "address"=> {
12
- "city"=> "San Francisco",
13
- "street1"=> "12345 Main",
14
- "street2"=> "300",
15
- "region1_code"=> "AL",
16
- "postal_code"=> "55555",
17
- "country_code"=> "USA",
18
- "for_billing"=> true,
19
- "validation_source"=> 3
20
- },
21
- "email"=> "test@affirm.com",
22
- "name"=> {
23
- "for_billing"=> true,
24
- "last"=> "Doe",
25
- "first"=> "John"
26
- }
27
- },
28
- "items"=> {
29
- "xxx-xx-xxx-x"=> {
30
- "sku"=> "xxx-xx-xxx-x",
31
- "item_url"=> "http=>//google.com/products/the-blue-hat",
32
- "display_name"=> "The Blue Hat",
33
- "unit_price"=> 85000,
34
- "qty"=> 1,
35
- "item_type"=> "physical",
36
- "item_image_url"=> "http=>//google.com/products/6/large/the-blue-hat"
37
- }
38
- },
39
- "shipping"=> {
40
- "name"=> {
41
- "last"=> "Doe",
42
- "first"=> "John"
43
- },
44
- "address"=> {
45
- "city"=> "San Francisco",
46
- "street1"=> "12345 Main Street",
47
- "street2"=> "300",
48
- "region1_code"=> "AL",
49
- "postal_code"=> "94110",
50
- "country_code"=> "USA",
51
- "validation_source"=> 3
52
- }
53
- },
54
- "checkout_id"=> "S123123-456",
55
- "currency"=> "USD",
56
- "meta"=> {
57
- "release"=> "true",
58
- "user_timezone"=> "America/Los_Angeles",
59
- "__affirm_tracking_uuid"=> "97570a41-cd07-4f52-8869-46c6d2588407"
60
- },
61
- "discount_code"=> "",
62
- "misc_fee_amount"=> 0,
63
- "shipping_type"=> "Free National UPS",
64
- "config"=> {
65
- "required_billing_fields"=> [
66
- "name",
67
- "address",
68
- "email"
69
- ]
70
- },
71
- "api_version"=> "v2",
72
- "shipping_amount"=> 0
73
- }
74
- end
75
-
76
- FactoryGirl.define do
77
- factory :affirm_checkout, class: Spree::AffirmCheckout do
78
- token "12345678910"
79
- association(:payment_method, factory: :affirm_payment_method)
80
- association(:order, factory: :order_with_line_items)
81
-
82
-
83
- transient do
84
- stub_details true
85
- shipping_address_mismatch false
86
- billing_address_mismatch false
87
- alternate_billing_address_format false
88
- billing_address_full_name false
89
- billing_email_mismatch false
90
- extra_line_item false
91
- missing_line_item false
92
- quantity_mismatch false
93
- price_mismatch false
94
- full_name_case_mismatch false
95
- end
96
-
97
- after(:build) do |checkout, evaluator|
98
-
99
- _details = BASE_CHECKOUT_DETAILS()
100
-
101
- # case mismatch
102
- unless evaluator.full_name_case_mismatch
103
- _details['billing']['name'] = {
104
- "full" => checkout.order.bill_address.firstname.upcase + " " +
105
- checkout.order.bill_address.lastname.upcase
106
- }
107
- end
108
-
109
- # shipping address
110
- unless evaluator.shipping_address_mismatch
111
- _details['shipping'] = {
112
- "name" => {
113
- "first" => checkout.order.ship_address.firstname,
114
- "last" => checkout.order.ship_address.lastname
115
- },
116
- "address"=> {
117
- "city"=> checkout.order.ship_address.city,
118
- "street1"=> checkout.order.ship_address.address1,
119
- "street2"=> checkout.order.ship_address.address2,
120
- "region1_code"=> checkout.order.ship_address.state.abbr,
121
- "postal_code"=> checkout.order.ship_address.zipcode,
122
- "country_code"=> checkout.order.ship_address.country.iso3
123
- }
124
- }
125
- end
126
-
127
- # billing address
128
- unless evaluator.billing_address_mismatch
129
- _details["billing"] = {
130
- "email" => "joe@schmoe.com",
131
- "name" => {
132
- "first" => checkout.order.bill_address.firstname,
133
- "last" => checkout.order.bill_address.lastname
134
- },
135
- "address"=> {
136
- "city"=> checkout.order.bill_address.city,
137
- "street1"=> checkout.order.bill_address.address1,
138
- "street2"=> checkout.order.bill_address.address2,
139
- "region1_code"=> checkout.order.bill_address.state.abbr,
140
- "postal_code"=> checkout.order.bill_address.zipcode,
141
- "country_code"=> checkout.order.bill_address.country.iso3
142
- }
143
- }
144
- end
145
-
146
- # use alternate format for billing address
147
- if evaluator.alternate_billing_address_format
148
- _details['billing']["address"] = {
149
- "city" => _details['billing']["address"]["city"],
150
- "line1"=> _details['billing']["address"]["street1"],
151
- "line2"=> _details['billing']["address"]["street2"],
152
- "state"=> _details['billing']["address"]["postal_code"],
153
- "zipcode"=> _details['billing']["address"]["region1_code"],
154
- "country"=> _details['billing']["address"]["country_code"]
155
- }
156
- end
157
-
158
- # use name.full instead of first/last
159
- if evaluator.billing_address_full_name
160
- _details['billing']['name'] = {
161
- 'full' => "#{_details['billing']['name']['first']} #{_details['billing']['name']['last']}"
162
- }
163
- end
164
-
165
-
166
- # billing email
167
- unless evaluator.billing_email_mismatch
168
- _details["billing"]["email"] = checkout.order.email
169
- end
170
-
171
- # setup items in cart
172
- _details['items'] = {}
173
- checkout.order.line_items.each do |item|
174
- _details['items'][item.variant.sku] = {
175
- "qty" => item.quantity.to_s,
176
- "unit_price" => (item.price*100).to_s,
177
- "display_name" => item.product.name
178
- }
179
- end
180
-
181
- if evaluator.extra_line_item
182
- _details['items']['extra-1-2-3'] = {
183
- "qty" => "1",
184
- "unit_price" => "12300",
185
- "display_name" => "Really cool hat"
186
- }
187
- end
188
-
189
- if evaluator.missing_line_item
190
- _details['items'].delete _details['items'].keys.last
191
- end
192
-
193
- if evaluator.quantity_mismatch
194
- _last_item = _details['items'][_details['items'].keys.last]
195
- _details['items'][_details['items'].keys.last]['qty'] = (_last_item['qty'].to_i + 1).to_s
196
- end
197
-
198
- if evaluator.price_mismatch
199
- _last_item = _details['items'][_details['items'].keys.last]
200
- _details['items'][_details['items'].keys.last]['unit_price'] = 456456
201
- end
202
-
203
- if evaluator.stub_details
204
- checkout.stub(details: _details)
205
- end
206
- end
207
-
208
- end
209
- end
210
-
211
-
@@ -1,8 +0,0 @@
1
- FactoryGirl.define do
2
- factory :affirm_payment_method, class: Spree::Gateway::Affirm do
3
- name "Staging Affirm Split Pay"
4
- active true
5
- environment "test"
6
- auto_capture false
7
- end
8
- end
@@ -1,10 +0,0 @@
1
- FactoryGirl.define do
2
- factory :affirm_payment, class: Spree::Payment do
3
- amount 45.75
4
- association(:payment_method, factory: :affirm_payment_method)
5
- association(:source, factory: :affirm_checkout)
6
- order
7
- state 'checkout'
8
- response_code '12345'
9
- end
10
- end
@@ -1,3 +0,0 @@
1
- module SpreeAffirm
2
- VERSION = "0.2.3"
3
- end
@@ -1,138 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::AffirmController do
4
- let(:user) { FactoryGirl.create(:user) }
5
- let(:checkout) { FactoryGirl.build(:affirm_checkout) }
6
- let(:bad_billing_checkout) { FactoryGirl.build(:affirm_checkout, billing_address_mismatch: true) }
7
- let(:bad_shipping_checkout) { FactoryGirl.build(:affirm_checkout, shipping_address_mismatch: true) }
8
- let(:bad_email_checkout) { FactoryGirl.build(:affirm_checkout, billing_email_mismatch: true) }
9
-
10
-
11
- describe "POST confirm" do
12
-
13
- def post_request(token, payment_id)
14
- post :confirm, checkout_token: token, payment_method_id: payment_id, use_route: 'spree'
15
- end
16
-
17
- before do
18
- controller.stub authenticate_spree_user!: true
19
- controller.stub spree_current_user: user
20
- end
21
-
22
- context "when the checkout matches the order" do
23
- before do
24
- Spree::AffirmCheckout.stub new: checkout
25
- controller.stub current_order: checkout.order
26
- end
27
-
28
- context "when no checkout_token is provided" do
29
- it "redirects to the current order state" do
30
- post_request(nil, nil)
31
- expect(response).to redirect_to(controller.checkout_state_path(checkout.order.state))
32
- end
33
- end
34
-
35
- context "when the order is complete" do
36
- before do
37
- checkout.order.state = 'complete'
38
- end
39
- it "redirects to the current order state" do
40
- post_request '123456789', checkout.payment_method.id
41
- expect(response).to redirect_to(controller.order_path(checkout.order))
42
- end
43
- end
44
-
45
- context "when the order state is payment" do
46
- before do
47
- checkout.order.state = 'payment'
48
- end
49
-
50
- it "creates a new payment" do
51
- post_request "123423423", checkout.payment_method.id
52
-
53
- expect(checkout.order.payments.first.source).to eq(checkout)
54
- end
55
-
56
- # it "transitions to complete if confirmation is not required" do
57
- # checkout.order.stub confirmation_required?: false
58
- # post_request "123423423", checkout.payment_method.id
59
-
60
- # expect(checkout.order.state).to eq("complete")
61
- # end
62
-
63
- it "transitions to confirm if confirmation is required" do
64
- checkout.order.stub confirmation_required?: true
65
- post_request "123423423", checkout.payment_method.id
66
-
67
- expect(checkout.order.reload.state).to eq("confirm")
68
- end
69
- end
70
-
71
- end
72
-
73
- context "when the billing_address does not match the order" do
74
- before do
75
- Spree::AffirmCheckout.stub new: bad_billing_checkout
76
- state = FactoryGirl.create(:state, abbr: bad_billing_checkout.details['billing']['address']['region1_code'])
77
- Spree::State.stub find_by_abbr: state, find_by_name: state
78
- controller.stub current_order: bad_billing_checkout.order
79
- end
80
-
81
- it "creates a new address record for the order" do
82
- _old_billing_address = bad_billing_checkout.order.bill_address
83
- post_request '12345789', bad_billing_checkout.payment_method.id
84
-
85
- expect(bad_billing_checkout.order.bill_address).not_to be(_old_billing_address)
86
- expect(bad_billing_checkout.valid?).to be(true)
87
- end
88
- end
89
-
90
-
91
- context "when the shipping_address does not match the order" do
92
- before do
93
- Spree::AffirmCheckout.stub new: bad_shipping_checkout
94
- state = FactoryGirl.create(:state, abbr: bad_shipping_checkout.details['shipping']['address']['region1_code'])
95
- Spree::State.stub find_by_abbr: state, find_by_name: state
96
- controller.stub current_order: bad_shipping_checkout.order
97
- end
98
-
99
- it "creates a new address record for the order" do
100
- _old_shipping_address = bad_shipping_checkout.order.ship_address
101
- post_request '12345789', bad_shipping_checkout.payment_method.id
102
-
103
- expect(bad_shipping_checkout.order.ship_address).not_to be(_old_shipping_address)
104
- expect(bad_shipping_checkout.valid?).to be(true)
105
- end
106
- end
107
-
108
-
109
-
110
- context "when the billing_email does not match the order" do
111
- before do
112
- Spree::AffirmCheckout.stub new: bad_email_checkout
113
- controller.stub current_order: bad_email_checkout.order
114
- end
115
-
116
- it "updates the billing_email on the order" do
117
- _old_email = bad_email_checkout.order.email
118
- post_request '12345789', bad_email_checkout.payment_method.id
119
-
120
- expect(bad_email_checkout.order.email).not_to be(_old_email)
121
- expect(bad_email_checkout.valid?).to be(true)
122
- end
123
- end
124
-
125
-
126
- context "there is no current order" do
127
- before(:each) do
128
- controller.stub current_order: nil
129
- end
130
-
131
- it "raises an ActiveRecord::RecordNotFound error" do
132
- expect do
133
- post_request nil, nil
134
- end.to raise_error(ActiveRecord::RecordNotFound)
135
- end
136
- end
137
- end
138
- end
@@ -1,294 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe ActiveMerchant::Billing::Affirm do
5
- let(:affirm_payment) { FactoryGirl.create(:affirm_payment) }
6
- let(:charge_id) { "1234-5678-9012" }
7
-
8
-
9
- def expect_request(method, url_regexp, data, successful_response=true, response={})
10
- affirm_payment.payment_method.provider.should_receive(:ssl_request) { |_method, _url, _data, _headers|
11
-
12
- # check method type
13
- _method.should be(method)
14
-
15
- # test path
16
- _url.should match(url_regexp) unless url_regexp.nil?
17
-
18
- # test data
19
- _data.should eq(data.to_json) unless data.nil?
20
-
21
- # create response
22
- _response = (response || {}).reverse_merge({
23
- id: charge_id,
24
- pending: true,
25
- amount: 8000
26
- })
27
-
28
- _response[:status_code] = 400 unless successful_response
29
-
30
- _response.to_json
31
- }.at_least(1).times
32
- end
33
-
34
-
35
- def expect_request_and_return_success(method, url_regexp, data, response=nil )
36
- expect_request(method, url_regexp, data, true, response)
37
- end
38
-
39
-
40
- def expect_request_and_return_failure(method, url_regexp, data, response=nil )
41
- expect_request(method, url_regexp, data, false, response)
42
- end
43
-
44
-
45
- def expect_post_and_return_success(url_regexp=nil, data=nil, response=nil )
46
- expect_request_and_return_success(:post, url_regexp, data, response)
47
- end
48
-
49
-
50
- def expect_post_and_return_failure(url_regexp=nil, data=nil, response=nil )
51
- expect_request_and_return_failure(:post, url_regexp, data, response)
52
- end
53
-
54
-
55
- def expect_get_and_return_success(url_regexp=nil, data=nil, response=nil )
56
- expect_request_and_return_success(:get, url_regexp, data, response)
57
- end
58
-
59
-
60
- def expect_get_and_return_failure(url_regexp=nil, data=nil, response=nil )
61
- expect_request_and_return_failure(:get, url_regexp, data, response)
62
- end
63
-
64
-
65
-
66
- describe "#authorize" do
67
- it "sends a POST to /api/v2/charges/ with the checkout_token" do
68
- expect_post_and_return_success(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token}, {amount: 8000})
69
- affirm_payment.payment_method.provider.authorize(8000, affirm_payment.source)
70
- end
71
-
72
-
73
- context "when the POST response is not successful" do
74
- it "returns the result from the POST" do
75
- expect_post_and_return_failure nil, nil, {message: "Whoops!"}
76
- result = affirm_payment.payment_method.provider.authorize(8000, affirm_payment.source)
77
- expect(result.success?).to be(false)
78
- expect(result.message).to eq("Whoops!")
79
- end
80
- end
81
-
82
-
83
- context "when the POST respone is successful" do
84
- context "when the auth amount does not match the given amount" do
85
- it "returns an error response for that error" do
86
- expect_post_and_return_success(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token}, {amount: 9000})
87
- result = affirm_payment.payment_method.provider.authorize(1999, affirm_payment.source)
88
- expect(result.success?).to be(false)
89
- expect(result.message).to eq("Auth amount does not match charge amount")
90
- end
91
- end
92
-
93
-
94
- context "when the charge is not pending in the response" do
95
- it "returns an error response for that error" do
96
- expect_post_and_return_success(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token}, {amount: 9000, pending: false})
97
- result = affirm_payment.payment_method.provider.authorize(9000, affirm_payment.source)
98
- expect(result.success?).to be(false)
99
- expect(result.message).to eq("There was an error authorizing this Charge")
100
- end
101
- end
102
-
103
-
104
- it "returns the result from the POST" do
105
- expect_post_and_return_success(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token}, {amount: 9000})
106
- result = affirm_payment.payment_method.provider.authorize(9000, affirm_payment.source)
107
- expect(result.success?).to be(true)
108
- expect(result.params['amount']).to eq(9000)
109
- end
110
- end
111
- end
112
-
113
-
114
-
115
- describe "#purchase" do
116
- it "authorizes the charge" do
117
- expect_post_and_return_failure(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token})
118
- affirm_payment.payment_method.provider.purchase(8000, affirm_payment.source)
119
- end
120
-
121
-
122
- context "when the authorize response is not successful" do
123
- it "returns the response of the auth response" do
124
- expect_post_and_return_failure(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token})
125
- purchase_result = affirm_payment.payment_method.provider.purchase(8000, affirm_payment.source)
126
- auth_result = affirm_payment.payment_method.provider.authorize(8000, affirm_payment.source)
127
-
128
- [:success?, :params, :message].each do |method|
129
- expect(purchase_result.send(method)).to eq(auth_result.send(method))
130
- end
131
- end
132
-
133
-
134
- it "does not attempt to capture the charge" do
135
- expect_post_and_return_failure(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token})
136
- affirm_payment.payment_method.provider.should_not_receive(:capture)
137
- affirm_payment.payment_method.provider.purchase(8000, affirm_payment.source)
138
- end
139
- end
140
-
141
-
142
- context "when the authorize response is successful" do
143
- it "captures the charge" do
144
- expect_post_and_return_success(/\/api\/v2\/charges/, {checkout_token: affirm_payment.source.token})
145
- affirm_payment.payment_method.provider.should_receive(:capture)
146
- affirm_payment.payment_method.provider.purchase(8000, affirm_payment.source)
147
- end
148
- end
149
- end
150
-
151
-
152
-
153
-
154
- describe "#capture" do
155
- it "calls a POST to charges/[charge_ari]/capture" do
156
- expect_post_and_return_success(/\/api\/v2\/charges\/#{charge_id}\/capture/, {amount: "8000"})
157
- affirm_payment.payment_method.provider.capture(8000, charge_id)
158
- end
159
-
160
- context "when the capture response is not successful" do
161
- it "returns the response" do
162
- expect_post_and_return_failure(/\/api\/v2\/charges\/#{charge_id}\/capture/, {amount: "8000"},
163
- message: "buuuuuusted"
164
- )
165
- result = affirm_payment.payment_method.provider.capture(8000, charge_id)
166
- expect(result.success?).to be(false)
167
- expect(result.message).to eq("buuuuuusted")
168
- end
169
- end
170
-
171
-
172
- context "when the capture response is successful" do
173
- it "returns the response" do
174
- expect_post_and_return_success(/\/api\/v2\/charges\/#{charge_id}\/capture/, {amount: "8000"},
175
- amount: 8000
176
- )
177
- result = affirm_payment.payment_method.provider.capture(8000, charge_id)
178
- expect(result.success?).to be(true)
179
- expect(result.params['amount']).to eq(8000)
180
- end
181
-
182
-
183
- context "when the captured amount does not equal the requested amount" do
184
- it "returns a failed response" do
185
- expect_post_and_return_success(/\/api\/v2\/charges\/#{charge_id}\/capture/, {amount: "8000"},
186
- amount: 299
187
- )
188
- result = affirm_payment.payment_method.provider.capture(8000, charge_id)
189
- expect(result.success?).to be(false)
190
- expect(result.message).to eq("Capture amount does not match charge amount")
191
- end
192
- end
193
- end
194
- end
195
-
196
-
197
-
198
- describe "#void" do
199
- it "calls POST on /charges/[charge_ari]/void" do
200
- expect_post_and_return_success(/\/api\/v2\/charges\/#{charge_id}\/void/)
201
- affirm_payment.payment_method.provider.void(charge_id)
202
- end
203
-
204
-
205
- it "returns the response from the POST request" do
206
- expect_post_and_return_success(/\/api\/v2\/charges\/#{charge_id}\/void/)
207
- result = affirm_payment.payment_method.provider.void(charge_id)
208
- expect(result.success?).to be(true)
209
- expect(result.message).to eq("Transaction approved")
210
- end
211
- end
212
-
213
-
214
-
215
- describe "#refund" do
216
- it "calls POST on /charges/[charge_ari]/refund" do
217
- expect_post_and_return_success(/\/api\/v2\/charges\/#{charge_id}\/refund/, {amount: "8000"})
218
- affirm_payment.payment_method.provider.refund(8000, charge_id)
219
- end
220
-
221
-
222
- it "returns the response from the POST request" do
223
- expect_post_and_return_success(/\/api\/v2\/charges\/#{charge_id}\/refund/, {amount: "8000"})
224
- result = affirm_payment.payment_method.provider.refund(8000, charge_id)
225
- expect(result.success?).to be(true)
226
- expect(result.message).to eq("Transaction approved")
227
- end
228
- end
229
-
230
-
231
-
232
- describe "#credit" do
233
- context "when the requested amount is non zero" do
234
- it "calls refund with the requested amount" do
235
- affirm_payment.payment_method.provider.should_receive(:refund).with(8000, charge_id, {})
236
- affirm_payment.payment_method.provider.credit(8000, charge_id)
237
- end
238
- end
239
-
240
- context "when the requested amount is zero" do
241
- it "returns a valid response and does not call refund()" do
242
- affirm_payment.payment_method.provider.should_not_receive(:refund)
243
- result = affirm_payment.payment_method.provider.credit(0, charge_id)
244
- expect(result.success?).to be(true)
245
- end
246
- end
247
- end
248
-
249
-
250
-
251
- describe "#get_checkout" do
252
- it "makes a GET request to /checkout/[checkout_token]" do
253
- expect_get_and_return_success(/\/api\/v2\/checkout\/#{affirm_payment.source.token}/)
254
- affirm_payment.payment_method.provider.get_checkout(affirm_payment.source.token)
255
- end
256
- end
257
-
258
-
259
- describe "#commit" do
260
- context "when a ResponseError is returned" do
261
- it "returns an invalid response error" do
262
- http_response = double()
263
- http_response.stub(:code).and_return('400')
264
- http_response.stub(:body).and_return("{}")
265
- affirm_payment.payment_method.provider.should_receive(:ssl_request).and_raise ActiveMerchant::ResponseError.new(http_response)
266
- result = affirm_payment.payment_method.provider.commit(:post, "test")
267
- expect(result.success?).to be(false)
268
- end
269
-
270
- context "when the error response has malformed json" do
271
- it "returns an invalid response error" do
272
- http_response = double()
273
- http_response.stub(:code).and_return('400')
274
- http_response.stub(:body).and_return("{///xdf2fas!!+")
275
- affirm_payment.payment_method.provider.should_receive(:parse).and_raise JSON::ParserError
276
- affirm_payment.payment_method.provider.should_receive(:ssl_request).and_raise ActiveMerchant::ResponseError.new(http_response)
277
- result = affirm_payment.payment_method.provider.commit(:post, "test")
278
- expect(result.success?).to be(false)
279
- expect(result.params['error']['message']).to match(/The raw response returned by the API was/)
280
- end
281
- end
282
- end
283
-
284
-
285
- context "when malformed JSON is returned" do
286
- it "returns an invalid response error" do
287
- affirm_payment.payment_method.provider.should_receive(:ssl_request).and_raise JSON::ParserError
288
- result = affirm_payment.payment_method.provider.commit(:post, "test")
289
- expect(result.success?).to be(false)
290
- expect(result.params['error']['message']).to match(/The raw response returned by the API was/)
291
- end
292
- end
293
- end
294
- end