2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
2013-02-20 17:14:46 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
2011-08-10 17:11:11 +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.
|
|
|
|
*
|
2012-01-27 16:38:39 +00:00
|
|
|
* @class WC_Payment_Gateway
|
2012-08-15 18:15:06 +00:00
|
|
|
* @extends WC_Settings_API
|
2013-09-27 12:22:04 +00:00
|
|
|
* @version 2.1.0
|
2013-02-20 17:14:46 +00:00
|
|
|
* @package WooCommerce/Abstracts
|
|
|
|
* @category Abstract Class
|
2012-08-15 18:15:06 +00:00
|
|
|
* @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
|
|
|
|
2012-08-15 18:15:06 +00:00
|
|
|
/** @var string Payment method ID. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $id;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2014-01-28 11:25:52 +00:00
|
|
|
/** @var string Set if the place order button should be renamed on selection. */
|
|
|
|
var $order_button_text;
|
|
|
|
|
2012-08-15 18:15:06 +00:00
|
|
|
/** @var string Payment method title. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $title;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/** @var string Chosen payment method id. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $chosen;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/** @var bool True if the gateway shows fields on the checkout. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $has_fields;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/** @var array Array of countries this gateway is allowed for. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $countries;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/** @var string Available for all counties or specific. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $availability;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2013-11-04 10:29:20 +00:00
|
|
|
/** @var string 'yes' if the method is enabled. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $enabled;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/** @var string Icon for the gateway. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $icon;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/** @var string Description for the gateway. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $description;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2013-09-27 12:22:04 +00:00
|
|
|
/** @var array Array of supported features such as 'default_credit_card_form' */
|
2012-08-15 18:15:06 +00:00
|
|
|
var $supports = array( 'products' );
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2014-04-24 17:38:24 +00:00
|
|
|
/** @var int Maximum transaction amount, zero does not define a maximum */
|
|
|
|
var $max_amount = 0;
|
|
|
|
|
2011-10-23 11:25:22 +00:00
|
|
|
/**
|
|
|
|
* Get the return url (thank you page)
|
|
|
|
*
|
2012-08-15 18:15:06 +00:00
|
|
|
* @access public
|
|
|
|
* @param string $order (default: '')
|
|
|
|
* @return string
|
2011-10-23 11:25:22 +00:00
|
|
|
*/
|
2013-09-27 12:22:04 +00:00
|
|
|
public function get_return_url( $order = '' ) {
|
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 {
|
2013-11-25 14:07:22 +00:00
|
|
|
$return_url = wc_get_endpoint_url( 'order-received', '', get_permalink( wc_get_page_id( 'checkout' ) ) );
|
2013-11-24 10:42:36 +00:00
|
|
|
}
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2013-11-24 10:42:36 +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
|
|
|
|
2012-05-29 12:33:21 +00:00
|
|
|
return apply_filters( 'woocommerce_get_return_url', $return_url );
|
2011-10-23 11:25:22 +00:00
|
|
|
}
|
2012-08-15 17:08:42 +00:00
|
|
|
|
2014-04-24 17:38:24 +00:00
|
|
|
/**
|
|
|
|
* Get the order total in checkout and pay_for_order.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function order_total() {
|
|
|
|
$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-04-24 17:38:24 +00:00
|
|
|
$order = new WC_Order( $order_id );
|
|
|
|
$total = (float) $order->order_total;
|
|
|
|
|
|
|
|
// 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
|
|
|
/**
|
|
|
|
* Check If The Gateway Is Available For Use
|
|
|
|
*
|
2012-08-15 18:15:06 +00:00
|
|
|
* @access public
|
|
|
|
* @return bool
|
2011-09-27 08:41:38 +00:00
|
|
|
*/
|
2013-09-27 12:22:04 +00:00
|
|
|
public function is_available() {
|
2014-04-24 17:38:24 +00:00
|
|
|
$is_available = ( 'yes' === $this->enabled ) ? true : false;
|
|
|
|
|
|
|
|
if ( 0 < $this->order_total() && $this->max_amount >= $this->order_total() ) {
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* has_fields function.
|
2012-08-15 17:08:42 +00:00
|
|
|
*
|
2012-06-29 18:44:33 +00:00
|
|
|
* @access public
|
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
|
|
|
/**
|
|
|
|
* Return the gateways title
|
2012-08-15 17:08:42 +00:00
|
|
|
*
|
2012-06-29 18:44:33 +00:00
|
|
|
* @access public
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the gateways description
|
2012-08-15 17:08:42 +00:00
|
|
|
*
|
2012-06-29 18:44:33 +00:00
|
|
|
* @access public
|
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
|
|
|
/**
|
|
|
|
* get_icon function.
|
2012-08-15 17:08:42 +00:00
|
|
|
*
|
2012-06-29 18:44:33 +00:00
|
|
|
* @access public
|
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() {
|
2012-08-15 17:08:42 +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
|
|
|
/**
|
|
|
|
* Set As Current Gateway.
|
|
|
|
*
|
|
|
|
* Set this as the current gateway.
|
|
|
|
*
|
2012-08-15 18:15:06 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
2011-09-27 08:41:38 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Process Payment
|
|
|
|
*
|
|
|
|
* Process the payment. Override this in your gateway.
|
|
|
|
*
|
2013-07-24 11:24:52 +00:00
|
|
|
* @param int $order_id
|
2012-08-15 18:15:06 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
2011-09-27 08:41:38 +00:00
|
|
|
*/
|
2013-09-27 12:22:04 +00:00
|
|
|
public function process_payment( $order_id ) {}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2011-09-27 08:41:38 +00:00
|
|
|
/**
|
|
|
|
* Validate Frontend Fields
|
|
|
|
*
|
|
|
|
* Validate payment fields on the frontend.
|
|
|
|
*
|
2012-08-15 18:15:06 +00:00
|
|
|
* @access public
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function payment_fields() {
|
|
|
|
if ( $description = $this->get_description() ) {
|
|
|
|
echo wpautop( wptexturize( $description ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->supports( 'default_credit_card_form' ) ) {
|
|
|
|
$this->credit_card_form();
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*
|
2012-08-15 18:15:06 +00:00
|
|
|
* @access public
|
2012-05-22 01:04:05 +00:00
|
|
|
* @param $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
|
|
|
}
|
|
|
|
|
2013-09-27 12:22:04 +00:00
|
|
|
/**
|
|
|
|
* Core credit card form which gateways can used if needed.
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
*/
|
|
|
|
public function credit_card_form( $args = array(), $fields = array() ) {
|
|
|
|
wp_enqueue_script( 'wc-credit-card-form' );
|
|
|
|
|
|
|
|
$default_args = array(
|
|
|
|
'fields_have_names' => true, // Some gateways like stripe don't need names as the form is tokenized
|
|
|
|
);
|
|
|
|
|
|
|
|
$args = wp_parse_args( $args, apply_filters( 'woocommerce_credit_card_form_args', $default_args, $this->id ) );
|
|
|
|
|
|
|
|
$default_fields = array(
|
|
|
|
'card-number-field' => '<p class="form-row form-row-wide">
|
2014-03-23 22:49:52 +00:00
|
|
|
<label for="' . esc_attr( $this->id ) . '-card-number">' . __( 'Card Number', 'woocommerce' ) . ' <span class="required">*</span></label>
|
2013-11-20 19:11:59 +00:00
|
|
|
<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' : '' ) . '" />
|
2013-09-27 12:22:04 +00:00
|
|
|
</p>',
|
|
|
|
'card-expiry-field' => '<p class="form-row form-row-first">
|
2014-03-23 22:49:52 +00:00
|
|
|
<label for="' . esc_attr( $this->id ) . '-card-expiry">' . __( 'Expiry (MM/YY)', 'woocommerce' ) . ' <span class="required">*</span></label>
|
|
|
|
<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' : '' ) . '" />
|
2013-09-27 12:22:04 +00:00
|
|
|
</p>',
|
|
|
|
'card-cvc-field' => '<p class="form-row form-row-last">
|
2014-03-23 22:49:52 +00:00
|
|
|
<label for="' . esc_attr( $this->id ) . '-card-cvc">' . __( 'Card Code', 'woocommerce' ) . ' <span class="required">*</span></label>
|
|
|
|
<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' : '' ) . '" />
|
2013-09-27 12:22:04 +00:00
|
|
|
</p>'
|
|
|
|
);
|
|
|
|
|
|
|
|
$fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) );
|
|
|
|
?>
|
|
|
|
<fieldset id="<?php echo $this->id; ?>-cc-form">
|
2013-12-14 12:03:28 +00:00
|
|
|
<?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
|
2014-02-06 13:00:00 +00:00
|
|
|
<?php
|
|
|
|
foreach ( $fields as $field ) {
|
|
|
|
echo $field;
|
|
|
|
}
|
|
|
|
?>
|
2013-12-14 12:03:28 +00:00
|
|
|
<?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
|
2013-09-27 12:22:04 +00:00
|
|
|
<div class="clear"></div>
|
|
|
|
</fieldset>
|
|
|
|
<?php
|
|
|
|
}
|
2013-10-11 09:17:23 +00:00
|
|
|
}
|