PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.2
WooCommerce Square v5.4.2
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 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
Blocks_Handler.php
263 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\StoreApi\Payments\PaymentContext;
26 use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;
27 use Automattic\WooCommerce\Blocks\Package;
28 use WooCommerce\Square\Plugin;
29 use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway_Helper;
30 use WooCommerce\Square\Utilities\Order_Ajax_Authorization;
31
32 class Blocks_Handler extends AbstractPaymentMethodType {
33
34 /**
35 * @var string $name
36 */
37 protected $name = 'square_credit_card';
38
39 /**
40 * @var Plugin $plugin
41 */
42 protected $plugin = null;
43
44 /**
45 * @var Gateway $gateway
46 */
47 protected $gateway = null;
48
49 protected $digital_wallets_handler;
50
51 /**
52 * Init Square Cart and Checkout Blocks handler class
53 *
54 * @since 2.5
55 */
56 public function __construct() {
57 $this->plugin = wc_square();
58
59 add_action( 'woocommerce_rest_checkout_process_payment_with_context', array( $this, 'log_js_data' ), 10, 2 );
60
61 add_action( 'admin_notices', array( $this, 'display_compatible_version_notice' ) );
62 }
63
64
65 /**
66 * Initializes the payment method type.
67 */
68 public function initialize() {
69 $this->settings = get_option( 'woocommerce_square_credit_card_settings', array() );
70 $this->digital_wallets_handler = wc_square()->get_gateway()->get_digital_wallet_handler();
71 }
72
73 /**
74 * Returns if this payment method should be active. If false, the scripts will not be enqueued.
75 *
76 * @return boolean
77 */
78 public function is_active() {
79 return ! empty( $this->get_gateway() ) ? $this->get_gateway()->is_available() : false;
80 }
81
82 /**
83 * Register scripts
84 *
85 * @return array
86 */
87 public function get_payment_method_script_handles() {
88 $asset_path = $this->plugin->get_plugin_path() . '/build/index.asset.php';
89 $version = Plugin::VERSION;
90 $dependencies = array();
91
92 if ( file_exists( $asset_path ) ) {
93 $asset = require $asset_path;
94 $version = is_array( $asset ) && isset( $asset['version'] ) ? $asset['version'] : $version;
95 $dependencies = is_array( $asset ) && isset( $asset['dependencies'] ) ? $asset['dependencies'] : $dependencies;
96 }
97
98 wp_enqueue_style( 'wc-square-cart-checkout-block', $this->plugin->get_plugin_url() . '/build/assets/frontend/wc-square-cart-checkout-blocks.css', array(), Plugin::VERSION );
99 wp_register_script(
100 'wc-square-credit-card-blocks-integration',
101 $this->plugin->get_plugin_url() . '/build/index.js',
102 $dependencies,
103 $version,
104 true
105 );
106
107 wp_set_script_translations( 'wc-square-credit-card-blocks-integration', 'woocommerce-square' );
108
109 return array( 'wc-square-credit-card-blocks-integration' );
110 }
111
112 /**
113 * Returns an array of key=>value pairs of data made available to the payment methods script.
114 *
115 * @since 2.5
116 * @return array
117 */
118 public function get_payment_method_data() {
119 return empty( $this->get_gateway() ) ? array() : array_merge(
120 array(
121 'title' => $this->get_title(),
122 'application_id' => $this->get_gateway()->get_application_id(),
123 'location_id' => $this->plugin->get_settings_handler()->get_location_id(),
124 'is_sandbox' => $this->plugin->get_settings_handler()->is_sandbox(),
125 'available_card_types' => $this->get_available_card_types(),
126 'logging_enabled' => $this->get_gateway()->debug_log(),
127 'general_error' => __( 'An error occurred, please try again or try an alternate form of payment.', 'woocommerce-square' ),
128 'supports' => $this->get_supported_features(),
129 'show_saved_cards' => $this->get_gateway()->tokenization_enabled(),
130 'show_save_option' => $this->get_gateway()->tokenization_enabled() && ! $this->get_gateway()->get_payment_form_instance()->tokenization_forced(),
131 'is_tokenization_forced' => $this->get_gateway()->get_payment_form_instance()->tokenization_forced(),
132 'is_digital_wallets_enabled' => $this->gateway->is_digital_wallet_available() && 'yes' === $this->gateway->get_option( 'enable_digital_wallets', 'yes' ),
133 'payment_token_nonce' => wp_create_nonce( 'payment_token_nonce' ),
134 'is_pay_for_order_page' => is_wc_endpoint_url( 'order-pay' ),
135 'recalculate_totals_nonce' => wp_create_nonce( 'wc-square-recalculate-totals' ),
136 'should_charge_order_nonce' => wp_create_nonce( 'wc_' . $this->get_gateway()->get_id() . '_should_charge_order' ),
137 'order_id' => absint( get_query_var( 'order-pay' ) ),
138 'order_key' => Order_Ajax_Authorization::get_order_key_for_frontend_localization(),
139 'is_change_payment_method' => $this->get_gateway()->is_change_payment_method_request(),
140 ),
141 $this->digital_wallets_handler->get_localised_data()
142 );
143 }
144
145 /**
146 * Helper function to get title of Square gateway to be displayed as Label on checkout block.
147 * Defaults to "Credit Card"
148 *
149 * @since 2.5
150 * @return string
151 */
152 private function get_title() {
153 return ! empty( $this->get_setting( 'title' ) ) ? $this->get_setting( 'title' ) : esc_html__( 'Credit Card', 'woocommerce-square' );
154 }
155
156 /**
157 * Get a list of available card types
158 *
159 * @since 2.5
160 * @return array
161 */
162 private function get_available_card_types() {
163 $card_types = array();
164 $square_card_types = array(
165 'visa' => 'visa',
166 'mastercard' => 'masterCard',
167 'amex' => 'americanExpress',
168 'dinersclub' => 'discoverDiners',
169 'jcb' => 'JCB',
170 'discover' => 'discover',
171 );
172
173 $enabled_card_types = is_array( $this->get_gateway()->get_card_types() ) ? $this->get_gateway()->get_card_types() : array();
174 $enabled_card_types = array_map( array( Payment_Gateway_Helper::class, 'normalize_card_type' ), $enabled_card_types );
175
176 foreach ( $enabled_card_types as $card_type ) {
177 if ( ! empty( $square_card_types[ $card_type ] ) ) {
178 $card_types[ $card_type ] = $square_card_types[ $card_type ];
179 }
180 }
181
182 return array_flip( $card_types );
183 }
184
185 /**
186 * Get a list of features supported by Square
187 *
188 * @since 2.5
189 * @return array
190 */
191 public function get_supported_features() {
192 $gateway = $this->get_gateway();
193 return ! empty( $gateway ) ? array_filter( $gateway->supports, array( $gateway, 'supports' ) ) : array();
194 }
195
196 /**
197 * Hooked on before `process_legacy_payment` and logs any data recording during
198 * the checkout process on client-side.
199 *
200 * If the checkout recorded an error, skip validating the checkout fields by setting the
201 * 'error' status on the PaymentResult.
202 *
203 * @since 2.5
204 * @param PaymentContext $context Holds context for the payment.
205 * @param PaymentResult $result Result of the payment.
206 */
207 public function log_js_data( PaymentContext $context, PaymentResult &$result ) {
208 if ( 'square_credit_card' === $context->payment_method ) {
209 if ( ! empty( $context->payment_data['log-data'] ) ) {
210 $log_data = json_decode( $context->payment_data['log-data'], true );
211
212 if ( ! empty( $log_data ) ) {
213 foreach ( $log_data as $data ) {
214 $message = sprintf( "[Checkout Block] Square.js Response:\n %s", print_r( wc_clean( $data ), true ) ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
215 $this->plugin->log( $message, $this->get_gateway()->get_id() );
216 }
217 }
218 }
219
220 if ( ! empty( $context->payment_data['checkout-notices'] ) ) {
221 $payment_details = $result->payment_details;
222 $payment_details['checkoutNotices'] = $context->payment_data['checkout-notices'];
223
224 $result->set_payment_details( $payment_details );
225 $result->set_status( 'error' );
226 }
227 }
228 }
229
230 /**
231 * Display an admin notice for stores that are running WooCommerce Blocks < 4.8 and also have 3D Secure turned on.
232 *
233 * @since 2.5
234 */
235 public function display_compatible_version_notice() {
236 $wc_blocks_version = Package::get_version();
237
238 if ( version_compare( $wc_blocks_version, '4.8.0', '<' ) && 'yes' === $this->get_gateway()->get_option( 'enabled', 'no' ) ) {
239 ?>
240 <div class="notice notice-warning is-dismissible">
241 <?php // translators: %1$s - opening bold HTML tag, %2$s - closing bold HTML tag, %3$s - version number ?>
242 <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>
243 </div>
244 <?php
245 }
246 }
247
248 /**
249 * Helper function to get and store an instance of the Square gateway
250 *
251 * @since 2.5
252 * @return Gateway
253 */
254 private function get_gateway() {
255 if ( empty( $this->gateway ) ) {
256 $gateways = $this->plugin->get_gateways();
257 $this->gateway = ! empty( $gateways ) ? $gateways[0] : null;
258 }
259
260 return $this->gateway;
261 }
262 }
263