Skip to content
transfer.py 10.5 KiB
Newer Older
Florent's avatar
Florent committed
# -*- coding: utf-8 -*-

# Copyright(C) 2009-2014  Romain Bignon, Florent Fourcot
Florent's avatar
Florent committed
#
# This file is part of a weboob module.
Florent's avatar
Florent committed
#
# This weboob module is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
Florent's avatar
Florent committed
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This weboob module is distributed in the hope that it will be useful,
Florent's avatar
Florent committed
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
Florent's avatar
Florent committed
#
# You should have received a copy of the GNU Lesser General Public License
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
Florent's avatar
Florent committed

Baptiste Delpey's avatar
Baptiste Delpey committed
from datetime import datetime

from weboob.capabilities.bank import Recipient, Transfer, TransferInvalidAmount
from weboob.capabilities import NotAvailable
from weboob.browser.pages import HTMLPage, LoggedPage
from weboob.browser.elements import ListElement, ItemElement, method
from weboob.browser.filters.standard import CleanText, CleanDecimal, Env
from weboob.browser.filters.html import Attr
Baptiste Delpey's avatar
Baptiste Delpey committed
from weboob.tools.capabilities.bank.transactions import FrenchTransaction
from weboob.tools.capabilities.bank.iban import is_iban_valid
Baptiste Delpey's avatar
Baptiste Delpey committed
from weboob.tools.date import parse_french_date

Sylvie Ye's avatar
Sylvie Ye committed
from ..api.login import INGVirtKeyboard
Baptiste Delpey's avatar
Baptiste Delpey committed
class MyRecipient(ItemElement):
    klass = Recipient

    def obj_enabled_at(self):
        return datetime.now().replace(microsecond=0)


Florent's avatar
Florent committed
class TransferPage(LoggedPage, HTMLPage):
Baptiste Delpey's avatar
Baptiste Delpey committed
    def able_to_transfer(self, origin):
        return [div for div in self.doc.xpath('//div[@id="internalAccounts"]//div[@data-acct-number]')
                if Attr('.', 'data-acct-number')(div) in origin.id and 'disabled' not in div.attrib['class']]
Florent's avatar
Florent committed

    @method
    class get_recipients(ListElement):
        class ExternalRecipients(ListElement):
Baptiste Delpey's avatar
Baptiste Delpey committed
            item_xpath = '//tr[@id="externalAccountsIsotopeWrapper"]//div[not(has-class("disabled")) and @data-acct-number]'
Baptiste Delpey's avatar
Baptiste Delpey committed
            class item(MyRecipient):
                obj_id = Attr('.', 'data-acct-number')
Baptiste Delpey's avatar
Baptiste Delpey committed
                obj_label = CleanText('.//span[@class="title"]')
                obj_category = u'Externe'
                obj_bank_name = CleanText(Attr('.//span[@class="bankname"]', 'title'))
                def obj_iban(self):
                    return self.obj_id(self) if is_iban_valid(self.obj_id(self)) else NotAvailable

        class InternalRecipients(ListElement):
Baptiste Delpey's avatar
Baptiste Delpey committed
            item_xpath = '//div[@id="internalAccounts"]//td/div[not(has-class("disabled"))]'

            class item(MyRecipient):

                obj_category = u'Interne'
                obj_currency = FrenchTransaction.Currency('.//span[@class="solde"]/label')
                obj_id = Env('id')
                obj_label = Env('label')
                obj_iban = Env('iban')
                obj_bank_name = u'ING'

                def parse(self, el):
                    _id = Attr('.', 'data-acct-number')(self)
                    accounts = [acc for acc in self.page.browser.get_accounts_list(fill_account=False, space=self.env['origin']._space) if _id in acc.id]
