Ignore:
Timestamp:
12 Mar 2015, 05:23:47 (10 years ago)
Author:
uli
Message:

Let cc payments basically run.

File:
1 edited

Legend:

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

    r12736 r12740  
     1import datetime
    12import grok
     3import random
     4import re
     5from zope.event import notify
    26from waeup.ikoba.interfaces import MessageFactory as _
    37from waeup.ikoba.browser.layout import IkobaEditFormPage, action
    48from waeup.ikoba.payments.interfaces import (
    5     IPaymentGatewayService, IPayment, IPayable, IPayer, IPayee,)
     9    IPaymentGatewayService, IPayment, IPayable, IPayer, IPayee,
     10    STATE_AWAITING_GATEWAY_CONFIRM, STATE_PAID)
    611from waeup.ikoba.payments.payment import (
    7     Payment, get_payment, find_payable_from_payable_id,
    8     PaymentProviderServiceBase)
     12    Payment, get_payment, find_payable_from_payable_id, format_amount,
     13    find_payer_from_payer_id, PaymentProviderServiceBase,
     14    PaymentWaitingForGatewayEvent, PaymentFinishedEvent)
    915
    1016
     
    1622    'Do you really want to submit?'
    1723)
     24
     25
     26RE_CC_NUMBER = re.compile('^[0-9]{9,25}$')
     27RE_CSC = re.compile('^[0-9]{3,4}$')
    1828
    1929
     
    4959        if payment is None:
    5060            return None, None
     61        if payment.state == STATE_AWAITING_GATEWAY_CONFIRM:
     62            return payment, 'demo_cc2'
    5163        return payment, 'demo_cc1'
    5264
     
    6173    pnav = 4
    6274
    63     def update(self):
    64         pass
     75    def validate_form(self):
     76        fields = ['first_name', 'last_name', 'cc_number', 'csc', 'exp_date']
     77        cls = 'form-group'
     78        if len(self.request.form):
     79            cls += ' has-success'
     80        result = dict([(x, cls) for x in fields])
     81        if not len(self.request.form):
     82            return True, result
     83        err = 'form-group has-error'
     84        if not self.first_name:
     85            result['first_name'] = err
     86        if not self.last_name:
     87            result['last_name'] = err
     88        if not RE_CC_NUMBER.match(self.cc_number):
     89            result['cc_number'] = err
     90        if not RE_CSC.match(self.csc):
     91            result['csc'] = err
     92        if err in result.values():
     93            return False, result
     94        return True, result
     95
     96    def update(self, first_name=None, last_name=None, cc_number=None,
     97               month=None, year=None):
     98        self.payer = IPayer(find_payer_from_payer_id(self.context.payer_id))
     99        self.payable = IPayable(find_payable_from_payable_id(
     100            self.context.payable_id))
     101        form = self.request.form
     102        self.first_name = form.get('first_name', self.payer.first_name)
     103        self.last_name = form.get('last_name', self.payer.last_name)
     104        self.cc_number = form.get('cc_number', '')
     105        self.month = int(form.get('exp_month', datetime.datetime.now().month))
     106        self.year = int(form.get('exp_year', datetime.datetime.now().year))
     107        self.csc = form.get('csc', '')
     108        self.amount = format_amount(
     109            self.context.amount, self.context.currency)
     110        self.months = ''.join([
     111            '<option%s>%s</option>' % (
     112                (x == self.month) and ' selected="selected"' or '',
     113                x) for x in range(1, 13)])
     114        self.years = ''.join([
     115            '<option%s>%s</option>' % (
     116                (x == self.year + 1) and ' selected="selected"' or '',
     117                x) for x in range(self.year - 1, self.year + 15)])
     118        self.ok, self.validations = self.validate_form()
    65119
    66120    @action(_('Authorize Payment'), warning=WARN_FINAL_SUBMIT,
    67121            style="primary")
    68122    def authorize(self, **data):
    69         print "AUTH!"
     123        if not self.ok:
     124            self.flash(_("Please review (red) entries below!"),
     125                       type='warning')
     126            return
     127        # XXX: payment really started, do lots of logging
     128        self.context.state = STATE_AWAITING_GATEWAY_CONFIRM
     129        notify(PaymentWaitingForGatewayEvent(self.context))
     130        self.redirect(self.url(self.context, 'demo_cc2'))
     131        return
    70132
    71133    @action(_('Cancel'))
     
    78140        if payed_item is not None:
    79141            # remove context/payment?
    80             target = self.url(payed_item)
     142            target = payed_item
    81143        else:
    82144            target = grok.getSite()
    83145        self.redirect(self.url(target))
     146
     147
     148class CreditCardStep2(IkobaEditFormPage):
     149    grok.context(IPayment)
     150    grok.name('demo_cc2')
     151    # XXX: Use own permissions for payments
     152    grok.require('waeup.Authenticated')
     153    label = "&nbsp;"
     154    grok.template('demo_cc_step2')
     155    pnav = 4
     156
     157    def update(self):
     158        cnt = int(self.request.form.get('cnt', '0'))
     159        self.cnt = cnt + 1
     160        threshold = random.choice(range(10))
     161        self.success = False
     162        # HERE WOULD WE REALLY ASK FOR VERIFICATION
     163        if threshold <= cnt:
     164            self.success = True
     165            self.flash(_("Your payment was finished successfully."))
     166        if self.request.form.get('SUBMIT', None):
     167            self.renew()
     168
     169    def renew(self):
     170        if not self.success:
     171            return
     172        payable_id = getattr(self.context, 'payable_id', '')
     173        payed_item = find_payable_from_payable_id(payable_id)
     174        self.context.state = STATE_PAID
     175        notify(PaymentFinishedEvent(self.context))
     176        self.redirect(self.url(payed_item))
Note: See TracChangeset for help on using the changeset viewer.