Business-GoCardless
view release on metacpan or search on metacpan
lib/Business/GoCardless/Client.pm view on Meta::CPAN
my ( $self,$params ) = @_;
my $data = $self->api_post(
'/redirect_flows',
{ redirect_flows => { %{ $params } } },
);
my $RedirectFlow = Business::GoCardless::RedirectFlow->new(
client => $self,
%{ $data->{redirect_flows} }
);
return $RedirectFlow->redirect_url;
}
sub _confirm_redirect_flow {
my ( $self,$redirect_flow_id ) = @_;
# first find the original session token
my $RedirectFlow = Business::GoCardless::RedirectFlow->new(
client => $self,
id => $redirect_flow_id,
);
$RedirectFlow->find_with_client( 'redirect_flows' );
# now confirm the redirect flow
my $data = $self->api_post(
"/redirect_flows/$redirect_flow_id/actions/complete",
{ data => { session_token => $RedirectFlow->session_token } },
);
$RedirectFlow = Business::GoCardless::RedirectFlow->new(
client => $self,
%{ $data->{redirect_flows} }
);
return $RedirectFlow;
}
sub _confirm_resource {
my ( $self,$params ) = @_;
if ( ! $self->signature_valid( $params,$self->app_secret ) ) {
Business::GoCardless::Exception->throw({
message => "Invalid signature for confirm_resource"
});
}
my $data = {
resource_id => $params->{resource_id},
resource_type => $params->{resource_type},
};
my $credentials = encode_base64( $self->app_id . ':' . $self->app_secret );
$credentials =~ s/\s//g;
my $ua = LWP::UserAgent->new;
$ua->agent( $self->user_agent );
my $req = HTTP::Request->new(
POST => join( '/',$self->base_url . $self->api_path,'confirm' )
);
$req->header( 'Authorization' => "Basic $credentials" );
$req->header( 'Accept' => 'application/json' );
$req->content_type( 'application/x-www-form-urlencoded' );
$req->content( $self->normalize_params( $data ) );
my $res = $ua->request( $req );
if ( $res->is_success ) {
my $class_suffix = ucfirst( $params->{resource_type} );
$class_suffix =~ s/_([A-z])/uc($1)/ge;
my $class = "Business::GoCardless::$class_suffix";
my $obj = $class->new(
client => $self,
id => $params->{resource_id}
);
return $obj->find_with_client;
}
else {
Business::GoCardless::Exception->throw({
message => $res->content,
code => $res->code,
response => $res->status_line,
});
}
}
=head1 METHODS
api_get
api_post
api_put
Make a request to the gocardless API:
my $data = $Client->api_get( '/merchants/123ABCD/bills',\%params );
In list context returns the links and pagination headers:
my ( $data,$links,$info ) = $Client->api_get( ... );
=cut
sub api_get {
my ( $self,$path,$params ) = @_;
return $self->_api_request( 'GET',$path,$params );
}
sub api_post {
my ( $self,$path,$params ) = @_;
return $self->_api_request( 'POST',$path,$params );
}
sub api_put {
my ( $self,$path,$params ) = @_;
return $self->_api_request( 'PUT',$path,$params );
}
sub _api_request {
my ( $self,$method,$path,$params ) = @_;
my $ua = LWP::UserAgent->new;
$ua->agent( $self->user_agent );
my $req = HTTP::Request->new(
# passing through the absolute URL means we don't build it
$method => my $uri = $path =~ /^http/
? $path : join( '/',$self->base_url . $self->api_path . $path ),
);
say STDERR "GOCARDLESS -> $uri" if $ENV{GOCARDLESS_DEBUG};
$req->header( 'Authorization' => "Bearer " . $self->token );
$req->header( 'Accept' => 'application/json' );
if ( $self->api_version > 1 ) {
# pegged to a specific version for this library and not user controlled
# https://developer.gocardless.com/api-reference/#making-requests-versions
$req->header( 'GoCardless-Version' => '2015-07-06' );
}
if ( $method =~ /POST|PUT/ ) {
if ( $self->api_version > 1 ) {
$req->content_type( 'application/json' );
my $json;
$json = JSON->new->utf8->canonical->encode( $params ) if $params;
$req->content( $json ) if $json;
$req->header( 'Content-Length' => 0 ) if ! $json; # always have a content length
say STDERR "GOCARDLESS -> $json" if $ENV{GOCARDLESS_DEBUG} && $json;
} else {
$req->content_type( 'application/x-www-form-urlencoded' );
$req->content( my $normalize_params = $self->normalize_params( $params ) );
say STDERR "GOCARDLESS -> $normalize_params" if $ENV{GOCARDLESS_DEBUG} && $normalize_params;
}
}
my $res = $ua->request( $req );
if ( $res->is_success ) {
my $data = JSON->new->canonical->decode( my $json = $res->content );
say STDERR "GOCARDLESS <- $json" if $ENV{GOCARDLESS_DEBUG};
my $links = $res->header( 'link' );
my $info = $res->header( 'x-pagination' );
return wantarray ? ( $data,$links,$info ) : $data;
}
else {
Business::GoCardless::Exception->throw({
message => $res->content,
code => $res->code,
response => $res->status_line,
});
}
}
=head1 AUTHOR
Lee Johnson - C<leejo@cpan.org>
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. If you would like to contribute documentation,
features, bug fixes, or anything else then please raise an issue / pull request:
https://github.com/Humanstate/business-gocardless
=cut
( run in 1.412 second using v1.01-cache-2.11-cpan-5837b0d9d2c )