@ -4,7 +4,6 @@ use Sipwise::Base;
use LWP::UserAgent ;
use URI ;
use POSIX ;
use UUID ;
use Module::Load::Conditional qw/can_load/ ;
use NGCP::Panel::Utils::Utf8 ;
@ -184,148 +183,6 @@ sub get_number_of_parts {
return ceil ( length ( $ text ) / $ maxlen ) ;
}
sub init_prepaid_billing {
my ( % args ) = @ _ ;
my $ c = $ args { c } ;
my $ prov_subscriber = $ args { prov_subscriber } ;
my $ parts = $ args { parts } ;
my $ caller = $ args { caller } ;
my $ callee = $ args { callee } ;
my ( $ uuid , $ session_id ) ;
UUID:: generate ( $ uuid ) ;
UUID:: unparse ( $ uuid , $ session_id ) ;
my $ session = {
caller = > $ caller ,
callee = > $ callee ,
status = > 'ok' ,
reason = > 'accepted' ,
parts = > [] ,
sid = > $ session_id ,
rpc = > $ parts ,
} ;
my ( $ prepaid_lib , $ is_prepaid ) ;
my $ bm_actual = NGCP::Panel::Utils::BillingMappings:: get_actual_billing_mapping ( c = > $ c , contract = > $ prov_subscriber - > contract , ) ;
$ prepaid_lib = $ bm_actual - > billing_profile - > prepaid_library if $ bm_actual ;
$ is_prepaid = ( $ bm_actual - > billing_profile - > prepaid ? 1 : 0 ) if $ bm_actual ;
# currently only inew rating supported, let others pass
unless ( $ is_prepaid && $ prepaid_lib && $ prepaid_lib eq "libinewrate" ) {
$ session - > { reason } = 'not prepaid/libinewrate' ;
return $ session ;
}
my $ use_list = { 'NGCP::Rating::Inew::SmsSession' = > undef } ;
unless ( can_load ( modules = > $ use_list , nocache = > 0 , autoload = > 0 ) ) {
$ c - > log - > error ( sprintf ( "Failed to load NGCP::Rating::Inew::SmsSession for sms=%s from=%s to=%s" , $ session_id , $ c - > qs ( $ caller ) , $ c - > qs ( $ callee ) ) ) ;
$ session - > { status } = 'failed' ;
$ session - > { reason } =
sprintf 'failed to init sms session sid=%s from=%s to=%s' ,
$ session_id , $ c - > qs ( $ caller ) , $ c - > qs ( $ callee ) ;
return ;
}
my $ amqr = NGCP::Rating::Inew::SmsSession:: init (
$ c - > config - > { libinewrate } - > { soap_uri } ,
$ c - > config - > { libinewrate } - > { openwire_uri } ,
) ;
unless ( $ amqr ) {
$ c - > log - > error ( sprintf ( "Failed to create sms amqr handle for sms sid=%s from=%s to=%s" , $ session_id , $ c - > qs ( $ caller ) , $ c - > qs ( $ callee ) ) ) ;
$ session - > { status } = 'failed' ;
$ session - > { reason } =
sprintf 'failed to create sms session sid=%s from=%s to=%s' ,
$ session_id , $ c - > qs ( $ caller ) , $ c - > qs ( $ callee ) ;
return ;
}
$ session - > { amqr_h } = $ amqr ;
# Reserve credit for each part, and then commit each reservation.
# If we can charge multiple times within one session - perfect.
# Otherwise we have to create one session per part, store it in an
# array, then after all reservations were successful, commit each
# of them!
for ( my $ i = 0 ; $ i < $ parts ; + + $ i ) {
my $ has_credit = 1 ;
my $ this_session_id = $ session_id . "-" . $ i ;
my $ sess = NGCP::Rating::Inew::SmsSession:: session_create (
$ amqr , $ this_session_id , $ caller , $ callee , sub {
$ has_credit = 0 ;
} ) ;
if ( not defined $ sess or $ sess == - 1 ) {
$ c - > log - > error ( "Failed to create sms rating session from " . $ c - > qs ( $ caller ) . ' to ' . $ c - > qs ( $ callee ) . " with session id $this_session_id" ) ;
$ session - > { status } = 'failed' ;
$ session - > { reason } = 'failed to create sms session' ;
last ;
} elsif ( $ sess == 0 ) {
$ c - > log - > error ( "Remote denied sms rating session from " . $ c - > qs ( $ caller ) . ' to ' . $ c - > qs ( $ callee ) . " with session id $this_session_id (subscriber=inactive)" ) ;
$ session - > { status } = 'failed' ;
$ session - > { reason } = 'remote denied sms session (subscriber=inactive)' ;
last ;
}
unless ( $ has_credit ) {
$ c - > log - > info ( "No credit for sms from " . $ c - > qs ( $ caller ) . ' to ' . $ c - > qs ( $ callee ) . " with session id $this_session_id" ) ;
$ session - > { status } = 'failed' ;
$ session - > { reason } = 'insufficient credit' ;
NGCP::Rating::Inew::SmsSession:: session_destroy ( $ sess ) ;
last ;
}
push @ { $ session - > { parts } } , $ sess ;
unless ( NGCP::Rating::Inew::SmsSession:: session_sms_reserve ( $ sess ) ) {
$ c - > log - > error ( "Failed to reserve sms session from " . $ c - > qs ( $ caller ) . ' to ' . $ c - > qs ( $ callee ) . " with session id $this_session_id" ) ;
$ session - > { status } = 'failed' ;
$ session - > { reason } = 'failed to reserve sms session' ;
last ;
}
}
return $ session ;
}
sub perform_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 ) } ;
if ( $ status eq 'ok' && $ rpc == $# { $ parts } + 1 ) {
foreach my $ sess ( @ { $ parts } ) {
NGCP::Rating::Inew::SmsSession:: session_sms_commit ( $ sess ) ;
NGCP::Rating::Inew::SmsSession:: session_destroy ( $ sess ) ;
}
NGCP::Rating::Inew::SmsSession:: destroy ( $ amqr ) ;
} else {
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 ;
}
sub add_journal_record {
my ( % args ) = @ _ ;
my $ c = $ args { c } ;