Braintree/TransparentRedirectGateway.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Braintree/DigestSHA256.pm ↑ Summary Braintree/Gateway.pm »
1 package Net::Braintree::TransparentRedirectGateway;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::Braintree::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::Braintree::Digest qw(hexdigest);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::Braintree::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::Braintree::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use Net::Braintree::TransparentRedirect::QueryString;
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 has 'gateway' => (is => 'ro');
13  
14 sub confirm {
15 my ($self, $query_string) = @_;
16 my $qs = Net::Braintree::TransparentRedirect::QueryString->new(config => $self->gateway->config);
17 $qs->validate($query_string);
18 my $params = $qs->parse($query_string);
19 $self->_make_request("/transparent_redirect_requests/". $params->{id} . "/confirm", "post", undef);
20 }
21  
22 sub transaction_data {
23 my ($self, $params) = @_;
24 $self->requires($params, qw(redirect_url transaction));
25 $self->requires_type($params);
26 $params->{'kind'} = 'create_transaction';
27 return $self->data_string($params);
28 }
29  
30 sub create_customer_data {
31 my ($self, $params) = @_;
32 $self->requires($params, qw(redirect_url));
33 $params->{'kind'} = 'create_customer';
34 return $self -> data_string($params);
35 }
36  
37 sub update_customer_data {
38 my ($self, $params) = @_;
39 $self->requires($params, qw(redirect_url customer_id));
40 $params->{'kind'} = 'update_customer';
41 return $self->data_string($params);
42 }
43  
44 sub create_credit_card_data {
45 my ($self, $params) = @_;
46 $self->requires($params, qw(redirect_url credit_card));
47 confess "ArgumentError: credit card must be associated with a customer" unless $params->{'credit_card'}->{'customer_id'};
48 $params->{'kind'} = 'create_payment_method';
49 return $self->data_string($params);
50 }
51  
52 sub update_credit_card_data {
53 my ($self, $params) = @_;
54 $self->requires($params, qw(redirect_url payment_method_token));
55 $params->{'kind'} = 'update_payment_method';
56 return $self->data_string($params);
57 }
58  
59 sub url {
60 return shift->gateway->config->base_merchant_url . "/transparent_redirect_requests";
61 }
62  
63 sub requires {
64 my ($self, $params, @required) = @_;
65 foreach my $field (@required) {
66 confess "ArgumentError: $field is required" unless $params->{$field}
67 }
68 }
69  
70 sub requires_type {
71 my ($self, $params) = @_;
72 confess "ArgumentError: Transaction type is required" unless $params->{'transaction'}->{'type'} ;
73 if ($params->{'transaction'}->{'type'} ne 'sale' &&
74 $params->{'transaction'}->{'type'} ne 'credit') {
75 confess "ArgumentError: Transaction type must be credit or sale";
76 }
77 }
78  
79 sub time_string {
80 my $dt = DateTime->now;
81 return $dt -> strftime("%Y%m%d%H%M%S");
82 }
83  
84 sub data_string {
85 my ($self, $params) = @_;
86 $params->{'api_version'} = $self->gateway->config->api_version;
87 $params->{'time'} = time_string;
88 $params->{'public_key'} = $self->gateway->config->public_key;
89 return $self->build_tr_data($params);
90 }
91  
92 sub build_tr_data {
93 my ($self, $params) = @_;
94 my $query = hash_to_query_string($params);
95 my $tr_hash = hexdigest($self->gateway->config->private_key, $query);
96 return "$tr_hash|$query";
97 }
98  
99 sub _make_request {
100 my($self, $path, $verb, $params) = @_;
101 my $response = $self->gateway->http->$verb($path, $params);
102 my $result = Net::Braintree::Result->new(response => $response);
103 return $result;
104 }
105  
106 1;
« Braintree/DigestSHA256.pm ↑ Summary Braintree/Gateway.pm »