Ignore:
Timestamp:
9 Mar 2015, 00:53:07 (10 years ago)
Author:
uli
Message:

payment item transformer.

Location:
main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/payment.py

    r12671 r12696  
    2525from zope.component import getUtilitiesFor
    2626from zope.event import notify
     27from waeup.ikoba.interfaces import MessageFactory as _
    2728from waeup.ikoba.utils.helpers import attrs_to_fields
     29from waeup.ikoba.utils.logger import Logger
    2830from waeup.ikoba.payments.interfaces import (
    2931    IPayment, STATE_UNPAID, STATE_FAILED, STATE_PAID,
     
    3133    IPaymentGatewayServicesLister,
    3234    )
    33 from waeup.ikoba.utils.logger import Logger
     35
     36
     37def format_payment_item_values(payment_item_values, currency):
     38    """Format tuples (description, currency, amount) for output.
     39
     40    `currency` passed in is the 'target' currency.
     41
     42    Returns a list of formated values. Last item is total sum.
     43    XXX: we do not really respect currency. If different items
     44         have different currencies, we are choked.
     45    """
     46    result = []
     47    total = decimal.Decimal("0.00")
     48    for descr, item_currency, amount in payment_item_values:
     49        total += amount
     50        if item_currency != currency:
     51            raise ValueError(
     52                "Different currencies in payment items not supported.")
     53        result.append((descr, '%s %0.2f' % (item_currency, amount)))
     54    result.append((_('Total'), '%s %0.2f' % (currency, total)))
     55    return result
    3456
    3557
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/tests/test_payment.py

    r12671 r12696  
    2828    )
    2929from waeup.ikoba.payments.payment import (
    30     Payment, get_payment_providers, PaymentItem,
     30    Payment, get_payment_providers, PaymentItem, format_payment_item_values,
    3131    )
    3232from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
     
    6161        assert result['some_name'] is fake_util
    6262
     63    def test_format_payment_item_values(self):
     64        # we can format lists of payment item values
     65        result = format_payment_item_values(
     66            [(u'Item 1', 'USD', decimal.Decimal("12.123")),
     67             (u'Item 2', 'USD', decimal.Decimal("12.002")),
     68             ], 'USD')
     69        self.assertEqual(
     70            result, [(u'Item 1', 'USD 12.12'),
     71                     (u'Item 2', 'USD 12.00'),
     72                     (u'Total', 'USD 24.12')]
     73            )
     74
     75    def test_format_payment_item_values_req_single_currency(self):
     76        # we require one currency for all items, yet.
     77        self.assertRaises(
     78            ValueError, format_payment_item_values,
     79            [(u'Item 1', 'USD', decimal.Decimal("12.12")),
     80             (u'Item 2', 'EUR', decimal.Decimal("50")),
     81             ],
     82            'USD')
     83
    6384
    6485class FunctionalHelperTests(FunctionalTestCase):
Note: See TracChangeset for help on using the changeset viewer.