File Coverage

blib/lib/WWW/PayPal/API/Plans.pm
Criterion Covered Total %
statement 36 53 67.9
branch 11 34 32.3
condition 3 6 50.0
subroutine 8 12 66.6
pod 6 6 100.0
total 64 111 57.6


line stmt bran cond sub pod time code
1             package WWW::PayPal::API::Plans;
2              
3             # ABSTRACT: PayPal Billing Plans API (v1)
4              
5 2     2   17 use Moo;
  2         4  
  2         16  
6 2     2   836 use Carp qw(croak);
  2         4  
  2         177  
7 2     2   1150 use WWW::PayPal::Plan;
  2         8  
  2         111  
8 2     2   29 use namespace::clean;
  2         6  
  2         9  
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.plans.create' => { method => 'POST', path => '/v1/billing/plans' },
24             'billing.plans.list' => { method => 'GET', path => '/v1/billing/plans' },
25             'billing.plans.get' => { method => 'GET', path => '/v1/billing/plans/{id}' },
26             'billing.plans.patch' => { method => 'PATCH', path => '/v1/billing/plans/{id}' },
27             'billing.plans.activate' => { method => 'POST', path => '/v1/billing/plans/{id}/activate' },
28             'billing.plans.deactivate' => { method => 'POST', path => '/v1/billing/plans/{id}/deactivate' },
29             };
30             },
31             );
32              
33             with 'WWW::PayPal::Role::OpenAPI';
34              
35             sub _wrap {
36 1     1   4 my ($self, $data) = @_;
37 1         15 return WWW::PayPal::Plan->new(client => $self->client, data => $data);
38             }
39              
40             sub create {
41 1     1 1 6 my ($self, %args) = @_;
42 1 50       4 croak 'product_id required' unless $args{product_id};
43 1 50       4 croak 'name required' unless $args{name};
44 1 50       7 croak 'billing_cycles required' unless ref $args{billing_cycles} eq 'ARRAY';
45              
46             my %body = (
47             product_id => $args{product_id},
48             name => $args{name},
49             billing_cycles => $args{billing_cycles},
50             payment_preferences => $args{payment_preferences} // {
51 1   50     12 auto_bill_outstanding => \1,
52             payment_failure_threshold => 3,
53             },
54             );
55 1 50       4 $body{description} = $args{description} if defined $args{description};
56 1 50       90 $body{status} = $args{status} if defined $args{status};
57 1 50       8 $body{taxes} = $args{taxes} if $args{taxes};
58 1 50       3 $body{quantity_supported} = $args{quantity_supported} if defined $args{quantity_supported};
59              
60 1         7 my $data = $self->call_operation('billing.plans.create', body => \%body);
61 1         59 return $self->_wrap($data);
62             }
63              
64              
65             sub create_monthly {
66 1     1 1 34 my ($self, %args) = @_;
67 1 50       9 croak 'product_id required' unless $args{product_id};
68 1 50       4 croak 'name required' unless $args{name};
69 1 50       4 croak 'price required' unless defined $args{price};
70 1   50     4 my $currency = $args{currency} || 'EUR';
71              
72 1         2 my @cycles;
73 1 50       5 if ($args{trial_days}) {
74             push @cycles, {
75             frequency => { interval_unit => 'DAY', interval_count => 0 + $args{trial_days} },
76 1         13 tenure_type => 'TRIAL',
77             sequence => 1,
78             total_cycles => 1,
79             pricing_scheme => {
80             fixed_price => { value => '0', currency_code => $currency },
81             },
82             };
83             }
84             push @cycles, {
85             frequency => { interval_unit => 'MONTH', interval_count => 1 },
86             tenure_type => 'REGULAR',
87             sequence => scalar(@cycles) + 1,
88 1   50     14 total_cycles => $args{total_cycles} // 0, # 0 = forever
89             pricing_scheme => {
90             fixed_price => { value => "$args{price}", currency_code => $currency },
91             },
92             };
93              
94             return $self->create(
95             product_id => $args{product_id},
96             name => $args{name},
97             description => $args{description},
98 1         7 billing_cycles => \@cycles,
99             );
100             }
101              
102              
103             sub get {
104 0     0 1   my ($self, $id) = @_;
105 0 0         croak 'plan id required' unless $id;
106 0           return $self->_wrap($self->call_operation('billing.plans.get', path => { id => $id }));
107             }
108              
109              
110             sub list {
111 0     0 1   my ($self, %args) = @_;
112 0           my %query;
113 0           for my $k (qw(product_id plan_ids page_size page total_required)) {
114 0 0         $query{$k} = $args{$k} if defined $args{$k};
115             }
116 0 0         my $data = $self->call_operation('billing.plans.list',
117             (%query ? (query => \%query) : ()));
118 0 0         return [ map { $self->_wrap($_) } @{ $data->{plans} || [] } ];
  0            
  0            
119             }
120              
121              
122             sub activate {
123 0     0 1   my ($self, $id) = @_;
124 0 0         croak 'plan id required' unless $id;
125 0           return $self->call_operation('billing.plans.activate', path => { id => $id }, body => '');
126             }
127              
128             sub deactivate {
129 0     0 1   my ($self, $id) = @_;
130 0 0         croak 'plan id required' unless $id;
131 0           return $self->call_operation('billing.plans.deactivate', path => { id => $id }, body => '');
132             }
133              
134              
135             1;
136              
137             __END__