PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.5
WooCommerce Square v2.2.5
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Gateway / Payment_Form.php
woocommerce-square / includes / Gateway Last commit date
API 5 years ago API.php 6 years ago Card_Handler.php 6 years ago Customer_Helper.php 6 years ago Payment_Form.php 5 years ago
Payment_Form.php
323 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
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
22 */
23
24 namespace WooCommerce\Square\Gateway;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework;
29
30 /**
31 * The payment form handler.
32 *
33 * @since 2.0.0
34 *
35 * @method \WooCommerce\Square\Gateway get_gateway()
36 */
37 class Payment_Form extends Framework\SV_WC_Payment_Gateway_Payment_Form {
38
39 /**
40 * Adds hooks for rendering the payment form.
41 * Extended from SV framework and remove render_js action
42 *
43 * @see SV_WC_Payment_Gateway_Payment_Form::render()
44 *
45 * @since 2.2.3
46 */
47 protected function add_hooks() {
48
49 $gateway_id = $this->get_gateway()->get_id();
50
51 // payment form description
52 add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_payment_form_description' ), 15 );
53
54 // saved payment methods
55 add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_saved_payment_methods' ), 20 );
56
57 // sample eCheck image (if eCheck gateway)
58 add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_sample_check' ), 25 );
59
60 // fieldset start
61 add_action( "wc_{$gateway_id}_payment_form_start", array( $this, 'render_fieldset_start' ), 30 );
62
63 // payment fields
64 add_action( "wc_{$gateway_id}_payment_form", array( $this, 'render_payment_fields' ), 0 );
65
66 // fieldset end
67 add_action( "wc_{$gateway_id}_payment_form_end", array( $this, 'render_fieldset_end' ), 5 );
68 }
69
70 /**
71 * Renders any additional billing information we need for processing on pages other than checkout
72 * e.g. pay page, add payment method page
73 *
74 * @since 2.1.0
75 */
76 public function render_supplementary_billing_info() {
77
78 $billing_data = array();
79 $billing_data_source = null;
80
81 if ( is_checkout_pay_page() ) {
82
83 if ( $order = wc_get_order( $this->get_gateway()->get_checkout_pay_page_order_id() ) ) {
84 $billing_data_source = $order;
85 }
86 } elseif ( WC()->customer && ! is_checkout() ) {
87
88 $billing_data_source = WC()->customer;
89 }
90
91 if ( $billing_data_source ) {
92
93 $billing_data = array( 'billing_postcode' => $billing_data_source->get_billing_postcode() );
94
95 // 3d secure requires the full billing info
96 if ( $this->get_gateway()->is_3d_secure_enabled() ) {
97
98 $billing_data = array_merge(
99 $billing_data,
100 array(
101 'billing_first_name' => $billing_data_source->get_billing_first_name(),
102 'billing_last_name' => $billing_data_source->get_billing_last_name(),
103 'billing_email' => $billing_data_source->get_billing_email(),
104 'billing_country' => $billing_data_source->get_billing_country(),
105 'billing_address_1' => $billing_data_source->get_billing_address_1(),
106 'billing_address_2' => $billing_data_source->get_billing_address_2(),
107 'billing_state' => $billing_data_source->get_billing_state(),
108 'billing_city' => $billing_data_source->get_billing_city(),
109 'billing_phone' => $billing_data_source->get_billing_phone(),
110 )
111 );
112 }
113 }
114
115 foreach ( $billing_data as $key => $value ) {
116 echo '<input type="hidden" id="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />';
117 }
118
119 if ( is_checkout_pay_page() ) {
120
121 $order = wc_get_order( $this->get_gateway()->get_checkout_pay_page_order_id() );
122
123 $total_amount = $order->get_total();
124
125 } else {
126
127 $total_amount = WC()->cart->total;
128 }
129
130 echo '<input type="hidden" name="wc-' . $this->get_gateway()->get_id_dasherized() . '-amount" value="' . esc_attr( $total_amount > 0 ? $total_amount : '' ) . '" />';
131
132 echo '<style> #sq-nudata-modal { z-index: 999999 !important; } </style>';
133 }
134
135
136 /**
137 * Renders the payment fields.
138 *
139 * @since 2.0.0
140 */
141 public function render_payment_fields() {
142
143 parent::render_payment_fields();
144
145 $fields = array(
146 'card-type',
147 'last-four',
148 'exp-month',
149 'exp-year',
150 'payment-nonce',
151 'payment-postcode',
152 );
153
154 if ( $this->get_gateway()->is_3d_secure_enabled() ) {
155 $fields[] = 'buyer-verification-token';
156 }
157
158 foreach ( $fields as $field_id ) {
159 echo '<input type="hidden" name="wc-' . esc_attr( $this->get_gateway()->get_id_dasherized() ) . '-' . esc_attr( $field_id ) . '" />';
160 }
161
162 $this->render_supplementary_billing_info();
163 }
164
165
166 /**
167 * Gets the credit card fields.
168 *
169 * Overridden to add special iframe classes.
170 *
171 * @since 2.0.0
172 *
173 * @return array
174 */
175 protected function get_credit_card_fields() {
176
177 $fields = parent::get_credit_card_fields();
178
179 // Square JS requires a postal code field for the form, but this is pre-filled and hidden
180 $fields['card-postal-code'] = array(
181 'id' => 'wc-' . $this->get_gateway()->get_id_dasherized() . '-postal-code',
182 'label' => __( 'Postal code', 'woocommerce-square' ),
183 'class' => array( 'form-row-wide' ),
184 'required' => true,
185 'input_class' => array( 'js-sv-wc-payment-gateway-credit-card-form-input', 'js-sv-wc-payment-gateway-credit-card-form-postal-code' ),
186 );
187
188 foreach ( array( 'card-number', 'card-expiry', 'card-csc', 'card-postal-code' ) as $field_key ) {
189
190 if ( isset( $fields[ $field_key ] ) ) {
191
192 // parent div classes - contains both the label and hosted field container div
193 $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" ) );
194
195 // hosted field container classes - contains the iframe element
196 $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" ) );
197 }
198 }
199
200 return $fields;
201 }
202
203
204 /**
205 * Renders a payment form field.
206 *
207 * @since 2.0.0
208 *
209 * @param array $field field to render
210 */
211 public function render_payment_field( $field ) {
212
213 ?>
214 <div class="form-row <?php echo implode( ' ', array_map( 'sanitize_html_class', $field['class'] ) ); ?>">
215 <label for="<?php echo esc_attr( $field['id'] ) . '-hosted'; ?>">
216 <?php
217 echo esc_html( $field['label'] );
218 if ( $field['required'] ) :
219 ?>
220 <abbr class="required" title="required">&nbsp;*</abbr> <?php endif; ?></label>
221 <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>
222 </div>
223 <?php
224 }
225
226
227 /**
228 * Renders the payment form JS.
229 *
230 * @since 2.0.0
231 */
232 public function render_js() {
233
234 $args = array(
235 'application_id' => $this->get_gateway()->get_application_id(),
236 'ajax_log_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_log_js_data' ),
237 'ajax_url' => admin_url( 'admin-ajax.php' ),
238 'csc_required' => $this->get_gateway()->csc_enabled(),
239 'currency_code' => get_woocommerce_currency(),
240 'general_error' => __( 'An error occurred, please try again or try an alternate form of payment.', 'woocommerce-square' ),
241 'id' => $this->get_gateway()->get_id(),
242 'id_dasherized' => $this->get_gateway()->get_id_dasherized(),
243 'is_3d_secure_enabled' => $this->get_gateway()->is_3d_secure_enabled(),
244 'is_checkout_registration_enabled' => 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'no' ),
245 'is_user_logged_in' => is_user_logged_in(),
246 'is_add_payment_method_page' => is_add_payment_method_page(),
247 'location_id' => wc_square()->get_settings_handler()->get_location_id(),
248 'logging_enabled' => $this->get_gateway()->debug_log(),
249 'ajax_wc_checkout_validate_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_checkout_validate' ),
250 'is_manual_order_payment' => is_checkout() && is_wc_endpoint_url( 'order-pay' ),
251 );
252
253 // map the unique square card type string to our framework standards
254 $square_card_types = array(
255 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_MASTERCARD => 'masterCard',
256 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_AMEX => 'americanExpress',
257 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_DINERSCLUB => 'discoverDiners',
258 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_JCB => 'JCB',
259 );
260
261 $card_types = is_array( $this->get_gateway()->get_card_types() ) ? $this->get_gateway()->get_card_types() : array();
262
263 $framework_card_types = array_map( array( Framework\SV_WC_Payment_Gateway_Helper::class, 'normalize_card_type' ), $card_types );
264 $square_card_types = array_merge( array_combine( $framework_card_types, $framework_card_types ), $square_card_types );
265
266 $args['enabled_card_types'] = $framework_card_types;
267 $args['square_card_types'] = array_flip( $square_card_types );
268
269 $input_styles = array(
270 array(
271 'backgroundColor' => 'transparent',
272 'fontSize' => '1.3em',
273 ),
274 );
275
276 /**
277 * Filters the the Square payment form input styles.
278 *
279 * @since 2.0.0
280 *
281 * @param array $styles array of input styles
282 */
283 $args['input_styles'] = (array) apply_filters( 'wc_' . $this->get_gateway()->get_id() . '_payment_form_input_styles', $input_styles, $this );
284
285 // TODO remove the deprecated hook in a future version
286 if ( has_filter( 'woocommerce_square_payment_input_styles' ) ) {
287
288 _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.' );
289
290 /**
291 * Filters the input styles (legacy filter).
292 *
293 * @since 1.0.0
294 *
295 * @param string $input_styles styles as JSON encoded array
296 */
297 $args['input_styles'] = json_decode( (string) apply_filters( 'woocommerce_square_payment_input_styles', wp_json_encode( $args['input_styles'] ) ), true );
298 }
299
300 /**
301 * Payment Gateway Payment Form JS Arguments Filter.
302 *
303 * Filter the arguments passed to the Payment Form handler JS class
304 *
305 * @since 3.0.0
306 *
307 * @param array $result {
308 * @type string $plugin_id plugin ID
309 * @type string $id gateway ID
310 * @type string $id_dasherized gateway ID dasherized
311 * @type string $type gateway payment type (e.g. 'credit-card')
312 * @type bool $csc_required true if CSC field display is required
313 * }
314 * @param Payment_Form $this payment form instance
315 */
316 $args = apply_filters( 'wc_' . $this->get_gateway()->get_id() . '_payment_form_js_args', $args, $this );
317
318 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 ) ) );
319 }
320
321
322 }
323