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