[7195] | 1 | ## $Id: interfaces.py 12671 2015-03-06 23:12:36Z uli $ |
---|
[6861] | 2 | ## |
---|
[7195] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[12311] | 18 | import decimal |
---|
| 19 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
[6864] | 20 | from zope import schema |
---|
[12311] | 21 | from zope.component import getUtilitiesFor |
---|
[12671] | 22 | from zope.container.interfaces import IContainer |
---|
| 23 | from zope.container.constraints import contains |
---|
| 24 | from zope.interface import Interface, Attribute |
---|
[11949] | 25 | from waeup.ikoba.interfaces import ( |
---|
[12311] | 26 | IIkobaObject, SimpleIkobaVocabulary, ContextualDictSourceFactoryBase) |
---|
[11949] | 27 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
[12311] | 28 | from waeup.ikoba.payments.currencies import ISO_4217_CURRENCIES_VOCAB |
---|
[6861] | 29 | |
---|
[12311] | 30 | #: Possible states of payments |
---|
| 31 | STATE_UNPAID = 1 |
---|
| 32 | STATE_PAID = 2 |
---|
| 33 | STATE_FAILED = 4 |
---|
| 34 | |
---|
[11949] | 35 | payment_states = SimpleIkobaVocabulary( |
---|
[12311] | 36 | (_('Not yet paid'), STATE_UNPAID), |
---|
| 37 | (_('Paid'), STATE_PAID), |
---|
| 38 | (_('Failed'), STATE_FAILED), |
---|
[7627] | 39 | ) |
---|
| 40 | |
---|
[12311] | 41 | |
---|
| 42 | class PaymentGatewayServicesSource(BasicSourceFactory): |
---|
| 43 | """A source that lists available payment services. |
---|
| 44 | |
---|
| 45 | Suitable for forms etc. Token and value correspond to the name the |
---|
| 46 | respective IPaymentGatewayService utility is registered with. |
---|
| 47 | """ |
---|
| 48 | |
---|
| 49 | _services = None |
---|
| 50 | |
---|
| 51 | @classmethod |
---|
| 52 | def services(cls): |
---|
| 53 | """Cache the services registered on startup. |
---|
| 54 | |
---|
| 55 | We assume that services do not change after startup. |
---|
| 56 | """ |
---|
| 57 | if cls._services is None: |
---|
| 58 | cls._services = dict(getUtilitiesFor(IPaymentGatewayService)) |
---|
| 59 | return cls._services |
---|
| 60 | |
---|
| 61 | def getValues(self): |
---|
| 62 | """Get payment gateway registration names. |
---|
| 63 | """ |
---|
| 64 | return sorted(PaymentGatewayServicesSource.services().keys()) |
---|
| 65 | |
---|
| 66 | def getTitle(self, value): |
---|
| 67 | """Get title of the respective service, if it exists. |
---|
| 68 | """ |
---|
| 69 | service = PaymentGatewayServicesSource.services().get(value, None) |
---|
| 70 | if service is not None: |
---|
| 71 | return service.title |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | class IPaymentGatewayService(Interface): |
---|
| 75 | """A financial gateway service. |
---|
| 76 | |
---|
| 77 | Any gateway provider might provide several services. For instance |
---|
| 78 | payments by credit card, scratch card, bank transfer, etc. An |
---|
| 79 | `IPaymentGatewayService` represents one of those services. |
---|
| 80 | |
---|
| 81 | Payment services are normally registered as a named global |
---|
| 82 | utility. |
---|
| 83 | """ |
---|
| 84 | title = schema.TextLine( |
---|
| 85 | title=u'Title', |
---|
| 86 | description=u'Human readable name of gateway service.', |
---|
| 87 | required=True, |
---|
| 88 | ) |
---|
| 89 | |
---|
[12671] | 90 | def create_payment(payer, payment_item_list, payee): |
---|
[12311] | 91 | """Create a payment. |
---|
| 92 | |
---|
| 93 | For all parameters we expect an object, that implements |
---|
| 94 | `IPayer`, `IPaymentItem`, or `IPayee` respectively. If not, |
---|
| 95 | then the given objects must be at least adaptable to the |
---|
| 96 | respective interface. |
---|
| 97 | |
---|
| 98 | Therfore you can pass in some `Customer` as long as there is |
---|
| 99 | some `IPayer` adapter for `Customer` objects defined. |
---|
| 100 | |
---|
| 101 | Returns an `IPayment` object. |
---|
| 102 | """ |
---|
| 103 | |
---|
| 104 | |
---|
[12671] | 105 | class IPaymentGatewayServicesLister(Interface): |
---|
| 106 | """A utility that lists all valid payment gateways. |
---|
| 107 | |
---|
| 108 | This is a subset of the available payment methods, as some might |
---|
| 109 | be disabled in some site. |
---|
| 110 | |
---|
| 111 | Register your own lister in customized sites! |
---|
| 112 | """ |
---|
| 113 | |
---|
| 114 | |
---|
[9405] | 115 | class PaymentCategorySource(ContextualDictSourceFactoryBase): |
---|
[9864] | 116 | """A payment category source delivers all categories of payments. |
---|
| 117 | |
---|
[9405] | 118 | """ |
---|
[11949] | 119 | #: name of dict to deliver from ikoba utils. |
---|
[9405] | 120 | DICT_NAME = 'PAYMENT_CATEGORIES' |
---|
[7627] | 121 | |
---|
[12311] | 122 | |
---|
[11949] | 123 | class IPaymentsContainer(IIkobaObject): |
---|
[6861] | 124 | """A container for all kind of payment objects. |
---|
| 125 | |
---|
| 126 | """ |
---|
[6864] | 127 | |
---|
[12311] | 128 | |
---|
[12671] | 129 | class ICreditCard(Interface): |
---|
| 130 | """A credit card. |
---|
| 131 | |
---|
| 132 | A credit card is connected to a Payer. |
---|
| 133 | """ |
---|
| 134 | credit_card_id = schema.TextLine( |
---|
| 135 | title=u'Internal Credit Card ID', |
---|
| 136 | required=True, |
---|
| 137 | ) |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | class IPaymentItem(Interface): |
---|
| 141 | """Something to sell. |
---|
| 142 | """ |
---|
| 143 | item_id = schema.TextLine( |
---|
| 144 | title=u'Payment Item ID', |
---|
| 145 | required=True, |
---|
| 146 | ) |
---|
| 147 | |
---|
| 148 | title = schema.TextLine( |
---|
| 149 | title=u'Title', |
---|
| 150 | description=u'A short title of the good sold.', |
---|
| 151 | required=True, |
---|
| 152 | default=u'Unnamed' |
---|
| 153 | ) |
---|
| 154 | |
---|
| 155 | amount = schema.Decimal( |
---|
| 156 | title=u'Amount', |
---|
| 157 | description=u'Total amount, includung any taxes, fees, etc.', |
---|
| 158 | required=True, |
---|
| 159 | default=decimal.Decimal('0.00'), |
---|
| 160 | ) |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | class IPayment(IContainer): |
---|
[6864] | 164 | """A base representation of payments. |
---|
| 165 | |
---|
[12311] | 166 | In a payment, a payer payes someone (the payee) for something, the |
---|
| 167 | item to pay. |
---|
| 168 | |
---|
| 169 | We currently support only the case where one payer pays one payee |
---|
| 170 | for one item. The item might include taxes, handling, |
---|
| 171 | shipping, etc. |
---|
| 172 | |
---|
| 173 | As in RL any payment is actually performed by some financial |
---|
| 174 | service provider (like paypal, interswitch, etc.), each of which |
---|
| 175 | might provide several types of payments (credit card, scratch |
---|
| 176 | card, you name it). |
---|
| 177 | |
---|
| 178 | In Ikoba we call financial service providers 'gateway' and their |
---|
| 179 | types of services are handled as gateway types. Therefore PayPal |
---|
| 180 | handling a credit card payment is different from PayPal handling a |
---|
| 181 | regular PayPal account transaction. |
---|
| 182 | |
---|
| 183 | A payment can be approve()d, which means the act of paying was |
---|
| 184 | really performed. It can also fail for any reason, in which case |
---|
| 185 | we mark the payment 'failed'. |
---|
[6864] | 186 | """ |
---|
[12671] | 187 | contains(IPaymentItem) |
---|
| 188 | |
---|
[12311] | 189 | payment_id = schema.TextLine( |
---|
| 190 | title=u'Payment Identifier', |
---|
| 191 | default=None, |
---|
| 192 | required=True, |
---|
| 193 | ) |
---|
[6864] | 194 | |
---|
[12311] | 195 | payer_id = schema.TextLine( |
---|
| 196 | title=u'Payer', |
---|
| 197 | default=None, |
---|
| 198 | required=True, |
---|
[6864] | 199 | ) |
---|
| 200 | |
---|
[12671] | 201 | payee_id = schema.TextLine( |
---|
| 202 | title=u'Payee', |
---|
[12311] | 203 | default=None, |
---|
[12671] | 204 | required=False, |
---|
| 205 | ) |
---|
[6864] | 206 | |
---|
[12311] | 207 | gateway_service = schema.Choice( |
---|
| 208 | title=u'Payment Gateway', |
---|
| 209 | description=u'Payment gateway that handles this transaction.', |
---|
| 210 | source=PaymentGatewayServicesSource(), |
---|
| 211 | default=None, |
---|
| 212 | required=True, |
---|
[9984] | 213 | ) |
---|
| 214 | |
---|
[12311] | 215 | state = schema.Choice( |
---|
| 216 | title=_(u'Payment State'), |
---|
| 217 | default=STATE_UNPAID, |
---|
| 218 | vocabulary=payment_states, |
---|
| 219 | required=True, |
---|
[7020] | 220 | ) |
---|
| 221 | |
---|
[8170] | 222 | creation_date = schema.Datetime( |
---|
[12311] | 223 | title=_(u'Creation Datetime'), |
---|
| 224 | readonly=False, |
---|
| 225 | required=False, |
---|
[6864] | 226 | ) |
---|
| 227 | |
---|
[8170] | 228 | payment_date = schema.Datetime( |
---|
[12311] | 229 | title=_(u'Payment Datetime'), |
---|
| 230 | required=False, |
---|
| 231 | readonly=False, |
---|
[6864] | 232 | ) |
---|
| 233 | |
---|
[12671] | 234 | currency = schema.Choice( |
---|
| 235 | title=u'Currency', |
---|
| 236 | source=ISO_4217_CURRENCIES_VOCAB, |
---|
[12311] | 237 | required=True, |
---|
[12671] | 238 | default='USD', |
---|
[6864] | 239 | ) |
---|
| 240 | |
---|
[12671] | 241 | amount = Attribute("Sum of amounts of items contained") |
---|
| 242 | |
---|
[12311] | 243 | def approve(): |
---|
| 244 | """Approve a payment. |
---|
| 245 | |
---|
| 246 | The payment was approved and can now be considered payed. This |
---|
| 247 | kind of approvement means the final one (in case there are |
---|
| 248 | several instances to ask). |
---|
| 249 | """ |
---|
| 250 | |
---|
| 251 | def mark_failed(reason=None): |
---|
| 252 | """Mark the payment as failed. |
---|
| 253 | |
---|
| 254 | A failed payment was canceled due to technical problems, |
---|
| 255 | insufficient funds, etc. |
---|
| 256 | """ |
---|
| 257 | |
---|
[12671] | 258 | def add_payment_item(item): |
---|
| 259 | """Payments contain payment items. |
---|
[12311] | 260 | |
---|
[12671] | 261 | Add one |
---|
| 262 | """ |
---|
[6864] | 263 | |
---|
| 264 | |
---|
[12311] | 265 | class IPayer(Interface): |
---|
| 266 | """A payer. |
---|
| 267 | """ |
---|
| 268 | payer_id = schema.TextLine( |
---|
| 269 | title=u'Payer ID', |
---|
| 270 | required=True, |
---|
| 271 | ) |
---|
| 272 | |
---|
| 273 | first_name = schema.TextLine( |
---|
| 274 | title=u'First Name', |
---|
[12319] | 275 | required=True, |
---|
[12311] | 276 | ) |
---|
| 277 | |
---|
| 278 | last_name = schema.TextLine( |
---|
| 279 | title=u'Last Name', |
---|
[12319] | 280 | required=True, |
---|
[12311] | 281 | ) |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | class IPayee(Interface): |
---|
| 285 | """A person or institution being paid. |
---|
| 286 | """ |
---|
| 287 | payee_id = schema.TextLine( |
---|
| 288 | title=u'Payee ID', |
---|
| 289 | required=True |
---|
| 290 | ) |
---|