WC_Gateway_Paypal::__constructpublicWC 1.0

Constructor for the gateway.

Method of the class: WC_Gateway_Paypal{}

No Hooks.

Returns

null. Nothing (null).

Usage

$WC_Gateway_Paypal = new WC_Gateway_Paypal();
$WC_Gateway_Paypal->__construct();

WC_Gateway_Paypal::__construct() code WC 10.7.0

public function __construct() {
	$this->id                = self::ID;
	$this->has_fields        = false;
	$this->order_button_text = __( 'Proceed to PayPal', 'woocommerce' );
	$this->method_title      = __( 'PayPal Standard', 'woocommerce' );
	/* translators: %s: Link to WC system status page */
	$this->method_description = __( 'PayPal Standard redirects customers to PayPal to enter their payment information.', 'woocommerce' );
	$this->supports           = array(
		PaymentGatewayFeature::PRODUCTS,
		PaymentGatewayFeature::REFUNDS,
	);

	// Load the settings.
	$this->init_form_fields();
	$this->init_settings();

	// Define user set variables.
	$this->title                        = $this->get_option( 'title' );
	$this->description                  = $this->get_option( 'description' );
	$this->testmode                     = 'yes' === $this->get_option( 'testmode', 'no' );
	$this->intent                       = 'sale' === $this->get_option( 'paymentaction', 'sale' ) ? 'capture' : 'authorize';
	$this->debug                        = 'yes' === $this->get_option( 'debug', 'no' );
	$this->email                        = $this->get_option( 'email' );
	$this->receiver_email               = $this->get_option( 'receiver_email', $this->email );
	$this->identity_token               = $this->get_option( 'identity_token' );
	$this->transact_onboarding_complete = 'yes' === $this->get_option( 'transact_onboarding_complete', 'no' );
	self::$log_enabled                  = $this->debug;

	if ( $this->testmode ) {
		/* translators: 1: Link to PayPal sandbox testing guide page, 2: Link to PayPal info page */
		$this->description .= '<br>' . sprintf( __( '<strong>Sandbox mode enabled</strong>. Only sandbox test accounts can be used. See the <a href="%1$s">PayPal Sandbox Testing Guide</a> for more details. <a href="%2$s" target="_blank">What is PayPal?</a>', 'woocommerce' ), 'https://developer.paypal.com/tools/sandbox/', esc_url( 'https://www.paypal.com/digital-wallet/how-paypal-works' ) );
		$this->description  = trim( $this->description );
	}

	// Actions.
	add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
	add_action( 'woocommerce_order_status_processing', array( $this, 'capture_payment' ) );
	add_action( 'woocommerce_order_status_completed', array( $this, 'capture_payment' ) );
	add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );

	if ( ! $this->is_valid_for_use() ) {
		$this->enabled = 'no';
	} else {
		include_once __DIR__ . '/includes/class-wc-gateway-paypal-ipn-handler.php';
		new WC_Gateway_Paypal_IPN_Handler( $this->testmode, $this->receiver_email );

		if ( $this->identity_token ) {
			include_once __DIR__ . '/includes/class-wc-gateway-paypal-pdt-handler.php';
			$pdt_handler = new WC_Gateway_Paypal_PDT_Handler( $this->testmode, $this->identity_token );
			$pdt_handler->set_receiver_email( $this->receiver_email );
		}
	}

	if ( 'yes' === $this->enabled ) {
		add_filter( 'woocommerce_thankyou_order_received_text', array( $this, 'order_received_text' ), 10, 2 );
		// Hide action buttons for pending orders as they take a while to be captured with orders v2.
		add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'hide_action_buttons' ), 10, 2 );

		add_filter( 'woocommerce_settings_api_form_fields_paypal', array( $this, 'maybe_remove_fields' ), 15 );

		// Hook for plugin upgrades.
		add_action( 'woocommerce_updated', array( $this, 'maybe_onboard_with_transact' ) );

		if ( $this->should_use_orders_v2() ) {
			// Hook for updating the shipping information on order approval (Orders v2).
			add_action( 'woocommerce_before_thankyou', array( $this, 'update_addresses_in_order' ), 10 );

			// Hook for PayPal order responses to manage account restriction notices.
			add_action( 'woocommerce_paypal_standard_order_created_response', array( $this, 'manage_account_restriction_status' ), 10, 3 );

			$buttons = new PayPalButtons( $this );
			if ( $buttons->is_enabled() && ! $this->needs_setup() ) {
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
				add_filter( 'wp_script_attributes', array( $this, 'add_paypal_sdk_attributes' ) );

				// Render the buttons container to load the buttons via PayPal JS SDK.
				// Classic checkout page.
				add_action( 'woocommerce_checkout_before_customer_details', array( $this, 'render_buttons_container' ) );
				// Classic cart page.
				add_action( 'woocommerce_after_cart_totals', array( $this, 'render_buttons_container' ) );
				// Product page.
				add_action( 'woocommerce_after_add_to_cart_form', array( $this, 'render_buttons_container' ) );
			}
		}
	}
}