Skip to content

Commit

Permalink
Merge pull request #15 from payjp/nyamanaka/add_3ds_features
Browse files Browse the repository at this point in the history
add 3DS features(token tds_finish, ThreeDSecureRequest)
  • Loading branch information
natsuki-yamanaka authored Nov 15, 2024
2 parents b8fa7f3 + 3347539 commit d1deec6
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
47 changes: 46 additions & 1 deletion lib/Net/Payjp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use Net::Payjp::Statement;
use Net::Payjp::Balance;
use Net::Payjp::Term;
use Net::Payjp::Object;
use Net::Payjp::ThreeDSecureRequest;

# ABSTRACT: API client for pay.jp

Expand Down Expand Up @@ -67,7 +68,7 @@ This is required. You get this from your Payjp Account settings.
=cut

our $VERSION = '0.3.1';
our $VERSION = '0.4.0';
our $API_BASE = 'https://api.pay.jp';
our $INITIAL_DELAY_SEC = 2;
our $MAX_DELAY_SEC = 32;
Expand Down Expand Up @@ -446,6 +447,15 @@ L<https://pay.jp/docs/api/#トークン情報を取得>
$payjp->token->retrieve('tok_eff34b780cbebd61e87f09ecc9c6');
=head2 tds_finish
Finish 3D-Secure flow of token
L<https://pay.jp/docs/api/#%E3%83%88%E3%83%BC%E3%82%AF%E3%83%B3%E3%81%AB%E5%AF%BE%E3%81%99%E3%82%8B3d%E3%82%BB%E3%82%AD%E3%83%A5%E3%82%A2%E3%83%95%E3%83%AD%E3%83%BC%E3%82%92%E5%AE%8C%E4%BA%86%E3%81%99%E3%82%8B>
$payjp->id('tok_xxxxxx');
$payjp->token->tds_finish();
=cut

sub token{
Expand Down Expand Up @@ -489,6 +499,41 @@ sub transfer{
return Net::Payjp::Transfer->new(%$self);
}

=head1 ThreeDSecureRequest Methods
=head2 create
Create a three_d_secure_request
L<https://pay.jp/docs/api/#3d%E3%82%BB%E3%82%AD%E3%83%A5%E3%82%A2%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E3%82%92%E4%BD%9C%E6%88%90>
$payjp->three_d_secure_request->create(
"resource_id" => "car_xxxx",
);
=head2 retrieve
Retrieve a three_d_secure_request
L<https://pay.jp/docs/api/#3d%E3%82%BB%E3%82%AD%E3%83%A5%E3%82%A2%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E6%83%85%E5%A0%B1%E3%82%92%E5%8F%96%E5%BE%97>
$payjp->three_d_secure_request->retrieve('tdsr_xxxx');
=head2 all
Returns the three_d_secure_request list
L<https://pay.jp/docs/api/#3d%E3%82%BB%E3%82%AD%E3%83%A5%E3%82%A2%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E3%83%AA%E3%82%B9%E3%83%88%E3%82%92%E5%8F%96%E5%BE%97>
$res = $payjp->three_d_secure_request->all(limit => 2, offset => 1);
=cut

sub three_d_secure_request{
my $self = shift;
return Net::Payjp::ThreeDSecureRequest->new(%$self);
}

=head1 Event Methods
=head2 retrieve
Expand Down
34 changes: 34 additions & 0 deletions lib/Net/Payjp/ThreeDSecureRequest.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package Net::Payjp::ThreeDSecureRequest;

use strict;
use warnings;

use base 'Net::Payjp';

sub create{
my $self = shift;
my %p = @_;

$self->_request(method => 'POST', url => $self->_class_url, param => \%p);
}

sub retrieve{
my $self = shift;
my $id = shift;
$self->id($id) if $id;

$self->_request(method => 'GET', url => $self->_instance_url);
}

sub all{
my $self = shift;
my %p = @_;

$self->_request(method => 'GET', url => $self->_class_url, param => \%p);
}

sub _class_url{
my $self = shift;
return $self->api_base.'/v1/three_d_secure_requests';
}
1;
6 changes: 6 additions & 0 deletions lib/Net/Payjp/Token.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ sub retrieve{
$self->_request(method => 'GET', url => $self->_instance_url);
}

sub tds_finish{
my $self = shift;

$self->_request(method => 'POST', url => $self->_instance_url.'/tds_finish');
}

1;
40 changes: 40 additions & 0 deletions t/three_d_secure_request.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/perl

use strict;
use warnings;

use Test::Mock::LWP;
use Net::Payjp;
use Test::More tests => 11;

my $payjp = Net::Payjp->new(api_key => 'api_key');

isa_ok($payjp->three_d_secure_request, 'Net::Payjp::ThreeDSecureRequest');
can_ok($payjp->three_d_secure_request, qw(retrieve create all));

$Mock_resp->mock( content => sub { '{"id":"tdsr_xxx"}' } );
$Mock_resp->mock( code => sub {200} );
$Mock_ua->mock( timeout => sub {} );
$Mock_ua->mock( default_header => sub {} );

#Create
$Mock_req->mock( content => sub {
my $p = $_[1];
is($p, 'resource_id=car_xxxx');
} );
my $tds_request = $payjp->three_d_secure_request;
my $res = $tds_request->create(resource_id => 'car_xxxx');
is($Mock_req->{new_args}[1], 'POST');
is($Mock_req->{new_args}[2], 'https://api.pay.jp/v1/three_d_secure_requests');
is($res->id, 'tdsr_xxx');
is($tds_request->id, 'tdsr_xxx');

#Retrieve
$tds_request->retrieve;
is($Mock_req->{new_args}[1], 'GET');
is($Mock_req->{new_args}[2], 'https://api.pay.jp/v1/three_d_secure_requests/tdsr_xxx');

#tds_finish
$tds_request->all;
is($Mock_req->{new_args}[1], 'GET');
is($Mock_req->{new_args}[2], 'https://api.pay.jp/v1/three_d_secure_requests');
9 changes: 7 additions & 2 deletions t/token.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use warnings;

use Test::Mock::LWP;
use Net::Payjp;
use Test::More tests => 12;
use Test::More tests => 14;

my $payjp = Net::Payjp->new(api_key => 'api_key');

isa_ok($payjp->token, 'Net::Payjp::Token');
can_ok($payjp->token, qw(retrieve create));
can_ok($payjp->token, qw(retrieve create tds_finish));
ok(!$payjp->token->can('all'));
ok(!$payjp->token->can('save'));
ok(!$payjp->token->can('delete'));
Expand All @@ -36,3 +36,8 @@ is($token->id, 'res1');
$token->retrieve;
is($Mock_req->{new_args}[1], 'GET');
is($Mock_req->{new_args}[2], 'https://api.pay.jp/v1/tokens/res1');

#tds_finish
$token->tds_finish;
is($Mock_req->{new_args}[1], 'POST');
is($Mock_req->{new_args}[2], 'https://api.pay.jp/v1/tokens/res1/tds_finish');

0 comments on commit d1deec6

Please sign in to comment.