| 1 | package NpsSDK::SoapClient; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use NpsSDK::Configuration; | ||
| 7 | use NpsSDK::Utils; | ||
| 8 | use NpsSDK::Constants; | ||
| 9 | use NpsSDK::Errors; | ||
| 10 | |||
| 11 | use Data::Structure::Util qw( unbless ); | ||
| 12 | |||
| 13 | use Encode qw(encode); | ||
| 14 | |||
| 15 | our $VERSION = '1.91'; # VERSION | ||
| 16 | |||
| 17 | my $connection; | ||
| 18 | my $response; | ||
| 19 | my $error; | ||
| 20 | |||
| 21 | sub _setup { | ||
| 22 | my $self = shift; | ||
| 23 | if (defined $NpsSDK::Configuration::logger) { | ||
| 24 | NpsSDK::LogException->error if ($NpsSDK::Configuration::logger->is_debug() and | ||
| 25 | $NpsSDK::Configuration::environment eq $NpsSDK::Constants::PRODUCTION_ENV); | ||
| 26 | |||
| 27 | eval { require SOAP::Lite; | ||
| 28 | SOAP::Lite->import(+trace => [ transport => sub { | ||
| 29 | my ($http_object) = @_; | ||
| 30 | NpsSDK::Utils::log_data($http_object); | ||
| 31 | }]); | ||
| 32 | } | ||
| 33 | |||
| 34 | } else { | ||
| 35 | use SOAP::Lite; | ||
| 36 | } | ||
| 37 | |||
| 38 | $connection = SOAP::Lite | ||
| 39 | -> on_action(sub {sprintf '%s%s', @_}) | ||
| 40 | -> on_fault( sub { | ||
| 41 | my ( $soap, $res ) = @_; | ||
| 42 | if (index($res, "timeout") != -1) { $error = 1; } | ||
| 43 | elsif (index($res, "connect") != -1) { $error = 2; } | ||
| 44 | }); | ||
| 45 | |||
| 46 | if ($NpsSDK::Configuration::cert_verify_peer == 1) { | ||
| 47 | use IO::Socket::SSL; | ||
| 48 | $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; | ||
| 49 | if (defined $NpsSDK::Configuration::proxy_url) { | ||
| 50 | $connection -> proxy(NpsSDK::Configuration::get_url(), | ||
| 51 | proxy => ['http' => $NpsSDK::Configuration::proxy_url], | ||
| 52 | timeout => $NpsSDK::Configuration::timeout, | ||
| 53 | ssl_opts => [ SSL_verify_mode => 0 ]); | ||
| 54 | } else { | ||
| 55 | $connection -> proxy(NpsSDK::Configuration::get_url(), | ||
| 56 | timeout => $NpsSDK::Configuration::timeout, | ||
| 57 | ssl_opts => [ SSL_verify_mode => 0 ]); | ||
| 58 | } | ||
| 59 | } else { | ||
| 60 | if (defined $NpsSDK::Configuration::proxy_url) { | ||
| 61 | $connection -> proxy(NpsSDK::Configuration::get_url(), | ||
| 62 | proxy => ['http' => $NpsSDK::Configuration::proxy_url], | ||
| 63 | timeout => $NpsSDK::Configuration::timeout); | ||
| 64 | } else { | ||
| 65 | $connection -> proxy(NpsSDK::Configuration::get_url(), | ||
| 66 | timeout => $NpsSDK::Configuration::timeout); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | if (defined $NpsSDK::Configuration::certificate and defined $NpsSDK::Configuration::certificate_key) { | ||
| 71 | $connection->transport->ssl_opts( | ||
| 72 | SSL_cert_file => $NpsSDK::Configuration::certificate, | ||
| 73 | SSL_key_file => $NpsSDK::Configuration::certificate_key | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub soap_call { | ||
| 79 | my ($service, $ref_params) = @_; | ||
| 80 | _setup(); | ||
| 81 | |||
| 82 | my $params = NpsSDK::Utils::add_extra_info($service, $ref_params); | ||
| 83 | |||
| 84 | $params = NpsSDK::Utils::check_sanitize(params=>$params, is_root=>1) | ||
| 85 | if (defined $NpsSDK::Configuration::sanitize); | ||
| 86 | |||
| 87 | $params = NpsSDK::Utils::add_secure_hash($NpsSDK::Configuration::secret_key, $params) | ||
| 88 | if (!(exists $params->{"psp_ClientSession"})); | ||
| 89 | |||
| 90 | eval {$response = $connection->$service(transform_params(("Requerimiento" => $params)));}; | ||
| 91 | |||
| 92 | if (defined $error) { | ||
| 93 | if ($error == 1) { | ||
| 94 | $NpsSDK::Configuration::logger->warn(encode("UTF-8", "Timeout error")); | ||
| 95 | return NpsSDK::TimeoutException->new(); | ||
| 96 | } elsif ($error == 2 ) { | ||
| 97 | $NpsSDK::Configuration::logger->warn(encode("UTF-8", "Connection error")); | ||
| 98 | return NpsSDK::ConnectionException->new(); | ||
| 99 | } else { | ||
| 100 | $NpsSDK::Configuration::logger->error(encode("UTF-8", "Unknown error")); | ||
| 101 | return NpsSDK::UnknownError->new(); | ||
| 102 | } | ||
| 103 | } else { | ||
| 104 | my $result = NpsSDK::Utils::encode_params(unbless($response->result)); | ||
| 105 | return $result; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub transform_params { | ||
| 110 | my %params = @_; | ||
| 111 | my @params; | ||
| 112 | foreach my $key (keys %params) { | ||
| 113 | my $item = SOAP::Data->name($key => $params{$key}); | ||
| 114 | push(@params, $item); | ||
| 115 | } | ||
| 116 | return @params; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 |