add code to compute fee based on the number of documents

This commit is contained in:
Frédéric Péters 2017-05-31 09:52:16 +02:00
parent 8c85cfaacc
commit e8435bf309
1 changed files with 21 additions and 0 deletions

View File

@ -14,9 +14,13 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
from passerelle.base.models import BaseResource
from passerelle.utils.api import endpoint
class ExtraFees(BaseResource):
@ -28,3 +32,20 @@ class ExtraFees(BaseResource):
@classmethod
def get_connector_slug(cls):
return 'extra-fees'
@endpoint(methods=['post'])
def compute(self, request, **kwargs):
data = json.loads(request.body)
nb_documents = 0
for basket_item in data['data']:
try:
nb_documents += int(basket_item['request_data']['nb_documents'])
except KeyError:
# basket item not associated with any document, no fee
pass
# compute fee
postage_fee = 3 + nb_documents * 1
return {'data': [{'subject': force_text(_('Postage')), 'amount': str(postage_fee)}]}