[paypal] Get converted amount for foreign currencies

This commit is contained in:
Kitof 2015-02-25 09:43:01 +01:00 committed by Florent Fourcot
commit 131b01c599
3 changed files with 55 additions and 21 deletions

View file

@ -19,7 +19,7 @@
from weboob.deprecated.browser import Browser, BrowserIncorrectPassword
from .pages import LoginPage, AccountPage, DownloadHistoryPage, LastDownloadHistoryPage, SubmitPage, HistoryParser, UselessPage, HistoryPage, CSVAlreadyAsked
from .pages import LoginPage, AccountPage, DownloadHistoryPage, LastDownloadHistoryPage, SubmitPage, HistoryParser, UselessPage, HistoryPage, CSVAlreadyAsked, HistoryDetailsPage
from .newpages import NewHomePage, NewAccountPage, NewProHistoryPage, NewPartHistoryPage
import datetime
@ -45,6 +45,7 @@ class Paypal(Browser):
'/cgi-bin/webscr\?cmd=_history-download-recent$': LastDownloadHistoryPage,
'/cgi-bin/webscr\?dispatch=[a-z0-9]+$': (SubmitPage, HistoryParser()),
'/cgi-bin/webscr\?cmd=_history-download-recent-submit&dispatch=[a-z0-9]+$': (SubmitPage, HistoryParser()),
'https://history.paypal.com/cgi-bin/webscr\?cmd=_history-details-from-hub&id=[A-Z0-9]+$': HistoryDetailsPage,
'https://www.paypal.com/webapps/business/\?nav=0.0': NewHomePage,
'https://www.paypal.com/webapps/business/\?country_lang.x=true': NewHomePage,
'https://www.paypal.com/myaccount/\?nav=0.0': NewHomePage,
@ -56,7 +57,7 @@ class Paypal(Browser):
DEFAULT_TIMEOUT = 30 # CSV export is slow
BEGINNING = datetime.date(1998,6,1) # The day PayPal was founded
BEGINNING = datetime.date(1998, 6, 1) # The day PayPal was founded
website = None
def find_website_version(self):
@ -144,18 +145,19 @@ class Paypal(Browser):
else:
step_min = 90
step_max = 180
def fetch_fn(start, end):
if self.website == "old" and self.download_history(start, end).rows:
return self.page.iter_transactions(account)
elif self.download_history(start, end):
return self.page.iter_transactions(account)
assert step_max <= 365*2 # PayPal limitations as of 2014-06-16
assert step_max <= 365*2 # PayPal limitations as of 2014-06-16
try:
for i in self.smart_fetch(beginning=self.BEGINNING,
end=datetime.date.today(),
step_min=step_min,
step_max=step_max,
fetch_fn=fetch_fn):
end=datetime.date.today(),
step_min=step_min,
step_max=step_max,
fetch_fn=fetch_fn):
yield i
except CSVAlreadyAsked:
for i in self.download_last_history(account):
@ -198,7 +200,7 @@ class Paypal(Browser):
else:
s = start.strftime('%d/%m/%Y')
e = end.strftime('%d/%m/%Y')
#Settings a big magic number so we get all transaction for the period
# Settings a big magic number so we get all transaction for the period
LIMIT = '9999'
if self.account_type == "pro":
self.location('/webapps/business/activity?fromdate=' + s + '&todate=' + e + '&transactiontype=ALL_TRANSACTIONS&currency=ALL_TRANSACTIONS_CURRENCY&limit=' + LIMIT)
@ -214,3 +216,13 @@ class Paypal(Browser):
def transfer(self, from_id, to_id, amount, reason=None):
raise NotImplementedError()
def convert_amount(self, account, trans):
if(trans['actions']['details']['action'] == 'ACTIVITY_DETAILS'):
self.location(trans['actions']['details']['url'])
if self.is_on_page(HistoryDetailsPage):
cc = self.page.get_converted_amount(account)
if cc:
trans['originalAmount'] = trans['netAmount']
trans['netAmount'] = cc
return trans

View file

@ -29,6 +29,7 @@ from weboob.tools.date import parse_french_date
class NewHomePage(Page):
pass
class NewAccountPage(Page):
def get_account(self, _id):
return self.get_accounts().get(_id)
@ -52,6 +53,7 @@ class NewAccountPage(Page):
return accounts
class NewProHistoryPage(Page):
def iter_transactions(self, account):
@ -77,22 +79,22 @@ class NewProHistoryPage(Page):
def transaction_left(self):
return (len(self.document.xpath('//div[@class="no-records"]')) == 0)
class NewPartHistoryPage(Page):
def transaction_left(self):
return (len(self.document['data']['activity']['COMPLETED']) > 0 or len(self.document['data']['activity']['PENDING']) > 0)
def iter_transactions(self, account):
for trans in self.parse():
if trans._currency == account.currency:
yield trans
for trans in self.parse(account):
yield trans
def parse(self):
def parse(self, account):
transactions = list()
for status in ['PENDING', 'COMPLETED']:
transac = self.document['data']['activity'][status]
for t in transac:
tran = self.parse_transaction(t)
tran = self.parse_transaction(t, account)
if tran:
transactions.append(tran)
@ -100,23 +102,32 @@ class NewPartHistoryPage(Page):
for t in transactions:
yield t
def parse_transaction(self, transaction):
def parse_transaction(self, transaction, account):
t = FrenchTransaction(transaction['activityId'])
date = parse_french_date(transaction['date'])
raw = transaction.get('counterparty', transaction['displayType'])
t.parse(date=date, raw=raw)
if transaction['currencyCode'] != account.currency:
transaction = self.browser.convert_amount(account, transaction)
try:
t.original_amount = self.format_amount(transaction['originalAmount'], transaction["isCredit"])
t.original_currency = transaction["currencyCode"]
except KeyError:
return
try:
m = re.search(r"\D", transaction['netAmount'][::-1])
amount = Decimal(re.sub(r'[^\d]', '', transaction['netAmount']))/Decimal((10 ** m.start()))
t.amount = self.format_amount(transaction['netAmount'], transaction["isCredit"])
except KeyError:
return
if transaction['isCredit']:
t.amount = abs(amount)
else:
t.amount = - abs(amount)
t._currency = transaction['currencyCode']
return t
def format_amount(self, to_format, is_credit):
m = re.search(r"\D", to_format[::-1])
amount = Decimal(re.sub(r'[^\d]', '', to_format))/Decimal((10 ** m.start()))
if is_credit:
return abs(amount)
else:
return -abs(amount)

View file

@ -141,7 +141,7 @@ class DownloadHistoryPage(Page):
class LastDownloadHistoryPage(Page):
def download(self):
self.browser.select_form(nr=1)
log_select = self.document.xpath('//table//form//input[@type="radio"]')[0].attrib['value']
log_select = self.document.xpath('//table//form//input[@type="radio"]')[0].attrib['value']
self.browser['log_select'] = [log_select]
self.browser.submit()
@ -366,3 +366,14 @@ class HistoryPage(Page):
for trans in self.parse():
if trans._currency == account.currency:
yield trans
class HistoryDetailsPage(Page):
def get_converted_amount(self, account):
convert_td = self.document.xpath('//td[contains(text(),"' + account.currency + ')")]')[0].text
m = re.match('.* ([^ ]+) ' + account.currency + '\).*', convert_td)
if m:
return m.group(1)
else:
return False