class-wc-payment-gateway-cc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Class WC_Payment_Gateway_CC file.
  4. *
  5. * @package WooCommerce\Gateways
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * Credit Card Payment Gateway
  12. *
  13. * @since 2.6.0
  14. * @package WooCommerce/Classes
  15. */
  16. class WC_Payment_Gateway_CC extends WC_Payment_Gateway {
  17. /**
  18. * Builds our payment fields area - including tokenization fields for logged
  19. * in users, and the actual payment fields.
  20. *
  21. * @since 2.6.0
  22. */
  23. public function payment_fields() {
  24. if ( $this->supports( 'tokenization' ) && is_checkout() ) {
  25. $this->tokenization_script();
  26. $this->saved_payment_methods();
  27. $this->form();
  28. $this->save_payment_method_checkbox();
  29. } else {
  30. $this->form();
  31. }
  32. }
  33. /**
  34. * Output field name HTML
  35. *
  36. * Gateways which support tokenization do not require names - we don't want the data to post to the server.
  37. *
  38. * @since 2.6.0
  39. * @param string $name Field name.
  40. * @return string
  41. */
  42. public function field_name( $name ) {
  43. return $this->supports( 'tokenization' ) ? '' : ' name="' . esc_attr( $this->id . '-' . $name ) . '" ';
  44. }
  45. /**
  46. * Outputs fields for entering credit card information.
  47. *
  48. * @since 2.6.0
  49. */
  50. public function form() {
  51. wp_enqueue_script( 'wc-credit-card-form' );
  52. $fields = array();
  53. $cvc_field = '<p class="form-row form-row-last">
  54. <label for="' . esc_attr( $this->id ) . '-card-cvc">' . esc_html__( 'Card code', 'woocommerce' ) . '&nbsp;<span class="required">*</span></label>
  55. <input id="' . esc_attr( $this->id ) . '-card-cvc" class="input-text wc-credit-card-form-card-cvc" inputmode="numeric" autocomplete="off" autocorrect="no" autocapitalize="no" spellcheck="no" type="tel" maxlength="4" placeholder="' . esc_attr__( 'CVC', 'woocommerce' ) . '" ' . $this->field_name( 'card-cvc' ) . ' style="width:100px" />
  56. </p>';
  57. $default_fields = array(
  58. 'card-number-field' => '<p class="form-row form-row-wide">
  59. <label for="' . esc_attr( $this->id ) . '-card-number">' . esc_html__( 'Card number', 'woocommerce' ) . '&nbsp;<span class="required">*</span></label>
  60. <input id="' . esc_attr( $this->id ) . '-card-number" class="input-text wc-credit-card-form-card-number" inputmode="numeric" autocomplete="cc-number" autocorrect="no" autocapitalize="no" spellcheck="no" type="tel" placeholder="&bull;&bull;&bull;&bull; &bull;&bull;&bull;&bull; &bull;&bull;&bull;&bull; &bull;&bull;&bull;&bull;" ' . $this->field_name( 'card-number' ) . ' />
  61. </p>',
  62. 'card-expiry-field' => '<p class="form-row form-row-first">
  63. <label for="' . esc_attr( $this->id ) . '-card-expiry">' . esc_html__( 'Expiry (MM/YY)', 'woocommerce' ) . '&nbsp;<span class="required">*</span></label>
  64. <input id="' . esc_attr( $this->id ) . '-card-expiry" class="input-text wc-credit-card-form-card-expiry" inputmode="numeric" autocomplete="cc-exp" autocorrect="no" autocapitalize="no" spellcheck="no" type="tel" placeholder="' . esc_attr__( 'MM / YY', 'woocommerce' ) . '" ' . $this->field_name( 'card-expiry' ) . ' />
  65. </p>',
  66. );
  67. if ( ! $this->supports( 'credit_card_form_cvc_on_saved_method' ) ) {
  68. $default_fields['card-cvc-field'] = $cvc_field;
  69. }
  70. $fields = wp_parse_args( $fields, apply_filters( 'woocommerce_credit_card_form_fields', $default_fields, $this->id ) );
  71. ?>
  72. <fieldset id="wc-<?php echo esc_attr( $this->id ); ?>-cc-form" class='wc-credit-card-form wc-payment-form'>
  73. <?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?>
  74. <?php
  75. foreach ( $fields as $field ) {
  76. echo $field; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
  77. }
  78. ?>
  79. <?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?>
  80. <div class="clear"></div>
  81. </fieldset>
  82. <?php
  83. if ( $this->supports( 'credit_card_form_cvc_on_saved_method' ) ) {
  84. echo '<fieldset>' . $cvc_field . '</fieldset>'; // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
  85. }
  86. }
  87. }