PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-payment-gateway.php

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