        <div class="col-md-12">
            <?php
            echo (isset($message) ? $message : null);

            $this->Form->create(null, ['class' => 'disable-on-submit']);
            $this->Form->fieldHidden('pay_with', (isset($vars->pay_with) ? $vars->pay_with : null), ['id' => 'pay_with']);

            $this->WidgetClient->clear();
            $this->WidgetClient->create($this->_('ClientPay.method.boxtitle_method', true), ['id' => 'client_method'], (isset($render_section) ? $render_section : null));
            $this->WidgetClient->startBody();

            // Determine how many columns to draw
            $column_widths = 12;
            $columns = 3;

            if (empty($payment_accounts) || count($payment_accounts) <= 1 || (isset($require_passphrase) ? $require_passphrase : null)) {
                $columns--;
            }
            if (empty($payment_types) || count($payment_types) <= 1) {
                $columns--;
            }
            if (empty($nm_gateways)) {
                $columns--;
            }

            $column_widths /= ($columns == 0 ? 1 : $columns);

            // No payment options available
            if ($columns == 0) {
            ?>
            <div class="alert alert-info">
                <?php $this->_('ClientPay.method.no_options');?>
            </div>
            <?php
            }

            // If the invoice has already been paid, remove all payment methods
            if ((isset($invoice_paid) ? $invoice_paid : null)) {
                $payment_accounts = [];
                $payment_types = [];
                $nm_gateways = [];
                $columns = 0;
            }

            // Set payment option container
            if ($columns > 0) {
            ?>
            <div class="row mb-2">
            <?php
            }

            // Show column for payment accounts, if any
            if (!empty($payment_accounts) && count($payment_accounts) > 1 && !(isset($require_passphrase) ? $require_passphrase : null)) {
            ?>
                <div class="col-md-<?php (print (isset($column_widths) ? $this->Html->safe($column_widths) : null));?><?php echo $columns > 1 ? ' col-border' : '';?>">
                    <h4><?php $this->_('ClientPay.method.field_useaccount');?></h4>
                    <?php
                    $this->Form->fieldSelect('payment_account', (isset($payment_accounts) ? $payment_accounts : null), (isset($vars->payment_account) ? $vars->payment_account : null), ['id' => 'account', 'class' => 'form-control form-control-sm']);
                    ?>
                </div>
            <?php
            }

            // Show column for new payment types, if any
            if (!empty($payment_types) && count($payment_types) > 1) {
            ?>
                <div class="col-md-<?php (print (isset($column_widths) ? $this->Html->safe($column_widths) : null));?><?php echo $columns > 2 ? ' col-border' : '';?>">
                    <h4><?php $this->_('ClientPay.method.field_newdetails');?></h4>
                    <?php
                    $this->Form->fieldSelect('payment_type', (isset($payment_types) ? $payment_types : null), (isset($vars->payment_type) ? $vars->payment_type : null), ['id' => 'details', 'class' => 'form-control form-control-sm']);
                    ?>
                </div>
            <?php
            }

            // Show column for non-merchant gateway payment options, if any
            if (!empty($nm_gateways)) {
                $options_heading = $this->_('ClientPay.method.heading_other_payment_options', true);
                if ($columns == 1) {
                    $options_heading = $this->_('ClientPay.method.heading_payment_options', true);
                }
            ?>
                <div class="col-md-<?php (print (isset($column_widths) ? $this->Html->safe($column_widths) : null));?>">
                    <h4><?php echo (isset($options_heading) ? $options_heading : null);?></h4>
                    <?php
                    // Set non-merchant gateways
                    foreach ((isset($nm_gateways) ? $nm_gateways : []) as $gateway) {
                    ?>
                    <div class="radio">
                        <label>
                            <?php
                            $this->Form->fieldRadio('gateway', (isset($gateway->id) ? $gateway->id : null), ((isset($vars->pay_with) ? $vars->pay_with : null) == (isset($gateway->id) ? $gateway->id : null)), ['class' => 'gateway']);
                            (print (isset($gateway->name) ? $this->Html->safe($gateway->name) : null));
                            ?>
                        </label>
                    </div>
                    <?php
                    }
                    ?>
                </div>
            <?php
            }

            // Close payment option container
            if ($columns > 0) {
            ?>
                <div class="clearfix mb-2"></div>
            </div>
            <?php
            }
            ?>
            <div id="payment_details" style="display:none;">
                <?php
                echo (isset($contact_info) ? $contact_info : null);
                ?>
                <div id="payment_details_cc" style="display:none;">
                    <?php echo (isset($cc_info) ? $cc_info : null);?>
                </div>
                <div id="payment_details_ach" style="display:none;">
                    <?php echo (isset($ach_info) ? $ach_info : null);?>
                </div>
            </div>
            <?php
            // Allow credits to be selected
            if ((isset($credits['amount']) ? $credits['amount'] : null) > 0 && !(isset($invoice_paid) ? $invoice_paid : null)) {
            ?>
            <div class="checkbox">
                <label>
                <?php
                $this->Form->fieldCheckbox('apply_credit', 'true', (isset($vars->apply_credit) ? $vars->apply_credit : null) == 'true', ['id' => 'apply_credit']);
                $this->_('ClientPay.method.field_apply_credit', false, $this->CurrencyFormat->format($credits['amount'], $credits['currency']));
                ?>
                </label>
            </div>
            <?php
            }
            ?>

            <hr />
            <?php
            if (!empty($invoice)) {
            ?>
            <div class="mb-2">
                <h4><?php $this->_('ClientPay.method.heading_invoice', false, (isset($invoice->id_code) ? $this->Html->safe($invoice->id_code) : null));?></h4>
                <div class="table-responsive">
                    <table class="table table-curved table-striped">
                        <thead>
                            <tr>
                                <th><?php $this->_('ClientPay.method.invoice_description');?></th>
                                <th><?php $this->_('ClientPay.method.invoice_quantity');?></th>
                                <th><?php $this->_('ClientPay.method.invoice_amount');?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $currency_options = [
                                'prefix' => false,
                                'suffix' => false,
                                'code' => false,
                                'with_separator' => false,
                                'html_code' => false,
                                'decimals' => (isset($currency->precision) ? $currency->precision : null)
                            ];
                            foreach ((isset($invoice->line_items) ? $invoice->line_items : []) as $line) {
                            ?>
                            <tr>
                                <td><?php (print (isset($line->description) ? $this->Html->safe($line->description) : null));?></td>
                                <td><?php echo $this->Html->safe($this->CurrencyFormat->truncateDecimal((isset($line->qty) ? $line->qty : null), 0));?></td>
                                <td><?php echo $this->CurrencyFormat->format((isset($line->amount) ? $line->amount : null), (isset($currency->code) ? $currency->code : null), $currency_options);?></td>
                            </tr>
                            <?php
                            }
                            ?>
                        </tbody>
                    </table>
                </div>

                <?php
                // Show invoice totals breakdown if payments have been applied
                $paid_amount = $invoice->total - $invoice->due;
                if ($paid_amount > 0) {
                ?>
                <div class="row">
                    <div class="col-md-7"></div>
                    <div class="col-md-5">
                        <div class="float-right">
                            <div class="table-responsive my-2">
                                <table class="table table-curved">
                                    <tbody>
                                        <tr>
                                            <td><strong><?php $this->_('ClientPay.method.invoice_total');?></strong></td>
                                            <td><?php echo $this->CurrencyFormat->format($invoice->total, $currency->code);?></td>
                                        </tr>
                                        <tr>
                                            <td><strong><?php $this->_('ClientPay.method.invoice_paid_amount');?></strong></td>
                                            <td><?php echo $this->CurrencyFormat->format($paid_amount, $currency->code);?></td>
                                        </tr>
                                        <tr>
                                            <td><strong><?php $this->_('ClientPay.method.invoice_balance_due');?></strong></td>
                                            <td><?php echo $this->CurrencyFormat->format($invoice->due, $currency->code);?></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
                <?php
                }
                ?>
            </div>
            <?php
            }
            ?>

            <div class="float-right">
                <div class="card bg-light d-block p-2 mb-2">
                    <?php $this->_('ClientPay.confirm.total');?>
                    <strong><?php echo $this->CurrencyFormat->format((isset($total) ? $total : null), (isset($currency->code) ? $currency->code : null));?></strong>
                </div>
            </div>
            <div class="clearfix"></div>

            <button type="submit"<?php echo ($columns <= 0 ? 'style="display:none;"' : '');?> class="btn btn-light float-right" id="submit_payment">
                <i class="fas fa-arrow-circle-right"></i>
                <span class="confirm"><?php $this->_('ClientPay.method.field_submit');?></span>
                <span class="credit_confirm" style="display:none;"><?php $this->_('ClientPay.method.field_credit_submit');?></span>
            </button>
            <?php
            $this->WidgetClient->endBody();
            $this->WidgetClient->end();

            $this->Form->end();
            ?>
        </div>

