File Coverage

blib/lib/Business/GoCardless/Merchant.pm
Criterion Covered Total %
statement 56 58 96.5
branch 3 8 37.5
condition 3 6 50.0
subroutine 16 16 100.0
pod 0 6 0.0
total 78 94 82.9


line stmt bran cond sub pod time code
1             package Business::GoCardless::Merchant;
2              
3             =head1 NAME
4              
5             Business::GoCardless::Merchant
6              
7             =head1 DESCRIPTION
8              
9             A class for a gocardless merchant, extends L
10              
11             =cut
12              
13 19     19   144 use strict;
  19         57  
  19         621  
14 19     19   101 use warnings;
  19         55  
  19         504  
15              
16 19     19   107 use Moo;
  19         41  
  19         114  
17             extends 'Business::GoCardless::Resource';
18              
19 19     19   7312 use Carp qw/ carp /;
  19         65  
  19         1101  
20              
21 19     19   125 use Business::GoCardless::Bill;
  19         39  
  19         643  
22 19     19   7512 use Business::GoCardless::PreAuthorization;
  19         64  
  19         660  
23 19     19   8347 use Business::GoCardless::Payout;
  19         62  
  19         646  
24 19     19   8462 use Business::GoCardless::User;
  19         64  
  19         664  
25 19     19   8714 use Business::GoCardless::Paginator;
  19         79  
  19         10970  
26              
27             =head1 ATTRIBUTES
28              
29             balance
30             created_at
31             description
32             email
33             eur_balance
34             eur_pending_balance
35             first_name
36             gbp_balance
37             gbp_pending_balance
38             hide_variable_amount
39             id
40             last_name
41             name
42             next_payout_amount
43             next_payout_date
44             pending_balance
45             sub_resource_uris
46             uri
47              
48             =cut
49              
50             has [ qw/
51             balance
52             created_at
53             description
54             email
55             eur_balance
56             eur_pending_balance
57             first_name
58             gbp_balance
59             gbp_pending_balance
60             hide_variable_amount
61             id
62             last_name
63             name
64             next_payout_amount
65             next_payout_date
66             pending_balance
67             sub_resource_uris
68             uri
69             / ] => (
70             is => 'rw',
71             );
72              
73             sub BUILD {
74 7     7 0 81 my ( $self ) = @_;
75              
76 7         65 my $data = $self->client->api_get( sprintf( $self->endpoint,$self->id ) );
77              
78 7         19 foreach my $attr ( keys( %{ $data } ) ) {
  7         36  
79 113         163 eval { $self->$attr( $data->{$attr} ); };
  113         293  
80 113 50       218 $@ && do {
81 0         0 carp( "Couldn't set $attr on @{[ ref( $self ) ]}: $@" );
  0         0  
82             };
83             }
84              
85 7         76 return $self;
86             }
87              
88             =head1 List operations on a merchant
89              
90             bills
91             pre_authorizations
92             subscriptions
93             payouts
94             users
95              
96             my @bills = $Merchant->bills( \%filter );
97              
98             Note that these methods marked have a dual interface, when called in list context
99             they will return the first 100 resource objects, when called in scalar context
100             they will return a L object.
101              
102             =cut
103              
104 2     2 0 12 sub bills { shift->_list( 'bills',shift ) }
105 1     1 0 6 sub pre_authorizations { shift->_list( 'pre_authorizations',shift )}
106 1     1 0 5 sub subscriptions { shift->_list( 'subscriptions',shift ) }
107 1     1 0 6 sub payouts { shift->_list( 'payouts',shift ) }
108 1     1 0 9 sub users { shift->_list( 'users',shift ) }
109              
110             sub _list {
111 6     6   17 my ( $self,$endpoint,$filters ) = @_;
112              
113             my $class = {
114             bills => 'Bill',
115             pre_authorizations => 'PreAuthorization',
116             subscriptions => 'Subscription',
117             payouts => 'Payout',
118             users => 'User',
119 6         27 }->{ $endpoint };
120              
121 6   50     19 $filters //= {};
122 6   50     35 $filters->{per_page} ||= 100;
123 6   50     26 $filters->{page} ||= 1;
124              
125 6         36 my $uri = sprintf( $self->endpoint,$self->id ) . "/$endpoint";
126              
127 6 50       14 if ( keys( %{ $filters } ) ) {
  6         20  
128 6         27 $uri .= '?' . $self->client->normalize_params( $filters );
129             }
130              
131 6         26 my ( $data,$links,$info ) = $self->client->api_get( $uri );
132              
133 6         19 $class = "Business::GoCardless::$class";
134 10         103 my @objects = map { $class->new( client => $self->client,%{ $_ } ) }
  10         222  
135 6         18 @{ $data };
  6         14  
136              
137 6 0       202 return wantarray ? ( @objects ) : Business::GoCardless::Paginator->new(
    50          
138             class => $class,
139             client => $self->client,
140             links => $links,
141             info => $info ? JSON->new->decode( $info ) : {},
142             objects => \@objects,
143             );
144             }
145              
146             =head1 AUTHOR
147              
148             Lee Johnson - C
149              
150             This library is free software; you can redistribute it and/or modify it under
151             the same terms as Perl itself. If you would like to contribute documentation,
152             features, bug fixes, or anything else then please raise an issue / pull request:
153              
154             https://github.com/Humanstate/business-gocardless
155              
156             =cut
157              
158             1;
159              
160             # vim: ts=4:sw=4:et