File Coverage

blib/lib/Stancer/Role/Payment/Auth.pm
Criterion Covered Total %
statement 53 53 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 17 17 100.0
pod n/a
total 79 79 100.0


line stmt bran cond sub pod time code
1             package Stancer::Role::Payment::Auth;
2              
3 15     15   171110 use 5.020;
  15         63  
4 15     15   92 use strict;
  15         33  
  15         420  
5 15     15   74 use warnings;
  15         56  
  15         1267  
6              
7             # ABSTRACT: Payment authentication relative role
8             our $VERSION = '1.0.3'; # VERSION
9              
10 15     15   507 use Stancer::Core::Types qw(coerce_instance AuthInstance DeviceInstance Maybe);
  15         35  
  15         1369  
11              
12 15     15   655 use Stancer::Auth;
  15         34  
  15         461  
13 15     15   82 use Stancer::Auth::Status;
  15         86  
  15         430  
14 15     15   791 use Stancer::Device;
  15         67  
  15         418  
15 15     15   127 use Stancer::Exceptions::InvalidIpAddress;
  15         33  
  15         746  
16 15     15   80 use Stancer::Exceptions::InvalidPort;
  15         51  
  15         499  
17 15     15   118 use Scalar::Util qw(blessed);
  15         35  
  15         894  
18 15     15   106 use Try::Tiny;
  15         37  
  15         1097  
19              
20 15     15   127 use Moo::Role;
  15         32  
  15         154  
21              
22             requires qw(_add_modified _attribute_builder);
23              
24 15     15   10796 use namespace::clean;
  15         38  
  15         134  
25              
26              
27             has auth => (
28             is => 'rw',
29             isa => Maybe[AuthInstance],
30 18     18   1033 builder => sub { $_[0]->_attribute_builder('auth') },
31             coerce => sub {
32             return if not defined $_[0];
33             return if "$_[0]" eq q/0/ || "$_[0]" eq q//;
34             return $_[0] if blessed($_[0]) and blessed($_[0]) eq 'Stancer::Auth';
35             return Stancer::Auth->new() if "$_[0]" eq q/1/;
36             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::ATTEMPTED;
37             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::AVAILABLE;
38             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::DECLINED;
39             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::EXPIRED;
40             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::FAILED;
41             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::REQUESTED;
42             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::SUCCESS;
43             return Stancer::Auth->new(status => $_[0]) if $_[0] eq Stancer::Auth::Status::UNAVAILABLE;
44             return Stancer::Auth->new(return_url => $_[0]) if ref(\$_[0]) eq 'SCALAR';
45             return Stancer::Auth->new($_[0]);
46             },
47             lazy => 1,
48             predicate => 1,
49             trigger => sub { $_[0]->_add_modified('auth') },
50             );
51              
52              
53             has device => (
54             is => 'rw',
55             isa => Maybe[DeviceInstance],
56 15     15   625 builder => sub { $_[0]->_attribute_builder('device') },
57             coerce => coerce_instance('Stancer::Device'),
58             lazy => 1,
59             predicate => 1,
60             trigger => sub { $_[0]->_add_modified('device') },
61             );
62              
63             sub _create_device {
64 23     23   2616 my $this = shift;
65              
66 23 100       754 return $this unless defined $this->method;
67              
68 19 100       692 if (defined $this->device) {
69 4         131 $this->device->hydrate_from_env();
70              
71 2         25 return $this;
72             }
73              
74 15         845 my $device = Stancer::Device->new();
75              
76 15 100 100     325 if (defined $this->auth and defined $this->auth->return_url) {
77 6         250 $device->hydrate_from_env();
78 2         55 $this->device($device);
79             } else {
80             try {
81 9     9   449 $device->hydrate_from_env();
82 4         107 $this->device($device);
83 9         454 };
84             }
85              
86 11         4827 return $this;
87             }
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             Stancer::Role::Payment::Auth - Payment authentication relative role
100              
101             =head1 VERSION
102              
103             version 1.0.3
104              
105             =head1 ATTRIBUTES
106              
107             =head2 C<auth>
108              
109             Read/Write instance of C<Stancer::Auth>.
110              
111             May accept a boolean if you use our payment page or a HTTPS url as an alias for `Stancer::Auth::return_url`.
112              
113             =head2 C<device>
114              
115             Read/Write instance of C<Stancer::Device>.
116              
117             Information about device fulfuling the payment.
118              
119             C<Stancer::Device> needs IP address and port to work, it will automatically used environment
120             variables as created by Apache or nginx (aka C<SERVER_ADDR> and C<SERVER_PORT>).
121              
122             If variables are not available or if you are using a proxy, you must give IP and port at object instanciation.
123              
124             $payment->device(ip => $ip, port => $port);
125              
126             =head1 USAGE
127              
128             =head2 Logging
129              
130              
131              
132             We use the L<Log::Any> framework for logging events.
133             You may tell where it should log using any available L<Log::Any::Adapter> module.
134              
135             For example, to log everything to a file you just have to add a line to your script, like this:
136             #! /usr/bin/env perl
137             use Log::Any::Adapter (File => '/var/log/payment.log');
138             use Stancer::Role::Payment::Auth;
139              
140             You must import C<Log::Any::Adapter> before our libraries, to initialize the logger instance before use.
141              
142             You can choose your log level on import directly:
143             use Log::Any::Adapter (File => '/var/log/payment.log', log_level => 'info');
144              
145             Read the L<Log::Any> documentation to know what other options you have.
146              
147             =cut
148              
149             =head1 SECURITY
150              
151             =over
152              
153             =item *
154              
155             Never, never, NEVER register a card or a bank account number in your database.
156              
157             =item *
158              
159             Always uses HTTPS in card/SEPA in communication.
160              
161             =item *
162              
163             Our API will never give you a complete card/SEPA number, only the last four digits.
164             If you need to keep track, use these last four digit.
165              
166             =back
167              
168             =cut
169              
170             =head1 BUGS
171              
172             Please report any bugs or feature requests on the bugtracker website
173             L<https://gitlab.com/wearestancer/library/lib-perl/-/issues> or by email to
174             L<bug-stancer@rt.cpan.org|mailto:bug-stancer@rt.cpan.org>.
175              
176             When submitting a bug or request, please include a test-file or a
177             patch to an existing test-file that illustrates the bug or desired
178             feature.
179              
180             =head1 AUTHOR
181              
182             Joel Da Silva <jdasilva@cpan.org>
183              
184             =head1 COPYRIGHT AND LICENSE
185              
186             This software is Copyright (c) 2018-2024 by Stancer / Iliad78.
187              
188             This is free software, licensed under:
189              
190             The Artistic License 2.0 (GPL Compatible)
191              
192             =cut