<script type="text/javascript">
    $(document).ready(function() {
        var payment_type = '<?php echo (isset($vars->payment_type) ? $vars->payment_type : null);?>';
        if (payment_type == 'ach' || payment_type == 'cc') {
            $("#payment_details").show();
            $("#payment_details_" + payment_type).show();
        }

        $('#account, #details').on('change', function() {
            $('#pay_with').val($(this).attr('id'));
            $('.gateway').prop('checked', false);

            if ($(this).attr('id') == 'details') {
                if ($(this).val() == 'cc' || $(this).val() == 'ach') {
                    $('#payment_details').show();
                    togglePayDetails($(this).val());

                    $('html,body').animate({scrollTop: $('#payment_details').offset().top}, 'slow');
                }
                else
                    $('#payment_details').hide();

                $('#account').val('');
            }
            else {
                $('#payment_details, #payment_details_cc, #payment_details_ach').hide();
                $('#details').val('');
            }
        });

        $('.gateway').on('change', function() {
            $('#pay_with').val($(this).val());
            $('#payment_details, #payment_details_cc, #payment_details_ach').hide();
            $('#account, #details').val('');
        });

        toggleCredit();
        $('#apply_credit').change(function() {
            toggleCredit();
        });
    });

    function toggleCredit() {
        if ($('#apply_credit').prop('checked')) {
            $('span.credit_confirm').show();
            $('#submit_payment').show();
            $('span.confirm').hide();
        }
        else {
            $('span.confirm').show();
            $('span.credit_confirm').hide();

            if (<?php echo $columns;?> <= 0) {
                $('#submit_payment').hide();
            }
        }
    }

    function togglePayDetails(val) {
        if (val == "cc") {
            $("#payment_details_ach").hide();
            $("#payment_details_cc").show();
        }
        else {
            $("#payment_details_cc").hide();
            $("#payment_details_ach").show();
        }
    }
</script>