View | Details | Raw Unified | Return to bug 15165
Collapse All | Expand All

(-)a/Koha/REST/V1/Accountline.pm (-24 / +33 lines)
Lines 25-30 use C4::Auth qw( haspermission ); Link Here
25
use Koha::Account::Lines;
25
use Koha::Account::Lines;
26
use Koha::Account;
26
use Koha::Account;
27
27
28
use Try::Tiny;
29
28
sub list {
30
sub list {
29
    my ($c, $args, $cb) = @_;
31
    my ($c, $args, $cb) = @_;
30
32
Lines 55-87 sub edit { Link Here
55
sub pay {
57
sub pay {
56
    my ($c, $args, $cb) = @_;
58
    my ($c, $args, $cb) = @_;
57
59
58
    my $accountline = Koha::Account::Lines->find($args->{accountlines_id});
60
    return try {
59
    unless ($accountline) {
61
        my $accountline = Koha::Account::Lines->find($args->{accountlines_id});
60
        return $c->$cb({error => "Accountline not found"}, 404);
62
        unless ($accountline) {
61
    }
63
            return $c->$cb({error => "Accountline not found"}, 404);
62
64
        }
63
    my $body = $c->req->json;
64
    my $amount = $body->{amount};
65
    my $note = $body->{note} || '';
66
67
    if ($amount && !looks_like_number($amount)) {
68
        return $c->$cb({error => "Invalid amount"}, 400);
69
    }
70
65
71
    Koha::Account->new(
66
        my $body = $c->req->json;
72
        {
67
        my $amount = $body->{amount};
73
            patron_id => $accountline->borrowernumber,
68
        my $note = $body->{note} || '';
69
70
        Koha::Account->new(
71
            {
72
                patron_id => $accountline->borrowernumber,
73
            }
74
          )->pay(
75
            {
76
                lines  => [$accountline],
77
                amount => $amount,
78
                note => $note,
79
            }
80
          );
81
82
        $accountline = Koha::Account::Lines->find($args->{accountlines_id});
83
        return $c->$cb($accountline->unblessed(), 200);
84
    } catch {
85
        if ($_->isa('DBIx::Class::Exception')) {
86
            return $c->$cb({ error => $_->msg }, 500);
74
        }
87
        }
75
      )->pay(
88
        else {
76
        {
89
            return $c->$cb({
77
            lines  => [$accountline],
90
                error => 'Something went wrong, check the logs.'
78
            amount => $amount,
91
            }, 500);
79
            note => $note,
80
        }
92
        }
81
      );
93
    };
82
83
    $accountline = Koha::Account::Lines->find($args->{accountlines_id});
84
    return $c->$cb($accountline->unblessed(), 200);
85
}
94
}
86
95
87
96
(-)a/Koha/REST/V1/Patron.pm (-22 / +43 lines)
Lines 25-30 use C4::Auth qw( haspermission ); Link Here
25
use Koha::Patrons;
25
use Koha::Patrons;
26
use Koha::Account;
26
use Koha::Account;
27
27
28
use Try::Tiny;
29
28
sub list {
30
sub list {
29
    my ($c, $args, $cb) = @_;
31
    my ($c, $args, $cb) = @_;
30
32
Lines 51-81 sub get { Link Here
51
sub pay {
53
sub pay {
52
    my ($c, $args, $cb) = @_;
54
    my ($c, $args, $cb) = @_;
53
55
54
    my $patron = Koha::Patrons->find($args->{borrowernumber});
56
    return try {
55
    unless ($patron) {
57
        my $patron = Koha::Patrons->find($args->{borrowernumber});
56
        return $c->$cb({error => "Patron not found"}, 404);
58
        unless ($patron) {
57
    }
59
            return $c->$cb({error => "Patron not found"}, 404);
58
60
        }
59
    my $body = $c->req->json;
60
    my $amount = $body->{amount};
61
    my $note = $body->{note} || '';
62
63
    unless ($amount && looks_like_number($amount)) {
64
        return $c->$cb({error => "Invalid amount"}, 400);
65
    }
66
61
67
    Koha::Account->new(
62
        my $body = $c->req->json;
68
        {
63
        my $amount = $body->{amount};
69
            patron_id => $args->{borrowernumber},
64
        my $note = $body->{note} || '';
65
66
        Koha::Account->new(
67
            {
68
                patron_id => $args->{borrowernumber},
69
            }
70
          )->pay(
71
            {
72
                amount => $amount,
73
                note => $note,
74
            }
75
          );
76
77
        return $c->$cb('', 204);
78
    } catch {
79
        if ($_->isa('DBIx::Class::Exception')) {
80
            return $c->$cb({ error => $_->msg }, 500);
70
        }
81
        }
71
      )->pay(
82
        elsif ($_->isa('Koha::Exceptions::InvalidAmount')) {
72
        {
83
            return $c->$cb({
73
            amount => $amount,
84
                error => 'Invalid amount given',
74
            note => $note,
85
                amount => $_->amount,
86
            }, 400);
75
        }
87
        }
76
      );
88
        elsif ($_->isa('Koha::Exceptions::MissingParameter')) {
77
89
            return $c->$cb({
78
    return $c->$cb('', 204);
90
                error => "Missing required parameter",
91
                parameter => $_->parameter,
92
            }, 400);
93
        }
94
        else {
95
            return $c->$cb({
96
                error => 'Something went wrong, check the logs.'
97
            }, 500);
98
        }
99
    };
79
}
100
}
80
101
81
1;
102
1;
(-)a/api/v1/swagger/paths/accountlines.json (-6 / +15 lines)
Lines 55-67 Link Here
55
            "type": "object",
55
            "type": "object",
56
            "properties": {
56
            "properties": {
57
              "amount": {
57
              "amount": {
58
                "description": "Amount"
58
                "description": "Amount",
59
                "type": ["number", "null"]
59
              },
60
              },
60
              "amountoutstanding": {
61
              "amountoutstanding": {
61
                "description": "Amount outstanding"
62
                "description": "Amount outstanding",
63
                "type": ["number", "null"]
62
              },
64
              },
63
              "note": {
65
              "note": {
64
                "description": "Accountline note"
66
                "description": "Accountline note",
67
                "type": ["string", "null"]
65
              }
68
              }
66
            }
69
            }
67
          }
70
          }
Lines 108-124 Link Here
108
        {
111
        {
109
          "name": "body",
112
          "name": "body",
110
          "in": "body",
113
          "in": "body",
114
          "required": true,
111
          "description": "A JSON object containing fields to modify",
115
          "description": "A JSON object containing fields to modify",
112
          "schema": {
116
          "schema": {
113
            "type": "object",
117
            "type": "object",
114
            "properties": {
118
            "properties": {
115
              "amount": {
119
              "amount": {
116
                "description": "Amount to pay"
120
                "description": "Amount to pay. If not given, the whole accountline will be paid.",
121
                "type": "number",
122
                "exclusiveMinimum": true,
123
                "minimum": 0
117
              },
124
              },
118
              "note": {
125
              "note": {
119
                "description": "Payment note"
126
                "description": "Payment note",
127
                "type": "string"
120
              }
128
              }
121
            }
129
            },
130
            "required": ["amount"]
122
          }
131
          }
123
        }
132
        }
124
      ],
133
      ],
(-)a/api/v1/swagger/paths/patrons.json (-3 / +8 lines)
Lines 88-99 Link Here
88
            "type": "object",
88
            "type": "object",
89
            "properties": {
89
            "properties": {
90
              "amount": {
90
              "amount": {
91
                "description": "Amount to pay"
91
                "description": "Amount to pay",
92
                "type": "number",
93
                "exclusiveMinimum": true,
94
                "minimum": 0
92
              },
95
              },
93
              "note": {
96
              "note": {
94
                "description": "Payment note"
97
                "description": "Payment note",
98
                "type": ["string", "null"]
95
              }
99
              }
96
            }
100
            },
101
            "required": ["amount"]
97
          }
102
          }