Baptiste Delpey's avatar
Baptiste Delpey committed
                    assert len(accounts) == 1
                    account = accounts[0]
                    self.env['id'] = account.id
                    self.env['label'] = account.label
                    self.env['iban'] = account.iban

    def get_origin_account_id(self, origin):
        return [Attr('.', 'data-acct-number')(div) for div in self.doc.xpath('//div[@id="internalAccounts"]//div[@data-acct-number]')
                if Attr('.', 'data-acct-number')(div) in origin.id][0]

    def update_origin_account_estimated_balance(self, origin):
        for div in self.doc.xpath('//div[@id="internalAccounts"]//div[@data-acct-number]'):
            if Attr('.', 'data-acct-number')(div) in origin.id:
                origin._estimated_balance = CleanDecimal('.//span[@class="solde"]', replace_dots=True, default=NotAvailable)(div)

    def update_origin_account_label(self, origin):
        # 'Compte Courant Joint' can become 'Compte Courant'
        # search for the account label used to do transfer
        for div in self.doc.xpath('//div[@id="internalAccounts"]//div[@data-acct-number]'):
            if Attr('.', 'data-acct-number')(div) in origin.id:
                origin._account_label = CleanText('.//span[@class="title"]', default=NotAvailable)(div)

    def update_recipient_account_label(self, recipient):
        # 'Compte Courant Joint' can become 'Compte Courant'
        # search for the account label used to do transfer
        for div in self.doc.xpath('//div[@id="internalAccounts"]//div[@data-acct-number]'):
            if Attr('.', 'data-acct-number')(div) in recipient.id:
                recipient._account_label = CleanText('.//span[@class="title"]', default=NotAvailable)(div)

Baptiste Delpey's avatar
Baptiste Delpey committed
    def get_transfer_form(self, txt):
        form = self.get_form(xpath='//form[script[contains(text(), "%s")]]' % txt)
        form['AJAXREQUEST'] = '_viewRoot'
        form['AJAX:EVENTS_COUNT'] = '1'
        param = Attr('//form[script[contains(text(), "RenderTransferDetail")]]/script[contains(text(), "%s")]' % txt, 'id')(self.doc)
        form[param] = param
        return form

    def go_to_recipient_selection(self, origin):
        form = self.get_transfer_form('SetScreenStep')
        form['screenStep'] = '1'
        form.submit()

        # update account estimated balance and account label for the origin account check on summary page
        self.update_origin_account_estimated_balance(origin)
        self.update_origin_account_label(origin)
Baptiste Delpey's avatar
Baptiste Delpey committed
        # Select debit account
        form = self.get_transfer_form('SetDebitAccount')
        form['selectedDebitAccountNumber'] = self.get_origin_account_id(origin)
        form.submit()

        # Render available accounts
        form = self.get_transfer_form('ReRenderAccountList')
        form.submit()

    def do_transfer(self, account, recipient, transfer):
        self.go_to_recipient_selection(account)

        # update recipient account label for the recipient check on summary page
        self.update_recipient_account_label(recipient)
Baptiste Delpey's avatar
Baptiste Delpey committed
        form = self.get_transfer_form('SetScreenStep')
        form['screenStep'] = '2'
        form.submit()

        form = self.get_transfer_form('SetCreditAccount')
Sylvie Ye's avatar
Sylvie Ye committed
        # intern id is like XX-XXXXXXXXXXXX but in request, only the second part is necessary
        form['selectedCreditAccountNumber'] = recipient.id.split('-')[-1]
Baptiste Delpey's avatar
Baptiste Delpey committed
        form.submit()

        form = self.get_transfer_form('ReRenderAccountList')
        form.submit()

        form = self.get_transfer_form('ReRenderStepTwo')
        form.submit()

        form = self.get_form()
ntome's avatar
ntome committed
        keys = [k for k in form if '_link_hidden' in k or 'j_idcl' in k]
Baptiste Delpey's avatar
Baptiste Delpey committed
        for k in keys:
            form.pop(k)
Florent's avatar
Florent committed
        form['AJAXREQUEST'] = "_viewRoot"
        form['AJAX:EVENTS_COUNT'] = "1"
Baptiste Delpey's avatar
Baptiste Delpey committed
        form["transfer_form:transferAmount"] = str(transfer.amount)
Florent's avatar
Florent committed
        form["transfer_form:validateDoTransfer"] = "needed"
