2011-08-09 15:16:18 +00:00
< ? php
2013-02-20 17:14:46 +00:00
2014-09-20 18:39:49 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2015-11-03 18:20:08 +00:00
exit ;
2014-09-20 18:39:49 +00:00
}
2013-02-20 17:14:46 +00:00
2011-08-09 15:16:18 +00:00
/**
2015-11-03 12:28:01 +00:00
* WooCommerce Payment Gateway class .
2012-08-15 17:08:42 +00:00
*
2011-08-10 17:11:11 +00:00
* Extended by individual payment gateways to handle payments .
*
2014-08-31 05:49:58 +00:00
* @ class WC_Payment_Gateway
* @ extends WC_Settings_API
* @ version 2.1 . 0
* @ package WooCommerce / Abstracts
* @ category Abstract Class
* @ author WooThemes
2011-08-10 17:11:11 +00:00
*/
2012-12-31 18:25:09 +00:00
abstract class WC_Payment_Gateway extends WC_Settings_API {
2012-08-15 17:08:42 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Set if the place order button should be renamed on selection .
2014-11-20 02:14:06 +00:00
* @ var string
*/
2014-06-27 12:12:10 +00:00
public $order_button_text ;
2014-01-28 11:25:52 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-12-11 16:41:38 +00:00
* yes or no based on whether the method is enabled .
* @ var string
*/
public $enabled = 'yes' ;
/**
* Payment method title for the frontend .
2014-11-20 02:14:06 +00:00
* @ var string
*/
2014-06-27 12:12:10 +00:00
public $title ;
2012-08-15 18:15:06 +00:00
2015-12-11 16:41:38 +00:00
/**
* Payment method description for the frontend .
* @ var string
*/
public $description ;
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Chosen payment method id .
2014-11-20 02:14:06 +00:00
* @ var bool
*/
2014-06-27 12:12:10 +00:00
public $chosen ;
2012-08-15 18:15:06 +00:00
2015-12-11 16:41:38 +00:00
/**
* Gateway title .
* @ var string
*/
public $method_title = '' ;
/**
* Gateway description .
* @ var string
*/
public $method_description = '' ;
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* True if the gateway shows fields on the checkout .
2014-11-20 02:14:06 +00:00
* @ var bool
*/
2014-06-27 12:12:10 +00:00
public $has_fields ;
2012-08-15 18:15:06 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Countries this gateway is allowed for .
2014-11-20 02:14:06 +00:00
* @ var array
*/
2014-06-27 12:12:10 +00:00
public $countries ;
2012-08-15 18:15:06 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Available for all counties or specific .
2014-11-20 02:14:06 +00:00
* @ var string
*/
2014-06-27 12:12:10 +00:00
public $availability ;
2012-08-15 18:15:06 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Icon for the gateway .
2014-11-20 02:14:06 +00:00
* @ var string
*/
2014-06-27 12:12:10 +00:00
public $icon ;
2012-08-15 18:15:06 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Supported features such as 'default_credit_card_form' , 'refunds' .
2014-11-20 02:14:06 +00:00
* @ var array
*/
2014-08-31 05:49:58 +00:00
public $supports = array ( 'products' );
2012-08-15 17:08:42 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Maximum transaction amount , zero does not define a maximum .
2014-11-20 02:14:06 +00:00
* @ var int
*/
2014-04-24 18:49:41 +00:00
public $max_amount = 0 ;
2014-04-24 17:38:24 +00:00
2014-11-20 02:14:06 +00:00
/**
2015-11-03 12:28:01 +00:00
* Optional URL to view a transaction .
2014-11-20 02:14:06 +00:00
* @ var string
*/
2014-06-27 12:12:10 +00:00
public $view_transaction_url = '' ;
2016-02-18 19:43:25 +00:00
/**
* Optional label to show for " new payment method " in the payment
* method / token selection radio selection .
* @ var string
*/
public $new_method_label = '' ;
2016-03-01 17:24:32 +00:00
/**
* Contains a users saved tokens for this gateway .
* @ var array
*/
2016-03-01 17:09:29 +00:00
protected $tokens = array ();
2016-03-01 17:24:32 +00:00
/**
* Returns a users saved tokens for this gateway .
* @ since 2.6 . 0
* @ return array
*/
2016-03-01 17:09:29 +00:00
public function get_tokens () {
if ( sizeof ( $this -> tokens ) > 0 ) {
return $this -> tokens ;
}
2016-05-09 16:36:40 +00:00
if ( is_user_logged_in () && $this -> supports ( 'tokenization' ) ) {
2016-03-01 17:09:29 +00:00
$this -> tokens = WC_Payment_Tokens :: get_customer_tokens ( get_current_user_id (), $this -> id );
}
return $this -> tokens ;
}
2015-12-11 16:41:38 +00:00
/**
* Return the title for admin screens .
* @ return string
*/
public function get_method_title () {
return apply_filters ( 'woocommerce_gateway_method_title' , $this -> method_title , $this );
}
/**
* Return the description for admin screens .
* @ return string
*/
public function get_method_description () {
return apply_filters ( 'woocommerce_gateway_method_description' , $this -> method_description , $this );
}
/**
* Output the gateway settings screen .
*/
public function admin_options () {
echo '<h2>' . esc_html ( $this -> get_method_title () ) . '</h2>' ;
echo wp_kses_post ( wpautop ( $this -> get_method_description () ) );
parent :: admin_options ();
}
2015-12-14 14:03:46 +00:00
/**
* Init settings for gateways .
*/
public function init_settings () {
parent :: init_settings ();
$this -> enabled = ! empty ( $this -> settings [ 'enabled' ] ) && 'yes' === $this -> settings [ 'enabled' ] ? 'yes' : 'no' ;
}
2011-10-23 11:25:22 +00:00
/**
2015-11-03 12:28:01 +00:00
* Get the return url ( thank you page ) .
2011-10-23 11:25:22 +00:00
*
2014-10-07 10:03:13 +00:00
* @ param WC_Order $order
2012-08-15 18:15:06 +00:00
* @ return string
2011-10-23 11:25:22 +00:00
*/
2014-10-07 10:03:13 +00:00
public function get_return_url ( $order = null ) {
2013-11-24 10:42:36 +00:00
if ( $order ) {
2013-05-31 15:13:14 +00:00
$return_url = $order -> get_checkout_order_received_url ();
2013-11-24 10:42:36 +00:00
} else {
2015-02-15 19:13:22 +00:00
$return_url = wc_get_endpoint_url ( 'order-received' , '' , wc_get_page_permalink ( 'checkout' ) );
2013-11-24 10:42:36 +00:00
}
2012-08-15 17:08:42 +00:00
2016-09-02 01:51:31 +00:00
if ( is_ssl () || get_option ( 'woocommerce_force_ssl_checkout' ) == 'yes' ) {
2012-05-29 12:33:21 +00:00
$return_url = str_replace ( 'http:' , 'https:' , $return_url );
2013-11-24 10:42:36 +00:00
}
2012-08-15 17:08:42 +00:00
2015-04-28 12:48:39 +00:00
return apply_filters ( 'woocommerce_get_return_url' , $return_url , $order );
2011-10-23 11:25:22 +00:00
}
2012-08-15 17:08:42 +00:00
2014-06-27 12:12:10 +00:00
/**
2015-11-03 12:28:01 +00:00
* Get a link to the transaction on the 3 rd party gateway size ( if applicable ) .
2014-08-28 01:51:03 +00:00
*
2015-11-03 12:28:01 +00:00
* @ param WC_Order $order the order object .
* @ return string transaction URL , or empty string .
2014-06-27 12:12:10 +00:00
*/
2014-08-28 01:51:03 +00:00
public function get_transaction_url ( $order ) {
2014-08-31 05:49:58 +00:00
2014-06-27 12:12:10 +00:00
$return_url = '' ;
2014-08-28 01:51:03 +00:00
$transaction_id = $order -> get_transaction_id ();
2014-06-27 12:12:10 +00:00
if ( ! empty ( $this -> view_transaction_url ) && ! empty ( $transaction_id ) ) {
$return_url = sprintf ( $this -> view_transaction_url , $transaction_id );
}
2014-08-28 01:51:03 +00:00
return apply_filters ( 'woocommerce_get_transaction_url' , $return_url , $order , $this );
2014-08-07 18:57:29 +00:00
}
2014-06-27 12:12:10 +00:00
2014-04-24 17:38:24 +00:00
/**
* Get the order total in checkout and pay_for_order .
*
2015-08-13 17:05:55 +00:00
* @ return float
2014-04-24 17:38:24 +00:00
*/
2014-04-24 18:49:41 +00:00
protected function get_order_total () {
2014-08-31 05:49:58 +00:00
2014-04-24 17:38:24 +00:00
$total = 0 ;
2014-04-24 17:52:17 +00:00
$order_id = absint ( get_query_var ( 'order-pay' ) );
2014-04-24 17:38:24 +00:00
// Gets order total from "pay for order" page.
2014-04-24 17:52:17 +00:00
if ( 0 < $order_id ) {
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2014-04-24 18:49:41 +00:00
$total = ( float ) $order -> get_total ();
2014-04-24 17:38:24 +00:00
// Gets order total from cart/checkout.
} elseif ( 0 < WC () -> cart -> total ) {
$total = ( float ) WC () -> cart -> total ;
}
return $total ;
}
2011-09-27 08:41:38 +00:00
/**
2015-11-03 12:28:01 +00:00
* Check if the gateway is available for use .
2011-09-27 08:41:38 +00:00
*
2012-08-15 18:15:06 +00:00
* @ return bool
2011-09-27 08:41:38 +00:00
*/
2013-09-27 12:22:04 +00:00
public function is_available () {
2016-01-19 23:30:56 +00:00
$is_available = ( 'yes' === $this -> enabled );
2014-04-24 17:38:24 +00:00
2014-08-17 20:10:35 +00:00
if ( WC () -> cart && 0 < $this -> get_order_total () && 0 < $this -> max_amount && $this -> max_amount < $this -> get_order_total () ) {
2014-04-24 17:38:24 +00:00
$is_available = false ;
}
return $is_available ;
2011-08-09 15:16:18 +00:00
}
2012-08-15 17:08:42 +00:00
2012-06-29 18:44:33 +00:00
/**
2016-01-03 09:22:53 +00:00
* Check if the gateway has fields on the checkout .
2012-08-15 17:08:42 +00:00
*
2012-08-15 18:15:06 +00:00
* @ return bool
2012-06-29 18:44:33 +00:00
*/
2013-09-27 12:22:04 +00:00
public function has_fields () {
2012-06-29 18:44:33 +00:00
return $this -> has_fields ? true : false ;
}
2012-08-15 17:08:42 +00:00
2012-06-29 18:44:33 +00:00
/**
2015-11-03 12:28:01 +00:00
* Return the gateway ' s title .
2012-08-15 17:08:42 +00:00
*
2012-08-15 18:15:06 +00:00
* @ return string
2012-06-29 18:44:33 +00:00
*/
2013-09-27 12:22:04 +00:00
public function get_title () {
2012-06-29 18:44:33 +00:00
return apply_filters ( 'woocommerce_gateway_title' , $this -> title , $this -> id );
}
/**
2015-11-03 12:28:01 +00:00
* Return the gateway ' s description .
2012-08-15 17:08:42 +00:00
*
2012-08-15 18:15:06 +00:00
* @ return string
2012-06-29 18:44:33 +00:00
*/
2013-09-27 12:22:04 +00:00
public function get_description () {
2012-06-29 18:44:33 +00:00
return apply_filters ( 'woocommerce_gateway_description' , $this -> description , $this -> id );
}
2012-08-15 17:08:42 +00:00
2012-06-29 18:44:33 +00:00
/**
2016-01-03 09:22:53 +00:00
* Return the gateway ' s icon .
2012-08-15 17:08:42 +00:00
*
2012-08-15 18:15:06 +00:00
* @ return string
2012-06-29 18:44:33 +00:00
*/
2013-09-27 12:22:04 +00:00
public function get_icon () {
2014-08-31 05:49:58 +00:00
2013-11-20 19:11:59 +00:00
$icon = $this -> icon ? '<img src="' . WC_HTTPS :: force_https_url ( $this -> icon ) . '" alt="' . esc_attr ( $this -> get_title () ) . '" />' : '' ;
2012-08-15 17:08:42 +00:00
2012-06-29 18:44:33 +00:00
return apply_filters ( 'woocommerce_gateway_icon' , $icon , $this -> id );
}
2012-08-15 17:08:42 +00:00
2011-09-27 08:41:38 +00:00
/**
2014-08-31 05:49:58 +00:00
* Set as current gateway .
2011-09-27 08:41:38 +00:00
*
* Set this as the current gateway .
*/
2013-09-27 12:22:04 +00:00
public function set_current () {
2011-08-09 15:16:18 +00:00
$this -> chosen = true ;
}
2012-08-15 17:08:42 +00:00
2011-09-27 08:41:38 +00:00
/**
2015-11-03 12:28:01 +00:00
* Process Payment .
2011-09-27 08:41:38 +00:00
*
2015-11-03 12:28:01 +00:00
* Process the payment . Override this in your gateway . When implemented , this should .
* return the success and redirect in an array . e . g :
2014-07-07 10:43:27 +00:00
*
2014-08-31 05:49:58 +00:00
* return array (
* 'result' => 'success' ,
* 'redirect' => $this -> get_return_url ( $order )
* );
2011-09-27 08:41:38 +00:00
*
2013-07-24 11:24:52 +00:00
* @ param int $order_id
2014-07-07 10:43:27 +00:00
* @ return array
2011-09-27 08:41:38 +00:00
*/
2014-08-07 18:57:29 +00:00
public function process_payment ( $order_id ) {
2014-07-07 10:43:27 +00:00
return array ();
}
/**
2015-11-03 12:28:01 +00:00
* Process refund .
2014-07-07 10:43:27 +00:00
*
2015-11-03 12:28:01 +00:00
* If the gateway declares 'refunds' support , this will allow it to refund .
2014-07-07 10:43:27 +00:00
* a passed in amount .
2014-08-07 18:57:29 +00:00
*
2014-07-07 10:43:27 +00:00
* @ param int $order_id
* @ param float $amount
2014-08-07 18:57:29 +00:00
* @ param string $reason
2016-06-06 16:50:18 +00:00
* @ return boolean True or false based on success , or a WP_Error object .
2014-07-07 10:43:27 +00:00
*/
2014-08-11 13:07:09 +00:00
public function process_refund ( $order_id , $amount = null , $reason = '' ) {
2014-07-07 10:43:27 +00:00
return false ;
}
2012-08-15 18:15:06 +00:00
2011-09-27 08:41:38 +00:00
/**
2015-11-03 12:28:01 +00:00
* Validate frontend fields .
2011-09-27 08:41:38 +00:00
*
* Validate payment fields on the frontend .
2016-01-03 09:22:53 +00:00
*
* @ return bool
2011-09-27 08:41:38 +00:00
*/
2013-09-27 12:22:04 +00:00
public function validate_fields () { return true ; }
2012-08-15 18:15:06 +00:00
2014-03-23 22:49:52 +00:00
/**
* If There are no payment fields show the description if set .
* Override this in your gateway if you have some .
*/
public function payment_fields () {
if ( $description = $this -> get_description () ) {
echo wpautop ( wptexturize ( $description ) );
}
if ( $this -> supports ( 'default_credit_card_form' ) ) {
2016-03-01 17:24:32 +00:00
$this -> credit_card_form (); // Deprecated, will be removed in a future version.
2014-03-23 22:49:52 +00:00
}
}
2012-08-15 17:08:42 +00:00
2012-05-22 01:04:05 +00:00
/**
* Check if a gateway supports a given feature .
2012-08-15 17:08:42 +00:00
*
* Gateways should override this to declare support ( or lack of support ) for a feature .
* For backward compatibility , gateways support 'products' by default , but nothing else .
*
2014-09-07 23:37:55 +00:00
* @ param string $feature string The name of a feature to test support for .
2012-08-15 17:08:42 +00:00
* @ return bool True if the gateway supports the feature , false otherwise .
2012-05-22 01:04:05 +00:00
* @ since 1.5 . 7
*/
2013-09-27 12:22:04 +00:00
public function supports ( $feature ) {
2012-05-29 12:33:21 +00:00
return apply_filters ( 'woocommerce_payment_gateway_supports' , in_array ( $feature , $this -> supports ) ? true : false , $feature , $this );
2012-05-22 01:04:05 +00:00
}
2016-05-26 10:48:02 +00:00
/**
2017-07-10 05:56:28 +00:00
* Core credit card form which gateways can used if needed . Deprecated - inherit WC_Payment_Gateway_CC instead .
2016-05-26 10:48:02 +00:00
* @ param array $args
* @ param array $fields
*/
public function credit_card_form ( $args = array (), $fields = array () ) {
2016-11-23 16:15:00 +00:00
wc_deprecated_function ( 'credit_card_form' , '2.6' , 'WC_Payment_Gateway_CC->form' );
2016-05-26 10:48:02 +00:00
$cc_form = new WC_Payment_Gateway_CC ;
$cc_form -> id = $this -> id ;
$cc_form -> supports = $this -> supports ;
$cc_form -> form ();
}
2016-05-26 10:55:11 +00:00
2016-03-01 17:24:32 +00:00
/**
* Enqueues our tokenization script to handle some of the new form options .
* @ since 2.6 . 0
*/
public function tokenization_script () {
2016-03-01 17:09:29 +00:00
wp_enqueue_script (
2016-03-01 19:47:57 +00:00
'woocommerce-tokenization-form' ,
2016-09-02 01:51:31 +00:00
plugins_url ( '/assets/js/frontend/tokenization-form' . ( defined ( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js' , WC_PLUGIN_FILE ),
2016-03-01 17:09:29 +00:00
array ( 'jquery' ),
WC () -> version
);
}
2013-09-27 12:22:04 +00:00
/**
2016-03-01 17:09:29 +00:00
* Grab and display our saved payment methods .
* @ since 2.6 . 0
2013-09-27 12:22:04 +00:00
*/
2016-03-01 17:09:29 +00:00
public function saved_payment_methods () {
2016-05-26 11:42:29 +00:00
$html = '<ul class="woocommerce-SavedPaymentMethods wc-saved-payment-methods" data-count="' . esc_attr ( count ( $this -> get_tokens () ) ) . '">' ;
2016-05-26 10:55:11 +00:00
2016-03-01 17:09:29 +00:00
foreach ( $this -> get_tokens () as $token ) {
2016-05-26 11:42:29 +00:00
$html .= $this -> get_saved_payment_method_option_html ( $token );
2016-03-01 17:09:29 +00:00
}
2016-05-26 10:55:11 +00:00
2016-05-26 11:42:29 +00:00
$html .= $this -> get_new_payment_method_option_html ();
2016-05-10 14:05:55 +00:00
$html .= '</ul>' ;
2016-05-26 10:55:11 +00:00
2016-03-01 17:09:29 +00:00
echo apply_filters ( 'wc_payment_gateway_form_saved_payment_methods_html' , $html , $this );
}
2014-08-31 05:49:58 +00:00
2016-03-01 17:09:29 +00:00
/**
2016-05-26 10:55:11 +00:00
* Gets saved payment method HTML from a token .
2016-03-01 17:09:29 +00:00
* @ since 2.6 . 0
* @ param WC_Payment_Token $token Payment Token
* @ return string Generated payment method HTML
*/
2016-05-26 11:42:29 +00:00
public function get_saved_payment_method_option_html ( $token ) {
2016-03-01 17:09:29 +00:00
$html = sprintf (
2016-05-26 11:42:29 +00:00
' < li class = " woocommerce-SavedPaymentMethods-token " >
< input id = " wc-%1 $s -payment-token-%2 $s " type = " radio " name = " wc-%1 $s -payment-token " value = " %2 $s " style = " width:auto; " class = " woocommerce-SavedPaymentMethods-tokenInput " % 4 $s />
< label for = " wc-%1 $s -payment-token-%2 $s " >% 3 $s </ label >
</ li > ' ,
2016-03-01 17:09:29 +00:00
esc_attr ( $this -> id ),
esc_attr ( $token -> get_id () ),
2016-05-26 11:42:29 +00:00
esc_html ( $token -> get_display_name () ),
2016-03-01 17:09:29 +00:00
checked ( $token -> is_default (), true , false )
);
2013-09-27 12:22:04 +00:00
2016-05-26 11:42:29 +00:00
return apply_filters ( 'woocommerce_payment_gateway_get_saved_payment_method_option_html' , $html , $token , $this );
2016-03-01 17:09:29 +00:00
}
/**
2016-05-26 11:42:29 +00:00
* Displays a radio button for entering a new payment method ( new CC details ) instead of using a saved method .
* Only displayed when a gateway supports tokenization .
2016-03-01 17:09:29 +00:00
* @ since 2.6 . 0
*/
2016-05-26 11:42:29 +00:00
public function get_new_payment_method_option_html () {
$label = apply_filters ( 'woocommerce_payment_gateway_get_new_payment_method_option_html_label' , $this -> new_method_label ? $this -> new_method_label : __ ( 'Use a new payment method' , 'woocommerce' ), $this );
$html = sprintf (
' < li class = " woocommerce-SavedPaymentMethods-new " >
< input id = " wc-%1 $s -payment-token-new " type = " radio " name = " wc-%1 $s -payment-token " value = " new " style = " width:auto; " class = " woocommerce-SavedPaymentMethods-tokenInput " />
< label for = " wc-%1 $s -payment-token-new " >% 2 $s </ label >
</ li > ' ,
esc_attr ( $this -> id ),
esc_html ( $label )
);
2016-03-01 17:09:29 +00:00
2016-05-26 11:42:29 +00:00
return apply_filters ( 'woocommerce_payment_gateway_get_new_payment_method_option_html' , $html , $this );
2016-03-01 17:09:29 +00:00
}
/**
2016-03-01 17:24:32 +00:00
* Outputs a checkbox for saving a new payment method to the database .
2016-03-01 17:09:29 +00:00
* @ since 2.6 . 0
*/
public function save_payment_method_checkbox () {
2017-03-13 05:39:46 +00:00
printf (
2016-05-26 11:42:29 +00:00
' < p class = " form-row woocommerce-SavedPaymentMethods-saveNew " >
< input id = " wc-%1 $s -new-payment-method " name = " wc-%1 $s -new-payment-method " type = " checkbox " value = " true " style = " width:auto; " />
< label for = " wc-%1 $s -new-payment-method " style = " display:inline; " >% 2 $s </ label >
</ p > ' ,
2016-03-01 17:09:29 +00:00
esc_attr ( $this -> id ),
2016-10-12 10:16:30 +00:00
esc_html__ ( 'Save to account' , 'woocommerce' )
2016-03-01 17:09:29 +00:00
);
2013-09-27 12:22:04 +00:00
}
2017-08-07 11:38:50 +00:00
/**
* Add payment method via account screen . This should be extended by gateway plugins .
*
* @ since 3.2 . 0 Included here from 3.2 . 0 , but supported from 3.0 . 0.
* @ return array
*/
public function add_payment_method () {
return array (
'result' => 'failure' ,
'redirect' => wc_get_endpoint_url ( 'payment-methods' ),
);
}
2013-10-11 09:17:23 +00:00
}