1 | import grok |
---|
2 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
3 | from waeup.ikoba.browser.layout import IkobaEditFormPage, action |
---|
4 | from waeup.ikoba.payments.interfaces import ( |
---|
5 | IPaymentGatewayService, IPayment, IPayable, IPayer, IPayee,) |
---|
6 | from waeup.ikoba.payments.payment import ( |
---|
7 | Payment, get_payment, find_payable_from_payable_id, |
---|
8 | PaymentProviderServiceBase) |
---|
9 | |
---|
10 | |
---|
11 | grok.templatedir('templates') |
---|
12 | |
---|
13 | |
---|
14 | class DemoCreditcardPaymentService(PaymentProviderServiceBase): |
---|
15 | """A demo payment gateway service. |
---|
16 | |
---|
17 | This one supports credit card payments. |
---|
18 | """ |
---|
19 | grok.implements(IPaymentGatewayService) |
---|
20 | grok.name('demo_creditcard') |
---|
21 | |
---|
22 | title = _(u'Credit Card (Demo Payments)') |
---|
23 | |
---|
24 | def create_payment(self, payer, payable, payee=None): |
---|
25 | """Create a payment. |
---|
26 | """ |
---|
27 | if not IPayer.providedBy(payer): |
---|
28 | payer = IPayer(payer) |
---|
29 | if not IPayable.providedBy(payable): |
---|
30 | payable = IPayable(payable) |
---|
31 | if (payee is not None) and (not IPayee.providedBy(payee)): |
---|
32 | payee = IPayee(payee) |
---|
33 | payment = Payment(payer, payable, payee) |
---|
34 | payment.gateway_service = 'demo_creditcard' # must be grok.name above |
---|
35 | return payment |
---|
36 | |
---|
37 | def next_step(self, payment_id): |
---|
38 | """Tell where to go next. |
---|
39 | |
---|
40 | Returns (context, view_name). Both may be none. |
---|
41 | """ |
---|
42 | payment = get_payment(payment_id) |
---|
43 | if payment is None: |
---|
44 | return None, None |
---|
45 | return payment, 'demo_cc1' |
---|
46 | |
---|
47 | |
---|
48 | class CreditCardStep1(IkobaEditFormPage): |
---|
49 | grok.context(IPayment) |
---|
50 | grok.name('demo_cc1') |
---|
51 | # XXX: Use own permissions for payments |
---|
52 | grok.require('waeup.Authenticated') |
---|
53 | label = "Enter Credit Card Details" |
---|
54 | grok.template('demo_cc_step1') |
---|
55 | pnav = 4 |
---|
56 | |
---|
57 | def update(self): |
---|
58 | pass |
---|
59 | |
---|
60 | @action(_('Authorize Payment'), style="primary") |
---|
61 | def authorize(self, **data): |
---|
62 | print "AUTH!" |
---|
63 | |
---|
64 | @action(_('Cancel')) |
---|
65 | def cancel(self, **data): |
---|
66 | """Go back to the payable (if possible) or site home. |
---|
67 | """ |
---|
68 | payable_id = getattr(self.context, 'payable_id', '') |
---|
69 | payed_item = find_payable_from_payable_id(payable_id) |
---|
70 | self.flash(_("Payment cancelled.")) |
---|
71 | if payed_item is not None: |
---|
72 | # remove context/payment? |
---|
73 | target = self.url(payed_item) |
---|
74 | else: |
---|
75 | target = grok.getSite() |
---|
76 | self.redirect(self.url(target)) |
---|