diff --git a/lib/NGCP/Panel/Controller/API/SMS.pm b/lib/NGCP/Panel/Controller/API/SMS.pm index 12692a1111..05ddf41bcc 100644 --- a/lib/NGCP/Panel/Controller/API/SMS.pm +++ b/lib/NGCP/Panel/Controller/API/SMS.pm @@ -118,6 +118,7 @@ sub create_item { my $test_mode = $c->request->params->{test_mode} // ''; my $session; my $smsc_peer = 'default'; + my $pp_billing_performed = 0; try { my $parts = NGCP::Panel::Utils::SMS::get_number_of_parts($resource->{text}); $session = NGCP::Panel::Utils::SMS::init_prepaid_billing(c => $c, @@ -157,14 +158,23 @@ sub create_item { ); } + NGCP::Panel::Utils::SMS::perform_prepaid_billing(c => $c, session => $session ); + $pp_billing_performed = 1; + if ($session->{status} eq 'failed') { die $session->{reason}."\n"; } + } catch($e) { $c->log->error($e); + unless ($pp_billing_performed) { + NGCP::Panel::Utils::SMS::cancel_prepaid_billing(c => $c, + session => $session + ); + } if ($session && $session->{reason} eq 'insufficient credit') { $self->error($c, HTTP_PAYMENT_REQUIRED, "Not enough credit to send the sms"); } else { diff --git a/lib/NGCP/Panel/Utils/SMS.pm b/lib/NGCP/Panel/Utils/SMS.pm index 0bd905bdaf..7e5100cb7d 100644 --- a/lib/NGCP/Panel/Utils/SMS.pm +++ b/lib/NGCP/Panel/Utils/SMS.pm @@ -303,13 +303,9 @@ sub perform_prepaid_billing { my (%args) = @_; my $c = $args{c}; my $session = $args{session} // return; - my ($amqr, $rpc, $status, $reason, $parts) = - @{$session}{qw(amqr_h rpc status reason parts)}; - - return unless $session; - return unless $amqr; - - $reason //= 'unknown'; + my $amqr = $session->{amqr_h} // return; + my ($rpc, $status, $reason, $parts) = + @{$session}{qw(rpc status reason parts)}; if ($status eq 'ok' && $rpc == $#{$parts}+1) { foreach my $sess (@{$parts}) { @@ -318,13 +314,28 @@ sub perform_prepaid_billing { } NGCP::Rating::Inew::SmsSession::destroy($amqr); } else { - foreach my $sess (@{$parts}) { - NGCP::Rating::Inew::SmsSession::session_set_cancel_reason($sess, $reason); - NGCP::Rating::Inew::SmsSession::session_sms_discard($sess); - NGCP::Rating::Inew::SmsSession::session_destroy($sess); - } - NGCP::Rating::Inew::SmsSession::destroy($amqr); + cancel_prepaid_billing(%args); + } + return; +} + +sub cancel_prepaid_billing { + my (%args) = @_; + my $c = $args{c}; + my $session = $args{session} // return; + my $amqr = $session->{amqr_h} // return; + my ($rpc, $status, $reason, $parts) = + @{$session}{qw(rpc status reason parts)}; + + $reason //= 'unknown'; + + foreach my $sess (@{$parts}) { + NGCP::Rating::Inew::SmsSession::session_set_cancel_reason($sess, $reason); + NGCP::Rating::Inew::SmsSession::session_sms_discard($sess); + NGCP::Rating::Inew::SmsSession::session_destroy($sess); } + NGCP::Rating::Inew::SmsSession::destroy($amqr); + return; } @@ -335,6 +346,7 @@ sub add_journal_record { $args{status} //= ''; $args{reason} //= ''; + $args{coding} //= 0; $args{subscriber_id} = $args{prov_subscriber}->id; delete $args{c};