PluginProbe
WooCommerce Square / 3.4.2
WooCommerce Square v3.4.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 All 132 releases
woocommerce-square / includes / Gateway / Blocks_Handler.php
Blocks_Handler.php
318 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
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(),
140 );
141 }
142
143 /**
144 * Helper function to get title of Square gateway to be displayed as Label on checkout block.
145 * Defaults to "Credit Card"
146 *
147 * @since 2.5
148 * @return string
149 */
150 private function get_title() {
151 return ! empty( $this->get_setting( 'title' ) ) ? $this->get_setting( 'title' ) : esc_html__( 'Credit Card', 'woocommerce-square' );
152 }
153
154 /**
155 * Get Square Payment Form iframe input styles
156 *
157 * @since 2.5
158 * @return array
159 */
160 private function get_input_styles() {
161 $input_styles = array(
162 array(
163 'backgroundColor' => 'transparent',
164 'fontSize' => '1.3em',
165 ),
166 );
167
168 /**
169 * Filters the the Square payment form input styles.
170 *
171 * @since 2.5
172 * @param array $styles array of input styles
173 */
174 return (array) apply_filters( 'wc_square_credit_card_payment_form_input_styles', $input_styles, $this );
175 }
176
177 /**
178 * Get a list of available card types
179 *
180 * @since 2.5
181 * @return array
182 */
183 private function get_available_card_types() {
184 $card_types = array();
185 $square_card_types = array(
186 'visa' => 'visa',
187 'mastercard' => 'masterCard',
188 'amex' => 'americanExpress',
189 'dinersclub' => 'discoverDiners',
190 'jcb' => 'JCB',
191 'discover' => 'discover',
192 );
193
194 $enabled_card_types = is_array( $this->get_gateway()->get_card_types() ) ? $this->get_gateway()->get_card_types() : array();
195 $enabled_card_types = array_map( array( Payment_Gateway_Helper::class, 'normalize_card_type' ), $enabled_card_types );
196
197 foreach ( $enabled_card_types as $card_type ) {
198 if ( ! empty( $square_card_types[ $card_type ] ) ) {
199 $card_types[ $card_type ] = $square_card_types[ $card_type ];
200 }
201 }
202
203 return array_flip( $card_types );
204 }
205
206 /**
207 * Get a list of features supported by Square
208 *
209 * @since 2.5
210 * @return array
211 */
212 public function get_supported_features() {
213 $gateway = $this->get_gateway();
214 return ! empty( $gateway ) ? array_filter( $gateway->supports, array( $gateway, 'supports' ) ) : array();
215 }
216
217 /**
218 * Manually adds the customers Square cards to the list of cards returned
219 * by the `woocommerce_saved_payment_methods_list` filter.
220 *
221 * With Square, we don't store customer cards in WC's payment token table so
222 * in order for them to appear on the checkout block we have to manually add them.
223 *
224 * @since 2.5
225 * @param array $saved_methods
226 * @param int $user_id
227 * @return array
228 */
229 public function add_square_saved_payment_methods( $saved_methods, $user_id ) {
230 $tokens = $this->get_gateway()->get_payment_tokens_handler()->get_tokens( $user_id );
231
232 foreach ( $tokens as $token ) {
233 $saved_method = array(
234 'method' => array(
235 'gateway' => 'square-credit-card', // using the dasherized version of the payment method ID so that the element ID for the saved card is correct
236 'last4' => $token->get_last_four(),
237 'brand' => wc_get_credit_card_type_label( $token->get_card_type() ),
238 ),
239 'expires' => $token->get_exp_date(),
240 'is_default' => $token->is_default(),
241 'actions' => array(),
242 'tokenId' => $token->get_id(),
243 );
244
245 $saved_methods['cc'][] = $saved_method;
246 }
247
248 return $saved_methods;
249 }
250
251 /**
252 * Hooked on before `process_legacy_payment` and logs any data recording during
253 * the checkout process on client-side.
254 *
255 * If the checkout recorded an error, skip validating the checkout fields by setting the
256 * 'error' status on the PaymentResult.
257 *
258 * @since 2.5
259 * @param PaymentContext $context Holds context for the payment.
260 * @param PaymentResult $result Result of the payment.
261 */
262 public function log_js_data( PaymentContext $context, PaymentResult &$result ) {
263 if ( 'square_credit_card' === $context->payment_method ) {
264 if ( ! empty( $context->payment_data['log-data'] ) ) {
265 $log_data = json_decode( $context->payment_data['log-data'], true );
266
267 if ( ! empty( $log_data ) ) {
268 foreach ( $log_data as $data ) {
269 $message = sprintf( "[Checkout Block] Square.js Response:\n %s", print_r( wc_clean( $data ), true ) ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
270 $this->plugin->log( $message, $this->get_gateway()->get_id() );
271 }
272 }
273 }
274
275 if ( ! empty( $context->payment_data['checkout-notices'] ) ) {
276 $payment_details = $result->payment_details;
277 $payment_details['checkoutNotices'] = $context->payment_data['checkout-notices'];
278
279 $result->set_payment_details( $payment_details );
280 $result->set_status( 'error' );
281 }
282 }
283 }
284
285 /**
286 * Display an admin notice for stores that are running WooCommerce Blocks < 4.8 and also have 3D Secure turned on.
287 *
288 * @since 2.5
289 */
290 public function display_compatible_version_notice() {
291 $wc_blocks_version = Package::get_version();
292
293 if ( version_compare( $wc_blocks_version, '4.8.0', '<' ) && 'yes' === $this->get_gateway()->get_option( 'enabled', 'no' ) ) {
294 ?>
295 <div class="notice notice-warning is-dismissible">
296 <?php // translators: %1$s - opening bold HTML tag, %2$s - closing bold HTML tag, %3$s - version number ?>
297 <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>
298 </div>
299 <?php
300 }
301 }
302
303 /**
304 * Helper function to get and store an instance of the Square gateway
305 *
306 * @since 2.5
307 * @return Gateway
308 */
309 private function get_gateway() {
310 if ( empty( $this->gateway ) ) {
311 $gateways = $this->plugin->get_gateways();
312 $this->gateway = ! empty( $gateways ) ? array_pop( $gateways ) : null;
313 }
314
315 return $this->gateway;
316 }
317 }
318