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
Gift_Card.php
289 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WooCommerce\Square\Gateway; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | use Square\Models\RetrieveGiftCardFromGANResponse; |
| 8 | use WooCommerce\Square\Framework\Square_Helper; |
| 9 | use WooCommerce\Square\Plugin; |
| 10 | use WooCommerce\Square\Utilities\Money_Utility; |
| 11 | |
| 12 | class Gift_Card { |
| 13 | /** |
| 14 | * @var \WooCommerce\Square\Gateway $gateway |
| 15 | */ |
| 16 | public $gateway = null; |
| 17 | |
| 18 | /** |
| 19 | * Checks if Gift Card is enabled. |
| 20 | * |
| 21 | * @since 3.7.0 |
| 22 | * @return bool |
| 23 | */ |
| 24 | public function is_gift_card_enabled() { |
| 25 | return 'yes' === $this->gateway->get_option( 'enable_gift_cards', 'no' ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Setup the Gift Card class |
| 30 | * |
| 31 | * @param \WooCommerce\Square\Gateway $gateway The payment gateway object. |
| 32 | * @since 3.7.0 |
| 33 | */ |
| 34 | public function __construct( $gateway ) { |
| 35 | $this->gateway = $gateway; |
| 36 | |
| 37 | add_action( 'wp', array( $this, 'init_gift_cards' ) ); |
| 38 | add_action( 'wp_ajax_check_gift_card_balance', array( $this, 'apply_gift_card' ) ); |
| 39 | add_action( 'wp_ajax_nopriv_check_gift_card_balance', array( $this, 'apply_gift_card' ) ); |
| 40 | add_action( 'wp_ajax_gift_card_remove', array( $this, 'remove_gift_card' ) ); |
| 41 | add_action( 'wp_ajax_nopriv_gift_card_remove', array( $this, 'remove_gift_card' ) ); |
| 42 | add_filter( 'woocommerce_update_order_review_fragments', array( $this, 'add_gift_card_fragments' ) ); |
| 43 | add_filter( 'woocommerce_checkout_order_processed', array( $this, 'delete_sessions' ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Loads resources required for the Gift Card feature. |
| 48 | * |
| 49 | * @since 3.7.0 |
| 50 | */ |
| 51 | public function init_gift_cards() { |
| 52 | $cart_has_subscription = false; |
| 53 | |
| 54 | if ( class_exists( '\WC_Subscriptions_Cart' ) ) { |
| 55 | $cart_has_subscription = \WC_Subscriptions_Cart::cart_contains_subscription(); |
| 56 | } |
| 57 | |
| 58 | if ( 'yes' === $this->gateway->get_option( 'enabled', 'no' ) && $this->is_gift_card_enabled() && ! $cart_has_subscription ) { |
| 59 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Enqueue scripts ans=d styles required for Gift Cards. |
| 65 | * |
| 66 | * @since 3.7.0 |
| 67 | */ |
| 68 | public function enqueue_scripts() { |
| 69 | if ( ! is_checkout() ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Hook to filter JS args for Gift cards. |
| 75 | * |
| 76 | * @since 3.7.0 |
| 77 | * @param array Array of args. |
| 78 | */ |
| 79 | $args = apply_filters( |
| 80 | 'wc_square_gift_card_js_args', |
| 81 | array( |
| 82 | 'applicationId' => $this->gateway->get_application_id(), |
| 83 | 'locationId' => wc_square()->get_settings_handler()->get_location_id(), |
| 84 | 'gatewayId' => $this->gateway->get_id(), |
| 85 | 'gatewayIdDasherized' => $this->gateway->get_id_dasherized(), |
| 86 | 'generalError' => __( 'An error occurred, please try again or try an alternate form of payment.', 'woocommerce-square' ), |
| 87 | 'ajaxUrl' => \WC_AJAX::get_endpoint( '%%endpoint%%' ), |
| 88 | 'applyGiftCardNonce' => wp_create_nonce( 'wc-square-apply-gift-card' ), |
| 89 | 'logging_enabled' => $this->gateway->debug_log(), |
| 90 | 'orderId' => absint( get_query_var( 'order-pay' ) ), |
| 91 | 'removeGiftCardText' => esc_html__( 'Remove Gift Card?', 'woocommerce-square' ), |
| 92 | 'applyDiffGiftCard' => esc_html__( 'Try another Gift Card?', 'woocommerce-square' ), |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | wp_enqueue_script( |
| 97 | 'wc-square-gift-card', |
| 98 | $this->gateway->get_plugin()->get_plugin_url() . '/assets/js/frontend/gift-card.min.js', |
| 99 | array( 'jquery' ), |
| 100 | Plugin::VERSION, |
| 101 | true |
| 102 | ); |
| 103 | |
| 104 | wc_enqueue_js( sprintf( 'window.wc_square_gift_card_handler = new WC_Square_Gift_Card_Handler( %s );', wp_json_encode( $args ) ) ); |
| 105 | |
| 106 | wp_enqueue_style( |
| 107 | 'wc-square-gift-card', |
| 108 | $this->gateway->get_plugin()->get_plugin_url() . '/assets/css/frontend/wc-square-gift-card.min.css', |
| 109 | array(), |
| 110 | Plugin::VERSION |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Filters order review fragments to load Gift Card HTML. |
| 116 | * |
| 117 | * @since 3.7.0 |
| 118 | * |
| 119 | * @param array $fragments Array of fragments. |
| 120 | */ |
| 121 | public function add_gift_card_fragments( $fragments ) { |
| 122 | $payment_token = WC()->session->woocommerce_square_gift_card_payment_token; |
| 123 | $is_sandbox = wc_square()->get_settings_handler()->is_sandbox(); |
| 124 | $cart_total = WC()->cart->total; |
| 125 | $gift_card = null; |
| 126 | $balance_amount = 0; |
| 127 | $response = array( |
| 128 | 'is_error' => false, |
| 129 | 'message' => '', |
| 130 | ); |
| 131 | |
| 132 | if ( $is_sandbox ) { |
| 133 | // The card allowed for testing with the Sandbox account has fund of $1. |
| 134 | if ( 1 < $cart_total ) { |
| 135 | $response['is_error'] = false; |
| 136 | $response['message'] = esc_html__( 'Gift Card has insufficient funds.', 'woocommerce-square' ); |
| 137 | } else { |
| 138 | $response['is_error'] = true; |
| 139 | $response['message'] = esc_html__( 'Gift Card applied!', 'woocommerce-square' ); |
| 140 | $balance_amount = 1 - $cart_total; |
| 141 | } |
| 142 | } else { |
| 143 | if ( $payment_token ) { |
| 144 | $response = $this->gateway->get_api()->retrieve_gift_card( $payment_token ); |
| 145 | $gift_card_data = $response->get_data(); |
| 146 | |
| 147 | if ( $gift_card_data instanceof \Square\Models\RetrieveGiftCardFromNonceResponse ) { |
| 148 | $gift_card = $gift_card_data->getGiftCard(); |
| 149 | $balance_money = $gift_card->getBalanceMoney(); |
| 150 | $balance_amount = (float) Square_Helper::number_format( Money_Utility::cents_to_float( $balance_money->getAmount() ) ); |
| 151 | |
| 152 | if ( $balance_amount < $cart_total ) { |
| 153 | $response['message'] = esc_html__( 'Gift Card has insufficient funds.', 'woocommerce-square' ); |
| 154 | } else { |
| 155 | $response['message'] = esc_html__( 'Gift Card applied!', 'woocommerce-square' ); |
| 156 | } |
| 157 | } |
| 158 | } else { |
| 159 | $response['is_error'] = true; |
| 160 | $response['message'] = esc_html__( 'Gift Card payment token missing.', 'woocommerce-square' ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | ob_start(); |
| 165 | ?> |
| 166 | <div id="square-gift-card-wrapper"> |
| 167 | <div id="square-gift-card-application" <?php echo $payment_token ? 'style="display: none;"' : 'style="display: flex;"'; ?>> |
| 168 | <div id="square-gift-card-title"><?php esc_html_e( 'Have a Square Gift Card?', 'woocommerce-square' ); ?></div> |
| 169 | <div id="square-gift-card-fields-input"></div> |
| 170 | |
| 171 | <div id="square-gift-card-apply-button-wrapper"> |
| 172 | <button type="button" id="square-gift-card-apply-btn"> |
| 173 | <?php esc_html_e( 'Apply', 'woocommerce-square' ); ?> |
| 174 | </button> |
| 175 | </div> |
| 176 | </div> |
| 177 | |
| 178 | <div id="square-gift-card-response" style="<?php echo $payment_token ? 'display: flex;' : ''; ?>"> |
| 179 | <div id="square-gift-card-balance-info"> |
| 180 | <?php |
| 181 | printf( |
| 182 | /* Translators: %1$s - Gift card last 4 digits, %2$s balance amount. */ |
| 183 | __( 'Your gift card ending in %1$s has a remaining balance of %2$s.', 'woocommerce-square' ), |
| 184 | $gift_card ? esc_html( substr( $gift_card->getGan(), -4 ) ) : '0000', |
| 185 | wp_kses_post( |
| 186 | wc_price( |
| 187 | $balance_amount, |
| 188 | array( 'currency' => get_woocommerce_currency() ) |
| 189 | ) |
| 190 | ) |
| 191 | ); |
| 192 | ?> |
| 193 | </div> |
| 194 | <div id="square-gift-card-funds-message" role="alert" <?php echo $response['is_error'] ? 'class="woocommerce-message"' : 'class="woocommerce-error"'; ?>> |
| 195 | <?php echo esc_html( $response['message'] ); ?> |
| 196 | </div> |
| 197 | <a id="square-gift-card-remove" href="#"> |
| 198 | <?php |
| 199 | if ( $response['is_error'] ) { |
| 200 | esc_html_e( 'Remove Gift Card?', 'woocommerce-square' ); |
| 201 | } else { |
| 202 | esc_html_e( 'Try another Gift Card?', 'woocommerce-square' ); |
| 203 | } |
| 204 | ?> |
| 205 | </a> |
| 206 | </div> |
| 207 | |
| 208 | <?php if ( $response['is_error'] ) : ?> |
| 209 | <div id="square-gift-card-hidden-fields"> |
| 210 | <input id="square-gift-card-payment-nonce" name="square-gift-card-payment-nonce" type="hidden" value="<?php echo esc_attr( $payment_token ); ?>" /> |
| 211 | <input id="square-gift-card-payment-method" name="payment_method" value="square_credit_card" type="hidden" /> |
| 212 | </div> |
| 213 | <?php endif; ?> |
| 214 | </div> |
| 215 | <?php |
| 216 | |
| 217 | if ( $payment_token ) { |
| 218 | ob_start(); |
| 219 | |
| 220 | if ( WC()->cart->needs_payment() ) { |
| 221 | $available_gateways = WC()->payment_gateways()->get_available_payment_gateways(); |
| 222 | WC()->payment_gateways()->set_current_gateway( $available_gateways ); |
| 223 | } else { |
| 224 | $available_gateways = array(); |
| 225 | } |
| 226 | |
| 227 | wc_get_template( |
| 228 | 'Templates/payment.php', |
| 229 | array( |
| 230 | 'checkout' => WC()->checkout(), |
| 231 | 'available_gateways' => $available_gateways, |
| 232 | // PHPCS ignored as it is a Woo core hook. |
| 233 | 'order_button_text' => apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce-square' ) ), // phpcs:ignore |
| 234 | 'is_error' => $response['is_error'], |
| 235 | ), |
| 236 | '', |
| 237 | WC_SQUARE_PLUGIN_PATH . 'includes/Gateway/' |
| 238 | ); |
| 239 | |
| 240 | $payment_methods = ob_get_clean(); |
| 241 | |
| 242 | $fragments['.woocommerce-checkout-payment'] = $payment_methods; |
| 243 | } |
| 244 | |
| 245 | $fragments['.woocommerce-square-gift-card-html'] = ob_get_clean(); |
| 246 | |
| 247 | return $fragments; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Ajax callback to apply a Gift Card. |
| 252 | * |
| 253 | * @since 3.7.0 |
| 254 | */ |
| 255 | public function apply_gift_card() { |
| 256 | check_ajax_referer( 'wc-square-apply-gift-card', 'security' ); |
| 257 | |
| 258 | $payment_token = isset( $_POST['token'] ) ? wc_clean( wp_unslash( $_POST['token'] ) ) : false; |
| 259 | |
| 260 | if ( ! $payment_token ) { |
| 261 | wp_send_json_error(); |
| 262 | } |
| 263 | |
| 264 | WC()->session->set( 'woocommerce_square_gift_card_payment_token', $payment_token ); |
| 265 | |
| 266 | wp_send_json_success(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Ajax callback to remove Gift Card. |
| 271 | * |
| 272 | * @since 3.7.0 |
| 273 | */ |
| 274 | public function remove_gift_card() { |
| 275 | WC()->session->set( 'woocommerce_square_gift_card_payment_token', null ); |
| 276 | |
| 277 | wp_send_json_success(); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Delete Gift card session after order complete. |
| 282 | * |
| 283 | * @since 3.7.0 |
| 284 | */ |
| 285 | public function delete_sessions() { |
| 286 | WC()->session->set( 'woocommerce_square_gift_card_payment_token', null ); |
| 287 | } |
| 288 | } |
| 289 |