abstract-wc-payment-gateway.php 13 KB

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