-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
refund_spec.rb
240 lines (191 loc) · 7.39 KB
/
refund_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Spree::Refund, type: :model do
let(:amount) { 100.0 }
let(:amount_in_cents) { amount * 100 }
let(:payment) { create(:payment, amount: payment_amount, payment_method: payment_method) }
let(:payment_amount) { amount * 2 }
let(:payment_method) { create(:credit_card_payment_method) }
let(:refund_reason) { create(:refund_reason) }
let(:transaction_id) { nil }
let(:refund) do
create(
:refund,
payment: payment,
amount: amount,
reason: refund_reason,
transaction_id: transaction_id
)
end
describe 'create' do
subject { refund }
it "creates a refund record" do
expect{ subject }.to change { Spree::Refund.count }.by(1)
end
it "saves the amount" do
expect(subject.reload.amount).to eq amount
expect(subject.money).to be_a(Spree::Money)
end
it "does not attempt to process a transaction" do
expect(subject.transaction_id).to be_nil
end
context "with a european price format" do
let(:amount) { "100,00" }
let(:payment_amount) { 200.0 }
before do
expect(I18n).to receive(:t).with(:'number.currency.format.separator') do
","
end
end
it "creates a refund record" do
expect { subject }.to change { Spree::Refund.count }.by(1)
end
end
end
describe "#perform!" do
subject { refund.perform! }
it "sets #perform_response with the gateway response from the payment provider" do
expect { subject }.to change { refund.perform_response }.from(nil)
expect(refund.perform_response).to be_a(ActiveMerchant::Billing::Response)
expect(refund.perform_response.message).to include(Spree::PaymentMethod::BogusCreditCard::SUCCESS_MESSAGE)
end
it "sets a transaction_id" do
expect { subject }.to change { refund.transaction_id }.from(nil)
end
it "adds a Spree::LogEntry" do
expect { subject }.to change(Spree::LogEntry, :count)
end
context "when transaction_id exists" do
let(:transaction_id) { "12kfjas0" }
it "maintains the transaction id" do
expect { subject }.not_to change { refund.transaction_id }
end
it "does not attempt to process a transaction" do
expect(payment.payment_method).not_to receive(:credit)
subject
end
end
context "when transaction_id is nil" do
let(:transaction_id) { nil }
context "processing is successful" do
it 'creates a refund' do
expect{ subject }.to change(Spree::Refund, :count).by(1)
end
it 'saves the returned authorization value' do
expect { subject }.to change { refund.reload.transaction_id }.from(nil).to(Spree::PaymentMethod::BogusCreditCard::AUTHORIZATION_CODE)
end
it 'saves the passed amount as the refund amount' do
subject
expect(refund.reload.amount).to eq amount
end
it 'creates a log entry' do
subject
expect(refund.reload.log_entries).to be_present
end
it "attempts to process a transaction" do
expect(payment.payment_method).to receive(:credit).once.and_call_original
subject
end
it 'should update the payment total' do
expect(payment.order).to receive(:recalculate)
subject
end
end
context "when processing fails" do
let(:failure_message) { Spree::PaymentMethod::BogusCreditCard::FAILURE_MESSAGE }
let(:gateway_response) {
ActiveMerchant::Billing::Response.new(
false,
failure_message,
{},
test: true,
authorization: Spree::PaymentMethod::BogusCreditCard::AUTHORIZATION_CODE
)
}
before do
allow(payment.payment_method)
.to receive(:credit)
.with(amount_in_cents, payment.source, payment.transaction_id, { originator: an_instance_of(Spree::Refund) })
.and_return(gateway_response)
end
context 'without performing after create' do
it 'raises a GatewayError' do
expect { subject }.to raise_error(Spree::Core::GatewayError, failure_message)
end
end
end
context 'without payment profiles supported' do
before do
allow(payment.payment_method).to receive(:payment_profiles_supported?) { false }
end
it 'should not supply the payment source' do
expect(payment.payment_method)
.to receive(:credit)
.with(amount * 100, payment.transaction_id, { originator: an_instance_of(Spree::Refund) })
.and_call_original
subject
end
end
context 'with payment profiles supported' do
before do
allow(payment.payment_method).to receive(:payment_profiles_supported?) { true }
end
it 'should supply the payment source' do
expect(payment.payment_method)
.to receive(:credit)
.with(amount_in_cents, payment.source, payment.transaction_id, { originator: an_instance_of(Spree::Refund) })
.and_call_original
subject
end
end
context 'with an activemerchant gateway connection error' do
before do
expect(payment.payment_method)
.to receive(:credit)
.with(amount_in_cents, payment.source, payment.transaction_id, { originator: an_instance_of(Spree::Refund) })
.and_raise(ActiveMerchant::ConnectionError.new("foo", nil))
end
it 'raises Spree::Core::GatewayError' do
expect { subject }.to raise_error(Spree::Core::GatewayError, I18n.t('spree.unable_to_connect_to_gateway'))
end
end
context 'with amount too large' do
let(:payment_amount) { 10 }
let(:amount) { payment_amount * 2 }
it 'is invalid' do
expect { subject }.to raise_error { |error|
expect(error).to be_a(ActiveRecord::RecordInvalid)
expect(error.record.errors.full_messages).to eq ["Amount #{I18n.t('activerecord.errors.models.spree/refund.attributes.amount.greater_than_allowed')}"]
}
end
end
end
context 'when payment is not present' do
let(:refund) { build(:refund, payment: nil) }
it 'returns a validation error' do
expect { refund.save! }.to raise_error 'Validation failed: Payment can\'t be blank'
end
end
end
describe 'total_amount_reimbursed_for' do
let(:customer_return) { reimbursement.customer_return }
let(:reimbursement) { create(:reimbursement) }
let!(:default_refund_reason) { Spree::RefundReason.find_or_create_by!(name: Spree::RefundReason::RETURN_PROCESSING_REASON, mutable: false) }
let(:created_by_user) { create(:user, email: '[email protected]') }
subject { Spree::Refund.total_amount_reimbursed_for(reimbursement) }
context 'with reimbursements performed' do
before { reimbursement.perform!(created_by: created_by_user) }
it 'returns the total amount' do
amount = Spree::Refund.total_amount_reimbursed_for(reimbursement)
expect(amount).to be > 0
expect(amount).to eq reimbursement.total
end
end
context 'without reimbursements performed' do
it 'returns zero' do
amount = Spree::Refund.total_amount_reimbursed_for(reimbursement)
expect(amount).to eq 0
end
end
end
end