1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27namespace PrestaShop\PrestaShop\Core\Payment;
28
29use DOMDocument;
30
31class PaymentOptionFormDecorator
32{
33    public function addHiddenSubmitButton($formHTML, $optionId)
34    {
35        $doc = new DOMDocument();
36        $doc->encoding = 'UTF-8';
37
38        $doc->loadHTML('<?xml encoding="UTF-8">' . $formHTML);
39
40        $forms = $doc->getElementsByTagName('form');
41        if ($forms->length !== 1) {
42            return false;
43        }
44
45        $hiddenSubmitButton = $doc->createElement('button');
46
47        $styleAttr = $doc->createAttribute('style');
48        $styleAttr->value = 'display:none';
49
50        $idAttr = $doc->createAttribute('id');
51        $idAttr->value = 'pay-with-' . $optionId;
52
53        $typeAttr = $doc->createAttribute('type');
54        $typeAttr->value = 'submit';
55
56        $hiddenSubmitButton->appendChild($styleAttr);
57        $hiddenSubmitButton->appendChild($idAttr);
58        $hiddenSubmitButton->appendChild($typeAttr);
59
60        $forms->item(0)->appendChild($hiddenSubmitButton);
61
62        $body = $doc->getElementsByTagName('body')->item(0);
63        $html = '';
64
65        foreach ($body->childNodes as $node) {
66            $html .= $doc->saveHTML($node);
67        }
68
69        return $html;
70    }
71}
72