__init__.py 4.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2018  Hugo Levy-Falk
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
# Custom Payment methods

When creating an invoice with a classic payment method, the creation view calls
the `end_payment` method of the `Payment` object of the invoice. This method
checks for a payment method associated to the `Payment` and if nothing is
found, adds a message for payment confirmation and redirects the user towards
their profil page. This is fine for most of the payment method, but you might
want to define custom payment methods. As an example for negociating with an
other server for online payment or updating some fields in your models.

# Defining a custom payment method
To define a custom payment method, you can add a Python module to
`cotisations/payment_methods/`. This module should be organized like
a Django application.
As an example, if you want to add the payment method `foo`.

## Basic

The first thing to do is to create a `foo` Python module with a `models.py`.

```
payment_methods
├── foo
│   ├── __init__.py
│   └── models.py
├── forms.py
├── __init__.py
├── mixins.py
└── urls.py
```

Then, in `models.py` you could add a model like this :
```python
from django.db import models

from cotisations.models import Paiement
from cotisations.payment_methods.mixins import PaymentMethodMixin


# The `PaymentMethodMixin` defines the default `end_payment`
class FooPayment(PaymentMethodMixin, models.Model):

    # This field is required, it is used by `Paiement` in order to
    # determine if a payment method is associated to it.
    payment = models.OneToOneField(
        Paiement,
        on_delete=models.CASCADE,
        related_name='payment_method',
        editable=False
    )
```

And in `__init__.py` :
```python
from . import models
NAME = "FOO" # Name displayed when you crate a payment type
PaymentMethod = models.FooPayment # You must define this alias
```

Then you just have to register your payment method in
`payment_methods/__init__.py` in the `PAYMENT_METHODS` list :

```
from . import ... # Some existing imports
from . import foo

PAYMENT_METHODS = [
    # Some already registered payment methods...
    foo
]
```

And... that's it, you can use your new payment method after running
`makemigrations` and `migrate`.

But this payment method is not really usefull, since it does noting !

## A payment method which does something

You have to redefine the `end_payment` method. Here is its prototype :

```python
def end_payment(self, invoice, request):
    pass
```

With `invoice` the invoice being created and `request` the request which
created it. This method has to return an HttpResponse-like object.

## Additional views

You can add specific urls for your payment method like in any django app. To
register these urls, modify `payment_methods/urls.py`.

## Alter the `Paiement` object after creation

You can do that by adding a `alter_payment(self, payment)`
method to your model.

## Validate the creation field

You may want to perform some additionals verifications on the form
creating the payment. You can do that by adding a `valid_form(self, form)`
method to your model, where `form` is an instance of
`cotisations.payment_methods.forms.PaymentMethodForm`.
"""


130
from . import comnpay, cheque, balance, urls
131 132 133 134

PAYMENT_METHODS = [
    comnpay,
    cheque,
135
    balance,
136
]