File Coverage

blib/lib/WebService/Braintree/TransparentRedirectGateway.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package WebService::Braintree::TransparentRedirectGateway;
2             $WebService::Braintree::TransparentRedirectGateway::VERSION = '0.93';
3 1     1   10 use Moose;
  1         6  
  1         14  
4             with 'WebService::Braintree::Role::MakeRequest';
5              
6 1     1   13939 use Carp qw(confess);
  1         4  
  1         133  
7 1     1   1029 use DateTime;
  1         588043  
  1         47  
8 1     1   8 use WebService::Braintree::Util;
  1         2  
  1         97  
9 1     1   417 use WebService::Braintree::Digest qw(hexdigest);
  1         8  
  1         97  
10 1     1   478 use WebService::Braintree::HTTP;
  0            
  0            
11             use WebService::Braintree::Result;
12             use WebService::Braintree::TransparentRedirect::QueryString;
13              
14             has 'gateway' => (is => 'ro');
15              
16             sub confirm {
17             my ($self, $query_string) = @_;
18             my $qs = WebService::Braintree::TransparentRedirect::QueryString->new(config => $self->gateway->config);
19             $qs->validate($query_string);
20             my $params = $qs->parse($query_string);
21             $self->_make_request("/transparent_redirect_requests/". $params->{id} . "/confirm", "post", undef);
22             }
23              
24             sub transaction_data {
25             my ($self, $params) = @_;
26             $self->requires($params, qw(redirect_url transaction));
27             $self->requires_type($params);
28             $params->{'kind'} = 'create_transaction';
29             return $self->data_string($params);
30             }
31              
32             sub create_customer_data {
33             my ($self, $params) = @_;
34             $self->requires($params, qw(redirect_url));
35             $params->{'kind'} = 'create_customer';
36             return $self -> data_string($params);
37             }
38              
39             sub update_customer_data {
40             my ($self, $params) = @_;
41             $self->requires($params, qw(redirect_url customer_id));
42             $params->{'kind'} = 'update_customer';
43             return $self->data_string($params);
44             }
45              
46             sub create_credit_card_data {
47             my ($self, $params) = @_;
48             $self->requires($params, qw(redirect_url credit_card));
49             confess "ArgumentError: credit card must be associated with a customer" unless $params->{'credit_card'}->{'customer_id'};
50             $params->{'kind'} = 'create_payment_method';
51             return $self->data_string($params);
52             }
53              
54             sub update_credit_card_data {
55             my ($self, $params) = @_;
56             $self->requires($params, qw(redirect_url payment_method_token));
57             $params->{'kind'} = 'update_payment_method';
58             return $self->data_string($params);
59             }
60              
61             sub url {
62             return shift->gateway->config->base_merchant_url . "/transparent_redirect_requests";
63             }
64              
65             sub requires {
66             my ($self, $params, @required) = @_;
67             foreach my $field (@required) {
68             confess "ArgumentError: $field is required" unless $params->{$field}
69             }
70             }
71              
72             sub requires_type {
73             my ($self, $params) = @_;
74             confess "ArgumentError: Transaction type is required" unless $params->{'transaction'}->{'type'} ;
75             if ($params->{'transaction'}->{'type'} ne 'sale' &&
76             $params->{'transaction'}->{'type'} ne 'credit') {
77             confess "ArgumentError: Transaction type must be credit or sale";
78             }
79             }
80              
81             sub time_string {
82             my $dt = DateTime->now;
83             return $dt -> strftime("%Y%m%d%H%M%S");
84             }
85              
86             sub data_string {
87             my ($self, $params) = @_;
88             $params->{'api_version'} = $self->gateway->config->api_version;
89             $params->{'time'} = time_string;
90             $params->{'public_key'} = $self->gateway->config->public_key;
91             return $self->build_tr_data($params);
92             }
93              
94             sub build_tr_data {
95             my ($self, $params) = @_;
96             my $query = hash_to_query_string($params);
97             my $tr_hash = hexdigest($self->gateway->config->private_key, $query);
98             return "$tr_hash|$query";
99             }
100              
101             __PACKAGE__->meta->make_immutable;
102             1;
103