PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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