/includes/abstracts/abstract-wc-payment-gateway.php

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { 
  4. exit; 
  5.  
  6. /** 
  7. * WooCommerce Payment Gateway class. 
  8. * 
  9. * Extended by individual payment gateways to handle payments. 
  10. * 
  11. * @class WC_Payment_Gateway 
  12. * @extends WC_Settings_API 
  13. * @version 2.1.0 
  14. * @package WooCommerce/Abstracts 
  15. * @category Abstract Class 
  16. * @author WooThemes 
  17. */ 
  18. abstract class WC_Payment_Gateway extends WC_Settings_API { 
  19.  
  20. /** 
  21. * Set if the place order button should be renamed on selection. 
  22. * @var string 
  23. */ 
  24. public $order_button_text; 
  25.  
  26. /** 
  27. * yes or no based on whether the method is enabled. 
  28. * @var string 
  29. */ 
  30. public $enabled = 'yes'; 
  31.  
  32. /** 
  33. * Payment method title for the frontend. 
  34. * @var string 
  35. */ 
  36. public $title; 
  37.  
  38. /** 
  39. * Payment method description for the frontend. 
  40. * @var string 
  41. */ 
  42. public $description; 
  43.  
  44. /** 
  45. * Chosen payment method id. 
  46. * @var bool 
  47. */ 
  48. public $chosen; 
  49.  
  50. /** 
  51. * Gateway title. 
  52. * @var string 
  53. */ 
  54. public $method_title = ''; 
  55.  
  56. /** 
  57. * Gateway description. 
  58. * @var string 
  59. */ 
  60. public $method_description = ''; 
  61.  
  62. /** 
  63. * True if the gateway shows fields on the checkout. 
  64. * @var bool 
  65. */ 
  66. public $has_fields; 
  67.  
  68. /** 
  69. * Countries this gateway is allowed for. 
  70. * @var array 
  71. */ 
  72. public $countries; 
  73.  
  74. /** 
  75. * Available for all counties or specific. 
  76. * @var string 
  77. */ 
  78. public $availability; 
  79.  
  80. /** 
  81. * Icon for the gateway. 
  82. * @var string 
  83. */ 
  84. public $icon; 
  85.  
  86. /** 
  87. * Supported features such as 'default_credit_card_form', 'refunds'. 
  88. * @var array 
  89. */ 
  90. public $supports = array( 'products' ); 
  91.  
  92. /** 
  93. * Maximum transaction amount, zero does not define a maximum. 
  94. * @var int 
  95. */ 
  96. public $max_amount = 0; 
  97.  
  98. /** 
  99. * Optional URL to view a transaction. 
  100. * @var string 
  101. */ 
  102. public $view_transaction_url = ''; 
  103.  
  104. /** 
  105. * Optional label to show for "new payment method" in the payment 
  106. * method/token selection radio selection. 
  107. * @var string 
  108. */ 
  109. public $new_method_label = ''; 
  110.  
  111. /** 
  112. * Contains a users saved tokens for this gateway. 
  113. * @var array 
  114. */ 
  115. protected $tokens = array(); 
  116.  
  117. /** 
  118. * Returns a users saved tokens for this gateway. 
  119. * @since 2.6.0 
  120. * @return array 
  121. */ 
  122. public function get_tokens() { 
  123. if ( sizeof( $this->tokens ) > 0 ) { 
  124. return $this->tokens; 
  125.  
  126. if ( is_user_logged_in() && $this->supports( 'tokenization' ) ) { 
  127. $this->tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), $this->id ); 
  128.  
  129. return $this->tokens; 
  130.  
  131. /** 
  132. * Return the title for admin screens. 
  133. * @return string 
  134. */ 
  135. public function get_method_title() { 
  136. return apply_filters( 'woocommerce_gateway_method_title', $this->method_title, $this ); 
  137.  
  138. /** 
  139. * Return the description for admin screens. 
  140. * @return string 
  141. */ 
  142. public function get_method_description() { 
  143. return apply_filters( 'woocommerce_gateway_method_description', $this->method_description, $this ); 
  144.  
  145. /** 
  146. * Output the gateway settings screen. 
  147. */ 
  148. public function admin_options() { 
  149. echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>'; 
  150. echo wp_kses_post( wpautop( $this->get_method_description() ) ); 
  151. parent::admin_options(); 
  152.  
  153. /** 
  154. * Init settings for gateways. 
  155. */ 
  156. public function init_settings() { 
  157. parent::init_settings(); 
  158. $this->enabled = ! empty( $this->settings['enabled'] ) && 'yes' === $this->settings['enabled'] ? 'yes' : 'no'; 
  159.  
  160. /** 
  161. * Get the return url (thank you page). 
  162. * 
  163. * @param WC_Order $order 
  164. * @return string 
  165. */ 
  166. public function get_return_url( $order = null ) { 
  167. if ( $order ) { 
  168. $return_url = $order->get_checkout_order_received_url(); 
  169. } else { 
  170. $return_url = wc_get_endpoint_url( 'order-received', '', wc_get_page_permalink( 'checkout' ) ); 
  171.  
  172. if ( is_ssl() || get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) { 
  173. $return_url = str_replace( 'http:', 'https:', $return_url ); 
  174.  
  175. return apply_filters( 'woocommerce_get_return_url', $return_url, $order ); 
  176.  
  177. /** 
  178. * Get a link to the transaction on the 3rd party gateway size (if applicable). 
  179. * 
  180. * @param WC_Order $order the order object. 
  181. * @return string transaction URL, or empty string. 
  182. */ 
  183. public function get_transaction_url( $order ) { 
  184.  
  185. $return_url = ''; 
  186. $transaction_id = $order->get_transaction_id(); 
  187.  
  188. if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { 
  189. $return_url = sprintf( $this->view_transaction_url, $transaction_id ); 
  190.  
  191. return apply_filters( 'woocommerce_get_transaction_url', $return_url, $order, $this ); 
  192.  
  193. /** 
  194. * Get the order total in checkout and pay_for_order. 
  195. * 
  196. * @return float 
  197. */ 
  198. protected function get_order_total() { 
  199.  
  200. $total = 0; 
  201. $order_id = absint( get_query_var( 'order-pay' ) ); 
  202.  
  203. // Gets order total from "pay for order" page. 
  204. if ( 0 < $order_id ) { 
  205. $order = wc_get_order( $order_id ); 
  206. $total = (float) $order->get_total(); 
  207.  
  208. // Gets order total from cart/checkout. 
  209. } elseif ( 0 < WC()->cart->total ) { 
  210. $total = (float) WC()->cart->total; 
  211.  
  212. return $total; 
  213.  
  214. /** 
  215. * Check if the gateway is available for use. 
  216. * 
  217. * @return bool 
  218. */ 
  219. public function is_available() { 
  220. $is_available = ( 'yes' === $this->enabled ); 
  221.  
  222. if ( WC()->cart && 0 < $this->get_order_total() && 0 < $this->max_amount && $this->max_amount < $this->get_order_total() ) { 
  223. $is_available = false; 
  224.  
  225. return $is_available; 
  226.  
  227. /** 
  228. * Check if the gateway has fields on the checkout. 
  229. * 
  230. * @return bool 
  231. */ 
  232. public function has_fields() { 
  233. return $this->has_fields ? true : false; 
  234.  
  235. /** 
  236. * Return the gateway's title. 
  237. * 
  238. * @return string 
  239. */ 
  240. public function get_title() { 
  241. return apply_filters( 'woocommerce_gateway_title', $this->title, $this->id ); 
  242.  
  243. /** 
  244. * Return the gateway's description. 
  245. * 
  246. * @return string 
  247. */ 
  248. public function get_description() { 
  249. return apply_filters( 'woocommerce_gateway_description', $this->description, $this->id ); 
  250.  
  251. /** 
  252. * Return the gateway's icon. 
  253. * 
  254. * @return string 
  255. */ 
  256. public function get_icon() { 
  257.  
  258. $icon = $this->icon ? '<img src="' . WC_HTTPS::force_https_url( $this->icon ) . '" alt="' . esc_attr( $this->get_title() ) . '" />' : ''; 
  259.  
  260. return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id ); 
  261.  
  262. /** 
  263. * Set as current gateway. 
  264. * 
  265. * Set this as the current gateway. 
  266. */ 
  267. public function set_current() { 
  268. $this->chosen = true; 
  269.  
  270. /** 
  271. * Process Payment. 
  272. * 
  273. * Process the payment. Override this in your gateway. When implemented, this should. 
  274. * return the success and redirect in an array. e.g: 
  275. * 
  276. * return array( 
  277. * 'result' => 'success',  
  278. * 'redirect' => $this->get_return_url( $order ) 
  279. * ); 
  280. * 
  281. * @param int $order_id 
  282. * @return array 
  283. */ 
  284. public function process_payment( $order_id ) { 
  285. return array(); 
  286.  
  287. /** 
  288. * Process refund. 
  289. * 
  290. * If the gateway declares 'refunds' support, this will allow it to refund. 
  291. * a passed in amount. 
  292. * 
  293. * @param int $order_id 
  294. * @param float $amount 
  295. * @param string $reason 
  296. * @return boolean True or false based on success, or a WP_Error object. 
  297. */ 
  298. public function process_refund( $order_id, $amount = null, $reason = '' ) { 
  299. return false; 
  300.  
  301. /** 
  302. * Validate frontend fields. 
  303. * 
  304. * Validate payment fields on the frontend. 
  305. * 
  306. * @return bool 
  307. */ 
  308. public function validate_fields() { return true; } 
  309.  
  310. /** 
  311. * If There are no payment fields show the description if set. 
  312. * Override this in your gateway if you have some. 
  313. */ 
  314. public function payment_fields() { 
  315. if ( $description = $this->get_description() ) { 
  316. echo wpautop( wptexturize( $description ) ); 
  317.  
  318. if ( $this->supports( 'default_credit_card_form' ) ) { 
  319. $this->credit_card_form(); // Deprecated, will be removed in a future version. 
  320.  
  321. /** 
  322. * Check if a gateway supports a given feature. 
  323. * 
  324. * Gateways should override this to declare support (or lack of support) for a feature. 
  325. * For backward compatibility, gateways support 'products' by default, but nothing else. 
  326. * 
  327. * @param string $feature string The name of a feature to test support for. 
  328. * @return bool True if the gateway supports the feature, false otherwise. 
  329. * @since 1.5.7 
  330. */ 
  331. public function supports( $feature ) { 
  332. return apply_filters( 'woocommerce_payment_gateway_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this ); 
  333.  
  334. /** 
  335. * Core credit card form which gateways can used if needed. Deprecated - inheirt WC_Payment_Gateway_CC instead. 
  336. * @param array $args 
  337. * @param array $fields 
  338. */ 
  339. public function credit_card_form( $args = array(), $fields = array() ) { 
  340. wc_deprecated_function( 'credit_card_form', '2.6', 'WC_Payment_Gateway_CC->form' ); 
  341. $cc_form = new WC_Payment_Gateway_CC; 
  342. $cc_form->id = $this->id; 
  343. $cc_form->supports = $this->supports; 
  344. $cc_form->form(); 
  345.  
  346. /** 
  347. * Enqueues our tokenization script to handle some of the new form options. 
  348. * @since 2.6.0 
  349. */ 
  350. public function tokenization_script() { 
  351. wp_enqueue_script( 
  352. 'woocommerce-tokenization-form',  
  353. plugins_url( '/assets/js/frontend/tokenization-form' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js', WC_PLUGIN_FILE ),  
  354. array( 'jquery' ),  
  355. WC()->version 
  356. ); 
  357.  
  358. /** 
  359. * Grab and display our saved payment methods. 
  360. * @since 2.6.0 
  361. */ 
  362. public function saved_payment_methods() { 
  363. $html = '<ul class="woocommerce-SavedPaymentMethods wc-saved-payment-methods" data-count="' . esc_attr( count( $this->get_tokens() ) ) . '">'; 
  364.  
  365. foreach ( $this->get_tokens() as $token ) { 
  366. $html .= $this->get_saved_payment_method_option_html( $token ); 
  367.  
  368. $html .= $this->get_new_payment_method_option_html(); 
  369. $html .= '</ul>'; 
  370.  
  371. echo apply_filters( 'wc_payment_gateway_form_saved_payment_methods_html', $html, $this ); 
  372.  
  373. /** 
  374. * Gets saved payment method HTML from a token. 
  375. * @since 2.6.0 
  376. * @param WC_Payment_Token $token Payment Token 
  377. * @return string Generated payment method HTML 
  378. */ 
  379. public function get_saved_payment_method_option_html( $token ) { 
  380. $html = sprintf( 
  381. '<li class="woocommerce-SavedPaymentMethods-token"> 
  382. <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 /> 
  383. <label for="wc-%1$s-payment-token-%2$s">%3$s</label> 
  384. </li>',  
  385. esc_attr( $this->id ),  
  386. esc_attr( $token->get_id() ),  
  387. esc_html( $token->get_display_name() ),  
  388. checked( $token->is_default(), true, false ) 
  389. ); 
  390.  
  391. return apply_filters( 'woocommerce_payment_gateway_get_saved_payment_method_option_html', $html, $token, $this ); 
  392.  
  393. /** 
  394. * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. 
  395. * Only displayed when a gateway supports tokenization. 
  396. * @since 2.6.0 
  397. */ 
  398. public function get_new_payment_method_option_html() { 
  399. $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 ); 
  400. $html = sprintf( 
  401. '<li class="woocommerce-SavedPaymentMethods-new"> 
  402. <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" /> 
  403. <label for="wc-%1$s-payment-token-new">%2$s</label> 
  404. </li>',  
  405. esc_attr( $this->id ),  
  406. esc_html( $label ) 
  407. ); 
  408.  
  409. return apply_filters( 'woocommerce_payment_gateway_get_new_payment_method_option_html', $html, $this ); 
  410.  
  411. /** 
  412. * Outputs a checkbox for saving a new payment method to the database. 
  413. * @since 2.6.0 
  414. */ 
  415. public function save_payment_method_checkbox() { 
  416. printf( 
  417. '<p class="form-row woocommerce-SavedPaymentMethods-saveNew"> 
  418. <input id="wc-%1$s-new-payment-method" name="wc-%1$s-new-payment-method" type="checkbox" value="true" style="width:auto;" /> 
  419. <label for="wc-%1$s-new-payment-method" style="display:inline;">%2$s</label> 
  420. </p>',  
  421. esc_attr( $this->id ),  
  422. esc_html__( 'Save to account', 'woocommerce' ) 
  423. ); 
.