98
        }
103
        }
99
      ],
104
      ],
(-)a/t/db_dependent/api/v1/accountlines.t (-3 / +23 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
20
21
use Test::More tests => 37;
21
use Test::More tests => 43;
22
use Test::Mojo;
22
use Test::Mojo;
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
24
24
Lines 160-166 is($accountline_edited->{amountoutstanding}, '-19.000000'); Link Here
160
160
161
161
162
# Payment tests
162
# Payment tests
163
$tx = $t->ua->build_tx(POST => "/api/v1/accountlines/4562765765/payment");
163
$tx = $t->ua->build_tx(POST => "/api/v1/accountlines/4562765765/payment" => json => { amount => 1000 });
164
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
164
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
165
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
165
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
166
$t->request_ok($tx)
166
$t->request_ok($tx)
Lines 171-176 $tx = $t->ua->build_tx(POST => "/api/v1/accountlines/$accountline_to_pay->{accou Link Here
171
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
171
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
172
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
172
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
173
$t->request_ok($tx)
173
$t->request_ok($tx)
174
  ->status_is(400);
175
176
$tx = $t->ua->build_tx(POST => "/api/v1/accountlines/$accountline_to_pay->{accountlines_id}/payment"
177
                       => json => { amount => 0 });
178
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
179
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
180
$t->request_ok($tx)
181
  ->status_is(400);
182
183
$tx = $t->ua->build_tx(POST => "/api/v1/accountlines/$accountline_to_pay->{accountlines_id}/payment"
184
                       => json => { amount => "no" });
185
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
186
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
187
$t->request_ok($tx)
188
  ->status_is(400);
189
190
$tx = $t->ua->build_tx(POST => "/api/v1/accountlines/$accountline_to_pay->{accountlines_id}/payment"
191
                       => json => { amount => 20 });
192
$tx->req->cookies({name => 'CGISESSID', value => $session->id});
193
$tx->req->env({REMOTE_ADDR => '127.0.0.1'});
194
$t->request_ok($tx)
174
  ->status_is(200);
195
  ->status_is(200);
175
#$t->content_is('toto');
196
#$t->content_is('toto');
176
197
177
- 

Return to bug 15165