PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
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 / Blocks_Handler.php
woocommerce-square / includes / Gateway Last commit date
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
Blocks_Handler.php
319 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
20 namespace WooCommerce\Square\Gateway;
21
22 defined( 'ABSPATH' ) || exit;
23
24 use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
25 use Automattic\WooCommerce\Blocks\Payments\PaymentResult;
26 use Automattic\WooCommerce\Blocks\Payments\PaymentContext;
27 use Automattic\WooCommerce\Blocks\Package;
28 use WooCommerce\Square\Plugin;
29 use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway_Helper;
30
31 class Blocks_Handler extends AbstractPaymentMethodType {
32
33 /**
34 * @var string $name
35 */
36 protected $name = 'square_credit_card';
37
38 /**
39 * @var Plugin $plugin
40 */
41 protected $plugin = null;
42
43 /**
44 * @var Gateway $gateway
45 */
46 protected $gateway = null;
47
48 /**
49 * Init Square Cart and Checkout Blocks handler class
50 *
51 * @since 2.5
52 */
53 public function __construct() {
54 $this->plugin = wc_square();
55
56 add_action(
57 'woocommerce_blocks_enqueue_checkout_block_scripts_before',
58 function() {
59 add_filter( 'woocommerce_saved_payment_methods_list', array( $this, 'add_square_saved_payment_methods' ), 10, 2 );
60 }
61 );
62 add_action(
63 'woocommerce_blocks_enqueue_checkout_block_scripts_after',
64 function () {
65 remove_filter( 'woocommerce_saved_payment_methods_list', array( $this, 'add_square_saved_payment_methods' ) );
66 }
67 );
68
69 add_action( 'woocommerce_rest_checkout_process_payment_with_context', array( $this, 'log_js_data' ), 10, 2 );
70
71 add_action( 'admin_notices', array( $this, 'display_compatible_version_notice' ) );
72 }
73
74
75 /**
76 * Initializes the payment method type.
77 */
78 public function initialize() {
79 $this->settings = get_option( 'woocommerce_square_credit_card_settings', array() );
80 }
81
82 /**
83 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
84 *
85 * @return boolean
86 */
87 public function is_active() {
88 return ! empty( $this->get_gateway() ) ? $this->get_gateway()->is_available() : false;
89 }
90
91 /**
92 * Register scripts
93 *
94 * @return array
95 */
96 public function get_payment_method_script_handles() {
97 $asset_path = $this->plugin->get_plugin_path() . '/assets/blocks/build/index.asset.php';
98 $version = Plugin::VERSION;
99 $dependencies = array();
100
101 if ( file_exists( $asset_path ) ) {
102 $asset = require $asset_path;
103 $version = is_array( $asset ) && isset( $asset['version'] ) ? $asset['version'] : $version;
104 $dependencies = is_array( $asset ) && isset( $asset['dependencies'] ) ? $asset['dependencies'] : $dependencies;
105 }
106
107 wp_enqueue_style( 'wc-square-cart-checkout-block', $this->plugin->get_plugin_url() . '/assets/css/frontend/wc-square-cart-checkout-blocks.min.css', array(), Plugin::VERSION );
108 wp_register_script(
109 'wc-square-credit-card-blocks-integration',
110 $this->plugin->get_plugin_url() . '/build/index.js',
111 $dependencies,
112 $version,
113 true
114 );
115
116 wp_set_script_translations( 'wc-square-credit-card-blocks-integration', 'woocommerce-square' );
117
118 return array( 'wc-square-credit-card-blocks-integration' );
119 }
120
121 /**
122 * Returns an array of key=>value pairs of data made available to the payment methods script.
123 *
124 * @since 2.5
125 * @return array
126 */
127 public function get_payment_method_data() {
128 return empty( $this->get_gateway() ) ? array() : array(
129 'title' => $this->get_setting( 'title' ),
130 'application_id' => $this->get_gateway()->get_application_id(),
131 'location_id' => $this->plugin->get_settings_handler()->get_location_id(),
132 'is_sandbox' => $this->plugin->get_settings_handler()->is_sandbox(),
133 'input_styles' => $this->get_input_styles(),
134 'available_card_types' => $this->get_available_card_types(),
135 'logging_enabled' => $this->get_gateway()->debug_log(),
136 'general_error' => __( 'An error occurred, please try again or try an alternate form of payment.', 'woocommerce-square' ),
137 'supports' => $this->get_supported_features(),
138 'show_saved_cards' => $this->get_gateway()->tokenization_enabled(),
139 'show_save_option' => $this->get_gateway()->tokenization_enabled() && ! $this->get_gateway()->get_payment_form_instance()->tokenization_forced(),
140 'is_tokenization_forced' => $this->get_gateway()->get_payment_form_instance()->tokenization_forced(),
141 );
142 }
143
144 /**
145 * Helper function to get title of Square gateway to be displayed as Label on checkout block.
146 * Defaults to "Credit Card"
147 *
148 * @since 2.5
149 * @return string
150 */
151 private function get_title() {
152 return ! empty( $this->get_setting( 'title' ) ) ? $this->get_setting( 'title' ) : esc_html__( 'Credit Card', 'woocommerce-square' );
153 }
154
155 /**
156 * Get Square Payment Form iframe input styles
157 *
158 * @since 2.5
159 * @return array
160 */
161 private function get_input_styles() {
162 $input_styles = array(
163 array(
164 'backgroundColor' => 'transparent',
165 'fontSize' => '1.3em',
166 ),
167 );
168
169 /**
170 * Filters the the Square payment form input styles.
171 *
172 * @since 2.5
173 * @param array $styles array of input styles
174 */
175 return (array) apply_filters( 'wc_square_credit_card_payment_form_input_styles', $input_styles, $this );
176 }
177
178 /**
179 * Get a list of available card types
180 *
181 * @since 2.5
182 * @return array
183 */
184 private function get_available_card_types() {
185 $card_types = array();
186 $square_card_types = array(
187 'visa' => 'visa',
188 'mastercard' => 'masterCard',
189 'amex' => 'americanExpress',
190 'dinersclub' => 'discoverDiners',
191 'jcb' => 'JCB',
192 'discover' => 'discover',
193 );
194
195 $enabled_card_types = is_array( $this->get_gateway()->get_card_types() ) ? $this->get_gateway()->get_card_types() : array();
196 $enabled_card_types = array_map( array( Payment_Gateway_Helper::class, 'normalize_card_type' ), $enabled_card_types );
197
198 foreach ( $enabled_card_types as $card_type ) {
199 if ( ! empty( $square_card_types[ $card_type ] ) ) {
200 $card_types[ $card_type ] = $square_card_types[ $card_type ];
201 }
202 }
203
204 return array_flip( $card_types );
205 }
206
207 /**
208 * Get a list of features supported by Square
209 *
210 * @since 2.5
211 * @return array
212 */
213 public function get_supported_features() {
214 $gateway = $this->get_gateway();
215 return ! empty( $gateway ) ? array_filter( $gateway->supports, array( $gateway, 'supports' ) ) : array();
216 }
217
218 /**
219 * Manually adds the customers Square cards to the list of cards returned
220 * by the `woocommerce_saved_payment_methods_list` filter.
221 *
222 * With Square, we don't store customer cards in WC's payment token table so
223 * in order for them to appear on the checkout block we have to manually add them.
224 *
225 * @since 2.5
226 * @param array $saved_methods
227 * @param int $user_id
228 * @return array
229 */
230 public function add_square_saved_payment_methods( $saved_methods, $user_id ) {
231 $tokens = $this->get_gateway()->get_payment_tokens_handler()->get_tokens( $user_id );
232
233 foreach ( $tokens as $token ) {
234 $saved_method = array(
235 'method' => array(
236 'gateway' => 'square_credit_card',
237 'last4' => $token->get_last4(),
238 'brand' => wc_get_credit_card_type_label( $token->get_card_type() ),
239 ),
240 'expires' => $token->get_exp_date(),
241 'is_default' => $token->is_default(),
242 'actions' => array(),
243 'tokenId' => $token->get_id(),
244 );
245
246 $saved_methods['cc'][] = $saved_method;
247 }
248
249 return $saved_methods;
250 }
251
252 /**
253 * Hooked on before `process_legacy_payment` and logs any data recording during
254 * the checkout process on client-side.
255 *
256 * If the checkout recorded an error, skip validating the checkout fields by setting the
257 * 'error' status on the PaymentResult.
258 *
259 * @since 2.5
260 * @param PaymentContext $context Holds context for the payment.
261 * @param PaymentResult $result Result of the payment.
262 */
263 public function log_js_data( PaymentContext $context, PaymentResult &$result ) {
264 if ( 'square_credit_card' === $context->payment_method ) {
265 if ( ! empty( $context->payment_data['log-data'] ) ) {
266 $log_data = json_decode( $context->payment_data['log-data'], true );
267
268 if ( ! empty( $log_data ) ) {
269 foreach ( $log_data as $data ) {
270 $message = sprintf( "[Checkout Block] Square.js Response:\n %s", print_r( wc_clean( $data ), true ) ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
271 $this->plugin->log( $message, $this->get_gateway()->get_id() );
272 }
273 }
274 }
275
276 if ( ! empty( $context->payment_data['checkout-notices'] ) ) {
277 $payment_details = $result->payment_details;
278 $payment_details['checkoutNotices'] = $context->payment_data['checkout-notices'];
279
280 $result->set_payment_details( $payment_details );
281 $result->set_status( 'error' );
282 }
283 }
284 }
285
286 /**
287 * Display an admin notice for stores that are running WooCommerce Blocks < 4.8 and also have 3D Secure turned on.
288 *
289 * @since 2.5
290 */
291 public function display_compatible_version_notice() {
292 $wc_blocks_version = Package::get_version();
293
294 if ( version_compare( $wc_blocks_version, '4.8.0', '<' ) && 'yes' === $this->get_gateway()->get_option( 'enabled', 'no' ) ) {
295 ?>
296 <div class="notice notice-warning is-dismissible">
297 <?php // translators: %1$s - opening bold HTML tag, %2$s - closing bold HTML tag, %3$s - version number ?>
298 <p><?php echo sprintf( esc_html__( '%1$sWarning!%2$s Some Square + Checkout Block features do not work with your version of WooCommerce Blocks (%3$s). Please update to the latest version of WooCommerce Blocks or WooCommerce to fix these issues.', 'woocommerce-square' ), '<strong>', '</strong>', esc_html( $wc_blocks_version ) ); ?></p>
299 </div>
300 <?php
301 }
302 }
303
304 /**
305 * Helper function to get and store an instance of the Square gateway
306 *
307 * @since 2.5
308 * @return Gateway
309 */
310 private function get_gateway() {
311 if ( empty( $this->gateway ) ) {
312 $gateways = $this->plugin->get_gateways();
313 $this->gateway = ! empty( $gateways ) ? array_pop( $gateways ) : null;
314 }
315
316 return $this->gateway;
317 }
318 }
319