Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

  • is_pos
  • is_pos_admin
  • wc_pos_get_option
  • wc_pos_json_encode
  • wc_pos_locate_template
  • wc_pos_trim_html_string
  • wc_pos_update_option
  • wc_pos_url
  1 <?php
  2 
  3 /**
  4  * POS Gateways
  5  *
  6  * @class    WC_POS_API_Gateways
  7  * @package  WooCommerce POS
  8  * @author   Paul Kilmurray <paul@kilbot.com.au>
  9  * @link     http://www.woopos.com.au
 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    * Register routes for POS Params
 27    *
 28    * GET /pos
 29    *
 30    * @param array $routes
 31    * @return array
 32    */
 33   public function register_routes( array $routes ) {
 34 
 35     # GET /pos/params
 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    * @param null $wc_pos_admin
 46    * @return array
 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    * Sanitize payment icon
 73    * - some gateways include junk in icon property, eg: paypal link
 74    *
 75    * @param WC_Payment_Gateway $gateway
 76    * @return string
 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    * Sanitize payment fields
 90    * - some gateways include js in their payment fields
 91    *
 92    * @param WC_Payment_Gateway $gateway
 93    * @return mixed|string
 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       // remove script tags
105       $html = $this->removeDomNodes( $html, '//script' );
106     }
107 
108     return $html;
109   }
110 
111   /**
112    * Removes dom nodes, eg: <script> elements
113    *
114    * @param $html
115    * @param $xpathString
116    * @return string
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     // suppress warnings for malformed HTML
126     libxml_use_internal_errors(true);
127 
128     // Libxml constants not available on all servers (Libxml < 2.7.8)
129     // $html->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
130     $dom->loadHtml( '<?xml encoding="UTF-8">' . '<div class="form-group">' . $html . '</div>' );
131     $dom->preserveWhiteSpace = false;
132     // remove <!DOCTYPE
133     $dom->removeChild( $dom->doctype );
134     // remove <?xml encoding="UTF-8">
135     $dom->removeChild( $dom->firstChild );
136     // <html><body></body></html>
137     $dom->replaceChild( $dom->firstChild->firstChild->firstChild, $dom->firstChild );
138 
139     // remove the required node
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    * @param $html
150    * @return array
151    */
152   private function parseImgSrc( $html ){
153     if( ! class_exists('DOMDocument') ){
154       // simple preg_match
155       preg_match_all( '/< *img[^>]*src *= *["\']?([^"\']*)/i', $html, $matches );
156       return $matches[ 1 ];
157     }
158 
159     $icons = array();
160     $dom = new DOMDocument();
161 
162     // suppress warnings for malformed HTML
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 }
API documentation generated by ApiGen