Baptiste Delpey's avatar
Baptiste Delpey committed
        form['transfer_form:transferMotive'] = transfer.label
        form['transfer_form:ipt-date-exec'] = transfer.exec_date.strftime('%d/%m/%Y')
        form['transfer_form'] = 'transfer_form'
        form['transfer_form:valide'] = 'transfer_form:valide'
        form.submit()

    def continue_transfer(self, password):
        form = self.get_form(xpath='//form[h2[contains(text(), "Saisissez votre code secret pour valider la transaction")]]')
        vk = INGVirtKeyboard(self)
        for k in form:
            if 'mrltransfer' in k:
                form[k] = vk.get_coordinates(password)
Florent's avatar
Florent committed
        form.submit()
Florent's avatar
Florent committed

Florent's avatar
Florent committed
    def confirm(self, password):
        vk = INGVirtKeyboard(self)
Baptiste Delpey's avatar
Baptiste Delpey committed
        form = self.get_form(xpath='//form[h2[contains(text(), "Saisissez votre code secret pour valider la transaction")]]')
Florent's avatar
Florent committed
        for elem in form:
            if "_link_hidden_" in elem or "j_idcl" in elem:
                form.pop(elem)

        form['AJAXREQUEST'] = '_viewRoot'
        form['%s:mrgtransfer' % form.name] = '%s:mrgtransfer' % form.name
        form['%s:mrltransfer' % form.name] = vk.get_coordinates(password)
Florent's avatar
Florent committed
        form.submit()
Florent's avatar
Florent committed

Baptiste Delpey's avatar
Baptiste Delpey committed
    def recap(self, origin, recipient, transfer):
        error = CleanText(u'//div[@id="transfer_form:moveMoneyDetailsBody"]//span[@class="error"]', default=None)(self.doc) or \
                CleanText(u'//p[contains(text(), "Nous sommes désolés. Le solde de votre compte ne doit pas être inférieur au montant de votre découvert autorisé. Veuillez saisir un montant inférieur.")]', default=None)(self.doc)
Laurent Bachelier's avatar
Laurent Bachelier committed
            raise TransferInvalidAmount(message=error)
Baptiste Delpey's avatar
Baptiste Delpey committed
        t = Transfer()
        t.label = transfer.label
        t.amount = CleanDecimal('//div[@id="transferSummary"]/div[@id="virementLabel"]\
                                 //label[@class="digits positive"]', replace_dots=True)(self.doc)
Baptiste Delpey's avatar
Baptiste Delpey committed
        t.currency = FrenchTransaction.Currency('//div[@id="transferSummary"]/div[@id="virementLabel"]\
                                                 //label[@class="digits positive"]')(self.doc)
        # check origin account balance
        origin_balance = CleanDecimal('//div[@id="transferSummary"]/div[has-class("debit")]\
                                       //label[has-class("digits")]', replace_dots=True)(self.doc)
        assert (origin_balance == origin.balance) or (origin_balance == origin._estimated_balance)
Baptiste Delpey's avatar
Baptiste Delpey committed
        t.account_balance = origin.balance

        # check account label for origin and recipient
        origin_label = CleanText('//div[@id="transferSummary"]/div[has-class("debit")]\
                                  //span[@class="title"]')(self.doc)
        recipient_label = CleanText('//div[@id="transferSummary"]/div[has-class("credit")]\
                                     //span[@class="title"]')(self.doc)
        assert (origin.label == origin_label) or (origin._account_label == origin_label)
        assert (recipient.label == recipient_label) or (recipient._account_label == recipient_label)

Baptiste Delpey's avatar
Baptiste Delpey committed
        t.account_label = origin.label
        t.account_iban = origin.iban
        t.account_id = origin.id

        t.recipient_label = recipient.label
        t.recipient_iban = recipient.iban
        t.recipient_id = recipient.id

        t.exec_date = parse_french_date(CleanText('//p[has-class("exec-date")]', children=False,
Laurent Bachelier's avatar
Laurent Bachelier committed
                                        replace=[('le', ''), (u'exécuté', ''), ('demain', ''), ('(', ''), (')', ''),
                                                 ("aujourd'hui", '')])(self.doc)).date()
Baptiste Delpey's avatar
Baptiste Delpey committed
        return t