File: test_transaction.py

package info (click to toggle)
python-braintree 4.31.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: python: 28,946; makefile: 9; sh: 8
file content (382 lines) | stat: -rw-r--r-- 15,795 bytes parent folder | download
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
from tests.test_helper import *
from braintree.test.credit_card_numbers import CreditCardNumbers
from braintree.meta_checkout_card import MetaCheckoutCard
from braintree.meta_checkout_token import MetaCheckoutToken
from datetime import datetime
from datetime import date
from braintree.authorization_adjustment import AuthorizationAdjustment
from unittest.mock import MagicMock

class TestTransaction(unittest.TestCase):
    def test_clone_transaction_raises_exception_with_bad_keys(self):
        with self.assertRaisesRegex(KeyError, "'Invalid keys: bad_key'"):
            Transaction.clone_transaction("an id", {"bad_key": "value"})

    def test_sale_raises_exception_with_bad_keys(self):
        with self.assertRaisesRegex(KeyError, "'Invalid keys: bad_key'"):
            Transaction.sale({"bad_key": "value"})

    def test_sale_raises_exception_with_nested_bad_keys(self):
        with self.assertRaisesRegex(KeyError, "'Invalid keys: credit_card\[bad_key\]'"):
            Transaction.sale({"credit_card": {"bad_key": "value"}})

    def test_finding_empty_id_raises_not_found_exception(self):
        with self.assertRaises(NotFoundError):
            Transaction.find(" ")

    def test_finding_none_raises_not_found_exception(self):
        with self.assertRaises(NotFoundError):
            Transaction.find(None)

    def test_constructor_includes_disbursement_information(self):
        attributes = {
            'amount': '27.00',
            'tax_amount': '1.00',
            'customer_id': '4096',
            'merchant_account_id': '8192',
            'order_id': '106601',
            'channel': '101',
            'payment_method_token': 'sometoken',
            'purchase_order_number': '20202',
            'recurring': 'False',
            'disbursement_details': {
                'settlement_amount': '27.00',
                'settlement_currency_iso_code': 'USD',
                'settlement_currency_exchange_rate': '1',
                'disbursement_date': date(2013, 4, 10),
                'funds_held': False
            }
        }

        transaction = Transaction(None, attributes)

        self.assertEqual(transaction.disbursement_details.settlement_amount, Decimal('27.00'))
        self.assertEqual(transaction.disbursement_details.settlement_currency_iso_code, 'USD')
        self.assertEqual(transaction.disbursement_details.settlement_currency_exchange_rate, Decimal('1'))
        self.assertEqual(transaction.disbursement_details.disbursement_date, date(2013, 4, 10))
        self.assertEqual(transaction.disbursement_details.funds_held, False)
        self.assertEqual(transaction.is_disbursed, True)

    def test_constructor_includes_sepa_direct_debit_return_code(self):
        attributes = {
            'amount': '27.00',
            'sepa_direct_debit_return_code': 'AM04'
        }

        transaction = Transaction(None, attributes)

        self.assertEqual(transaction.sepa_direct_debit_return_code, 'AM04')

    def test_transaction_handles_nil_risk_data(self):
        attributes = {
            'amount': '27.00',
            'tax_amount': '1.00',
            'customer_id': '4096',
            'merchant_account_id': '8192',
            'order_id': '106601',
            'channel': '101',
            'payment_method_token': 'sometoken',
            'purchase_order_number': '20202',
            'recurring': 'False',
        }

        transaction = Transaction(None, attributes)

        self.assertEqual(transaction.risk_data, None)

    def test_is_disbursed_false(self):
        attributes = {
            'amount': '27.00',
            'tax_amount': '1.00',
            'customer_id': '4096',
            'merchant_account_id': '8192',
            'order_id': '106601',
            'channel': '101',
            'payment_method_token': 'sometoken',
            'purchase_order_number': '20202',
            'recurring': 'False',
            'disbursement_details': {
                'settlement_amount': None,
                'settlement_currency_iso_code': None,
                'settlement_currency_exchange_rate': None,
                'disbursement_date': None,
                'funds_held': None,
            }
        }

        transaction = Transaction(None, attributes)

        self.assertEqual(transaction.is_disbursed, False)

    def test_sale_with_skip_advanced_fraud_checking_value_as_true(self):
        attributes = {
            "amount": TransactionAmounts.Authorize,
            "credit_card": {
                "number": CreditCardNumbers.Visa,
                "expiration_date": "05/2009"
            },
            "options": {
                "skip_advanced_fraud_checking": True
            }
        }

        transaction_gateway = self.setup_transaction_gateway_and_mock_post()
        transaction_gateway.sale(attributes)
        transaction_param = transaction_gateway._post.call_args[0][1]
        self.assertTrue(transaction_param['transaction']['options']['skip_advanced_fraud_checking'])

    def test_sale_with_skip_advanced_fraud_checking_value_as_false(self):
        attributes = {
            "amount": TransactionAmounts.Authorize,
            "credit_card": {
                "number": CreditCardNumbers.Visa,
                "expiration_date": "05/2009"
            },
            "options": {
                "skip_advanced_fraud_checking": False
            }
        }

        transaction_gateway = self.setup_transaction_gateway_and_mock_post()
        transaction_gateway.sale(attributes)
        transaction_param = transaction_gateway._post.call_args[0][1]
        self.assertFalse(transaction_param['transaction']['options']['skip_advanced_fraud_checking'])

    def test_sale_without_skip_advanced_fraud_checking_value_option(self):
        attributes = {
            "amount": TransactionAmounts.Authorize,
            "credit_card": {
                "number": CreditCardNumbers.Visa,
                "expiration_date": "05/2009"
            },
            "options": {
                "submit_for_settlement": True
            }
        }

        transaction_gateway = self.setup_transaction_gateway_and_mock_post()
        transaction_gateway.sale(attributes)
        transaction_param = transaction_gateway._post.call_args[0][1]
        self.assertTrue('skip_advanced_fraud_checking' not in transaction_param['transaction']['options'])

    def test_sale_with_external_network_token_option(self):
        attributes = {
            "amount": TransactionAmounts.Authorize,
            "credit_card": {
                "number": CreditCardNumbers.Visa,
                "expiration_date": "05/2009",
                "network_tokenization_attributes": {
                    "cryptogram": "/wAAAAAAAcb8AlGUF/1JQEkAAAA=",
                    "ecommerce_indicator": "45310020105",
                    "token_requestor_id" : "05"
                }
            }
        }

        transaction_gateway = self.setup_transaction_gateway_and_mock_post()
        transaction_gateway.sale(attributes)
        transaction_param = transaction_gateway._post.call_args[0][1]
        self.assertTrue('network_tokenization_attributes' in transaction_param['transaction']['credit_card'])
        self.assertEqual(transaction_param['transaction']['credit_card']['network_tokenization_attributes']['cryptogram'], "/wAAAAAAAcb8AlGUF/1JQEkAAAA=")
        self.assertEqual(transaction_param['transaction']['credit_card']['network_tokenization_attributes']['ecommerce_indicator'], "45310020105")
        self.assertEqual(transaction_param['transaction']['credit_card']['network_tokenization_attributes']['token_requestor_id'], "05")

    def setup_transaction_gateway_and_mock_post(self):
        transaction_gateway = TransactionGateway(BraintreeGateway(None))
        transaction_gateway._post = MagicMock(name='config.http.post')
        return transaction_gateway

    def test_constructor_doesnt_includes_auth_adjustments(self):
        attributes = {
            'amount': '27.00',
            'customer_id': '4096',
            'merchant_account_id': '8192',
            'payment_method_token': 'sometoken',
            'purchase_order_number': '20202',
            'recurring': 'False',
            'tax_amount': '1.00',
        }

        transaction = Transaction(None, attributes)
        self.assertFalse(hasattr(transaction, 'authorization_adjustments'))

    def test_constructor_includes_auth_adjustments(self):
        attributes = {
            'amount': '27.00',
            'customer_id': '4096',
            'merchant_account_id': '8192',
            'payment_method_token': 'sometoken',
            'purchase_order_number': '20202',
            'recurring': 'False',
            'tax_amount': '1.00',
            'authorization_adjustments': [{
                "amount": "20.00",
                "timestamp": datetime(2017, 7, 12, 1, 2, 3),
                "success": True,
                "processor_response_code": "1000",
                "processor_response_text": "Approved",
            }],
        }

        transaction = Transaction(None, attributes)
        transaction_adjustment = transaction.authorization_adjustments[0]
        self.assertEqual(transaction_adjustment.amount, Decimal("20.00"))
        self.assertEqual(transaction_adjustment.timestamp, datetime(2017, 7, 12, 1, 2, 3))
        self.assertEqual(transaction_adjustment.success, True)
        self.assertEqual(transaction_adjustment.processor_response_code, "1000")
        self.assertEqual(transaction_adjustment.processor_response_text, "Approved")

    def test_constructor_parses_shipments_into_packages(self):
        attributes = {
            'amount': '27.00',
            'customer_id': '4096',
            'merchant_account_id': '8192',
            'payment_method_token': 'sometoken',
            'purchase_order_number': '20202',
            'recurring': 'False',
            'tax_amount': '1.00',
            'shipments': [
                 {
                    'id': 'id1',
                    'carrier': 'UPS',
                    'tracking_number': 'tracking_number_1',
                    # NEXT_MAJOR_VERSION remove paypal_tracking_id
                    'paypal_tracking_id': 'pp_tracking_number_1',
                    'paypal_tracker_id': 'pp_tracker_id_1',
                },
                {
                    'id': 'id2',
                    'carrier': 'FEDEX',
                    'tracking_number': 'tracking_number_2',
                    # NEXT_MAJOR_VERSION remove paypal_tracking_id
                    'paypal_tracking_id': 'pp_tracking_number_2',
                    'paypal_tracker_id': 'pp_tracker_id_2',
                },
            ],
        }

        transaction = Transaction(None, attributes)
        package_detail_1 = transaction.packages[0]
        self.assertEqual(package_detail_1.id, "id1")
        self.assertEqual(package_detail_1.carrier, "UPS")
        self.assertEqual(package_detail_1.tracking_number, "tracking_number_1")
        # NEXT_MAJOR_VERSION remove paypal_tracking_id assertions.
        self.assertEqual(package_detail_1.paypal_tracking_id, "pp_tracking_number_1")
        self.assertEqual(package_detail_1.paypal_tracker_id, "pp_tracker_id_1")

        package_detail_2 = transaction.packages[1]
        self.assertEqual(package_detail_2.id, "id2")
        self.assertEqual(package_detail_2.carrier, "FEDEX")
        self.assertEqual(package_detail_2.tracking_number, "tracking_number_2")
        # NEXT_MAJOR_VERSION remove paypal_tracking_id assertions.
        self.assertEqual(package_detail_2.paypal_tracking_id, "pp_tracking_number_2")
        self.assertEqual(package_detail_2.paypal_tracker_id, "pp_tracker_id_2")

    def test_constructor_works_with_empty_shipments_list(self):
        attributes = {
            'amount': '27.00',
            'customer_id': '4096',
            'merchant_account_id': '8192',
            'payment_method_token': 'sometoken',
            'purchase_order_number': '20202',
            'recurring': 'False',
            'tax_amount': '1.00',
            'shipments': [],
        }

        transaction = Transaction(None, attributes)
        self.assertEqual(len(transaction.packages), 0)

    def test_constructor_includes_network_transaction_id_and_response_code_and_response_text(self):
        attributes = {
            'amount': '27.00',
            'tax_amount': '1.00',
            'network_transaction_id': '123456789012345',
            'network_response_code': '00',
            'network_response_text': 'Successful approval/completion or V.I.P. PIN verification is successful'
        }

        transaction = Transaction(None, attributes)
        self.assertEqual(transaction.network_transaction_id, "123456789012345")
        self.assertEqual(transaction.network_response_code, "00")
        self.assertEqual(transaction.network_response_text, "Successful approval/completion or V.I.P. PIN verification is successful")

    def test_constructor_includes_installment_count(self):
        attributes = {
            'amount': '27.00',
            'tax_amount': '1.00',
            'installments': {
                'count': 4
            }
        }

        transaction = Transaction(None, attributes)
        self.assertEqual(transaction.installments["count"], 4)

    def test_gateway_rejection_reason_for_excessive_retry(self):
        attributes = {
            'amount': '27.00',
            'gateway_rejection_reason': 'excessive_retry'
        }

        transaction = Transaction(None, attributes)
        self.assertEqual(transaction.gateway_rejection_reason, braintree.Transaction.GatewayRejectionReason.ExcessiveRetry)

    def test_merchant_advice_code(self):
        attributes = {
            'amount': TransactionAmounts.Decline,
            'merchant_advice_code': "01",
            'merchant_advice_code_text': "New account information available"
        }

        transaction = Transaction(None, attributes)
        self.assertEqual(transaction.merchant_advice_code, "01")
        self.assertEqual(transaction.merchant_advice_code_text, "New account information available")

    def test_retry_ids_and_retried_transaction_id(self):
        attributes = {
            'amount': TransactionAmounts.Decline,
            'retry_ids': ['retry_id_1','retry_id2'],
            'retried_transaction_id': '12345',
            'retried': True
        }

        transaction = Transaction(None, attributes)
        self.assertEqual(transaction.retry_ids, ['retry_id_1','retry_id2'])
        self.assertEqual(transaction.retried_transaction_id, "12345")
        self.assertTrue(transaction.retried)

    def test_debit_network(self):
        attributes = {
            'amount': '27.00',
            'debit_network' : CreditCard.DebitNetwork.Star
        }
        transaction = Transaction(None, attributes)
        self.assertEqual(transaction.debit_network, CreditCard.DebitNetwork.Star)

    def test_transaction_meta_checkout_card_attributes(self):
        attributes = {
            'amount': '420',
            'meta_checkout_card': {}
        }

        transaction = Transaction(None, attributes)
        self.assertIsInstance(transaction.meta_checkout_card_details, MetaCheckoutCard)

    def test_transaction_meta_checkout_token_attributes(self):
        attributes = {
            'amount': '69',
            'meta_checkout_token': {}
        }

        transaction = Transaction(None, attributes)
        self.assertIsInstance(transaction.meta_checkout_token_details, MetaCheckoutToken)

    def test_foreign_retailer(self):
        attributes = {
            'amount': TransactionAmounts.Authorize,
            'foreign_retailer': True,
        }

        transaction = Transaction(None, attributes)
        self.assertTrue(transaction.foreign_retailer)