File Coverage

blib/lib/WWW/PayPal/API/Subscriptions.pm
Criterion Covered Total %
statement 13 56 23.2
branch 0 38 0.0
condition 0 8 0.0
subroutine 5 14 35.7
pod 7 7 100.0
total 25 123 20.3


line stmt bran cond sub pod time code
1             package WWW::PayPal::API::Subscriptions;
2              
3             # ABSTRACT: PayPal Billing Subscriptions API (v1)
4              
5 2     2   15 use Moo;
  2         4  
  2         16  
6 2     2   935 use Carp qw(croak);
  2         4  
  2         143  
7 2     2   1126 use WWW::PayPal::Subscription;
  2         9  
  2         83  
8 2     2   15 use namespace::clean;
  2         3  
  2         11  
9              
10             our $VERSION = '0.002';
11              
12              
13             has client => (
14             is => 'ro',
15             required => 1,
16             weak_ref => 1,
17             );
18              
19             has openapi_operations => (
20             is => 'lazy',
21             builder => sub {
22             return {
23 1     1   30 'billing.subscriptions.create' => { method => 'POST', path => '/v1/billing/subscriptions' },
24             'billing.subscriptions.get' => { method => 'GET', path => '/v1/billing/subscriptions/{id}' },
25             'billing.subscriptions.patch' => { method => 'PATCH', path => '/v1/billing/subscriptions/{id}' },
26             'billing.subscriptions.revise' => { method => 'POST', path => '/v1/billing/subscriptions/{id}/revise' },
27             'billing.subscriptions.suspend' => { method => 'POST', path => '/v1/billing/subscriptions/{id}/suspend' },
28             'billing.subscriptions.activate' => { method => 'POST', path => '/v1/billing/subscriptions/{id}/activate' },
29             'billing.subscriptions.cancel' => { method => 'POST', path => '/v1/billing/subscriptions/{id}/cancel' },
30             'billing.subscriptions.capture' => { method => 'POST', path => '/v1/billing/subscriptions/{id}/capture' },
31             'billing.subscriptions.transactions' => { method => 'GET', path => '/v1/billing/subscriptions/{id}/transactions' },
32             };
33             },
34             );
35              
36             with 'WWW::PayPal::Role::OpenAPI';
37              
38             sub _wrap {
39 0     0     my ($self, $data) = @_;
40 0           return WWW::PayPal::Subscription->new(client => $self->client, data => $data);
41             }
42              
43             sub create {
44 0     0 1   my ($self, %args) = @_;
45 0 0         croak 'plan_id required' unless $args{plan_id};
46              
47 0           my %body = ( plan_id => $args{plan_id} );
48 0 0         $body{subscriber} = $args{subscriber} if $args{subscriber};
49 0 0         $body{quantity} = $args{quantity} if defined $args{quantity};
50 0 0         $body{custom_id} = $args{custom_id} if defined $args{custom_id};
51 0 0         $body{start_time} = $args{start_time} if defined $args{start_time};
52 0 0         $body{shipping_amount} = $args{shipping_amount} if $args{shipping_amount};
53              
54 0           my %ctx;
55 0 0         $ctx{return_url} = $args{return_url} if $args{return_url};
56 0 0         $ctx{cancel_url} = $args{cancel_url} if $args{cancel_url};
57 0 0         $ctx{brand_name} = $args{brand_name} if $args{brand_name};
58 0 0         $ctx{locale} = $args{locale} if $args{locale};
59 0   0       $ctx{user_action} = $args{user_action} // 'SUBSCRIBE_NOW';
60             $ctx{shipping_preference} = $args{shipping_preference}
61 0 0         if $args{shipping_preference};
62 0 0         $body{application_context} = \%ctx if %ctx;
63              
64 0           my $data = $self->call_operation('billing.subscriptions.create', body => \%body);
65 0           return $self->_wrap($data);
66             }
67              
68              
69             sub get {
70 0     0 1   my ($self, $id) = @_;
71 0 0         croak 'subscription id required' unless $id;
72 0           return $self->_wrap($self->call_operation('billing.subscriptions.get', path => { id => $id }));
73             }
74              
75              
76             sub _action_with_reason {
77 0     0     my ($self, $op, $id, %args) = @_;
78 0 0         croak 'subscription id required' unless $id;
79 0   0       my $reason = $args{reason} // 'not specified';
80 0           return $self->call_operation($op,
81             path => { id => $id },
82             body => { reason => $reason },
83             );
84             }
85              
86 0     0 1   sub suspend { my $s = shift; $s->_action_with_reason('billing.subscriptions.suspend', @_) }
  0            
87 0     0 1   sub activate { my $s = shift; $s->_action_with_reason('billing.subscriptions.activate', @_) }
  0            
88 0     0 1   sub cancel { my $s = shift; $s->_action_with_reason('billing.subscriptions.cancel', @_) }
  0            
89              
90              
91             sub capture {
92 0     0 1   my ($self, $id, %args) = @_;
93 0 0         croak 'subscription id required' unless $id;
94 0 0         croak 'amount required' unless $args{amount};
95             my $body = {
96             note => $args{note} // 'Outstanding balance',
97             capture_type => $args{capture_type} // 'OUTSTANDING_BALANCE',
98             amount => $args{amount},
99 0   0       };
      0        
100 0           return $self->call_operation('billing.subscriptions.capture',
101             path => { id => $id },
102             body => $body,
103             );
104             }
105              
106              
107             sub transactions {
108 0     0 1   my ($self, $id, %args) = @_;
109 0 0         croak 'subscription id required' unless $id;
110 0 0         croak 'start_time required' unless $args{start_time};
111 0 0         croak 'end_time required' unless $args{end_time};
112             return $self->call_operation('billing.subscriptions.transactions',
113             path => { id => $id },
114             query => {
115             start_time => $args{start_time},
116             end_time => $args{end_time},
117             },
118 0           );
119             }
120              
121              
122             1;
123              
124             __END__