forked from saleor/saleor
/
test_payment.py
459 lines (355 loc) · 14.3 KB
/
test_payment.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
from decimal import Decimal
from unittest.mock import Mock, patch
import pytest
from saleor.checkout.calculations import checkout_total
from saleor.payment import (
ChargeStatus,
GatewayError,
PaymentError,
TransactionKind,
gateway,
)
from saleor.payment.error_codes import PaymentErrorCode
from saleor.payment.interface import CreditCardInfo, GatewayConfig, GatewayResponse
from saleor.payment.models import Payment
from saleor.payment.utils import (
ALLOWED_GATEWAY_KINDS,
clean_authorize,
clean_capture,
create_payment,
create_payment_information,
create_transaction,
validate_gateway_response,
)
NOT_ACTIVE_PAYMENT_ERROR = "This payment is no longer active."
EXAMPLE_ERROR = "Example dummy error"
@pytest.fixture
def card_details():
return CreditCardInfo(
last_4="1234", exp_year=2020, exp_month=8, brand="visa", name_on_card="Joe Doe"
)
@pytest.fixture
def gateway_response(settings, card_details):
return GatewayResponse(
is_success=True,
action_required=False,
transaction_id="transaction-token",
amount=Decimal(14.50),
currency=settings.DEFAULT_CURRENCY,
kind=TransactionKind.CAPTURE,
error=None,
raw_response={
"credit_card_four": "1234",
"transaction-id": "transaction-token",
},
card_info=card_details,
)
@pytest.fixture
def transaction_data(payment_dummy, gateway_response):
return {
"payment": payment_dummy,
"payment_information": create_payment_information(
payment_dummy, "payment-token"
),
"kind": TransactionKind.CAPTURE,
"gateway_response": gateway_response,
}
@pytest.fixture
def gateway_config():
return GatewayConfig(
gateway_name="Dummy",
auto_capture=True,
connection_params={"secret-key": "nobodylikesspanishinqusition"},
)
@pytest.fixture
def transaction_token():
return "transaction-token"
@pytest.fixture
def dummy_response(payment_dummy, transaction_token, card_details):
return GatewayResponse(
is_success=True,
action_required=False,
transaction_id=transaction_token,
error=EXAMPLE_ERROR,
amount=payment_dummy.total,
currency=payment_dummy.currency,
kind=TransactionKind.AUTH,
raw_response=None,
card_info=card_details,
)
def test_create_payment(checkout_with_item, address):
checkout_with_item.billing_address = address
checkout_with_item.save()
data = {
"gateway": "Dummy",
"payment_token": "token",
"total": checkout_total(checkout_with_item).gross.amount,
"currency": checkout_with_item.currency,
"email": "test@example.com",
"customer_ip_address": "127.0.0.1",
"checkout": checkout_with_item,
}
payment = create_payment(**data)
assert payment.gateway == "Dummy"
same_payment = create_payment(**data)
assert payment == same_payment
def test_create_payment_requires_order_or_checkout(settings):
data = {
"gateway": "Dummy",
"payment_token": "token",
"total": 10,
"currency": settings.DEFAULT_CURRENCY,
"email": "test@example.com",
}
with pytest.raises(TypeError) as e:
create_payment(**data)
assert e.value.args[0] == "Must provide checkout or order to create a payment."
def test_create_payment_from_checkout_requires_billing_address(
checkout_with_item, settings
):
checkout_with_item.billing_address = None
checkout_with_item.save()
data = {
"gateway": "Dummy",
"payment_token": "token",
"total": checkout_total(checkout_with_item),
"currency": checkout_with_item.currency,
"email": "test@example.com",
"checkout": checkout_with_item,
}
with pytest.raises(PaymentError) as e:
create_payment(**data)
assert e.value.code == PaymentErrorCode.BILLING_ADDRESS_NOT_SET.value
def test_create_payment_from_order_requires_billing_address(draft_order, settings):
draft_order.billing_address = None
draft_order.save()
data = {
"gateway": "Dummy",
"payment_token": "token",
"total": draft_order.total.gross.amount,
"currency": draft_order.currency,
"email": "test@example.com",
"order": draft_order,
}
with pytest.raises(PaymentError) as e:
create_payment(**data)
assert e.value.code == PaymentErrorCode.BILLING_ADDRESS_NOT_SET.value
def test_create_payment_information_for_checkout_payment(address, checkout_with_item):
checkout_with_item.billing_address = address
checkout_with_item.shipping_address = address
checkout_with_item.save()
data = {
"gateway": "Dummy",
"payment_token": "token",
"total": checkout_total(checkout_with_item).gross.amount,
"currency": checkout_with_item.currency,
"email": "test@example.com",
"customer_ip_address": "127.0.0.1",
"checkout": checkout_with_item,
}
payment = create_payment(**data)
payment_data = create_payment_information(payment, "token", payment.total)
billing = payment_data.billing
shipping = payment_data.shipping
assert billing
assert billing.first_name == address.first_name
assert billing.last_name == address.last_name
assert billing.street_address_1 == address.street_address_1
assert billing.city == address.city
assert shipping == billing
def test_create_payment_information_for_draft_order(draft_order):
data = {
"gateway": "Dummy",
"payment_token": "token",
"total": draft_order.total.gross.amount,
"currency": draft_order.currency,
"email": "test@example.com",
"customer_ip_address": "127.0.0.1",
"order": draft_order,
}
payment = create_payment(**data)
payment_data = create_payment_information(payment, "token", payment.total)
billing = payment_data.billing
shipping = payment_data.shipping
assert billing
assert billing.first_name == draft_order.billing_address.first_name
assert billing.last_name == draft_order.billing_address.last_name
assert billing.street_address_1 == draft_order.billing_address.street_address_1
assert billing.city == draft_order.billing_address.city
assert shipping == billing
def test_create_transaction(transaction_data):
txn = create_transaction(**transaction_data)
assert txn.payment == transaction_data["payment"]
gateway_response = transaction_data["gateway_response"]
assert txn.kind == gateway_response.kind
assert txn.amount == gateway_response.amount
assert txn.currency == gateway_response.currency
assert txn.token == gateway_response.transaction_id
assert txn.is_success == gateway_response.is_success
assert txn.gateway_response == gateway_response.raw_response
def test_create_transaction_no_gateway_response(transaction_data):
transaction_data.pop("gateway_response")
txn = create_transaction(**transaction_data)
assert txn.gateway_response == {}
@pytest.mark.parametrize(
"func",
[gateway.authorize, gateway.capture, gateway.confirm, gateway.refund, gateway.void],
)
def test_payment_needs_to_be_active_for_any_action(func, payment_dummy):
payment_dummy.is_active = False
with pytest.raises(PaymentError) as exc:
func(payment_dummy, "token")
assert exc.value.message == NOT_ACTIVE_PAYMENT_ERROR
@patch("saleor.order.actions.handle_fully_paid_order")
def test_gateway_charge_failed(
mock_handle_fully_paid_order, mock_get_manager, payment_txn_preauth, dummy_response
):
txn = payment_txn_preauth.transactions.first()
txn.is_success = False
payment = payment_txn_preauth
amount = payment.total
dummy_response.is_success = False
dummy_response.kind = TransactionKind.CAPTURE
mock_get_manager.capture_payment.return_value = dummy_response
with pytest.raises(PaymentError):
gateway.capture(payment, amount)
mock_get_manager.capture_payment.assert_called_once()
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.NOT_CHARGED
assert not payment.captured_amount
assert not mock_handle_fully_paid_order.called
def test_gateway_charge_errors(payment_dummy, transaction_token, settings):
payment = payment_dummy
gateway.authorize(payment, transaction_token)
with pytest.raises(PaymentError) as exc:
gateway.capture(payment, Decimal("0"))
assert exc.value.message == "Amount should be a positive number."
payment.charge_status = ChargeStatus.FULLY_REFUNDED
with pytest.raises(PaymentError) as exc:
gateway.capture(payment, Decimal("10"))
assert exc.value.message == "This payment cannot be captured."
payment.charge_status = ChargeStatus.NOT_CHARGED
with pytest.raises(PaymentError) as exc:
gateway.capture(payment, Decimal("1000000"))
assert exc.value.message == ("Unable to charge more than un-captured amount.")
def test_gateway_refund_errors(payment_txn_captured):
payment = payment_txn_captured
with pytest.raises(PaymentError) as exc:
gateway.refund(payment, Decimal("1000000"))
assert exc.value.message == "Cannot refund more than captured"
with pytest.raises(PaymentError) as exc:
gateway.refund(payment, Decimal("0"))
assert exc.value.message == "Amount should be a positive number."
payment.charge_status = ChargeStatus.NOT_CHARGED
with pytest.raises(PaymentError) as exc:
gateway.refund(payment, Decimal("1"))
assert exc.value.message == "This payment cannot be refunded."
def test_clean_authorize():
payment = Mock(can_authorize=Mock(return_value=True))
clean_authorize(payment)
payment = Mock(can_authorize=Mock(return_value=False))
with pytest.raises(PaymentError):
clean_authorize(payment)
def test_clean_capture():
# Amount should be a positive number
payment = Mock()
amount = Decimal("0.00")
with pytest.raises(PaymentError):
clean_capture(payment, amount)
# Payment cannot be captured
payment = Mock(can_capture=Mock(return_value=False))
amount = Decimal("1.00")
with pytest.raises(PaymentError):
clean_capture(payment, amount)
# Amount is larger than payment's total
payment = Mock(
can_capture=Mock(return_value=True),
total=Decimal("1.00"),
captured_amount=Decimal("0.00"),
)
amount = Decimal("2.00")
with pytest.raises(PaymentError):
clean_capture(payment, amount)
amount = Decimal("2.00")
payment = Mock(
can_capture=Mock(return_value=True),
total=amount,
captured_amount=Decimal("0.00"),
)
clean_capture(payment, amount)
def test_can_authorize(payment_dummy: Payment):
assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
payment_dummy.is_active = False
assert not payment_dummy.can_authorize()
payment_dummy.is_active = True
assert payment_dummy.can_authorize()
payment_dummy.charge_status = ChargeStatus.PARTIALLY_CHARGED
assert not payment_dummy.can_authorize()
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
assert not payment_dummy.can_authorize()
def test_can_capture(payment_txn_preauth: Payment):
assert payment_txn_preauth.charge_status == ChargeStatus.NOT_CHARGED
payment_txn_preauth.is_active = False
assert not payment_txn_preauth.can_capture()
payment_txn_preauth.is_active = True
assert payment_txn_preauth.can_capture()
payment_txn_preauth.charge_status = ChargeStatus.PARTIALLY_CHARGED
assert not payment_txn_preauth.can_capture()
payment_txn_preauth.charge_status = ChargeStatus.FULLY_CHARGED
assert not payment_txn_preauth.can_capture()
payment_txn_preauth.captured_amount = 0
payment_txn_preauth.transactions.all().delete()
assert not payment_txn_preauth.can_capture()
def test_can_void(payment_txn_preauth: Payment):
assert payment_txn_preauth.charge_status == ChargeStatus.NOT_CHARGED
payment_txn_preauth.is_active = False
assert not payment_txn_preauth.can_void()
payment_txn_preauth.is_active = True
assert payment_txn_preauth.can_void()
payment_txn_preauth.charge_status = ChargeStatus.PARTIALLY_CHARGED
assert not payment_txn_preauth.can_void()
payment_txn_preauth.charge_status = ChargeStatus.FULLY_CHARGED
assert not payment_txn_preauth.can_void()
payment_txn_preauth.charge_status = ChargeStatus.NOT_CHARGED
payment_txn_preauth.transactions.all().delete()
assert not payment_txn_preauth.can_void()
def test_can_refund(payment_dummy: Payment):
assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
payment_dummy.is_active = False
assert not payment_dummy.can_refund()
payment_dummy.is_active = True
assert not payment_dummy.can_refund()
payment_dummy.charge_status = ChargeStatus.PARTIALLY_CHARGED
assert payment_dummy.can_refund()
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
assert payment_dummy.can_refund()
def test_payment_get_authorized_amount(payment_txn_preauth):
payment = payment_txn_preauth
authorized_amount = payment.transactions.first().amount
assert payment.get_authorized_amount().amount == authorized_amount
assert payment.order.total_authorized.amount == authorized_amount
payment.transactions.create(
amount=payment.total,
kind=TransactionKind.CAPTURE,
gateway_response={},
is_success=True,
)
assert payment.get_authorized_amount().amount == Decimal(0)
payment.transactions.all().delete()
assert payment.get_authorized_amount().amount == Decimal(0)
def test_validate_gateway_response(gateway_response):
validate_gateway_response(gateway_response)
def test_validate_gateway_response_incorrect_transaction_kind(gateway_response):
gateway_response.kind = "incorrect-kind"
with pytest.raises(GatewayError) as e:
validate_gateway_response(gateway_response)
assert str(e.value) == (
"Gateway response kind must be one of {}".format(sorted(ALLOWED_GATEWAY_KINDS))
)
def test_validate_gateway_response_not_json_serializable(gateway_response):
class CustomClass(object):
pass
gateway_response.raw_response = CustomClass()
with pytest.raises(GatewayError) as e:
validate_gateway_response(gateway_response)
assert str(e.value) == "Gateway response needs to be json serializable"