[7195] | 1 | ## $Id: interfaces.py 12319 2014-12-26 08:37:55Z 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 |
---|
| 22 | from zope.interface import Interface |
---|
[11949] | 23 | from waeup.ikoba.interfaces import ( |
---|
[12311] | 24 | IIkobaObject, SimpleIkobaVocabulary, ContextualDictSourceFactoryBase) |
---|
[11949] | 25 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
[12311] | 26 | from waeup.ikoba.payments.currencies import ISO_4217_CURRENCIES_VOCAB |
---|
[6861] | 27 | |
---|
[12311] | 28 | #: Possible states of payments |
---|
| 29 | STATE_UNPAID = 1 |
---|
| 30 | STATE_PAID = 2 |
---|
| 31 | STATE_FAILED = 4 |
---|
| 32 | |
---|
[11949] | 33 | payment_states = SimpleIkobaVocabulary( |
---|
[12311] | 34 | (_('Not yet paid'), STATE_UNPAID), |
---|
| 35 | (_('Paid'), STATE_PAID), |
---|
| 36 | (_('Failed'), STATE_FAILED), |
---|
[7627] | 37 | ) |
---|
| 38 | |
---|
[12311] | 39 | |
---|
| 40 | class 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 | |
---|
| 72 | class IPaymentGatewayService(Interface): |
---|
| 73 | """A financial gateway service. |
---|
| 74 | |
---|
| 75 | Any gateway provider might provide several services. For instance |
---|
| 76 | payments by credit card, scratch card, bank transfer, etc. An |
---|
| 77 | `IPaymentGatewayService` represents one of those services. |
---|
| 78 | |
---|
| 79 | Payment services are normally registered as a named global |
---|
| 80 | utility. |
---|
| 81 | """ |
---|
| 82 | title = schema.TextLine( |
---|
| 83 | title=u'Title', |
---|
| 84 | description=u'Human readable name of gateway service.', |
---|
| 85 | required=True, |
---|
| 86 | ) |
---|
| 87 | |
---|
| 88 | def create_payment(payer, payment_item, payee): |
---|
| 89 | """Create a payment. |
---|
| 90 | |
---|
| 91 | For all parameters we expect an object, that implements |
---|
| 92 | `IPayer`, `IPaymentItem`, or `IPayee` respectively. If not, |
---|
| 93 | then the given objects must be at least adaptable to the |
---|
| 94 | respective interface. |
---|
| 95 | |
---|
| 96 | Therfore you can pass in some `Customer` as long as there is |
---|
| 97 | some `IPayer` adapter for `Customer` objects defined. |
---|
| 98 | |
---|
| 99 | Returns an `IPayment` object. |
---|
| 100 | """ |
---|
| 101 | |
---|
| 102 | |
---|
[9405] | 103 | class PaymentCategorySource(ContextualDictSourceFactoryBase): |
---|
[9864] | 104 | """A payment category source delivers all categories of payments. |
---|
| 105 | |
---|
[9405] | 106 | """ |
---|
[11949] | 107 | #: name of dict to deliver from ikoba utils. |
---|
[9405] | 108 | DICT_NAME = 'PAYMENT_CATEGORIES' |
---|
[7627] | 109 | |
---|
[12311] | 110 | |
---|
[11949] | 111 | class IPaymentsContainer(IIkobaObject): |
---|
[6861] | 112 | """A container for all kind of payment objects. |
---|
| 113 | |
---|
| 114 | """ |
---|
[6864] | 115 | |
---|
[12311] | 116 | |
---|
[11949] | 117 | class IPayment(IIkobaObject): |
---|
[6864] | 118 | """A base representation of payments. |
---|
| 119 | |
---|
[12311] | 120 | In a payment, a payer payes someone (the payee) for something, the |
---|
| 121 | item to pay. |
---|
| 122 | |
---|
| 123 | We currently support only the case where one payer pays one payee |
---|
| 124 | for one item. The item might include taxes, handling, |
---|
| 125 | shipping, etc. |
---|
| 126 | |
---|
| 127 | As in RL any payment is actually performed by some financial |
---|
| 128 | service provider (like paypal, interswitch, etc.), each of which |
---|
| 129 | might provide several types of payments (credit card, scratch |
---|
| 130 | card, you name it). |
---|
| 131 | |
---|
| 132 | In Ikoba we call financial service providers 'gateway' and their |
---|
| 133 | types of services are handled as gateway types. Therefore PayPal |
---|
| 134 | handling a credit card payment is different from PayPal handling a |
---|
| 135 | regular PayPal account transaction. |
---|
| 136 | |
---|
| 137 | A payment can be approve()d, which means the act of paying was |
---|
| 138 | really performed. It can also fail for any reason, in which case |
---|
| 139 | we mark the payment 'failed'. |
---|
[6864] | 140 | """ |
---|
[12311] | 141 | payment_id = schema.TextLine( |
---|
| 142 | title=u'Payment Identifier', |
---|
| 143 | default=None, |
---|
| 144 | required=True, |
---|
| 145 | ) |
---|
[6864] | 146 | |
---|
[12311] | 147 | payer_id = schema.TextLine( |
---|
| 148 | title=u'Payer', |
---|
| 149 | default=None, |
---|
| 150 | required=True, |
---|
[6864] | 151 | ) |
---|
| 152 | |
---|
[12311] | 153 | payed_item_id = schema.TextLine( |
---|
| 154 | title=u'Payed Item ID', |
---|
| 155 | default=None, |
---|
| 156 | required=True, |
---|
[6864] | 157 | ) |
---|
| 158 | |
---|
[12311] | 159 | gateway_service = schema.Choice( |
---|
| 160 | title=u'Payment Gateway', |
---|
| 161 | description=u'Payment gateway that handles this transaction.', |
---|
| 162 | source=PaymentGatewayServicesSource(), |
---|
| 163 | default=None, |
---|
| 164 | required=True, |
---|
[9984] | 165 | ) |
---|
| 166 | |
---|
[12311] | 167 | state = schema.Choice( |
---|
| 168 | title=_(u'Payment State'), |
---|
| 169 | default=STATE_UNPAID, |
---|
| 170 | vocabulary=payment_states, |
---|
| 171 | required=True, |
---|
[7020] | 172 | ) |
---|
| 173 | |
---|
[8170] | 174 | creation_date = schema.Datetime( |
---|
[12311] | 175 | title=_(u'Creation Datetime'), |
---|
| 176 | readonly=False, |
---|
| 177 | required=False, |
---|
[6864] | 178 | ) |
---|
| 179 | |
---|
[8170] | 180 | payment_date = schema.Datetime( |
---|
[12311] | 181 | title=_(u'Payment Datetime'), |
---|
| 182 | required=False, |
---|
| 183 | readonly=False, |
---|
[6864] | 184 | ) |
---|
| 185 | |
---|
[12311] | 186 | amount = schema.Decimal( |
---|
| 187 | title=_(u'Amount'), |
---|
| 188 | description=_( |
---|
| 189 | 'The overall sum payed, including all taxes fees, etc.'), |
---|
| 190 | default=decimal.Decimal("0.00"), |
---|
| 191 | required=True, |
---|
| 192 | readonly=False, |
---|
[6864] | 193 | ) |
---|
| 194 | |
---|
[12311] | 195 | def approve(): |
---|
| 196 | """Approve a payment. |
---|
| 197 | |
---|
| 198 | The payment was approved and can now be considered payed. This |
---|
| 199 | kind of approvement means the final one (in case there are |
---|
| 200 | several instances to ask). |
---|
| 201 | """ |
---|
| 202 | |
---|
| 203 | def mark_failed(reason=None): |
---|
| 204 | """Mark the payment as failed. |
---|
| 205 | |
---|
| 206 | A failed payment was canceled due to technical problems, |
---|
| 207 | insufficient funds, etc. |
---|
| 208 | """ |
---|
| 209 | |
---|
| 210 | |
---|
[6864] | 211 | class IOnlinePayment(IPayment): |
---|
| 212 | """A payment via payment gateways. |
---|
| 213 | |
---|
| 214 | """ |
---|
| 215 | |
---|
[6930] | 216 | ac = schema.TextLine( |
---|
[12311] | 217 | title=_(u'Activation Code'), |
---|
| 218 | default=None, |
---|
| 219 | required=False, |
---|
| 220 | readonly=False, |
---|
[6930] | 221 | ) |
---|
| 222 | |
---|
[7927] | 223 | r_amount_approved = schema.Float( |
---|
[12311] | 224 | title=_(u'Response Amount Approved'), |
---|
| 225 | default=0.0, |
---|
| 226 | required=False, |
---|
| 227 | readonly=False, |
---|
[6864] | 228 | ) |
---|
| 229 | |
---|
| 230 | r_code = schema.TextLine( |
---|
[12311] | 231 | title=_(u'Response Code'), |
---|
| 232 | default=None, |
---|
| 233 | required=False, |
---|
| 234 | readonly=False, |
---|
[6864] | 235 | ) |
---|
[8420] | 236 | |
---|
[8429] | 237 | r_desc = schema.TextLine( |
---|
[12311] | 238 | title=_(u'Response Description'), |
---|
| 239 | default=None, |
---|
| 240 | required=False, |
---|
| 241 | readonly=False, |
---|
[8429] | 242 | ) |
---|
| 243 | |
---|
[8420] | 244 | def approve(): |
---|
| 245 | "Approve an online payment and set to paid." |
---|
[12311] | 246 | |
---|
| 247 | |
---|
| 248 | class ICreditCard(Interface): |
---|
| 249 | """A credit card. |
---|
| 250 | |
---|
| 251 | A credit card is connected to a Payer. |
---|
| 252 | """ |
---|
| 253 | credit_card_id = schema.TextLine( |
---|
| 254 | title=u'Internal Credit Card ID', |
---|
| 255 | required=True, |
---|
| 256 | ) |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | class IPayer(Interface): |
---|
| 260 | """A payer. |
---|
| 261 | """ |
---|
| 262 | payer_id = schema.TextLine( |
---|
| 263 | title=u'Payer ID', |
---|
| 264 | required=True, |
---|
| 265 | ) |
---|
| 266 | |
---|
| 267 | first_name = schema.TextLine( |
---|
| 268 | title=u'First Name', |
---|
[12319] | 269 | required=True, |
---|
[12311] | 270 | ) |
---|
| 271 | |
---|
| 272 | last_name = schema.TextLine( |
---|
| 273 | title=u'Last Name', |
---|
[12319] | 274 | required=True, |
---|
[12311] | 275 | ) |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | class IPayee(Interface): |
---|
| 279 | """A person or institution being paid. |
---|
| 280 | """ |
---|
| 281 | payee_id = schema.TextLine( |
---|
| 282 | title=u'Payee ID', |
---|
| 283 | required=True |
---|
| 284 | ) |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | class IPaymentItem(Interface): |
---|
| 288 | """Something to sell. |
---|
| 289 | """ |
---|
| 290 | item_id = schema.TextLine( |
---|
| 291 | title=u'Payment Item ID', |
---|
| 292 | required=True, |
---|
| 293 | ) |
---|
| 294 | |
---|
| 295 | title = schema.TextLine( |
---|
| 296 | title=u'Title', |
---|
| 297 | description=u'A short title of the good sold.', |
---|
| 298 | required=True, |
---|
| 299 | default=u'Unnamed' |
---|
| 300 | ) |
---|
| 301 | |
---|
| 302 | amount = schema.Decimal( |
---|
| 303 | title=u'Amount', |
---|
| 304 | description=u'Total amount, includung any taxes, fees, etc.', |
---|
| 305 | required=True, |
---|
| 306 | default=decimal.Decimal('0.00'), |
---|
| 307 | ) |
---|
| 308 | |
---|
| 309 | currency = schema.Choice( |
---|
| 310 | title=u'Currency', |
---|
| 311 | source=ISO_4217_CURRENCIES_VOCAB, |
---|
| 312 | required=True, |
---|
| 313 | default='USD', |
---|
| 314 | ) |
---|