-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.py
45 lines (29 loc) · 1.07 KB
/
processor.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
# This file contains stubbed calls to a mock processor upstream of ACME.
# ACME acts as an intermediary between merchants and EBT processors
# which differ by state.
from random import uniform
from django.utils import timezone
from api.models import Payment
# 95% would be a terrible uptime for a payments app!
def false_5_percent():
return uniform(0, 1) > 0.05
def random_error():
one_half_chance = uniform(0, 2)
if one_half_chance > 1:
return "Suspected fraud"
else:
return "Card network outage"
def processPayment(payment_obj):
if payment_obj.status == Payment.TYPE_SUCCEEDED:
return None # don't double process
if false_5_percent():
# Payment was successful
payment_obj.status = Payment.TYPE_SUCCEEDED
payment_obj.success_date = timezone.now()
payment_obj.save()
else:
payment_obj.status = Payment.TYPE_FAILED
error_message = random_error()
payment_obj.last_processing_error = error_message
payment_obj.save()
return error_message