[7195] | 1 | ## $Id: interfaces.py 12796 2015-03-19 15:13:27Z henrik $ |
---|
[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 |
---|
[12741] | 22 | from zope.component.interfaces import IObjectEvent |
---|
[12671] | 23 | from zope.interface import Interface, Attribute |
---|
[11949] | 24 | from waeup.ikoba.interfaces import ( |
---|
[12311] | 25 | IIkobaObject, SimpleIkobaVocabulary, ContextualDictSourceFactoryBase) |
---|
[11949] | 26 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
[12311] | 27 | from waeup.ikoba.payments.currencies import ISO_4217_CURRENCIES_VOCAB |
---|
[6861] | 28 | |
---|
[12311] | 29 | #: Possible states of payments |
---|
| 30 | STATE_UNPAID = 1 |
---|
[12741] | 31 | STATE_AWAITING_USER_CONFIRM = 2 |
---|
| 32 | STATE_AWAITING_GATEWAY_CONFIRM = 4 |
---|
| 33 | STATE_PAID = 64 |
---|
| 34 | STATE_FAILED = 128 |
---|
[12311] | 35 | |
---|
[11949] | 36 | payment_states = SimpleIkobaVocabulary( |
---|
[12311] | 37 | (_('Not yet paid'), STATE_UNPAID), |
---|
[12741] | 38 | (_('Waiting for user confirm'), STATE_AWAITING_USER_CONFIRM), |
---|
| 39 | (_('Waiting for verification'), STATE_AWAITING_GATEWAY_CONFIRM), |
---|
[12311] | 40 | (_('Paid'), STATE_PAID), |
---|
| 41 | (_('Failed'), STATE_FAILED), |
---|
[7627] | 42 | ) |
---|
| 43 | |
---|
[12311] | 44 | |
---|
[12741] | 45 | class IPaymentWaitingForGatewayEvent(IObjectEvent): |
---|
| 46 | """Fired when a payment starts waiting for verification. |
---|
| 47 | """ |
---|
[12749] | 48 | object = Attribute("""The payment started waiting.""") |
---|
[12741] | 49 | |
---|
| 50 | |
---|
| 51 | class IPaymentFinishedEvent(IObjectEvent): |
---|
| 52 | """Fired when a payment failed or succeeded. |
---|
| 53 | """ |
---|
| 54 | object = Attribute("""The payment finished.""") |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | class IPaymentAborted(IObjectEvent): |
---|
| 58 | """Fired when a payment was aborted before external transactions. |
---|
| 59 | """ |
---|
| 60 | object = Attribute("""The payment aborted.""") |
---|
| 61 | |
---|
| 62 | |
---|
[12311] | 63 | class PaymentGatewayServicesSource(BasicSourceFactory): |
---|
| 64 | """A source that lists available payment services. |
---|
| 65 | |
---|
| 66 | Suitable for forms etc. Token and value correspond to the name the |
---|
| 67 | respective IPaymentGatewayService utility is registered with. |
---|
| 68 | """ |
---|
| 69 | |
---|
| 70 | _services = None |
---|
| 71 | |
---|
| 72 | @classmethod |
---|
| 73 | def services(cls): |
---|
| 74 | """Cache the services registered on startup. |
---|
| 75 | |
---|
| 76 | We assume that services do not change after startup. |
---|
| 77 | """ |
---|
| 78 | if cls._services is None: |
---|
| 79 | cls._services = dict(getUtilitiesFor(IPaymentGatewayService)) |
---|
| 80 | return cls._services |
---|
| 81 | |
---|
| 82 | def getValues(self): |
---|
| 83 | """Get payment gateway registration names. |
---|
| 84 | """ |
---|
| 85 | return sorted(PaymentGatewayServicesSource.services().keys()) |
---|
| 86 | |
---|
| 87 | def getTitle(self, value): |
---|
| 88 | """Get title of the respective service, if it exists. |
---|
| 89 | """ |
---|
| 90 | service = PaymentGatewayServicesSource.services().get(value, None) |
---|
| 91 | if service is not None: |
---|
| 92 | return service.title |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | class IPaymentGatewayService(Interface): |
---|
| 96 | """A financial gateway service. |
---|
| 97 | |
---|
| 98 | Any gateway provider might provide several services. For instance |
---|
| 99 | payments by credit card, scratch card, bank transfer, etc. An |
---|
| 100 | `IPaymentGatewayService` represents one of those services. |
---|
| 101 | |
---|
| 102 | Payment services are normally registered as a named global |
---|
| 103 | utility. |
---|
| 104 | """ |
---|
| 105 | title = schema.TextLine( |
---|
| 106 | title=u'Title', |
---|
| 107 | description=u'Human readable name of gateway service.', |
---|
| 108 | required=True, |
---|
| 109 | ) |
---|
| 110 | |
---|
[12741] | 111 | def create_payment(payer, payable, payee): |
---|
[12311] | 112 | """Create a payment. |
---|
| 113 | |
---|
| 114 | For all parameters we expect an object, that implements |
---|
[12741] | 115 | `IPayer`, `IPayable`, or `IPayee` respectively. If not, |
---|
[12311] | 116 | then the given objects must be at least adaptable to the |
---|
| 117 | respective interface. |
---|
| 118 | |
---|
| 119 | Therfore you can pass in some `Customer` as long as there is |
---|
| 120 | some `IPayer` adapter for `Customer` objects defined. |
---|
| 121 | |
---|
| 122 | Returns an `IPayment` object. |
---|
| 123 | """ |
---|
| 124 | |
---|
[12741] | 125 | def next_step(payment_id): |
---|
| 126 | """Returns a payment (as context) and a view name. |
---|
[12311] | 127 | |
---|
[12741] | 128 | May result in (None, None). |
---|
| 129 | """ |
---|
| 130 | |
---|
| 131 | def store(payment): |
---|
| 132 | """Store `payment` in site. |
---|
| 133 | """ |
---|
| 134 | |
---|
| 135 | |
---|
[12671] | 136 | class IPaymentGatewayServicesLister(Interface): |
---|
| 137 | """A utility that lists all valid payment gateways. |
---|
| 138 | |
---|
| 139 | This is a subset of the available payment methods, as some might |
---|
| 140 | be disabled in some site. |
---|
| 141 | |
---|
| 142 | Register your own lister in customized sites! |
---|
| 143 | """ |
---|
| 144 | |
---|
| 145 | |
---|
[9405] | 146 | class PaymentCategorySource(ContextualDictSourceFactoryBase): |
---|
[9864] | 147 | """A payment category source delivers all categories of payments. |
---|
| 148 | |
---|
[9405] | 149 | """ |
---|
[11949] | 150 | #: name of dict to deliver from ikoba utils. |
---|
[9405] | 151 | DICT_NAME = 'PAYMENT_CATEGORIES' |
---|
[7627] | 152 | |
---|
[12311] | 153 | |
---|
[11949] | 154 | class IPaymentsContainer(IIkobaObject): |
---|
[6861] | 155 | """A container for all kind of payment objects. |
---|
| 156 | |
---|
| 157 | """ |
---|
[6864] | 158 | |
---|
[12311] | 159 | |
---|
[12671] | 160 | class ICreditCard(Interface): |
---|
| 161 | """A credit card. |
---|
| 162 | |
---|
| 163 | A credit card is connected to a Payer. |
---|
| 164 | """ |
---|
| 165 | credit_card_id = schema.TextLine( |
---|
| 166 | title=u'Internal Credit Card ID', |
---|
| 167 | required=True, |
---|
| 168 | ) |
---|
| 169 | |
---|
| 170 | |
---|
[12741] | 171 | class IPayableFinder(Interface): |
---|
| 172 | """Finds payables. |
---|
| 173 | |
---|
| 174 | For each type of content you want to make payable, you should |
---|
| 175 | define an IPayableFinder that can lookup payables in the |
---|
| 176 | site. |
---|
| 177 | |
---|
| 178 | This enables access from payments (which store payable ids only) |
---|
| 179 | to arbitrary content objects (for which a payable finder is |
---|
| 180 | registered under some name). |
---|
| 181 | |
---|
| 182 | The other way round (getting an id and other relevant data from |
---|
| 183 | any content object) is ensured by IPayable adapters. |
---|
| 184 | """ |
---|
| 185 | def get_payable_by_id(item_id): |
---|
| 186 | """Get an item by its Id, or none. |
---|
| 187 | """ |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | class IPayerFinder(Interface): |
---|
| 191 | """Finds payers. |
---|
| 192 | |
---|
| 193 | For each type of content you understand as payer, you should |
---|
[12749] | 194 | define an IPayerFinder that can lookup payers in the site. |
---|
[12741] | 195 | |
---|
| 196 | This enables access from payments (which store payer ids only) |
---|
| 197 | to arbitrary content objects (for which a payer finder is |
---|
| 198 | registered under some name. |
---|
| 199 | |
---|
| 200 | The other way round (getting an id and other relevant data from |
---|
| 201 | any content object) is ensured by IPayer adapters. |
---|
| 202 | """ |
---|
| 203 | def get_payer_by_id(item_id): |
---|
| 204 | """Get a payer by its Id, or none. |
---|
| 205 | """ |
---|
| 206 | |
---|
| 207 | |
---|
[12671] | 208 | class IPaymentItem(Interface): |
---|
| 209 | """Something to sell. |
---|
| 210 | """ |
---|
| 211 | title = schema.TextLine( |
---|
| 212 | title=u'Title', |
---|
| 213 | description=u'A short title of the good sold.', |
---|
| 214 | required=True, |
---|
| 215 | default=u'Unnamed' |
---|
| 216 | ) |
---|
| 217 | |
---|
| 218 | amount = schema.Decimal( |
---|
| 219 | title=u'Amount', |
---|
| 220 | description=u'Total amount, includung any taxes, fees, etc.', |
---|
| 221 | required=True, |
---|
| 222 | default=decimal.Decimal('0.00'), |
---|
| 223 | ) |
---|
| 224 | |
---|
| 225 | |
---|
[12741] | 226 | class IPayable(Interface): |
---|
| 227 | """Something that can be payed. |
---|
| 228 | |
---|
| 229 | Designed to serve as adapter. IPayables turn arbitrary content |
---|
| 230 | objects into something with a standarized interfaces for use with |
---|
| 231 | payments. |
---|
| 232 | |
---|
| 233 | While currency is important to tell about the amount currency, the |
---|
| 234 | total amount is computed on-demand from payment items. |
---|
| 235 | """ |
---|
| 236 | payable_id = schema.TextLine( |
---|
| 237 | title=u'ID of a payable', |
---|
| 238 | description=(u'It should be possible to lookup the payable id ' |
---|
| 239 | u'by some registered IPayableFinder later on'), |
---|
| 240 | required=True, |
---|
| 241 | readonly=True, |
---|
| 242 | ) |
---|
| 243 | |
---|
| 244 | title = schema.TextLine( |
---|
| 245 | title=u'Title', |
---|
| 246 | description=u'A short description of the payed item.', |
---|
| 247 | required=True, |
---|
| 248 | default=u'', |
---|
| 249 | readonly=True, |
---|
| 250 | ) |
---|
| 251 | |
---|
| 252 | currency = schema.Choice( |
---|
| 253 | title=u'Currency', |
---|
| 254 | source=ISO_4217_CURRENCIES_VOCAB, |
---|
| 255 | required=True, |
---|
| 256 | default='USD', |
---|
| 257 | readonly=True, |
---|
| 258 | ) |
---|
| 259 | |
---|
| 260 | payment_items = schema.Tuple( |
---|
| 261 | title=u'Tuple of IPaymentItems.', |
---|
| 262 | value_type=schema.Object( |
---|
| 263 | title=u'Payment Item', |
---|
| 264 | schema=IPaymentItem, |
---|
| 265 | ), |
---|
[12773] | 266 | required=True, |
---|
[12741] | 267 | default=(), |
---|
[12773] | 268 | readonly=False, |
---|
[12741] | 269 | ) |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | class IPayment(IIkobaObject): |
---|
[6864] | 273 | """A base representation of payments. |
---|
| 274 | |
---|
[12749] | 275 | In a payment, a payer pays someone (the payee) for something, the |
---|
[12311] | 276 | item to pay. |
---|
| 277 | |
---|
| 278 | We currently support only the case where one payer pays one payee |
---|
| 279 | for one item. The item might include taxes, handling, |
---|
| 280 | shipping, etc. |
---|
| 281 | |
---|
| 282 | As in RL any payment is actually performed by some financial |
---|
| 283 | service provider (like paypal, interswitch, etc.), each of which |
---|
| 284 | might provide several types of payments (credit card, scratch |
---|
| 285 | card, you name it). |
---|
| 286 | |
---|
| 287 | In Ikoba we call financial service providers 'gateway' and their |
---|
| 288 | types of services are handled as gateway types. Therefore PayPal |
---|
| 289 | handling a credit card payment is different from PayPal handling a |
---|
| 290 | regular PayPal account transaction. |
---|
| 291 | |
---|
| 292 | A payment can be approve()d, which means the act of paying was |
---|
| 293 | really performed. It can also fail for any reason, in which case |
---|
| 294 | we mark the payment 'failed'. |
---|
[6864] | 295 | """ |
---|
[12311] | 296 | payment_id = schema.TextLine( |
---|
[12792] | 297 | title=_(u'Payment Identifier'), |
---|
[12311] | 298 | default=None, |
---|
| 299 | required=True, |
---|
| 300 | ) |
---|
[6864] | 301 | |
---|
[12741] | 302 | title = schema.TextLine( |
---|
[12792] | 303 | title=_(u'Payment Description'), |
---|
[12741] | 304 | default=u'', |
---|
| 305 | required=True, |
---|
| 306 | ) |
---|
| 307 | |
---|
[12311] | 308 | payer_id = schema.TextLine( |
---|
[12792] | 309 | title=_(u'Payer'), |
---|
[12311] | 310 | default=None, |
---|
| 311 | required=True, |
---|
[6864] | 312 | ) |
---|
| 313 | |
---|
[12671] | 314 | payee_id = schema.TextLine( |
---|
[12792] | 315 | title=_(u'Payee'), |
---|
[12311] | 316 | default=None, |
---|
[12671] | 317 | required=False, |
---|
[12741] | 318 | ) |
---|
[6864] | 319 | |
---|
[12741] | 320 | payable_id = schema.TextLine( |
---|
[12792] | 321 | title=_(u'ID of item/good being paid'), |
---|
[12741] | 322 | default=None, |
---|
| 323 | required=False, |
---|
| 324 | ) |
---|
| 325 | |
---|
[12311] | 326 | gateway_service = schema.Choice( |
---|
[12792] | 327 | title=_(u'Payment Gateway'), |
---|
[12796] | 328 | description=_(u'Payment gateway that handles this transaction.'), |
---|
[12311] | 329 | source=PaymentGatewayServicesSource(), |
---|
| 330 | default=None, |
---|
| 331 | required=True, |
---|
[9984] | 332 | ) |
---|
| 333 | |
---|
[12311] | 334 | state = schema.Choice( |
---|
| 335 | title=_(u'Payment State'), |
---|
| 336 | default=STATE_UNPAID, |
---|
| 337 | vocabulary=payment_states, |
---|
| 338 | required=True, |
---|
[7020] | 339 | ) |
---|
| 340 | |
---|
[8170] | 341 | creation_date = schema.Datetime( |
---|
[12792] | 342 | title=_(u'Creation Date'), |
---|
[12311] | 343 | readonly=False, |
---|
| 344 | required=False, |
---|
[6864] | 345 | ) |
---|
| 346 | |
---|
[8170] | 347 | payment_date = schema.Datetime( |
---|
[12792] | 348 | title=_(u'Payment Date'), |
---|
[12311] | 349 | required=False, |
---|
| 350 | readonly=False, |
---|
[6864] | 351 | ) |
---|
| 352 | |
---|
[12792] | 353 | amount = schema.Decimal( |
---|
[12795] | 354 | title=_(u'Amount'), |
---|
[12796] | 355 | description=_(u'Total amount, includung any taxes, fees, etc.'), |
---|
[12792] | 356 | required=True, |
---|
[12795] | 357 | readonly=True, |
---|
[12792] | 358 | default=decimal.Decimal('0.00'), |
---|
| 359 | ) |
---|
| 360 | |
---|
[12671] | 361 | currency = schema.Choice( |
---|
[12792] | 362 | title=_(u'Currency'), |
---|
[12671] | 363 | source=ISO_4217_CURRENCIES_VOCAB, |
---|
[12311] | 364 | required=True, |
---|
[12671] | 365 | default='USD', |
---|
[6864] | 366 | ) |
---|
| 367 | |
---|
[12786] | 368 | payment_items = schema.Tuple( |
---|
[12796] | 369 | title=_(u'Tuple of IPaymentItems'), |
---|
[12786] | 370 | value_type=schema.Object( |
---|
[12796] | 371 | title=_(u'Payment Item'), |
---|
[12786] | 372 | schema=IPaymentItem, |
---|
| 373 | ), |
---|
| 374 | required=True, |
---|
| 375 | default=(), |
---|
| 376 | readonly=False, |
---|
| 377 | ) |
---|
[12779] | 378 | |
---|
[12311] | 379 | def approve(): |
---|
| 380 | """Approve a payment. |
---|
| 381 | |
---|
| 382 | The payment was approved and can now be considered payed. This |
---|
| 383 | kind of approvement means the final one (in case there are |
---|
| 384 | several instances to ask). |
---|
| 385 | """ |
---|
| 386 | |
---|
| 387 | def mark_failed(reason=None): |
---|
| 388 | """Mark the payment as failed. |
---|
| 389 | |
---|
| 390 | A failed payment was canceled due to technical problems, |
---|
| 391 | insufficient funds, etc. |
---|
| 392 | """ |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | class IPayer(Interface): |
---|
| 396 | """A payer. |
---|
| 397 | """ |
---|
| 398 | payer_id = schema.TextLine( |
---|
| 399 | title=u'Payer ID', |
---|
| 400 | required=True, |
---|
| 401 | ) |
---|
| 402 | |
---|
| 403 | first_name = schema.TextLine( |
---|
| 404 | title=u'First Name', |
---|
[12319] | 405 | required=True, |
---|
[12311] | 406 | ) |
---|
| 407 | |
---|
| 408 | last_name = schema.TextLine( |
---|
| 409 | title=u'Last Name', |
---|
[12319] | 410 | required=True, |
---|
[12311] | 411 | ) |
---|
| 412 | |
---|
| 413 | |
---|
| 414 | class IPayee(Interface): |
---|
| 415 | """A person or institution being paid. |
---|
| 416 | """ |
---|
| 417 | payee_id = schema.TextLine( |
---|
| 418 | title=u'Payee ID', |
---|
| 419 | required=True |
---|
| 420 | ) |
---|