File Coverage

blib/lib/WebService/Braintree/Configuration.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package WebService::Braintree::Configuration;
2             $WebService::Braintree::Configuration::VERSION = '0.92';
3             =head1 NAME
4              
5             WebService::Braintree::Configuration
6              
7             =head1 PURPOSE
8              
9             This keeps all configuration information for your WebService::Braintree
10             installation.
11              
12             A singleton of this class is instantiated when you use L<WebService::Braintree>.
13             You are intended to set attributes of this class immediately, then the rest of
14             the distribution knows what to do.
15              
16             =cut
17              
18 1     1   241 use WebService::Braintree::Gateway;
  0            
  0            
19             use Moose;
20              
21             # IS THIS UNUSED? I cannot find reference in the current documentation for Ruby
22             # or Node.JS nor is it referenced anywhere else in the code.
23             has partner_id => (is => 'rw');
24              
25             =head1 ATTRIBUTES
26              
27             Get these values from L<Braintree's API credentials documentation|https://articles.braintreepayments.com/control-panel/important-gateway-credentials#api-credentials>.
28              
29             These attributes are standard mutators. If you invoke them with a value, they
30             will set the attribute to that value. If you invoke them without a value, they
31             will return the current value.
32              
33             =head2 merchant_id(?$value)
34              
35             This is your merchant_id.
36              
37             =cut
38              
39             has merchant_id => (is => 'rw');
40              
41             =head2 public_key(?$value)
42              
43             This is your public_key.
44              
45             =cut
46              
47             has public_key => (is => 'rw');
48              
49             =head2 private_key(?$value)
50              
51             This is your private_key.
52              
53             =cut
54              
55             has private_key => (is => 'rw');
56              
57             =head2 environment(?$value)
58              
59             This is your environment. The environment can be:
60              
61             =over 4
62              
63             =item development | integration
64              
65             This is when you're using a local server for testing. It's unlikely you will
66             ever want to use this.
67              
68             =item sandbox
69              
70             This is when you're using your Braintree sandbox for testing.
71              
72             =item qa
73              
74             This is when you're using qa-master.braintreegateway.com for testing.
75              
76             =item production
77              
78             This is when you're live and rocking.
79              
80             =back
81              
82             If you provide a value other than the ones listed above, a warning will be
83             thrown. This distribution will probably not work properly in that case.
84              
85             Management reserves the right to change this from a warning to a thrown
86             exception in the future.
87              
88             =head1 USAGE
89              
90             Do yourself a favor and store these values in a configuration file, not your
91             source-controlled code.
92              
93             =cut
94              
95             has environment => (
96             is => 'rw',
97             trigger => sub {
98             my ($self, $new_value, $old_value) = @_;
99             if ($new_value !~ /integration|development|sandbox|production|qa/) {
100             warn "Assigned invalid value to WebService::Braintree::Configuration::environment";
101             }
102             if ($new_value eq "integration") {
103             $self->public_key("integration_public_key");
104             $self->private_key("integration_private_key");
105             $self->merchant_id("integration_merchant_id");
106             }
107             }
108             );
109              
110             has gateway => (is => 'ro', lazy => 1, default => sub {
111             WebService::Braintree::Gateway->new({config => shift})
112             });
113              
114             sub base_merchant_path {
115             my $self = shift;
116             return "/merchants/" . $self->merchant_id;
117             }
118              
119             sub base_merchant_url {
120             my $self = shift;
121             return $self->base_url() . $self->base_merchant_path;
122             }
123              
124             sub base_url {
125             my $self = shift;
126             return $self->protocol . "://" . $self->server . ':' . $self->port;
127             }
128              
129             sub port {
130             my $self = shift;
131             if ($self->environment =~ /integration|development/) {
132             return $ENV{'GATEWAY_PORT'} || "3000"
133             } else {
134             return "443";
135             }
136             }
137              
138             sub server {
139             my $self = shift;
140             return "localhost" if $self->environment eq 'integration';
141             return "localhost" if $self->environment eq 'development';
142             return "api.sandbox.braintreegateway.com" if $self->environment eq 'sandbox';
143             return "api.braintreegateway.com" if $self->environment eq 'production';
144             return "qa-master.braintreegateway.com" if $self->environment eq 'qa';
145             }
146              
147             sub auth_url {
148             my $self = shift;
149             return "http://auth.venmo.dev:9292" if $self->environment eq 'integration';
150             return "http://auth.venmo.dev:9292" if $self->environment eq 'development';
151             return "https://auth.sandbox.venmo.com" if $self->environment eq 'sandbox';
152             return "https://auth.venmo.com" if $self->environment eq 'production';
153             return "https://auth.qa.venmo.com" if $self->environment eq 'qa';
154             }
155              
156             sub ssl_enabled {
157             my $self = shift;
158             return ($self->environment !~ /integration|development/);
159             }
160              
161             sub protocol {
162             my $self = shift;
163             return $self->ssl_enabled ? 'https' : 'http';
164             }
165              
166             =head1 METHODS
167              
168             There is one read-only method.
169              
170             =head2 api_version()
171              
172             This returns the Braintree API version this distribution speaks.
173              
174             =cut
175              
176             sub api_version {
177             return "4";
178             }
179              
180             __PACKAGE__->meta->make_immutable;
181              
182             1;
183             __END__