API
4 months ago
Templates
1 year ago
API.php
2 months ago
Blocks_Handler.php
3 months ago
Card_Handler.php
3 years ago
Cash_App_Pay_Blocks_Handler.php
3 months ago
Cash_App_Pay_Gateway.php
3 months ago
Customer_Helper.php
3 years ago
Digital_Wallet.php
3 months ago
Gift_Card.php
3 months ago
Payment_Form.php
3 months ago
Payment_Form.php
322 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 | use WooCommerce\Square\Utilities\Order_Ajax_Authorization; |
| 31 | |
| 32 | /** |
| 33 | * The payment form handler. |
| 34 | * |
| 35 | * @since 2.0.0 |
| 36 | * |
| 37 | * @method \WooCommerce\Square\Gateway get_gateway() |
| 38 | */ |
| 39 | class Payment_Form extends Payment_Gateway_Payment_Form { |
| 40 | |
| 41 | /** |
| 42 | * Adds hooks for rendering the payment form. |
| 43 | * Extended from SV framework and remove render_js action |
| 44 | * |
| 45 | * @see Payment_Gateway_Payment_Form::render() |
| 46 | * |
| 47 | * @since 2.2.3 |
| 48 | */ |
| 49 | protected function add_hooks() { |
| 50 | |
| 51 | $gateway_id = $this->get_gateway()->get_id(); |
| 52 | |
| 53 | // payment form description |
| 54 | add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_payment_form_description' ), 15 ); |
| 55 | |
| 56 | // saved payment methods |
| 57 | add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_saved_payment_methods' ), 20 ); |
| 58 | |
| 59 | // fieldset start |
| 60 | add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_fieldset_start' ), 30 ); |
| 61 | |
| 62 | // payment fields |
| 63 | add_action( "wc_{$gateway_id}_payment_form", array( $this, 'render_payment_fields' ), 0 ); |
| 64 | |
| 65 | // fieldset end |
| 66 | add_action( "wc_{$gateway_id}_payment_form_end", array( $this, 'render_fieldset_end' ), 5 ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Renders any additional billing information we need for processing on pages other than checkout |
| 71 | * e.g. pay page, add payment method page |
| 72 | * |
| 73 | * @since 2.1.0 |
| 74 | */ |
| 75 | public function render_supplementary_billing_info() { |
| 76 | |
| 77 | $billing_data = array(); |
| 78 | $billing_data_source = null; |
| 79 | |
| 80 | // Checkout pay page: use order billing when user may pay for it. Else use session customer (e.g. add-payment-method); never use order there (order-pay can be forged). |
| 81 | if ( ! is_add_payment_method_page() && is_checkout_pay_page() ) { |
| 82 | $order_id = $this->get_gateway()->get_checkout_pay_page_order_id(); |
| 83 | |
| 84 | if ( $order_id ) { |
| 85 | $order = wc_get_order( $order_id ); |
| 86 | |
| 87 | if ( $order && current_user_can( 'pay_for_order', $order_id ) ) { // phpcs:ignore WordPress.WP.Capabilities.Unknown |
| 88 | $billing_data_source = $order; |
| 89 | } |
| 90 | } |
| 91 | } elseif ( WC()->customer && ! is_checkout() ) { |
| 92 | $billing_data_source = WC()->customer; |
| 93 | } |
| 94 | |
| 95 | if ( $billing_data_source ) { |
| 96 | |
| 97 | $billing_data = array( 'billing_postcode' => $billing_data_source->get_billing_postcode() ); |
| 98 | |
| 99 | // 3d secure requires the full billing info |
| 100 | $billing_data = array_merge( |
| 101 | $billing_data, |
| 102 | array( |
| 103 | 'billing_first_name' => $billing_data_source->get_billing_first_name(), |
| 104 | 'billing_last_name' => $billing_data_source->get_billing_last_name(), |
| 105 | 'billing_email' => $billing_data_source->get_billing_email(), |
| 106 | 'billing_country' => $billing_data_source->get_billing_country(), |
| 107 | 'billing_address_1' => $billing_data_source->get_billing_address_1(), |
| 108 | 'billing_address_2' => $billing_data_source->get_billing_address_2(), |
| 109 | 'billing_state' => $billing_data_source->get_billing_state(), |
| 110 | 'billing_city' => $billing_data_source->get_billing_city(), |
| 111 | 'billing_phone' => $billing_data_source->get_billing_phone(), |
| 112 | ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | foreach ( $billing_data as $key => $value ) { |
| 117 | echo '<input type="hidden" id="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />'; |
| 118 | } |
| 119 | |
| 120 | echo '<style> #sq-nudata-modal { z-index: 999999 !important; } </style>'; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Renders the payment fields. |
| 126 | * |
| 127 | * @since 2.0.0 |
| 128 | */ |
| 129 | public function render_payment_fields() { |
| 130 | |
| 131 | $fields = array( |
| 132 | 'card-type', |
| 133 | 'last-four', |
| 134 | 'exp-month', |
| 135 | 'exp-year', |
| 136 | 'payment-nonce', |
| 137 | 'payment-postcode', |
| 138 | ); |
| 139 | |
| 140 | $fields[] = 'buyer-verification-token'; |
| 141 | $fields[] = 'verified-token'; |
| 142 | |
| 143 | foreach ( $fields as $field_id ) { |
| 144 | echo '<input type="hidden" name="wc-' . esc_attr( $this->get_gateway()->get_id_dasherized() ) . '-' . esc_attr( $field_id ) . '" />'; |
| 145 | } |
| 146 | |
| 147 | echo '<div id="wc-' . esc_attr( $this->get_gateway()->get_id_dasherized() ) . '-container"></div>'; |
| 148 | |
| 149 | $this->render_supplementary_billing_info(); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /** |
| 154 | * Gets the credit card fields. |
| 155 | * |
| 156 | * Overridden to add special iframe classes. |
| 157 | * |
| 158 | * @since 2.0.0 |
| 159 | * |
| 160 | * @return array |
| 161 | */ |
| 162 | protected function get_credit_card_fields() { |
| 163 | |
| 164 | $fields = parent::get_credit_card_fields(); |
| 165 | |
| 166 | // Square JS requires a postal code field for the form, but this is pre-filled and hidden |
| 167 | $fields['card-postal-code'] = array( |
| 168 | 'id' => 'wc-' . $this->get_gateway()->get_id_dasherized() . '-postal-code', |
| 169 | 'label' => __( 'Postal code', 'woocommerce-square' ), |
| 170 | 'class' => array( 'form-row-wide' ), |
| 171 | 'required' => true, |
| 172 | 'input_class' => array( 'js-sv-wc-payment-gateway-credit-card-form-input', 'js-sv-wc-payment-gateway-credit-card-form-postal-code' ), |
| 173 | ); |
| 174 | |
| 175 | foreach ( array( 'card-number', 'card-expiry', 'card-csc', 'card-postal-code' ) as $field_key ) { |
| 176 | |
| 177 | if ( isset( $fields[ $field_key ] ) ) { |
| 178 | |
| 179 | // parent div classes - contains both the label and hosted field container div |
| 180 | $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" ) ); |
| 181 | |
| 182 | // hosted field container classes - contains the iframe element |
| 183 | $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" ) ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return $fields; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Renders a payment form field. |
| 193 | * |
| 194 | * @since 2.0.0 |
| 195 | * |
| 196 | * @param array $field field to render |
| 197 | */ |
| 198 | public function render_payment_field( $field ) { |
| 199 | |
| 200 | ?> |
| 201 | <div class="form-row <?php echo implode( ' ', array_map( 'sanitize_html_class', $field['class'] ) ); ?>"> |
| 202 | <label for="<?php echo esc_attr( $field['id'] ) . '-hosted'; ?>"> |
| 203 | <?php |
| 204 | echo esc_html( $field['label'] ); |
| 205 | if ( $field['required'] ) : |
| 206 | ?> |
| 207 | <abbr class="required" title="required"> *</abbr> <?php endif; ?></label> |
| 208 | <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> |
| 209 | </div> |
| 210 | <?php |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Renders the payment form JS. |
| 216 | * |
| 217 | * @since 2.0.0 |
| 218 | */ |
| 219 | public function render_js() { |
| 220 | |
| 221 | $args = array( |
| 222 | 'application_id' => $this->get_gateway()->get_application_id(), |
| 223 | 'ajax_log_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_log_js_data' ), |
| 224 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 225 | 'csc_required' => $this->get_gateway()->csc_enabled(), |
| 226 | 'currency_code' => get_woocommerce_currency(), |
| 227 | 'general_error' => __( 'An error occurred, please try again or try an alternate form of payment.', 'woocommerce-square' ), |
| 228 | 'id' => $this->get_gateway()->get_id(), |
| 229 | 'id_dasherized' => $this->get_gateway()->get_id_dasherized(), |
| 230 | 'is_checkout_registration_enabled' => 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'no' ), |
| 231 | 'is_user_logged_in' => is_user_logged_in(), |
| 232 | 'is_add_payment_method_page' => is_add_payment_method_page(), |
| 233 | 'location_id' => wc_square()->get_settings_handler()->get_location_id(), |
| 234 | 'logging_enabled' => $this->get_gateway()->debug_log(), |
| 235 | 'ajax_wc_checkout_validate_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_checkout_validate' ), |
| 236 | 'is_manual_order_payment' => is_checkout() && is_wc_endpoint_url( 'order-pay' ), |
| 237 | 'payment_token_nonce' => wp_create_nonce( 'payment_token_nonce' ), |
| 238 | 'order_id' => absint( get_query_var( 'order-pay' ) ), |
| 239 | 'order_key' => Order_Ajax_Authorization::get_order_key_for_frontend_localization(), |
| 240 | 'ajax_get_order_amount_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_get_order_amount' ), |
| 241 | 'ajax_should_charge_order_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_should_charge_order' ), |
| 242 | 'is_change_payment_method_request' => $this->get_gateway()->is_change_payment_method_request(), |
| 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_' . esc_html( $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 | ob_start(); |
| 310 | ?> |
| 311 | jQuery(function($) { |
| 312 | window.wc_<?php echo esc_js( $this->get_gateway()->get_id() ); ?>_payment_form_handler = new WC_Square_Payment_Form_Handler( <?php echo wp_json_encode( $args ); ?> ); |
| 313 | }); |
| 314 | <?php |
| 315 | $javascript = ob_get_clean(); |
| 316 | $handle = 'wc-square-' . str_replace( '_', '-', $this->get_gateway()->get_id() ) . '-payment-form-inline'; |
| 317 | \WooCommerce\Square\Utilities\Helper::enqueue_inline_script( $handle, $javascript, array( 'jquery', 'wc-square' ) ); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | } |
| 322 |