| 1 | package Business::TrueLayer; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Business::TrueLayer - Perl library for interacting with the TrueLayer v3 API | ||||||||||
| 6 | (https://docs.truelayer.com/) | ||||||||||
| 7 | |||||||||||
| 8 | =head1 VERSION | ||||||||||
| 9 | |||||||||||
| 10 | v0.05 | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | my $TrueLayer = Business::TrueLayer->new( | ||||||||||
| 15 | |||||||||||
| 16 | # required constructor arguments | ||||||||||
| 17 | client_id => $truelayer_client_id, | ||||||||||
| 18 | client_secret => $truelauer_client_secret, | ||||||||||
| 19 | kid => $truelayer_kid, | ||||||||||
| 20 | private_key => '/path/to/private/key', | ||||||||||
| 21 | |||||||||||
| 22 | # optional constructor arguments (with defaults) | ||||||||||
| 23 | host => 'truelayer.com', | ||||||||||
| 24 | api_host => 'api.truelayer.com', | ||||||||||
| 25 | auth_host => 'auth.truelayer.com', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | # valid your setup (neither required in live usage): | ||||||||||
| 29 | $TrueLayer->test_signature; | ||||||||||
| 30 | my $access_token = $TrueLayer->access_token; | ||||||||||
| 31 | |||||||||||
| 32 | # create a payment | ||||||||||
| 33 | my $Payment = $TrueLayer->create_payment( $args ); | ||||||||||
| 34 | my $link = $Payment->hosted_payment_page_link( $redirect_uri ); | ||||||||||
| 35 | |||||||||||
| 36 | # get status of a payment | ||||||||||
| 37 | my $Payment = $TrueLayer->get_payment( $payment_id ); | ||||||||||
| 38 | |||||||||||
| 39 | if ( $Payment->settled ) { | ||||||||||
| 40 | ... | ||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # create a mandate, then create a payment | ||||||||||
| 44 | my $Mandate = $TrueLayer->create_mandate( $args ); | ||||||||||
| 45 | |||||||||||
| 46 | if ( $Mandate->authorized ) { | ||||||||||
| 47 | my $Payment = $TrueLayer->create_payment_from_mandate( | ||||||||||
| 48 | $Mandate,$amount_in_minor_units | ||||||||||
| 49 | ); | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||
| 53 | |||||||||||
| 54 | L<Business::TrueLayer> is a client library for interacting with the | ||||||||||
| 55 | TrueLayer v3 API. It implementes the necesary signing and transport logic | ||||||||||
| 56 | to allow you to just focus on just the endpoints you want to call. | ||||||||||
| 57 | |||||||||||
| 58 | The initial version of this distribution supports just those steps that | ||||||||||
| 59 | described at L<https://docs.truelayer.com/docs/quickstart-make-a-payment> | ||||||||||
| 60 | and others will be added as necessary (pull requests also welcome). | ||||||||||
| 61 | |||||||||||
| 62 | =head1 DEBUGGING | ||||||||||
| 63 | |||||||||||
| 64 | Set C<MOJO_CLIENT_DEBUG=1> for user agent and transport debug output. | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | 1 | ▶ | use feature qw/ signatures postderef /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 1 | extends 'Business::TrueLayer::Request'; | |||||||||
| 74 | 1 | ▶ | no warnings qw/ experimental::signatures experimental::postderef /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 75 | |||||||||||
| 76 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 77 | |||||||||||
| 78 | 1 | ▶ | use Business::TrueLayer::Authenticator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 1 | ▶ | use Business::TrueLayer::MerchantAccount; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 1 | ▶ | use Business::TrueLayer::Mandate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 81 | 1 | ▶ | use Business::TrueLayer::Payment; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 1 | ▶ | use Business::TrueLayer::Signer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 83 | 1 | ▶ | use Business::TrueLayer::Types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 84 | 1 | ▶ | use Business::TrueLayer::User; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 85 | |||||||||||
| 86 | 1 | $Business::TrueLayer::VERSION = '0.05'; | |||||||||
| 87 | |||||||||||
| 88 | =head1 METHODS | ||||||||||
| 89 | |||||||||||
| 90 | =head2 test_signature | ||||||||||
| 91 | |||||||||||
| 92 | Tests if your signature and signing is valid. | ||||||||||
| 93 | |||||||||||
| 94 | $TrueLayer->test_signature; | ||||||||||
| 95 | |||||||||||
| 96 | Returns 1 on success, throws an exception otherwise. | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | 1 | ▶ | sub test_signature ( $self ) { | ||||||||
|
Subroutine
test_signature: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 101 | |||||||||||
| 102 | 1 | $self->api_post( | |||||||||
| 103 | '/test-signature', | ||||||||||
| 104 | { nonce => "9f952b2e-1675-4be8-bb39-6f4343803c2f" }, | ||||||||||
| 105 | my $expect_json = 0, | ||||||||||
| 106 | ); | ||||||||||
| 107 | |||||||||||
| 108 | 1 | return 1; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | =head2 access_token | ||||||||||
| 112 | |||||||||||
| 113 | Get an access token. | ||||||||||
| 114 | |||||||||||
| 115 | my $access_token = $TrueLayer->access_token; | ||||||||||
| 116 | |||||||||||
| 117 | Returns an access token on success, throws an exception otherwise. | ||||||||||
| 118 | |||||||||||
| 119 | =cut | ||||||||||
| 120 | |||||||||||
| 121 | 0 | ▶ | sub access_token ( $self ) { | ||||||||
|
Subroutine
access_token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 122 | 0 | return $self->authenticator->access_token; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | =head2 merchant_accounts | ||||||||||
| 126 | |||||||||||
| 127 | Get a list of merchant accounts, C<$id> is optional to specifiy just one. | ||||||||||
| 128 | |||||||||||
| 129 | my @merchant_accounts = $TrueLayer->merchant_accounts( $id ); | ||||||||||
| 130 | |||||||||||
| 131 | Returns a list of L<Business::TrueLayer::MerchantAccount> objects. | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||
| 134 | |||||||||||
| 135 | 1 | ▶ | sub merchant_accounts ( | ||||||||
|
Subroutine
merchant_accounts: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 136 | $self, | ||||||||||
| 137 | $id = undef | ||||||||||
| 138 | 1 | ) { | |||||||||
| 139 | 1 | ▶ | my $data = $self->api_get( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | "/v3/merchant-accounts" . ( $id ? "/$id" : "" ) | ||||||||||
| 141 | ); | ||||||||||
| 142 | |||||||||||
| 143 | 1 | my @merchants_accounts; | |||||||||
| 144 | |||||||||||
| 145 | 1 | foreach my $item ( $data->{items}->@* ) { | |||||||||
| 146 | 1 | push( | |||||||||
| 147 | @merchants_accounts, | ||||||||||
| 148 | Business::TrueLayer::MerchantAccount->new( $item->%* ), | ||||||||||
| 149 | ); | ||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 1 | return @merchants_accounts; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | =head2 create_payment | ||||||||||
| 156 | |||||||||||
| 157 | Instantiates a L<Business::TrueLayer::Payment> object then calls the | ||||||||||
| 158 | API to create it - will return the object to allow you to inspect it | ||||||||||
| 159 | and call methods on it. | ||||||||||
| 160 | |||||||||||
| 161 | my $Payment = $TrueLayer->create_payment( $args ); | ||||||||||
| 162 | |||||||||||
| 163 | C<$args> should be a hash reference of the necessary attributes to | ||||||||||
| 164 | instantiate a L<Business::TrueLayer::Payment> object - see the perldoc | ||||||||||
| 165 | for that class for the attributes required. | ||||||||||
| 166 | |||||||||||
| 167 | Any issues here will result in an exception being thrown. | ||||||||||
| 168 | |||||||||||
| 169 | =cut | ||||||||||
| 170 | |||||||||||
| 171 | 1 | ▶ | sub create_payment ( | ||||||||
|
Subroutine
create_payment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 172 | $self, | ||||||||||
| 173 | $payment_constuctor_args, | ||||||||||
| 174 | ) { | ||||||||||
| 175 | # instantiate an object first to perform type checking before | ||||||||||
| 176 | # we send a request to the API | ||||||||||
| 177 | 1 | Business::TrueLayer::Payment->new( | |||||||||
| 178 | $payment_constuctor_args->%* | ||||||||||
| 179 | ); | ||||||||||
| 180 | |||||||||||
| 181 | # send request to the API | ||||||||||
| 182 | 1 | my $response = $self->api_post( | |||||||||
| 183 | '/v3/payments', | ||||||||||
| 184 | $payment_constuctor_args, | ||||||||||
| 185 | ); | ||||||||||
| 186 | |||||||||||
| 187 | # return a new instance of the Payment object, with the original | ||||||||||
| 188 | # args and the details from the response | ||||||||||
| 189 | return Business::TrueLayer::Payment->new( | ||||||||||
| 190 | $payment_constuctor_args->%*, | ||||||||||
| 191 | |||||||||||
| 192 | $response->%{ qw / id status resource_token /}, | ||||||||||
| 193 | |||||||||||
| 194 | host => $self->host, | ||||||||||
| 195 | payment_host => $self->payment_host, | ||||||||||
| 196 | |||||||||||
| 197 | user => Business::TrueLayer::User->new( | ||||||||||
| 198 | $payment_constuctor_args->{user}->%*, | ||||||||||
| 199 | id => $response->{user}{id}, | ||||||||||
| 200 | 1 | ), | |||||||||
| 201 | ); | ||||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | =head2 get_payment | ||||||||||
| 205 | |||||||||||
| 206 | Calls the API to get the details for a payment for the given id then | ||||||||||
| 207 | instantiates a L<Business::TrueLayer::Payment> object for return to | ||||||||||
| 208 | the caller | ||||||||||
| 209 | |||||||||||
| 210 | my $Payment = $TrueLayer->get_payment( $payment_id ); | ||||||||||
| 211 | |||||||||||
| 212 | Any issues here will result in an exception being thrown. | ||||||||||
| 213 | |||||||||||
| 214 | =cut | ||||||||||
| 215 | |||||||||||
| 216 | 1 | ▶ | sub get_payment ( | ||||||||
|
Subroutine
get_payment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 217 | $self, | ||||||||||
| 218 | $payment_id, | ||||||||||
| 219 | ) { | ||||||||||
| 220 | # send request to the API | ||||||||||
| 221 | 1 | my $response = $self->api_get( | |||||||||
| 222 | '/v3/payments/' . $payment_id, | ||||||||||
| 223 | ); | ||||||||||
| 224 | |||||||||||
| 225 | 1 | return Business::TrueLayer::Payment->new( | |||||||||
| 226 | $response->%*, | ||||||||||
| 227 | host => $self->host, | ||||||||||
| 228 | payment_host => $self->payment_host, | ||||||||||
| 229 | ); | ||||||||||
| 230 | } | ||||||||||
| 231 | |||||||||||
| 232 | =head2 create_mandate | ||||||||||
| 233 | |||||||||||
| 234 | Instantiates a L<Business::TrueLayer::Mandate> object then calls the | ||||||||||
| 235 | API to create it - will return the object to allow you to inspect it | ||||||||||
| 236 | and call methods on it. | ||||||||||
| 237 | |||||||||||
| 238 | my $Mandate = $TrueLayer->create_mandate( $args ); | ||||||||||
| 239 | |||||||||||
| 240 | C<$args> should be a hash reference of the necessary attributes to | ||||||||||
| 241 | instantiate a L<Business::TrueLayer::Mandate> object - see the perldoc | ||||||||||
| 242 | for that class for the attributes required. | ||||||||||
| 243 | |||||||||||
| 244 | Any issues here will result in an exception being thrown. | ||||||||||
| 245 | |||||||||||
| 246 | =cut | ||||||||||
| 247 | |||||||||||
| 248 | 1 | ▶ | sub create_mandate ( | ||||||||
|
Subroutine
create_mandate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 249 | $self, | ||||||||||
| 250 | $mandate_constuctor_args, | ||||||||||
| 251 | ) { | ||||||||||
| 252 | # instantiate an object first to perform type checking before | ||||||||||
| 253 | # we send a request to the API | ||||||||||
| 254 | 1 | Business::TrueLayer::Mandate->new( | |||||||||
| 255 | $mandate_constuctor_args, | ||||||||||
| 256 | ); | ||||||||||
| 257 | |||||||||||
| 258 | # send request to the API | ||||||||||
| 259 | 1 | my $response = $self->api_post( | |||||||||
| 260 | '/v3/mandates', | ||||||||||
| 261 | $mandate_constuctor_args, | ||||||||||
| 262 | ); | ||||||||||
| 263 | |||||||||||
| 264 | # return a new instance of the Payment object, with the original | ||||||||||
| 265 | # args and the details from the response | ||||||||||
| 266 | return Business::TrueLayer::Mandate->new({ | ||||||||||
| 267 | $mandate_constuctor_args->%*, | ||||||||||
| 268 | |||||||||||
| 269 | $response->%{ qw / id status / }, | ||||||||||
| 270 | |||||||||||
| 271 | host => $self->host, | ||||||||||
| 272 | payment_host => $self->payment_host, | ||||||||||
| 273 | |||||||||||
| 274 | user => Business::TrueLayer::User->new( | ||||||||||
| 275 | $mandate_constuctor_args->{user}->%*, | ||||||||||
| 276 | id => $response->{user}{id}, | ||||||||||
| 277 | 1 | ), | |||||||||
| 278 | }); | ||||||||||
| 279 | } | ||||||||||
| 280 | |||||||||||
| 281 | =head2 get_mandate | ||||||||||
| 282 | |||||||||||
| 283 | Calls the API to get the details for a mandate for the given id then | ||||||||||
| 284 | instantiates a L<Business::TrueLayer::Mandate> object for return to | ||||||||||
| 285 | the caller | ||||||||||
| 286 | |||||||||||
| 287 | my $Mandate = $TrueLayer->get_mandate( $mandate_id ); | ||||||||||
| 288 | |||||||||||
| 289 | Any issues here will result in an exception being thrown. | ||||||||||
| 290 | |||||||||||
| 291 | =cut | ||||||||||
| 292 | |||||||||||
| 293 | 1 | ▶ | sub get_mandate ( | ||||||||
|
Subroutine
get_mandate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 294 | $self, | ||||||||||
| 295 | $mandate_id, | ||||||||||
| 296 | ) { | ||||||||||
| 297 | # send request to the API | ||||||||||
| 298 | 1 | my $response = $self->api_get( | |||||||||
| 299 | '/v3/mandates/' . $mandate_id, | ||||||||||
| 300 | ); | ||||||||||
| 301 | |||||||||||
| 302 | 1 | return Business::TrueLayer::Mandate->new({ | |||||||||
| 303 | $response->%*, | ||||||||||
| 304 | host => $self->host, | ||||||||||
| 305 | payment_host => $self->payment_host, | ||||||||||
| 306 | }); | ||||||||||
| 307 | } | ||||||||||
| 308 | |||||||||||
| 309 | =head2 create_payment_from_mandate | ||||||||||
| 310 | |||||||||||
| 311 | Returns a L<Business::TrueLayer::Payment> object after having called | ||||||||||
| 312 | the TrueLayer API for a particular mandate | ||||||||||
| 313 | |||||||||||
| 314 | my $Payment = $TrueLayer->create_payment_from_mandate( | ||||||||||
| 315 | $Mandate, | ||||||||||
| 316 | $amount_in_minor_units, | ||||||||||
| 317 | ); | ||||||||||
| 318 | |||||||||||
| 319 | C<$Mandate> should be a Business::TrueLayer::Mandate object, and | ||||||||||
| 320 | C<$amount_in_minor_units> should be exactly that. | ||||||||||
| 321 | |||||||||||
| 322 | Any issues here will result in an exception being thrown. | ||||||||||
| 323 | |||||||||||
| 324 | =cut | ||||||||||
| 325 | |||||||||||
| 326 | 1 | ▶ | sub create_payment_from_mandate ( | ||||||||
|
Subroutine
create_payment_from_mandate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 327 | $self, | ||||||||||
| 328 | $Mandate, | ||||||||||
| 329 | $amount_in_minor_units, | ||||||||||
| 330 | ) { | ||||||||||
| 331 | 1 | my %request = ( | |||||||||
| 332 | payment_method => { | ||||||||||
| 333 | type => 'mandate', | ||||||||||
| 334 | mandate_id => $Mandate->id, | ||||||||||
| 335 | }, | ||||||||||
| 336 | amount_in_minor => $amount_in_minor_units, | ||||||||||
| 337 | currency => $Mandate->currency, | ||||||||||
| 338 | user => { $Mandate->user->%* }, | ||||||||||
| 339 | ); | ||||||||||
| 340 | |||||||||||
| 341 | 1 | my $response = $self->api_post( | |||||||||
| 342 | '/v3/payments', | ||||||||||
| 343 | \%request, | ||||||||||
| 344 | ); | ||||||||||
| 345 | |||||||||||
| 346 | return Business::TrueLayer::Payment->new( | ||||||||||
| 347 | $response->%{ qw / id status /}, | ||||||||||
| 348 | %request, | ||||||||||
| 349 | |||||||||||
| 350 | host => $self->host, | ||||||||||
| 351 | payment_host => $self->payment_host, | ||||||||||
| 352 | user => Business::TrueLayer::User->new( | ||||||||||
| 353 | $request{user}->%*, | ||||||||||
| 354 | id => $response->{user}{id}, | ||||||||||
| 355 | 1 | ), | |||||||||
| 356 | ); | ||||||||||
| 357 | } | ||||||||||
| 358 | |||||||||||
| 359 | 1 | 1; | |||||||||
| 360 | |||||||||||
| 361 | =head1 SEE ALSO | ||||||||||
| 362 | |||||||||||
| 363 | L<Business::TrueLayer::MerchantAccount> | ||||||||||
| 364 | |||||||||||
| 365 | L<Business::TrueLayer::Payment> | ||||||||||
| 366 | |||||||||||
| 367 | =head1 AUTHORS | ||||||||||
| 368 | |||||||||||
| 369 | Lee Johnson - C<leejo@cpan.org> | ||||||||||
| 370 | |||||||||||
| 371 | Nicholas Clark | ||||||||||
| 372 | |||||||||||
| 373 | =head1 LICENSE | ||||||||||
| 374 | |||||||||||
| 375 | This library is free software; you can redistribute it and/or modify it under | ||||||||||
| 376 | the same terms as Perl itself. If you would like to contribute documentation, | ||||||||||
| 377 | features, bug fixes, or anything else then please raise an issue / pull request: | ||||||||||
| 378 | |||||||||||
| 379 | https://github.com/payprop/business-truelayer | ||||||||||
| 380 | |||||||||||
| 381 | =cut | ||||||||||
| 382 | |||||||||||
| 383 | # vim: ts=4:sw=4:et | ||||||||||