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: %s: Link to PayPal sandbox testing guide page */
$this->description .= '
' . sprintf( __( 'Sandbox mode enabled. Only sandbox test accounts can be used. See the PayPal Sandbox Testing Guide for more details.', 'woocommerce' ), 'https://developer.paypal.com/tools/sandbox/' );
$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 WC_Gateway_Paypal_Buttons( $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' ) );
}
}
}
}
/**
* Update the shipping and billing information for the order.
* Hooked on 'woocommerce_before_thankyou'.
*
* @param int $order_id The order ID.
* @return void
*/
public function update_addresses_in_order( $order_id ) {
$order = wc_get_order( $order_id );
// Bail early if the order is not a PayPal order.
if ( ! $order || $order->get_payment_method() !== $this->id ) {
return;
}
// Bail early if not on Orders v2.
if ( ! $this->should_use_orders_v2() ) {
return;
}
$paypal_order_id = $order->get_meta( '_paypal_order_id' );
if ( empty( $paypal_order_id ) ) {
return;
}
try {
include_once WC_ABSPATH . 'includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php';
$paypal_request = new WC_Gateway_Paypal_Request( $this );
$paypal_order_details = $paypal_request->get_paypal_order_details( $paypal_order_id );
// Update the shipping information.
$full_name = $paypal_order_details['purchase_units'][0]['shipping']['name']['full_name'] ?? '';
if ( ! empty( $full_name ) ) {
$approximate_first_name = explode( ' ', $full_name )[0] ?? '';
$approximate_last_name = explode( ' ', $full_name )[1] ?? '';
$order->set_shipping_first_name( $approximate_first_name );
$order->set_shipping_last_name( $approximate_last_name );
}
$shipping_address = $paypal_order_details['purchase_units'][0]['shipping']['address'] ?? array();
if ( ! empty( $shipping_address ) ) {
$order->set_shipping_country( $shipping_address['country_code'] ?? '' );
$order->set_shipping_postcode( $shipping_address['postal_code'] ?? '' );
$order->set_shipping_state( $shipping_address['admin_area_1'] ?? '' );
$order->set_shipping_city( $shipping_address['admin_area_2'] ?? '' );
$order->set_shipping_address_1( $shipping_address['address_line_1'] ?? '' );
$order->set_shipping_address_2( $shipping_address['address_line_2'] ?? '' );
}
// Update the billing information.
$full_name = $paypal_order_details['payer']['name'] ?? array();
$email = $paypal_order_details['payer']['email_address'] ?? '';
if ( ! empty( $full_name ) ) {
$order->set_billing_first_name( $full_name['given_name'] ?? '' );
$order->set_billing_last_name( $full_name['surname'] ?? '' );
$order->set_billing_email( $email );
}
$billing_address = $paypal_order_details['payer']['address'] ?? array();
if ( ! empty( $billing_address ) ) {
$order->set_billing_country( $billing_address['country_code'] ?? '' );
$order->set_billing_postcode( $billing_address['postal_code'] ?? '' );
$order->set_billing_state( $billing_address['admin_area_1'] ?? '' );
$order->set_billing_city( $billing_address['admin_area_2'] ?? '' );
$order->set_billing_address_1( $billing_address['address_line_1'] ?? '' );
$order->set_billing_address_2( $billing_address['address_line_2'] ?? '' );
}
$order->save();
} catch ( Exception $e ) {
self::log( 'Error updating addresses for order #' . $order_id . ': ' . $e->getMessage(), 'error' );
}
}
/**
* Onboard the merchant with the Transact platform.
*
* @return void
*/
public function maybe_onboard_with_transact() {
if ( ! is_admin() || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
// Do not run if PayPal Standard is not enabled.
if ( 'yes' !== $this->enabled ) {
return;
}
/**
* Filters whether the gateway should use Orders v2 API.
*
* @param bool $use_orders_v2 Whether the gateway should use Orders v2 API.
*
* @since 10.2.0
*/
$use_orders_v2 = apply_filters(
'woocommerce_paypal_use_orders_v2',
WC_Gateway_Paypal_Helper::is_orders_v2_migration_eligible()
);
// If the conditions are met, but there is an override to not use Orders v2,
// respect the override. Bail early -- we don't need to onboard if not using Orders v2.
if ( ! $use_orders_v2 ) {
return;
}
include_once __DIR__ . '/includes/class-wc-gateway-paypal-transact-account-manager.php';
$transact_account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this );
$transact_account_manager->do_onboarding();
}
/**
* Check if the gateway is available for use.
*
* @return bool
*/
public function is_available() {
// For Orders v2, require a valid email address to be set up in the gateway settings.
if ( $this->should_use_orders_v2() && $this->needs_setup() ) {
return false;
}
return parent::is_available();
}
/**
* Return whether or not this gateway still requires setup to function.
*
* When this gateway is toggled on via AJAX, if this returns true a
* redirect will occur to the settings page instead.
*
* @since 3.4.0
* @return bool
*/
public function needs_setup() {
return empty( $this->email ) || ! is_email( $this->email );
}
/**
* Logging method.
*
* @param string $message Log message.
* @param string $level Optional. Default 'info'. Possible values:
* emergency|alert|critical|error|warning|notice|info|debug.
*/
public static function log( $message, $level = 'info' ) {
if ( is_null( self::$log_enabled ) ) {
$settings = get_option( 'woocommerce_paypal_settings' );
self::$log_enabled = 'yes' === ( $settings['debug'] ?? 'no' );
}
if ( self::$log_enabled ) {
if ( empty( self::$log ) ) {
self::$log = wc_get_logger();
}
self::$log->log( $level, $message, array( 'source' => self::ID ) );
}
}
/**
* Processes and saves options.
* If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
*
* @return bool was anything saved?
*/
public function process_admin_options() {
$saved = parent::process_admin_options();
// Maybe clear logs.
if ( 'yes' !== $this->get_option( 'debug', 'no' ) ) {
if ( empty( self::$log ) ) {
self::$log = wc_get_logger();
}
self::$log->clear( self::ID );
}
// Trigger Transact onboarding when settings are saved.
if ( $saved ) {
$this->maybe_onboard_with_transact();
}
return $saved;
}
/**
* Get gateway icon.
*
* @return string
*/
public function get_icon() {
// We need a base country for the link to work, bail if in the unlikely event no country is set.
$base_country = WC()->countries->get_base_country();
if ( empty( $base_country ) ) {
return '';
}
$icon_html = '';
$icon = (array) $this->get_icon_image( $base_country );
foreach ( $icon as $i ) {
$icon_html .= '';
}
$icon_html .= sprintf( '' . esc_attr__( 'What is PayPal?', 'woocommerce' ) . '', esc_url( $this->get_icon_url( $base_country ) ) );
return apply_filters( 'woocommerce_gateway_icon', $icon_html, $this->id );
}
/**
* Get the link for an icon based on country.
*
* @param string $country Country two letter code.
* @return string
*/
protected function get_icon_url( $country ) {
$url = 'https://www.paypal.com/' . strtolower( $country );
$home_counties = array( 'BE', 'CZ', 'DK', 'HU', 'IT', 'JP', 'NL', 'NO', 'ES', 'SE', 'TR', 'IN' );
$countries = array( 'DZ', 'AU', 'BH', 'BQ', 'BW', 'CA', 'CN', 'CW', 'FI', 'FR', 'DE', 'GR', 'HK', 'ID', 'JO', 'KE', 'KW', 'LU', 'MY', 'MA', 'OM', 'PH', 'PL', 'PT', 'QA', 'IE', 'RU', 'BL', 'SX', 'MF', 'SA', 'SG', 'SK', 'KR', 'SS', 'TW', 'TH', 'AE', 'GB', 'US', 'VN' );
if ( in_array( $country, $home_counties, true ) ) {
return $url . '/webapps/mpp/home';
} elseif ( in_array( $country, $countries, true ) ) {
return $url . '/webapps/mpp/paypal-popup';
} else {
return $url . '/cgi-bin/webscr?cmd=xpt/Marketing/general/WIPaypal-outside';
}
}
/**
* Get PayPal images for a country.
*
* @param string $country Country code.
* @return array of image URLs
*/
protected function get_icon_image( $country ) {
switch ( $country ) {
case 'US':
case 'NZ':
case 'CZ':
case 'HU':
case 'MY':
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg';
break;
case 'TR':
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_odeme_secenekleri.jpg';
break;
case 'GB':
$icon = 'https://www.paypalobjects.com/webstatic/mktg/Logo/AM_mc_vs_ms_ae_UK.png';
break;
case 'MX':
$icon = array(
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_visa_mastercard_amex.png',
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_debit_card_275x60.gif',
);
break;
case 'FR':
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_moyens_paiement_fr.jpg';
break;
case 'AU':
$icon = 'https://www.paypalobjects.com/webstatic/en_AU/mktg/logo/Solutions-graphics-1-184x80.jpg';
break;
case 'DK':
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_PayPal_betalingsmuligheder_dk.jpg';
break;
case 'RU':
$icon = 'https://www.paypalobjects.com/webstatic/ru_RU/mktg/business/pages/logo-center/AM_mc_vs_dc_ae.jpg';
break;
case 'NO':
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/banner_pl_just_pp_319x110.jpg';
break;
case 'CA':
$icon = 'https://www.paypalobjects.com/webstatic/en_CA/mktg/logo-image/AM_mc_vs_dc_ae.jpg';
break;
case 'HK':
$icon = 'https://www.paypalobjects.com/webstatic/en_HK/mktg/logo/AM_mc_vs_dc_ae.jpg';
break;
case 'SG':
$icon = 'https://www.paypalobjects.com/webstatic/en_SG/mktg/Logos/AM_mc_vs_dc_ae.jpg';
break;
case 'TW':
$icon = 'https://www.paypalobjects.com/webstatic/en_TW/mktg/logos/AM_mc_vs_dc_ae.jpg';
break;
case 'TH':
$icon = 'https://www.paypalobjects.com/webstatic/en_TH/mktg/Logos/AM_mc_vs_dc_ae.jpg';
break;
case 'JP':
$icon = 'https://www.paypal.com/ja_JP/JP/i/bnr/horizontal_solution_4_jcb.gif';
break;
case 'IN':
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg';
break;
default:
$icon = WC_HTTPS::force_https_url( WC()->plugin_url() . '/includes/gateways/paypal/assets/images/paypal.png' );
break;
}
return apply_filters( 'woocommerce_paypal_icon', $icon );
}
/**
* Check if this gateway is available in the user's country based on currency.
*
* @return bool
*/
public function is_valid_for_use() {
if ( $this->should_use_orders_v2() ) {
$valid_currencies = WC_Gateway_Paypal_Constants::SUPPORTED_CURRENCIES;
} else {
$valid_currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' );
}
return in_array(
get_woocommerce_currency(),
apply_filters(
'woocommerce_paypal_supported_currencies',
$valid_currencies
),
true
);
}
/**
* Admin Panel Options.
* - Options for bits like 'title' and availability on a country-by-country basis.
*
* @since 1.0.0
*/
public function admin_options() {
if ( $this->is_valid_for_use() ) {
parent::admin_options();
} elseif ( ! $this->should_use_orders_v2() ) {
?>
: