| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Business::CPI::Role::Gateway::FormCheckout; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Provide a checkout with simple HTML forms | 
| 3 | 6 |  |  | 6 |  | 3784 | use utf8; | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 54 |  | 
| 4 | 6 |  |  | 6 |  | 194 | use Moo::Role; | 
|  | 6 |  |  |  |  | 11 |  | 
|  | 6 |  |  |  |  | 44 |  | 
| 5 | 6 |  |  | 6 |  | 7915 | use HTML::Element; | 
|  | 6 |  |  |  |  | 124263 |  | 
|  | 6 |  |  |  |  | 37 |  | 
| 6 | 6 |  |  | 6 |  | 290 | use Data::Dumper; | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 3651 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.922'; # VERSION | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | requires 'map_object', 'get_hidden_inputs', 'checkout_url'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | has checkout_form_http_method => ( | 
| 13 |  |  |  |  |  |  | is => 'ro', | 
| 14 |  |  |  |  |  |  | default => sub { 'post' }, | 
| 15 |  |  |  |  |  |  | ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has checkout_form_submit_name => ( | 
| 18 |  |  |  |  |  |  | is => 'ro', | 
| 19 |  |  |  |  |  |  | default => sub { 'submit' }, | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | has checkout_form_submit_value => ( | 
| 23 |  |  |  |  |  |  | is => 'ro', | 
| 24 |  |  |  |  |  |  | default => sub { '' }, | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has checkout_form_submit_image => ( | 
| 28 |  |  |  |  |  |  | is => 'ro', | 
| 29 |  |  |  |  |  |  | default => sub { '' }, | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | has form_encoding => ( | 
| 33 |  |  |  |  |  |  | is      => 'ro', | 
| 34 |  |  |  |  |  |  | # TODO: use Encode::find_encoding() | 
| 35 |  |  |  |  |  |  | default => sub { 'UTF-8' }, | 
| 36 |  |  |  |  |  |  | ); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub _get_hidden_inputs_for_cart { | 
| 39 | 0 |  |  | 0 |  | 0 | my ($self, $cart) = @_; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 0 |  |  |  |  | 0 | return $self->map_object( $self->_checkout_form_cart_map, $cart ); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub _get_hidden_inputs_for_buyer { | 
| 45 | 0 |  |  | 0 |  | 0 | my ($self, $buyer) = @_; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 0 |  |  |  |  | 0 | return $self->map_object( $self->_checkout_form_buyer_map, $buyer ); | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub _get_hidden_inputs_for_items { | 
| 51 | 0 |  |  | 0 |  | 0 | my ($self, $items) = @_; | 
| 52 | 0 |  |  |  |  | 0 | my @result; | 
| 53 | 0 |  |  |  |  | 0 | my $i = 1; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  | 0 | for my $item (@$items) { | 
| 56 | 0 |  |  |  |  | 0 | push @result, | 
| 57 |  |  |  |  |  |  | $self->map_object( $self->_checkout_form_item_map( $i++ ), $item ); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  | 0 | return @result; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub _get_hidden_inputs_main { | 
| 64 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  | 0 | return $self->map_object( $self->_checkout_form_main_map, $self ); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub get_form { | 
| 70 | 3 |  |  | 3 | 1 | 7 | my ($self, $info) = @_; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 3 |  |  |  |  | 39 | $self->log->info("Get form for payment " . $info->{payment_id}); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 3 |  |  |  |  | 18 | my @hidden_inputs = $self->get_hidden_inputs($info); | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 3 | 50 |  |  |  | 23 | if ($self->log->is_debug) { | 
| 77 | 0 |  |  |  |  | 0 | $self->log->debug("Building form with inputs: " . Dumper(\@hidden_inputs)); | 
| 78 | 0 |  |  |  |  | 0 | $self->log->debug("form action => " . $self->checkout_url); | 
| 79 | 0 |  |  |  |  | 0 | $self->log->debug("form method => " . $self->checkout_form_http_method); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 3 |  |  |  |  | 65 | my $form = HTML::Element->new( | 
| 83 |  |  |  |  |  |  | 'form', | 
| 84 |  |  |  |  |  |  | action => $self->checkout_url, | 
| 85 |  |  |  |  |  |  | method => $self->checkout_form_http_method, | 
| 86 |  |  |  |  |  |  | ); | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 3 |  |  |  |  | 135 | while (@hidden_inputs) { | 
| 89 | 69 |  |  |  |  | 2090 | $form->push_content( | 
| 90 |  |  |  |  |  |  | HTML::Element->new( | 
| 91 |  |  |  |  |  |  | 'input', | 
| 92 |  |  |  |  |  |  | type  => 'hidden', | 
| 93 |  |  |  |  |  |  | value => pop @hidden_inputs, | 
| 94 |  |  |  |  |  |  | name  => pop @hidden_inputs | 
| 95 |  |  |  |  |  |  | ) | 
| 96 |  |  |  |  |  |  | ); | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 3 |  |  |  |  | 116 | my %submit = ( | 
| 100 |  |  |  |  |  |  | name  => $self->checkout_form_submit_name, | 
| 101 |  |  |  |  |  |  | type  => 'submit', | 
| 102 |  |  |  |  |  |  | ); | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 3 | 50 |  |  |  | 17 | if (my $value = $self->checkout_form_submit_value) { | 
| 105 | 0 |  |  |  |  | 0 | $submit{value} = $value; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 3 | 50 |  |  |  | 27 | if (my $src = $self->checkout_form_submit_image) { | 
| 109 | 0 |  |  |  |  | 0 | $submit{src}  = $src; | 
| 110 | 0 |  |  |  |  | 0 | $submit{type} = 'image'; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | $form->push_content( | 
| 114 | 3 |  |  |  |  | 12 | HTML::Element->new( 'input', %submit ) | 
| 115 |  |  |  |  |  |  | ); | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 3 |  |  |  |  | 105 | return $form; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | 1; | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | __END__ |