API
3 years ago
Templates
3 years ago
API.php
3 years ago
Blocks_Handler.php
3 years ago
Card_Handler.php
3 years ago
Customer_Helper.php
3 years ago
Digital_Wallet.php
3 years ago
Gift_Card.php
3 years ago
Payment_Form.php
3 years ago
Payment_Form.php
315 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Gateway; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway_Helper; |
| 29 | use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway_Payment_Form; |
| 30 | |
| 31 | /** |
| 32 | * The payment form handler. |
| 33 | * |
| 34 | * @since 2.0.0 |
| 35 | * |
| 36 | * @method \WooCommerce\Square\Gateway get_gateway() |
| 37 | */ |
| 38 | class Payment_Form extends Payment_Gateway_Payment_Form { |
| 39 | |
| 40 | /** |
| 41 | * Adds hooks for rendering the payment form. |
| 42 | * Extended from SV framework and remove render_js action |
| 43 | * |
| 44 | * @see Payment_Gateway_Payment_Form::render() |
| 45 | * |
| 46 | * @since 2.2.3 |
| 47 | */ |
| 48 | protected function add_hooks() { |
| 49 | |
| 50 | $gateway_id = $this->get_gateway()->get_id(); |
| 51 | |
| 52 | // payment form description |
| 53 | add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_payment_form_description' ), 15 ); |
| 54 | |
| 55 | // saved payment methods |
| 56 | add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_saved_payment_methods' ), 20 ); |
| 57 | |
| 58 | // fieldset start |
| 59 | add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_fieldset_start' ), 30 ); |
| 60 | |
| 61 | // payment fields |
| 62 | add_action( "wc_{$gateway_id}_payment_form", array( $this, 'render_payment_fields' ), 0 ); |
| 63 | |
| 64 | // fieldset end |
| 65 | add_action( "wc_{$gateway_id}_payment_form_end", array( $this, 'render_fieldset_end' ), 5 ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Renders any additional billing information we need for processing on pages other than checkout |
| 70 | * e.g. pay page, add payment method page |
| 71 | * |
| 72 | * @since 2.1.0 |
| 73 | */ |
| 74 | public function render_supplementary_billing_info() { |
| 75 | |
| 76 | $billing_data = array(); |
| 77 | $billing_data_source = null; |
| 78 | |
| 79 | if ( is_checkout_pay_page() ) { |
| 80 | |
| 81 | if ( $order = wc_get_order( $this->get_gateway()->get_checkout_pay_page_order_id() ) ) { |
| 82 | $billing_data_source = $order; |
| 83 | } |
| 84 | } elseif ( WC()->customer && ! is_checkout() ) { |
| 85 | |
| 86 | $billing_data_source = WC()->customer; |
| 87 | } |
| 88 | |
| 89 | if ( $billing_data_source ) { |
| 90 | |
| 91 | $billing_data = array( 'billing_postcode' => $billing_data_source->get_billing_postcode() ); |
| 92 | |
| 93 | // 3d secure requires the full billing info |
| 94 | $billing_data = array_merge( |
| 95 | $billing_data, |
| 96 | array( |
| 97 | 'billing_first_name' => $billing_data_source->get_billing_first_name(), |
| 98 | 'billing_last_name' => $billing_data_source->get_billing_last_name(), |
| 99 | 'billing_email' => $billing_data_source->get_billing_email(), |
| 100 | 'billing_country' => $billing_data_source->get_billing_country(), |
| 101 | 'billing_address_1' => $billing_data_source->get_billing_address_1(), |
| 102 | 'billing_address_2' => $billing_data_source->get_billing_address_2(), |
| 103 | 'billing_state' => $billing_data_source->get_billing_state(), |
| 104 | 'billing_city' => $billing_data_source->get_billing_city(), |
| 105 | 'billing_phone' => $billing_data_source->get_billing_phone(), |
| 106 | ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | foreach ( $billing_data as $key => $value ) { |
| 111 | echo '<input type="hidden" id="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />'; |
| 112 | } |
| 113 | |
| 114 | if ( is_checkout_pay_page() ) { |
| 115 | |
| 116 | $order = wc_get_order( $this->get_gateway()->get_checkout_pay_page_order_id() ); |
| 117 | |
| 118 | $total_amount = $order->get_total(); |
| 119 | |
| 120 | } else { |
| 121 | |
| 122 | $total_amount = WC()->cart->total; |
| 123 | } |
| 124 | |
| 125 | echo '<input type="hidden" name="wc-' . $this->get_gateway()->get_id_dasherized() . '-amount" value="' . esc_attr( $total_amount > 0 ? $total_amount : '' ) . '" />'; |
| 126 | |
| 127 | echo '<style> #sq-nudata-modal { z-index: 999999 !important; } </style>'; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Renders the payment fields. |
| 133 | * |
| 134 | * @since 2.0.0 |
| 135 | */ |
| 136 | public function render_payment_fields() { |
| 137 | |
| 138 | $fields = array( |
| 139 | 'card-type', |
| 140 | 'last-four', |
| 141 | 'exp-month', |
| 142 | 'exp-year', |
| 143 | 'payment-nonce', |
| 144 | 'payment-postcode', |
| 145 | ); |
| 146 | |
| 147 | $fields[] = 'buyer-verification-token'; |
| 148 | |
| 149 | foreach ( $fields as $field_id ) { |
| 150 | echo '<input type="hidden" name="wc-' . esc_attr( $this->get_gateway()->get_id_dasherized() ) . '-' . esc_attr( $field_id ) . '" />'; |
| 151 | } |
| 152 | |
| 153 | echo '<div id="wc-' . esc_attr( $this->get_gateway()->get_id_dasherized() ) . '-container"></div>'; |
| 154 | |
| 155 | $this->render_supplementary_billing_info(); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Gets the credit card fields. |
| 161 | * |
| 162 | * Overridden to add special iframe classes. |
| 163 | * |
| 164 | * @since 2.0.0 |
| 165 | * |
| 166 | * @return array |
| 167 | */ |
| 168 | protected function get_credit_card_fields() { |
| 169 | |
| 170 | $fields = parent::get_credit_card_fields(); |
| 171 | |
| 172 | // Square JS requires a postal code field for the form, but this is pre-filled and hidden |
| 173 | $fields['card-postal-code'] = array( |
| 174 | 'id' => 'wc-' . $this->get_gateway()->get_id_dasherized() . '-postal-code', |
| 175 | 'label' => __( 'Postal code', 'woocommerce-square' ), |
| 176 | 'class' => array( 'form-row-wide' ), |
| 177 | 'required' => true, |
| 178 | 'input_class' => array( 'js-sv-wc-payment-gateway-credit-card-form-input', 'js-sv-wc-payment-gateway-credit-card-form-postal-code' ), |
| 179 | ); |
| 180 | |
| 181 | foreach ( array( 'card-number', 'card-expiry', 'card-csc', 'card-postal-code' ) as $field_key ) { |
| 182 | |
| 183 | if ( isset( $fields[ $field_key ] ) ) { |
| 184 | |
| 185 | // parent div classes - contains both the label and hosted field container div |
| 186 | $fields[ $field_key ]['class'] = array_merge( $fields[ $field_key ]['class'], array( "wc-{$this->get_gateway()->get_id_dasherized()}-{$field_key}-parent", "wc-{$this->get_gateway()->get_id_dasherized()}-hosted-field-parent" ) ); |
| 187 | |
| 188 | // hosted field container classes - contains the iframe element |
| 189 | $fields[ $field_key ]['input_class'] = array_merge( $fields[ $field_key ]['input_class'], array( "wc-{$this->get_gateway()->get_id_dasherized()}-hosted-field-{$field_key}", "wc-{$this->get_gateway()->get_id_dasherized()}-hosted-field" ) ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return $fields; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /** |
| 198 | * Renders a payment form field. |
| 199 | * |
| 200 | * @since 2.0.0 |
| 201 | * |
| 202 | * @param array $field field to render |
| 203 | */ |
| 204 | public function render_payment_field( $field ) { |
| 205 | |
| 206 | ?> |
| 207 | <div class="form-row <?php echo implode( ' ', array_map( 'sanitize_html_class', $field['class'] ) ); ?>"> |
| 208 | <label for="<?php echo esc_attr( $field['id'] ) . '-hosted'; ?>"> |
| 209 | <?php |
| 210 | echo esc_html( $field['label'] ); |
| 211 | if ( $field['required'] ) : |
| 212 | ?> |
| 213 | <abbr class="required" title="required"> *</abbr> <?php endif; ?></label> |
| 214 | <div id="<?php echo esc_attr( $field['id'] ) . '-hosted'; ?>" class="<?php echo implode( ' ', array_map( 'sanitize_html_class', $field['input_class'] ) ); ?>" data-placeholder="<?php echo isset( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; ?>"></div> |
| 215 | </div> |
| 216 | <?php |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /** |
| 221 | * Renders the payment form JS. |
| 222 | * |
| 223 | * @since 2.0.0 |
| 224 | */ |
| 225 | public function render_js() { |
| 226 | |
| 227 | $args = array( |
| 228 | 'application_id' => $this->get_gateway()->get_application_id(), |
| 229 | 'ajax_log_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_log_js_data' ), |
| 230 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 231 | 'csc_required' => $this->get_gateway()->csc_enabled(), |
| 232 | 'currency_code' => get_woocommerce_currency(), |
| 233 | 'general_error' => __( 'An error occurred, please try again or try an alternate form of payment.', 'woocommerce-square' ), |
| 234 | 'id' => $this->get_gateway()->get_id(), |
| 235 | 'id_dasherized' => $this->get_gateway()->get_id_dasherized(), |
| 236 | 'is_checkout_registration_enabled' => 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'no' ), |
| 237 | 'is_user_logged_in' => is_user_logged_in(), |
| 238 | 'is_add_payment_method_page' => is_add_payment_method_page(), |
| 239 | 'location_id' => wc_square()->get_settings_handler()->get_location_id(), |
| 240 | 'logging_enabled' => $this->get_gateway()->debug_log(), |
| 241 | 'ajax_wc_checkout_validate_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_checkout_validate' ), |
| 242 | 'is_manual_order_payment' => is_checkout() && is_wc_endpoint_url( 'order-pay' ), |
| 243 | ); |
| 244 | |
| 245 | // map the unique square card type string to our framework standards |
| 246 | $square_card_types = array( |
| 247 | Payment_Gateway_Helper::CARD_TYPE_MASTERCARD => 'masterCard', |
| 248 | Payment_Gateway_Helper::CARD_TYPE_AMEX => 'americanExpress', |
| 249 | Payment_Gateway_Helper::CARD_TYPE_DINERSCLUB => 'discoverDiners', |
| 250 | Payment_Gateway_Helper::CARD_TYPE_JCB => 'JCB', |
| 251 | ); |
| 252 | |
| 253 | $card_types = is_array( $this->get_gateway()->get_card_types() ) ? $this->get_gateway()->get_card_types() : array(); |
| 254 | |
| 255 | $framework_card_types = array_map( array( Payment_Gateway_Helper::class, 'normalize_card_type' ), $card_types ); |
| 256 | $square_card_types = array_merge( array_combine( $framework_card_types, $framework_card_types ), $square_card_types ); |
| 257 | |
| 258 | $args['enabled_card_types'] = $framework_card_types; |
| 259 | $args['square_card_types'] = array_flip( $square_card_types ); |
| 260 | |
| 261 | $input_styles = array( |
| 262 | array( |
| 263 | 'backgroundColor' => 'transparent', |
| 264 | 'fontSize' => '1.3em', |
| 265 | ), |
| 266 | ); |
| 267 | |
| 268 | /** |
| 269 | * Filters the the Square payment form input styles. |
| 270 | * |
| 271 | * @since 2.0.0 |
| 272 | * |
| 273 | * @param array $styles array of input styles |
| 274 | */ |
| 275 | $args['input_styles'] = (array) apply_filters( 'wc_' . $this->get_gateway()->get_id() . '_payment_form_input_styles', $input_styles, $this ); |
| 276 | |
| 277 | // TODO remove the deprecated hook in a future version |
| 278 | if ( has_filter( 'woocommerce_square_payment_input_styles' ) ) { |
| 279 | |
| 280 | _deprecated_hook( 'woocommerce_square_payment_input_styles', '2.0.0', null, 'Use "wc_' . $this->get_gateway()->get_id() . '_payment_form_input_styles" as a replacement.' ); |
| 281 | |
| 282 | /** |
| 283 | * Filters the input styles (legacy filter). |
| 284 | * |
| 285 | * @since 1.0.0 |
| 286 | * |
| 287 | * @param string $input_styles styles as JSON encoded array |
| 288 | */ |
| 289 | $args['input_styles'] = json_decode( (string) apply_filters( 'woocommerce_square_payment_input_styles', wp_json_encode( $args['input_styles'] ) ), true ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Payment Gateway Payment Form JS Arguments Filter. |
| 294 | * |
| 295 | * Filter the arguments passed to the Payment Form handler JS class |
| 296 | * |
| 297 | * @since 3.0.0 |
| 298 | * |
| 299 | * @param array $result { |
| 300 | * @type string $plugin_id plugin ID |
| 301 | * @type string $id gateway ID |
| 302 | * @type string $id_dasherized gateway ID dasherized |
| 303 | * @type string $type gateway payment type (e.g. 'credit-card') |
| 304 | * @type bool $csc_required true if CSC field display is required |
| 305 | * } |
| 306 | * @param Payment_Form $this payment form instance |
| 307 | */ |
| 308 | $args = apply_filters( 'wc_' . $this->get_gateway()->get_id() . '_payment_form_js_args', $args, $this ); |
| 309 | |
| 310 | wc_enqueue_js( sprintf( 'window.wc_%s_payment_form_handler = new WC_Square_Payment_Form_Handler( %s );', esc_js( $this->get_gateway()->get_id() ), json_encode( $args ) ) ); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | } |
| 315 |