try { $subscriptions_endpoint->activate($subscription_id); } catch (RuntimeException $exception) { $error = $exception->getMessage(); if (is_a($exception, PayPalApiException::class)) { $error = $exception->get_details($error); } $logger = $c->get('woocommerce.logger.woocommerce'); $logger->error('Could not active subscription product on PayPal. ' . $error); } } } ); add_action('woocommerce_product_options_general_product_data', function () use ($c) { if (wcs_is_manual_renewal_enabled()) { return; } $settings = $c->get('wcgateway.settings'); assert($settings instanceof Settings); try { $subscriptions_mode = $settings->get('subscriptions_mode'); if ($subscriptions_mode === 'subscriptions_api') { /** * Needed for getting global post object. * * @psalm-suppress InvalidGlobal */ global $post; $product = wc_get_product($post->ID); if (!is_a($product, WC_Product::class)) { return; } $environment = $c->get('settings.environment'); echo '
'; } } catch (NotFoundException $exception) { return; } }); add_action( 'woocommerce_variation_options_pricing', /** * Param types removed to avoid third-party issues. * * @psalm-suppress MissingClosureParamType */ function ($loop, $variation_data, $variation) use ($c) { if (wcs_is_manual_renewal_enabled()) { return; } $settings = $c->get('wcgateway.settings'); assert($settings instanceof Settings); try { $subscriptions_mode = $settings->get('subscriptions_mode'); if ($subscriptions_mode === 'subscriptions_api') { $product = wc_get_product($variation->ID); if (!is_a($product, WC_Product_Subscription_Variation::class)) { return; } $environment = $c->get('settings.environment'); $this->render_paypal_subscription_fields($product, $environment); } } catch (NotFoundException $exception) { return; } }, 10, 3 ); add_action( 'admin_enqueue_scripts', /** * Param types removed to avoid third-party issues. * * @psalm-suppress MissingClosureParamType */ function ($hook) use ($c) { if (!is_string($hook) || wcs_is_manual_renewal_enabled()) { return; } $settings = $c->get('wcgateway.settings'); $subscription_mode = $settings->has('subscriptions_mode') ? $settings->get('subscriptions_mode') : ''; if (!in_array($hook, array('post.php', 'post-new.php'), \true) || $subscription_mode !== 'subscriptions_api') { return; } $asset_getter = $c->get('paypal-subscriptions.asset_getter'); assert($asset_getter instanceof AssetGetter); wp_enqueue_script('ppcp-paypal-subscription', $asset_getter->get_asset_url('paypal-subscription.js'), array('jquery'), $c->get('ppcp.asset-version'), \true); wp_set_script_translations('ppcp-paypal-subscription', 'woocommerce-paypal-payments'); $product = wc_get_product(); if (!$product) { return; } wp_localize_script('ppcp-paypal-subscription', 'PayPalCommerceGatewayPayPalSubscriptionProducts', array('ajax' => array('deactivate_plan' => array('endpoint' => \WC_AJAX::get_endpoint(\WooCommerce\PayPalCommerce\PayPalSubscriptions\DeactivatePlanEndpoint::ENDPOINT), 'nonce' => wp_create_nonce(\WooCommerce\PayPalCommerce\PayPalSubscriptions\DeactivatePlanEndpoint::ENDPOINT))), 'product_id' => $product->get_id(), 'i18n' => array('prices_must_be_above_zero' => __('Prices must be above zero for PayPal Subscriptions!', 'woocommerce-paypal-payments'), 'not_allowed_period_interval' => __('Not allowed period interval combination for PayPal Subscriptions!', 'woocommerce-paypal-payments')))); } ); add_action('wc_ajax_' . \WooCommerce\PayPalCommerce\PayPalSubscriptions\DeactivatePlanEndpoint::ENDPOINT, function () use ($c) { $c->get('paypal-subscriptions.deactivate-plan-endpoint')->handle_request(); }); add_action( 'add_meta_boxes', /** * Param types removed to avoid third-party issues. * * @psalm-suppress MissingClosureParamType */ function (string $post_type, $post_or_order_object) use ($c) { if (!function_exists('wcs_get_subscription')) { return; } $order = $post_or_order_object instanceof WP_Post ? wc_get_order($post_or_order_object->ID) : $post_or_order_object; if (!is_a($order, WC_Order::class)) { return; } $subscription = wcs_get_subscription($order->get_id()); if (!is_a($subscription, WC_Subscription::class)) { return; } $subscription_id = $subscription->get_meta('ppcp_subscription') ?? ''; if (!$subscription_id) { return; } $screen_id = wc_get_page_screen_id('shop_subscription'); remove_meta_box('woocommerce-subscription-schedule', $screen_id, 'side'); $host = $c->get('api.paypal-website-url'); add_meta_box('ppcp_paypal_subscription', __('PayPal Subscription', 'woocommerce-paypal-payments'), function () use ($subscription_id, $host) { $url = trailingslashit($host) . 'billing/subscriptions/' . $subscription_id; echo '' . esc_html__('This subscription is linked to a PayPal Subscription, Cancel it to unlink.', 'woocommerce-paypal-payments') . '
'; echo '' . esc_html__('Subscription:', 'woocommerce-paypal-payments') . ' ' . esc_attr($subscription_id) . '
'; }, $post_type, 'side'); }, 30, 2 ); return \true; } /** * Updates subscription product meta. * * @param WC_Product $product The product. * @param SubscriptionsApiHandler $subscriptions_api_handler The subscription api handler. * @return void * * @psalm-suppress PossiblyInvalidCast */ private function update_subscription_product_meta(WC_Product $product, \WooCommerce\PayPalCommerce\PayPalSubscriptions\SubscriptionsApiHandler $subscriptions_api_handler): void { // phpcs:ignore WordPress.Security.NonceVerification $enable_subscription_product = wc_string_to_bool((string) wc_clean(wp_unslash($_POST['_ppcp_enable_subscription_product'] ?? ''))); $product->update_meta_data('_ppcp_enable_subscription_product', wc_bool_to_string($enable_subscription_product)); if (!$enable_subscription_product) { $product->save(); return; } if (!$product->get_sold_individually()) { $product->set_sold_individually(\true); } $product->save(); if ($product->get_type() === 'subscription' || $product->get_type() === 'subscription_variation') { if ($product->meta_exists('ppcp_subscription_product') && $product->meta_exists('ppcp_subscription_plan')) { $subscriptions_api_handler->update_product($product); $subscriptions_api_handler->update_plan($product); return; } if (!$product->meta_exists('ppcp_subscription_product')) { $subscriptions_api_handler->create_product($product); } if ($product->meta_exists('ppcp_subscription_product') && !$product->meta_exists('ppcp_subscription_plan')) { // phpcs:ignore WordPress.Security.NonceVerification $subscription_plan_name = wc_clean(wp_unslash($_POST['_ppcp_subscription_plan_name'] ?? '')); if (!is_string($subscription_plan_name)) { return; } $product->update_meta_data('_ppcp_subscription_plan_name', $subscription_plan_name); $product->save(); $subscriptions_api_handler->create_plan($subscription_plan_name, $product); } } } /** * Render PayPal Subscriptions fields. * * @param WC_Product $product WC Product. * @param Environment $environment The environment. * @return void */ private function render_paypal_subscription_fields(WC_Product $product, Environment $environment): void { $enable_subscription_product = $product->get_meta('_ppcp_enable_subscription_product'); $style = $product->get_type() === 'subscription_variation' ? 'float:left; width:150px;' : ''; $subscription_product = $product->get_meta('ppcp_subscription_product'); $subscription_plan = $product->get_meta('ppcp_subscription_plan'); $subscription_plan_name = $product->get_meta('_ppcp_subscription_plan_name'); echo ''; echo sprintf( // translators: %1$s and %2$s are label open and close tags. esc_html__('%1$sConnect to PayPal%2$s', 'woocommerce-paypal-payments'), '' ); $plan_id = $subscription_plan['id'] ?? ''; echo ''; echo sprintf( // translators: %1$s and %2$s are label open and close tags. esc_html__('%1$sConnect Product to PayPal Subscriptions Plan%2$s', 'woocommerce-paypal-payments'), '', '' ); echo wc_help_tip(esc_html__('Create a subscription product and plan to bill customers at regular intervals. Be aware that certain subscription settings cannot be modified once the PayPal Subscription is linked to this product. Unlink the product to edit disabled fields.', 'woocommerce-paypal-payments')); echo '
'; if ($subscription_product || $subscription_plan) { $display_unlink_p = 'display:none;'; if ($enable_subscription_product !== 'yes') { $display_unlink_p = ''; } echo sprintf( // translators: %1$s and %2$s are button and wrapper html tags. esc_html__('%1$sUnlink PayPal Subscription Plan%2$s', 'woocommerce-paypal-payments'), '' ); echo sprintf( // translators: %1$s and %2$s is open and closing paragraph tag. esc_html__('%1$sPlan unlinked successfully ✔️%2$s', 'woocommerce-paypal-payments'), '' ); $host = $environment->current_environment_is(Environment::SANDBOX) ? 'https://www.sandbox.paypal.com' : 'https://www.paypal.com'; if ($subscription_product) { echo sprintf( // translators: %1$s and %2$s are wrapper html tags. esc_html__('%1$sProduct%2$s', 'woocommerce-paypal-payments'), '' ); } if ($subscription_plan) { echo sprintf( // translators: %1$s and %2$s are wrapper html tags. esc_html__('%1$sPlan%2$s', 'woocommerce-paypal-payments'), '' ); } } else { $display_plan_name_p = ''; if ($enable_subscription_product !== 'yes') { $display_plan_name_p = 'display:none;'; } echo sprintf( // translators: %1$s and %2$s are wrapper html tags. esc_html__('%1$sPlan Name%2$s', 'woocommerce-paypal-payments'), '' ); } } }