Added the InvoiceCommand, which is the base class for all the invoice commands
[breadcrumbs.git] / src / lib / Bcd / Commands / PayInvoiceCommand.pm
blobad7395fd46a9c97b8508f759769b1e884fdaa182
1 package Bcd::Commands::PayInvoiceCommand;
3 # This file is part of the breadcrumbs daemon (bcd).
4 # Copyright (C) 2007 Pasqualino Ferrentino
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
23 use strict;
24 use warnings;
26 use threads;
27 use threads::shared;
29 #My base class is the create user command
30 use Bcd::Commands::InvoiceCommand;
31 use base(qq/Bcd::Commands::InvoiceCommand/);
32 use Data::Dumper;
33 use Bcd::Constants::AdsConstants;
35 use constant NAME => "ws_pay_invoice";
37 sub get_name{
38 return NAME;
41 sub new {
42 my $class = shift;
43 my $self = $class->SUPER::new(@_);
45 $self->{privilege_required} = Bcd::Data::Users::ANT_ROLE;
47 #I have only one parameter...
48 #the way of payment is decided by the function... automatically
49 push (@{$self->{parameters}}, "id_invoice");
51 bless ($self, $class);
52 return $self;
55 sub _exec{
56 my ($self, $stash) = @_;
58 my $my_id = $stash->get_session_id($self->{session_id});
60 #let's check only that the invoice correspond to an ad
61 my $invoice = Bcd::Data::Invoices->get_invoice_hash($stash, $self->{id_invoice});
63 my $ad = Bcd::Data::Ads->get_ad_hash($stash, $invoice->{id_ad});
64 my $loc = Bcd::Data::Ads->get_ad_locality_hash($stash, $invoice->{id_ad}, $invoice->{id_locus});
66 #ok, the user must own the ad...
67 if ($invoice->{user_to} != $my_id){
68 $self->{exit_code} = Bcd::Errors::ErrorCodes::BEC_THIS_INVOICE_IS_NOT_FOR_YOU;
69 return;
72 #the invoice must be in the correct state
73 if ($invoice->{id_status} != Bcd::Constants::InvoicesConstants::EMITTED){
74 $self->{exit_code} = Bcd::Errors::ErrorCodes::BEC_INVALID_STATE;
75 return;
78 if ($self->_pay_invoice_fail($stash, $my_id, $invoice, $ad, $loc) == 1){
79 return;
82 #ok, it should be all ok
83 $self->{exit_code} = Bcd::Errors::ErrorCodes::BEC_OK;
86 #I have the long token as the output
87 sub _add_your_specific_output{
88 my ($self, $output) = @_;
89 $self->_add_scalar("long_token", $self->{long_token}, $output);