This repository has been archived by the owner on Mar 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
pagseguro_transaction.py
101 lines (87 loc) · 3.87 KB
/
pagseguro_transaction.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
# coding: utf-8
import datetime
import requests
import xmltodict
from django.conf import settings
from pythonbrasil8.subscription import models
class PagSeguro(object):
URL_TRANSACTIONS = 'https://ws.pagseguro.uol.com.br/v2/transactions'
URL_TRANSACTION_DETAIL = \
'https://ws.pagseguro.uol.com.br/v2/transactions/{}'
def __init__(self, email, token):
self.__parameters = {'email': email, 'token': token}
def get_transactions(self, initial_date, final_date):
'''Get all transactions in the interval
If more than one page is needed, it automatically gets ALL the pages.
`initial_date` and `final_date` should be in format
`YYYY-MM-DDTHH:MM:SS`.
PagSeguro's API documentation says it must be `YYYY-MM-DDTHH:MM:SS.sz`,
where 's' is microseconds and 'z' is timezone, but it is not needed and
it fails in some cases!
'''
page = 1
max_results = 100
finished = False
parameters = {'initialDate': initial_date, 'finalDate': final_date,
'maxPageResults': max_results}
parameters.update(self.__parameters)
transactions = []
while not finished:
parameters['page'] = page
response = requests.get(self.URL_TRANSACTIONS, params=parameters)
data = xmltodict.parse(response.text.encode('iso-8859-1'))
result = data['transactionSearchResult']
if int(result['resultsInThisPage']) > 0:
new_transactions = result['transactions']['transaction']
if type(new_transactions) is not list: # only one returned
new_transactions = [new_transactions]
transactions.extend(new_transactions)
total_pages = int(result['totalPages'])
if page < total_pages:
page += 1
elif page == total_pages:
finished = True
return transactions
def get_transaction(self, transaction_id):
'''Given a transaction id, get its information'''
url = self.URL_TRANSACTION_DETAIL.format(transaction_id)
response = requests.get(url, params=self.__parameters)
print response.status_code, response.text
return xmltodict.parse(response.text.encode('iso-8859-1'))['transaction']
if __name__ == '__main__':
email = settings.PAGSEGURO["email"]
token = settings.PAGSEGURO["token"]
ps = PagSeguro(email, token)
def get_all_transactions():
'''Get past transactions and save it to a CSV file
PagSeguro only allow us to get 6-months-old transactions.
'''
# Get all desired transactions
now = datetime.datetime.now()
yesterday = now - datetime.timedelta(days=1)
start = yesterday.strftime('%Y-%m-%dT%H:%M:%S-02:00')
end = now.strftime('%Y-%m-%dT%H:%M:%S-02:00')
whole_transactions = ps.get_transactions(start, end)
paid = [t for t in whole_transactions
if 'reference' in t
and t['status'] in ('3', '4')
and t['grossAmount'] in ('150.00', '250.00', '350.00')]
for transaction in paid:
subscription_id = transaction['reference']
transactions = models.Transaction.objects.select_related('subscription').filter(
subscription_id=subscription_id,
price=float(transaction['grossAmount']),
)
update = None
for transaction in transactions:
if transaction.status == 'pending':
update = transaction
elif transaction.status == 'done':
update = None
break
if update:
update.status = 'done'
update.save()
update.subscription.status = 'confirmed'
update.subscription.save()
get_all_transactions()