| 1 | package Business::PagOnline { | ||
| 2 | use SOAP::Lite +trace => [qw/all -objects/]; | ||
| 3 | #use SOAP::Lite; | ||
| 4 | use Digest::SHA qw/hmac_sha256 hmac_sha256_hex hmac_sha256_base64/; | ||
| 5 | use Moo; | ||
| 6 | use Carp qw/confess/; | ||
| 7 | use namespace::clean; | ||
| 8 | use version; | ||
| 9 | use v5.36; | ||
| 10 | |||
| 11 | our $VERSION = qv("v0.2.0"); | ||
| 12 | |||
| 13 | has soapclient => ( | ||
| 14 | is => 'ro', | ||
| 15 | default => sub { SOAP::Lite->new() }, | ||
| 16 | ); | ||
| 17 | |||
| 18 | has tid => ( is => 'ro' ); | ||
| 19 | has kSig => ( is => 'ro' ); | ||
| 20 | |||
| 21 | sub BUILD { | ||
| 22 | my ($self, $args) = @_; | ||
| 23 | #$self->soapclient->serializer->readable(1); | ||
| 24 | $self->soapclient->envprefix('soap'); # As per example | ||
| 25 | $self->soapclient->proxy($args->{url}); | ||
| 26 | $self->soapclient->ns('http://services.api.web.cg.igfs.apps.netsw.it/', 'ser'); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub payment_init { | ||
| 30 | my ($self, $args) = @_; | ||
| 31 | |||
| 32 | $args->{amount} = int ($args->{amount} * 100); | ||
| 33 | die 'Invalid-amount' if $args->{amount} !~ m/^\d+$/xs; | ||
| 34 | die 'Invalid-shopID' if $args->{shopID} !~ m/^\w+$/xs; | ||
| 35 | die 'Invalid-shopUserRef' if !$args->{shopUserRef}; | ||
| 36 | |||
| 37 | my %reqargs = ( | ||
| 38 | langID => $args->{lang}, | ||
| 39 | tid => $self->tid, | ||
| 40 | kSig => $self->kSig, | ||
| 41 | trType => 'PURCHASE', | ||
| 42 | shopID => $args->{shopID}, | ||
| 43 | shopUserRef => $args->{shopUserRef}, | ||
| 44 | amount => $args->{amount}, | ||
| 45 | currencyCode => 'EUR', | ||
| 46 | errorURL => $args->{errorURL}.'', | ||
| 47 | notifyURL => $args->{notifyURL}.'', | ||
| 48 | ); | ||
| 49 | |||
| 50 | my $signature_str = join('', map { ''.$reqargs{$_} } qw/ | ||
| 51 | tid shopID shopUserRef shopUserName shopUserAccount | ||
| 52 | trType amount currencyCode langID notifyURL | ||
| 53 | errorURL addInfo1 addInfo2 addInfo3 addInfo4 | ||
| 54 | addInfo5 description recurrent paymentReason freeText | ||
| 55 | validityExpire | ||
| 56 | /); | ||
| 57 | $reqargs{signature} = hmac_sha256_base64($signature_str, $self->kSig) . '='; | ||
| 58 | #die "$reqargs{kSig} - " . $reqargs{signature}; | ||
| 59 | #die Data::Dump::dump(\%reqargs); | ||
| 60 | |||
| 61 | my @soapdata; | ||
| 62 | for my $sk(keys %reqargs) { | ||
| 63 | next if $sk eq 'kSig'; # This is used to calculate signature but MUST NOT BE SENT or we'll get 500 Error | ||
| 64 | my $sd = SOAP::Data->new(name => $sk, value => $reqargs{$sk}); | ||
| 65 | # $sd->type(''); # As per example | ||
| 66 | push @soapdata, $sd; | ||
| 67 | } | ||
| 68 | |||
| 69 | # die Data::Dump::dump(\@soapdata); | ||
| 70 | my $sd_req = SOAP::Data->new(name => 'request', value => \@soapdata); | ||
| 71 | # $sd_req->type(''); | ||
| 72 | my $res = $self->soapclient->call('ser:Init', | ||
| 73 | $sd_req | ||
| 74 | )->result; | ||
| 75 | # die Data::Dump::dump($res); | ||
| 76 | confess Data::Dump::dump($res) if $res->{error} ne 'false'; | ||
| 77 | |||
| 78 | return $res; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub payment_verify { | ||
| 82 | my ($self, $args) = @_; | ||
| 83 | |||
| 84 | die 'Invalid-shopID' if $args->{shopID} !~ m/^\w+$/xs; | ||
| 85 | my %reqargs = ( | ||
| 86 | tid => $self->tid, | ||
| 87 | kSig => $self->kSig, | ||
| 88 | shopID => $args->{shopID}, | ||
| 89 | paymentID => $args->{paymentID}, | ||
| 90 | ); | ||
| 91 | |||
| 92 | my $signature_str = join('', map { ''.$reqargs{$_} } qw/ | ||
| 93 | tid shopID paymentID | ||
| 94 | /); | ||
| 95 | $reqargs{signature} = hmac_sha256_base64($signature_str, $self->kSig) . '='; | ||
| 96 | |||
| 97 | my @soapdata; | ||
| 98 | for my $sk(keys %reqargs) { | ||
| 99 | next if $sk eq 'kSig'; # This is used to calculate signature but MUST NOT BE SENT or we'll get 500 Error | ||
| 100 | my $sd = SOAP::Data->new(name => $sk, value => $reqargs{$sk}); | ||
| 101 | push @soapdata, $sd; | ||
| 102 | } | ||
| 103 | |||
| 104 | my $res = $self->soapclient->call('ser:Verify', | ||
| 105 | SOAP::Data->new(name => 'request', value => \@soapdata) | ||
| 106 | )->result; | ||
| 107 | |||
| 108 | return $res; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | =head1 NAME | ||
| 115 | |||
| 116 | Business::PagOnline - Perl library for Unicredit's PagOnline payment system | ||
| 117 | |||
| 118 | =head1 SYNOPSIS | ||
| 119 | |||
| 120 | use Business::PagOnline; | ||
| 121 | |||
| 122 | # TODO | ||
| 123 | |||
| 124 | =head1 DESCRIPTION | ||
| 125 | |||
| 126 | This is HIGHLY EXPERIMENTAL and in the works, do not use for now. | ||
| 127 | |||
| 128 | =head1 AUTHOR | ||
| 129 | |||
| 130 | Michele Beltrame, C<mb@blendgroup.it> | ||
| 131 | |||
| 132 | =head1 LICENSE | ||
| 133 | |||
| 134 | This library is free software under the Artistic License 2.0. | ||
| 135 | |||
| 136 | =cut |