1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace WC_POS\API;
13
14 use DOMDocument;
15 use DOMXPath;
16 use WC_API_Resource;
17 use WC_API_Server;
18 use WC_Payment_Gateway;
19 use WC_POS\Admin\Settings\Checkout;
20
21 class Gateways extends WC_API_Resource {
22
23 protected $base = '/pos/gateways';
24
25 26 27 28 29 30 31 32
33 public function register_routes( array $routes ) {
34
35
36 $routes[ $this->base ] = array(
37 array( array( $this, 'get_gateways' ), WC_API_Server::READABLE )
38 );
39
40 return $routes;
41
42 }
43
44 45 46 47
48 public function get_gateways( $wc_pos_admin = null ){
49 if( $wc_pos_admin ){
50 return;
51 }
52
53 $settings = Checkout::get_instance();
54 $gateways = $settings->load_enabled_gateways();
55 $payload = array();
56
57 if ( $gateways ): foreach ( $gateways as $gateway ):
58 $payload[] = array(
59 'method_id' => $gateway->id,
60 'method_title' => esc_html( $gateway->get_title() ),
61 'icons' => $this->sanitize_icons( $gateway ),
62 'active' => $gateway->default,
63 'payment_fields' => $this->sanitize_payment_fields( $gateway ),
64 'params' => apply_filters('woocommerce_pos_gateway_' . strtolower( $gateway->id ) . '_params', array(), $gateway, $this )
65 );
66 endforeach; endif;
67
68 return $payload;
69 }
70
71 72 73 74 75 76 77
78 private function sanitize_icons( WC_Payment_Gateway $gateway ) {
79 $icons = $gateway->show_icon ? $gateway->get_icon() : '';
80
81 if ( $icons !== '' ) {
82 $icons = $this->parseImgSrc($icons);
83 }
84
85 return $icons;
86 }
87
88 89 90 91 92 93 94
95 private function sanitize_payment_fields( WC_Payment_Gateway $gateway ) {
96 $html = '';
97 if ( $gateway->has_fields() || $gateway->get_description() ) {
98
99 ob_start();
100 $gateway->payment_fields();
101 $html = ob_get_contents();
102 ob_end_clean();
103
104
105 $html = $this->removeDomNodes( $html, '//script' );
106 }
107
108 return $html;
109 }
110
111 112 113 114 115 116 117
118 private function removeDomNodes( $html, $xpathString ) {
119 if( ! class_exists('DOMDocument') ){
120 return preg_replace('/<script.+?<\/script>/im', '', $html);
121 }
122
123 $dom = new DOMDocument();
124
125
126 libxml_use_internal_errors(true);
127
128
129
130 $dom->loadHtml( '<?xml encoding="UTF-8">' . '<div class="form-group">' . $html . '</div>' );
131 $dom->preserveWhiteSpace = false;
132
133 $dom->removeChild( $dom->doctype );
134
135 $dom->removeChild( $dom->firstChild );
136
137 $dom->replaceChild( $dom->firstChild->firstChild->firstChild, $dom->firstChild );
138
139
140 $xpath = new DOMXPath( $dom );
141 while ( $node = $xpath->query( $xpathString )->item( 0 ) ) {
142 $node->parentNode->removeChild( $node );
143 }
144
145 return $dom->saveHTML();
146 }
147
148 149 150 151
152 private function parseImgSrc( $html ){
153 if( ! class_exists('DOMDocument') ){
154
155 preg_match_all( '/< *img[^>]*src *= *["\']?([^"\']*)/i', $html, $matches );
156 return $matches[ 1 ];
157 }
158
159 $icons = array();
160 $dom = new DOMDocument();
161
162
163 libxml_use_internal_errors(true);
164
165 $dom->loadHTML($html);
166 $dom->preserveWhiteSpace = false;
167 $images = $dom->getElementsByTagName('img');
168 foreach ($images as $image) {
169 $icons[] = $image->getAttribute('src');
170 }
171
172 return $icons;
173 }
174
175 }