Changeset 12295 for main/waeup.ikoba


Ignore:
Timestamp:
22 Dec 2014, 16:14:04 (10 years ago)
Author:
uli
Message:

Extend interfaces.

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/interfaces.py

    r12276 r12295  
    1717##
    1818import decimal
     19from zc.sourcefactory.basic import BasicSourceFactory
    1920from zope import schema
     21from zope.component import getUtilitiesFor
    2022from zope.interface import Interface
    2123from waeup.ikoba.interfaces import (
    2224    IIkobaObject, SimpleIkobaVocabulary, ContextualDictSourceFactoryBase)
    2325from waeup.ikoba.interfaces import MessageFactory as _
    24 
     26from waeup.ikoba.payments.currencies import ISO_4217_CURRENCIES_VOCAB
    2527
    2628#: Possible states of payments
     
    3638
    3739
     40class PaymentGatewayServicesSource(BasicSourceFactory):
     41    """A source that lists available payment services.
     42
     43    Suitable for forms etc. Token and value correspond to the name the
     44    respective IPaymentGatewayService utility is registered with.
     45    """
     46
     47    _services = None
     48
     49    @classmethod
     50    def services(cls):
     51        """Cache the services registered on startup.
     52
     53        We assume that services do not change after startup.
     54        """
     55        if cls._services is None:
     56            cls._services = dict(getUtilitiesFor(IPaymentGatewayService))
     57        return cls._services
     58
     59    def getValues(self):
     60        """Get payment gateway registration names.
     61        """
     62        return sorted(PaymentGatewayServicesSource.services().keys())
     63
     64    def getTitle(self, value):
     65        """Get title of the respective service, if it exists.
     66        """
     67        service = PaymentGatewayServicesSource.services().get(value, None)
     68        if service is not None:
     69            return service.title
     70
     71
    3872class IPaymentGatewayService(Interface):
    3973    """A financial gateway service.
     
    5185        required=True,
    5286        )
     87
     88    def create_payment(payer_id, payee_id, payment_item_id):
     89        """Create a payment.
     90        """
    5391
    5492
     
    105143    payed_item_id = schema.TextLine(
    106144        title=u'Payed Item ID',
     145        default=None,
     146        required=True,
     147        )
     148
     149    gateway_service = schema.Choice(
     150        title=u'Payment Gateway',
     151        description=u'Payment gateway that handles this transaction.',
     152        source=PaymentGatewayServicesSource(),
    107153        default=None,
    108154        required=True,
     
    188234    def approve():
    189235        "Approve an online payment and set to paid."
     236
     237
     238class IPayer(Interface):
     239    """A payer.
     240    """
     241    payer_id = schema.TextLine(
     242        title=u'Payer ID',
     243        required=True,
     244        )
     245
     246
     247class IPayee(Interface):
     248    """A person or institution being paid.
     249    """
     250    payee_id = schema.TextLine(
     251        title=u'Payee ID',
     252        required=True
     253        )
     254
     255
     256class IPaymentItem(Interface):
     257    """Something to sell.
     258    """
     259    item_id = schema.TextLine(
     260        title=u'Payment Item ID',
     261        required=True,
     262        )
     263
     264    title = schema.TextLine(
     265        title=u'Title',
     266        description=u'A short title of the good sold.',
     267        required=True,
     268        default=u'Unnamed'
     269        )
     270
     271    amount = schema.Decimal(
     272        title=u'Amount',
     273        description=u'Total amount, includung any taxes, fees, etc.',
     274        required=True,
     275        default=decimal.Decimal('0.00'),
     276        )
     277
     278    currency = schema.Choice(
     279        title=u'Currency',
     280        source=ISO_4217_CURRENCIES_VOCAB,
     281        required=True,
     282        default='USD',
     283        )
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/payments/tests/test_interfaces.py

    r12159 r12295  
    2020"""
    2121from zope.interface.verify import verifyClass, verifyObject
    22 from waeup.ikoba.payments.interfaces import IPaymentsContainer
     22from waeup.ikoba.payments.interfaces import (
     23    IPaymentsContainer, PaymentGatewayServicesSource,
     24    )
    2325from waeup.ikoba.payments.container import PaymentsContainer
    2426from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
     
    4850        self.assertRaises(
    4951            NotImplementedError, container.clear)
     52
     53    def test_payment_gateway_services_source(self):
     54        # the payment gateway services source provides a list of registered
     55        # payment gateways
     56        source = PaymentGatewayServicesSource()
     57        services = list(source)
     58        assert len(services) > 0
     59
     60    def test_payment_gateway_services_source_title(self):
     61        # we can get titles from gateway sources
     62        source = PaymentGatewayServicesSource()
     63        service1 = list(source)[0]
     64        title = source.factory.getTitle(service1)
     65        assert title != service1
     66        assert isinstance(title, basestring)
Note: See TracChangeset for help on using the changeset viewer.