WC_Payment_Gateway

WooCommerce Payment Gateway class.

Defined (1)

The class is defined in the following location(s).

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