PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.2
WooCommerce Square v2.2.2
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 6 years ago API.php 6 years ago Card_Handler.php 6 years ago Customer_Helper.php 6 years ago Payment_Form.php 6 years ago
Payment_Form.php
293 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 /**
41 * Renders any additional billing information we need for processing on pages other than checkout
42 * e.g. pay page, add payment method page
43 *
44 * @since 2.1.0
45 */
46 public function render_supplementary_billing_info() {
47
48 $billing_data = array();
49 $billing_data_source = null;
50
51 if ( is_checkout_pay_page() ) {
52
53 if ( $order = wc_get_order( $this->get_gateway()->get_checkout_pay_page_order_id() ) ) {
54 $billing_data_source = $order;
55 }
56 } elseif ( WC()->customer && ! is_checkout() ) {
57
58 $billing_data_source = WC()->customer;
59 }
60
61 if ( $billing_data_source ) {
62
63 $billing_data = array( 'billing_postcode' => $billing_data_source->get_billing_postcode() );
64
65 // 3d secure requires the full billing info
66 if ( $this->get_gateway()->is_3d_secure_enabled() ) {
67
68 $billing_data = array_merge(
69 $billing_data,
70 array(
71 'billing_first_name' => $billing_data_source->get_billing_first_name(),
72 'billing_last_name' => $billing_data_source->get_billing_last_name(),
73 'billing_email' => $billing_data_source->get_billing_email(),
74 'billing_country' => $billing_data_source->get_billing_country(),
75 'billing_address_1' => $billing_data_source->get_billing_address_1(),
76 'billing_address_2' => $billing_data_source->get_billing_address_2(),
77 'billing_state' => $billing_data_source->get_billing_state(),
78 'billing_city' => $billing_data_source->get_billing_city(),
79 'billing_phone' => $billing_data_source->get_billing_phone(),
80 )
81 );
82 }
83 }
84
85 foreach ( $billing_data as $key => $value ) {
86 echo '<input type="hidden" id="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '" />';
87 }
88
89 if ( is_checkout_pay_page() ) {
90
91 $order = wc_get_order( $this->get_gateway()->get_checkout_pay_page_order_id() );
92
93 $total_amount = $order->get_total();
94
95 } else {
96
97 $total_amount = WC()->cart->total;
98 }
99
100 echo '<input type="hidden" name="wc-' . $this->get_gateway()->get_id_dasherized() . '-amount" value="' . esc_attr( $total_amount > 0 ? $total_amount : '' ) . '" />';
101
102 echo '<style> #sq-nudata-modal { z-index: 999999 !important; } </style>';
103 }
104
105
106 /**
107 * Renders the payment fields.
108 *
109 * @since 2.0.0
110 */
111 public function render_payment_fields() {
112
113 parent::render_payment_fields();
114
115 $fields = array(
116 'card-type',
117 'last-four',
118 'exp-month',
119 'exp-year',
120 'payment-nonce',
121 'payment-postcode',
122 );
123
124 if ( $this->get_gateway()->is_3d_secure_enabled() ) {
125 $fields[] = 'buyer-verification-token';
126 }
127
128 foreach ( $fields as $field_id ) {
129 echo '<input type="hidden" name="wc-' . esc_attr( $this->get_gateway()->get_id_dasherized() ) . '-' . esc_attr( $field_id ) . '" />';
130 }
131
132 $this->render_supplementary_billing_info();
133 }
134
135
136 /**
137 * Gets the credit card fields.
138 *
139 * Overridden to add special iframe classes.
140 *
141 * @since 2.0.0
142 *
143 * @return array
144 */
145 protected function get_credit_card_fields() {
146
147 $fields = parent::get_credit_card_fields();
148
149 // Square JS requires a postal code field for the form, but this is pre-filled and hidden
150 $fields['card-postal-code'] = array(
151 'id' => 'wc-' . $this->get_gateway()->get_id_dasherized() . '-postal-code',
152 'label' => __( 'Postal code', 'woocommerce-square' ),
153 'class' => array( 'form-row-wide' ),
154 'required' => true,
155 'input_class' => array( 'js-sv-wc-payment-gateway-credit-card-form-input', 'js-sv-wc-payment-gateway-credit-card-form-postal-code' ),
156 );
157
158 foreach ( array( 'card-number', 'card-expiry', 'card-csc', 'card-postal-code' ) as $field_key ) {
159
160 if ( isset( $fields[ $field_key ] ) ) {
161
162 // parent div classes - contains both the label and hosted field container div
163 $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" ) );
164
165 // hosted field container classes - contains the iframe element
166 $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" ) );
167 }
168 }
169
170 return $fields;
171 }
172
173
174 /**
175 * Renders a payment form field.
176 *
177 * @since 2.0.0
178 *
179 * @param array $field field to render
180 */
181 public function render_payment_field( $field ) {
182
183 ?>
184 <div class="form-row <?php echo implode( ' ', array_map( 'sanitize_html_class', $field['class'] ) ); ?>">
185 <label for="<?php echo esc_attr( $field['id'] ) . '-hosted'; ?>">
186 <?php
187 echo esc_html( $field['label'] );
188 if ( $field['required'] ) :
189 ?>
190 <abbr class="required" title="required">&nbsp;*</abbr> <?php endif; ?></label>
191 <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>
192 </div>
193 <?php
194 }
195
196
197 /**
198 * Renders the payment form JS.
199 *
200 * @since 2.0.0
201 */
202 public function render_js() {
203
204 $args = array(
205 'application_id' => $this->get_gateway()->get_application_id(),
206 'ajax_log_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_log_js_data' ),
207 'ajax_url' => admin_url( 'admin-ajax.php' ),
208 'csc_required' => $this->get_gateway()->csc_enabled(),
209 'currency_code' => get_woocommerce_currency(),
210 'general_error' => __( 'An error occurred, please try again or try an alternate form of payment.', 'woocommerce-square' ),
211 'id' => $this->get_gateway()->get_id(),
212 'id_dasherized' => $this->get_gateway()->get_id_dasherized(),
213 'is_3d_secure_enabled' => $this->get_gateway()->is_3d_secure_enabled(),
214 'is_checkout_registration_enabled' => 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'no' ),
215 'is_user_logged_in' => is_user_logged_in(),
216 'is_add_payment_method_page' => is_add_payment_method_page(),
217 'location_id' => wc_square()->get_settings_handler()->get_location_id(),
218 'logging_enabled' => $this->get_gateway()->debug_log(),
219 'ajax_wc_checkout_validate_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_checkout_validate' ),
220 'is_manual_order_payment' => is_checkout() && is_wc_endpoint_url( 'order-pay' ),
221 );
222
223 // map the unique square card type string to our framework standards
224 $square_card_types = array(
225 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_MASTERCARD => 'masterCard',
226 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_AMEX => 'americanExpress',
227 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_DINERSCLUB => 'discoverDiners',
228 Framework\SV_WC_Payment_Gateway_Helper::CARD_TYPE_JCB => 'JCB',
229 );
230
231 $card_types = is_array( $this->get_gateway()->get_card_types() ) ? $this->get_gateway()->get_card_types() : array();
232
233 $framework_card_types = array_map( array( Framework\SV_WC_Payment_Gateway_Helper::class, 'normalize_card_type' ), $card_types );
234 $square_card_types = array_merge( array_combine( $framework_card_types, $framework_card_types ), $square_card_types );
235
236 $args['enabled_card_types'] = $framework_card_types;
237 $args['square_card_types'] = array_flip( $square_card_types );
238
239 $input_styles = array(
240 array(
241 'backgroundColor' => 'transparent',
242 'fontSize' => '1.3em',
243 ),
244 );
245
246 /**
247 * Filters the the Square payment form input styles.
248 *
249 * @since 2.0.0
250 *
251 * @param array $styles array of input styles
252 */
253 $args['input_styles'] = (array) apply_filters( 'wc_' . $this->get_gateway()->get_id() . '_payment_form_input_styles', $input_styles, $this );
254
255 // TODO remove the deprecated hook in a future version
256 if ( has_filter( 'woocommerce_square_payment_input_styles' ) ) {
257
258 _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.' );
259
260 /**
261 * Filters the input styles (legacy filter).
262 *
263 * @since 1.0.0
264 *
265 * @param string $input_styles styles as JSON encoded array
266 */
267 $args['input_styles'] = json_decode( (string) apply_filters( 'woocommerce_square_payment_input_styles', wp_json_encode( $args['input_styles'] ) ), true );
268 }
269
270 /**
271 * Payment Gateway Payment Form JS Arguments Filter.
272 *
273 * Filter the arguments passed to the Payment Form handler JS class
274 *
275 * @since 3.0.0
276 *
277 * @param array $result {
278 * @type string $plugin_id plugin ID
279 * @type string $id gateway ID
280 * @type string $id_dasherized gateway ID dasherized
281 * @type string $type gateway payment type (e.g. 'credit-card')
282 * @type bool $csc_required true if CSC field display is required
283 * }
284 * @param Payment_Form $this payment form instance
285 */
286 $args = apply_filters( 'wc_' . $this->get_gateway()->get_id() . '_payment_form_js_args', $args, $this );
287
288 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 ) ) );
289 }
290
291
292 }
293