1 <?php
  2 
  3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  4 
  5 /**
  6  * WooCommerce Payment Gateway class
  7  *
  8  * Extended by individual payment gateways to handle payments.
  9  *
 10  * @class       WC_Payment_Gateway
 11  * @extends     WC_Settings_API
 12  * @version     2.1.0
 13  * @package     WooCommerce/Abstracts
 14  * @category    Abstract Class
 15  * @author      WooThemes
 16  */
 17 abstract class WC_Payment_Gateway extends WC_Settings_API {
 18 
 19     /** @var string Payment method ID. */
 20     var $id;
 21 
 22     /** @var string Set if the place order button should be renamed on selection. */
 23     var $order_button_text;
 24 
 25     /** @var string Payment method title. */
 26     var $title;
 27 
 28     /** @var string Chosen payment method id. */
 29     var $chosen;
 30 
 31     /** @var bool True if the gateway shows fields on the checkout. */
 32     var $has_fields;
 33 
 34     /** @var array Array of countries this gateway is allowed for. */
 35     var $countries;
 36 
 37     /** @var string Available for all counties or specific. */
 38     var $availability;
 39 
 40     /** @var string 'yes' if the method is enabled. */
 41     var $enabled;
 42 
 43     /** @var string Icon for the gateway. */
 44     var $icon;
 45 
 46     /** @var string Description for the gateway. */
 47     var $description;
 48 
 49     /** @var array Array of supported features such as 'default_credit_card_form' */
 50     var $supports       = array( 'products' );
 51 
 52     /** @var int Maximum transaction amount, zero does not define a maximum */
 53     public $max_amount = 0;
 54 
 55     /**
 56      * Get the return url (thank you page)
 57      *
 58      * @access public
 59      * @param string $order (default: '')
 60      * @return string
 61      */
 62     public function get_return_url( $order = '' ) {
 63         if ( $order ) {
 64             $return_url = $order->get_checkout_order_received_url();
 65         } else {
 66             $return_url = wc_get_endpoint_url( 'order-received', '', get_permalink( wc_get_page_id( 'checkout' ) ) );
 67         }
 68 
 69         if ( is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes' ) {
 70             $return_url = str_replace( 'http:', 'https:', $return_url );
 71         }
 72 
 73         return apply_filters( 'woocommerce_get_return_url', $return_url );
 74     }
 75 
 76     /**
 77      * Get the order total in checkout and pay_for_order.
 78      *
 79      * @return bool
 80      */
 81     protected function get_order_total() {
 82         $total = 0;
 83         $order_id = absint( get_query_var( 'order-pay' ) );
 84 
 85         // Gets order total from "pay for order" page.
 86         if ( 0 < $order_id ) {
 87             $order = new WC_Order( $order_id );
 88             $total = (float) $order->get_total();
 89 
 90         // Gets order total from cart/checkout.
 91         } elseif ( 0 < WC()->cart->total ) {
 92             $total = (float) WC()->cart->total;
 93         }
 94 
 95         return $total;
 96     }
 97 
 98     /**
 99      * Check If The Gateway Is Available For Use
100      *
101      * @access public
102      * @return bool
103      */
104     public function is_available() {
105         $is_available = ( 'yes' === $this->enabled ) ? true : false;
106 
107         if ( WC()->cart && 0 < $this->get_order_total() && $this->max_amount >= $this->get_order_total() ) {
108             $is_available = false;
109         }
110 
111         return $is_available;
112     }
113 
114     /**
115      * has_fields function.
116      *
117      * @access public
118      * @return bool
119      */
120     public function has_fields() {
121         return $this->has_fields ? true : false;
122     }
123 
124     /**
125      * Return the gateways title
126      *
127      * @access public
128      * @return string
129      */
130     public function get_title() {
131         return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id );
132     }
133 
134     /**
135      * Return the gateways description
136      *
137      * @access public
138      * @return string
139      */
140     public function get_description() {
141         return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id );
142     }
143 
144     /**
145      * get_icon function.
146      *
147      * @access public
148      * @return string
149      */
150     public function get_icon() {
151 
152         $icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : '';
153 
154         return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
155     }
156 
157     /**
158      * Set As Current Gateway.
159      *
160      * Set this as the current gateway.
161      *
162      * @access public
163      * @return void
164      */
165     public function set_current() {
166         $this->chosen = true;
167     }
168 
169     /**
170      * Process Payment
171      *
172      * Process the payment. Override this in your gateway.
173      *
174      * @param int $order_id
175      * @access public
176      * @return void
177      */
178     public function process_payment( $order_id ) {}
179 
180     /**
181      * Validate Frontend Fields
182      *
183      * Validate payment fields on the frontend.
184      *
185      * @access public
186      * @return bool
187      */
188     public function validate_fields() { return true; }
189 
190     /**
191      * If There are no payment fields show the description if set.
192      * Override this in your gateway if you have some.
193      *
194      * @access public
195      * @return void
196      */
197     public function payment_fields() {
198         if ( $description = $this->get_description() ) {
199             echo wpautop( wptexturize( $description ) );
200         }
201 
202         if ( $this->supports( 'default_credit_card_form' ) ) {
203             $this->credit_card_form();
204         }
205     }
206 
207     /**
208      * Check if a gateway supports a given feature.
209      *
210      * Gateways should override this to declare support (or lack of support) for a feature.
211      * For backward compatibility, gateways support 'products' by default, but nothing else.
212      *
213      * @access public
214      * @param $feature string The name of a feature to test support for.
215      * @return bool True if the gateway supports the feature, false otherwise.
216      * @since 1.5.7
217      */
218     public function supports( $feature ) {
219         return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this );
220     }
221 
222     /**
223      * Core credit card form which gateways can used if needed.
224      *
225      * @param  array $args
226      */
227     public function credit_card_form( $args = array(), $fields = array() ) {
228         wp_enqueue_script( 'wc-credit-card-form' );
229 
230         $default_args = array(
231             'fields_have_names' => true, // Some gateways like stripe don't need names as the form is tokenized
232         );
233 
234         $args = wp_parse_args( $args, apply_filters( 'woocommerce_credit_card_form_args', $default_args, $this->id ) );
235 
236         $default_fields = array(
237             'card-number-field' => '<p class="form-row form-row-wide">
238                 <label for="' . esc_attr( $this->id ) . '-card-number">' . __( 'Card Number', 'woocommerce' ) . ' <span class="required">*</span></label>
239                 <input id="' . esc_attr( $this->id ) . '-card-number" class="input-text wc-credit-card-form-card-number" type="text" maxlength="20" autocomplete="off" placeholder="•••• •••• •••• ••••" name="' . ( $args['fields_have_names'] ? $this->id . '-card-number' : '' ) . '" />
240             </p>',
241             'card-expiry-field' => '<p class="form-row form-row-first">
242                 <label for="' . esc_attr( $this->id ) . '-card-expiry">' . __( 'Expiry (MM/YY)', 'woocommerce' ) . ' <span class="required">*</span></label>
243                 <input id="' . esc_attr( $this->id ) . '-card-expiry" class="input-text wc-credit-card-form-card-expiry" type="text" autocomplete="off" placeholder="' . __( 'MM / YY', 'woocommerce' ) . '" name="' . ( $args['fields_have_names'] ? $this->id . '-card-expiry' : '' ) . '" />
244             </p>',
245             'card-cvc-field' => '<p class="form-row form-row-last">
246                 <label for="' . esc_attr( $this->id ) . '-card-cvc">' . __( 'Card Code', 'woocommerce' ) . ' <span class="required">*</span></label>
247                 <input id="' . esc_attr( $this->id ) . '-card-cvc" class="input-text wc-credit-card-form-card-cvc" type="text" autocomplete="off" placeholder="' . __( 'CVC', 'woocommerce' ) . '" name="' . ( $args['fields_have_names'] ? $this->id . '-card-cvc' : '' ) . '" />
248             </p>'
249         );
250 
251         $fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) );
252         ?>
253         <fieldset id="<?php echo $this->id; ?>-cc-form">
254             <?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
255             <?php
256                 foreach ( $fields as $field ) {
257                     echo $field;
258                 }
259             ?>
260             <?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
261             <div class="clear"></div>
262         </fieldset>
263         <?php
264     }
265 }
266 
WooCommerce API documentation generated by ApiGen 2.8.0