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