forked from OpenPaymentPlatform/python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_facade_concurrent.py
54 lines (41 loc) · 1.97 KB
/
example_facade_concurrent.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
__author__ = 'PAY.ON'
import opp
import logging
import threading
logger = logging.getLogger("example_facade_concurrent")
def success_worker(num, worker_api):
#success_worker creates a transaction and should succeed
# create payment parameters
basic_payment = opp.facade.BasicPayment(amount='92.00', currency='EUR', payment_brand='AMEX', descriptor='Test Amex')
# create card account parameters
card_account = opp.facade.CardAccount(holder='Jane Jones', number='377777777777770', expiry_month='05',
expiry_year='2018',
cvv='1234')
try:
preauthorization = worker_api.preauthorizations().create(basic_payment, card_account)
logger.debug("Success Worker #{0} RESPONSE: {1}".format(num, preauthorization.__dict__))
except opp.facade.ApiError as e:
#handle error
print(e)
def failure_worker(num, worker_api):
#failure_worker tries to create a transaction without sending any payment data and should fail with an exception,
#so output will come from except case
try:
worker_api.preauthorizations().create()
except opp.facade.ApiError as e:
#handle error
logger.debug("Failure Worker #{0} RESPONSE: {1}".format(num, e))
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, filename='opp.log')
threads = []
authentication = opp.facade.Authentication(user_id='8a8294174b7ecb28014b9699220015cc', password='sy6KJsT8',
entity_id='8a8294174b7ecb28014b9699220015ca')
# initialize OPP API with authentication object
api = opp.facade.API(authentication)
#start workers with passing through the shared opp.facade.API object
for i in range(5):
t = threading.Thread(target=success_worker, args=(i, api, ))
t2 = threading.Thread(target=failure_worker, args=(i, api, ))
threads.append(t)
t2.start()
t.start()