Skip to content

Commit

Permalink
feat(reader-revenue): make NYP and Stripe Gateway optional (#2866)
Browse files Browse the repository at this point in the history
* feat(reader-revenue): make Name Your Price optional

* feat(donate): support Reader Revenue without NYP

* feat(reader-revenue): don’t require Stripe Gateway

* fix: restore missing method and s
et donation product prices

* fix: remove experimental code

* feat: deprecate Address UI for reader revenue

* test: fix failing test

* feat: show plugin installer if Stripe Gateway not available

* fix: force untiered layout if NYP not available
  • Loading branch information
dkoo authored Feb 6, 2024
1 parent 3b7e60e commit fcfa88c
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 137 deletions.
6 changes: 0 additions & 6 deletions assets/wizards/readerRevenue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ const ReaderRevenueWizard = () => {
render: Views.Emails,
isHidden: usedPlatform !== NEWSPACK,
},
{
label: __( 'Address', 'newspack' ),
path: '/location-setup',
render: Views.LocationSetup,
isHidden: usedPlatform !== NEWSPACK,
},
{
label: __( 'Salesforce', 'newspack' ),
path: '/salesforce',
Expand Down
29 changes: 18 additions & 11 deletions assets/wizards/readerRevenue/views/donation/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* globals newspack_reader_revenue */

/**
* WordPress dependencies.
*/
Expand Down Expand Up @@ -103,6 +105,9 @@ export const DonationAmounts = () => {
// Minimum donation is returned by the REST API as a string.
const minimumDonationFloat = parseFloat( minimumDonation );

// Whether we can use the Name Your Price extension. If not, layout is forced to Tiered.
const canUseNameYourPrice = newspack_reader_revenue?.can_use_name_your_price;

return (
<>
<Card headerActions noBorder>
Expand All @@ -114,17 +119,19 @@ export const DonationAmounts = () => {
) }
noMargin
/>
<SelectControl
label={ __( 'Donation Type', 'newspack' ) }
onChange={ () => changeHandler( [ 'tiered' ] )( ! tiered ) }
buttonOptions={ [
{ value: true, label: __( 'Tiered', 'newspack' ) },
{ value: false, label: __( 'Untiered', 'newspack' ) },
] }
buttonSmall
value={ tiered }
hideLabelFromVision
/>
{ canUseNameYourPrice && (
<SelectControl
label={ __( 'Donation Type', 'newspack' ) }
onChange={ () => changeHandler( [ 'tiered' ] )( ! tiered ) }
buttonOptions={ [
{ value: true, label: __( 'Tiered', 'newspack' ) },
{ value: false, label: __( 'Untiered', 'newspack' ) },
] }
buttonSmall
value={ tiered }
hideLabelFromVision
/>
) }
</Card>
{ tiered ? (
<Grid columns={ 1 } gutter={ 16 }>
Expand Down
1 change: 0 additions & 1 deletion assets/wizards/readerRevenue/views/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { default as Donation } from './donation';
export { default as LocationSetup } from './location-setup';
export { default as NRHSettings } from './nrh-settings';
export { default as Platform } from './platform';
export { default as StripeSetup } from './stripe-setup';
Expand Down
80 changes: 0 additions & 80 deletions assets/wizards/readerRevenue/views/location-setup/index.js

This file was deleted.

7 changes: 1 addition & 6 deletions assets/wizards/readerRevenue/views/platform/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ const Platform = () => {
</Card>
{ NEWSPACK === wizardData.platform_data?.platform && ! wizardData.plugin_status && (
<PluginInstaller
plugins={ [
'woocommerce',
'woocommerce-subscriptions',
'woocommerce-name-your-price',
'woocommerce-gateway-stripe',
] }
plugins={ [ 'woocommerce', 'woocommerce-subscriptions' ] }
onStatus={ ( { complete } ) => {
if ( complete ) {
updateWizardSettings( {
Expand Down
38 changes: 37 additions & 1 deletion assets/wizards/readerRevenue/views/stripe-setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import { useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { Button, Grid, Notice, Settings, TextControl, Wizard } from '../../../../components/src';
import {
Button,
Grid,
Notice,
PluginInstaller,
Settings,
TextControl,
Wizard,
} from '../../../../components/src';
import { READER_REVENUE_WIZARD_SLUG } from '../../constants';
import './style.scss';

Expand Down Expand Up @@ -137,6 +145,34 @@ const StripeSetup = () => {
payloadPath: [ 'stripe_data' ],
} );

if ( ! data ) {
return (
<>
<p>
{ __(
'To configure Stripe, install the WooCommerce Stripe Gateway plugin.',
'newspack-plugin'
) }
</p>
<PluginInstaller
plugins={ [ 'woocommerce-gateway-stripe' ] }
onStatus={ ( { complete } ) => {
if ( complete ) {
console.log( complete );
updateWizardSettings( {
slug: 'newspack-reader-revenue-wizard',
path: [ 'stripe_data' ],
value: { activate: true },
} );
onSave();
}
} }
withoutFooterButton={ true }
/>
</>
);
}

return (
<>
{ errors.length > 0 &&
Expand Down
21 changes: 18 additions & 3 deletions includes/class-donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function should_skip_amp( $skip, $post_id ) {
* @return bool|WP_Error True if active. WP_Error if not.
*/
public static function is_woocommerce_suite_active() {
if ( ! function_exists( 'WC' ) || ! class_exists( 'WC_Subscriptions_Product' ) || ! class_exists( 'WC_Name_Your_Price_Helpers' ) ) {
if ( ! function_exists( 'WC' ) || ! class_exists( 'WC_Subscriptions_Product' ) ) {
return new WP_Error(
'newspack_missing_required_plugin',
esc_html__( 'The required plugins are not installed and activated. Install and/or activate them to access this feature.', 'newspack' ),
Expand All @@ -121,6 +121,15 @@ public static function is_woocommerce_suite_active() {
return true;
}

/**
* Check if the Name Your Price extension is available.
*
* @return bool True if available, false if not.
*/
public static function can_use_name_your_price() {
return class_exists( 'WC_Name_Your_Price_Helpers' );
}

/**
* Get the default donation settings.
*
Expand Down Expand Up @@ -238,7 +247,7 @@ private static function get_donation_product_child_products_ids() {
// Add the product IDs for each frequency.
foreach ( $product->get_children() as $child_id ) {
$child_product = wc_get_product( $child_id );
if ( ! $child_product || 'trash' === $child_product->get_status() || ! (bool) WC_Name_Your_Price_Helpers::is_nyp( $child_id ) ) {
if ( ! $child_product || 'trash' === $child_product->get_status() ) {
continue;
}
if ( 'subscription' === $child_product->get_type() ) {
Expand Down Expand Up @@ -422,6 +431,11 @@ public static function get_donation_settings() {
$parsed_settings['platform'] = self::get_platform_slug();
$parsed_settings['billingFields'] = self::get_billing_fields();

// If NYP isn't available, force untiered config.
if ( ! self::can_use_name_your_price() ) {
$parsed_settings['tiered'] = false;
}

return $parsed_settings;
}

Expand Down Expand Up @@ -536,6 +550,7 @@ public static function update_donation_product( $args = [] ) {
}

$child_product->set_name( $product_name );
$child_product->set_price( $price );
$child_product->set_regular_price( $price );
$child_product->update_meta_data( '_suggested_price', $price );
$child_product->update_meta_data( '_min_price', wc_format_decimal( $configuration['minimumDonation'] ) );
Expand Down Expand Up @@ -705,7 +720,7 @@ function ( $item ) {
$cart_item_data = apply_filters(
'newspack_donations_cart_item_data',
[
'nyp' => (float) \WC_Name_Your_Price_Helpers::standardize_number( $donation_value ),
'nyp' => class_exists( 'WC_Name_Your_Price_Helpers' ) ? (float) \WC_Name_Your_Price_Helpers::standardize_number( $donation_value ) : null,
'referer' => $referer,
'newspack_popup_id' => filter_input( INPUT_GET, 'newspack_popup_id', FILTER_SANITIZE_NUMBER_INT ),
]
Expand Down
2 changes: 1 addition & 1 deletion includes/class-plugin-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ private static function reduce_plugin_info( $plugins, $key ) {
$path = explode( '/', $key );
$folder = current( $path );

// Strip version info from key. (e.g. 'woocommerce-stripe-gateway-4.1.2' should just be 'woocommerce-stripe-gateway').
// Strip version info from key. (e.g. 'woocommerce-gateway-stripe-4.1.2' should just be 'woocommerce-gateway-stripe').
$folder = preg_replace( '/[\-0-9\.]+$/', '', $folder );

$plugins[ $folder ] = $key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function country_state_fields() {
* @return Array Array of payment gateways.
*/
public static function get_payment_gateways( $only_enabled = false ) {
if ( ! class_exists( '\WC_Payment_Gateways' ) ) {
if ( ! class_exists( 'WC_Payment_Gateways' ) ) {
return [];
}
$gateways = \WC_Payment_Gateways::instance()->payment_gateways();
Expand Down Expand Up @@ -149,24 +149,35 @@ public function update_location( $args ) {
return true;
}

/**
* Get Stripe payment gateway, if available.
*
* @param bool $only_enabled If true, only return the gateway if enabled.
*
* @return WC_Gateway_Stripe|bool WC_Gateway_Stripe instance if Stripe payment gateway is available, false if not.
*/
public static function get_stripe_gateway( $only_enabled = false ) {
$gateways = self::get_payment_gateways();
return isset( $gateways['stripe'] ) ? $gateways['stripe'] : false;
}

/**
* Retrieve Stripe data
*
* @return Array Array of Stripe data.
* @return Array|bool Array of Stripe data, or false if Stripe gateway isn't available.
*/
public function stripe_data() {
$gateways = self::get_payment_gateways();
if ( ! isset( $gateways['stripe'] ) ) {
return [];
$stripe = self::get_stripe_gateway();
if ( ! $stripe ) {
return false;
}
$gateway = $gateways['stripe'];
return [
'enabled' => 'yes' === $gateway->get_option( 'enabled', false ) ? true : false,
'testMode' => 'yes' === $gateway->get_option( 'testmode', false ) ? true : false,
'publishableKey' => $gateway->get_option( 'publishable_key', '' ),
'secretKey' => $gateway->get_option( 'secret_key', '' ),
'testPublishableKey' => $gateway->get_option( 'test_publishable_key', '' ),
'testSecretKey' => $gateway->get_option( 'test_secret_key', '' ),
'enabled' => 'yes' === $stripe->get_option( 'enabled', false ) ? true : false,
'testMode' => 'yes' === $stripe->get_option( 'testmode', false ) ? true : false,
'publishableKey' => $stripe->get_option( 'publishable_key', '' ),
'secretKey' => $stripe->get_option( 'secret_key', '' ),
'testPublishableKey' => $stripe->get_option( 'test_publishable_key', '' ),
'testSecretKey' => $stripe->get_option( 'test_secret_key', '' ),
];
}

Expand All @@ -177,25 +188,30 @@ public function stripe_data() {
* @return Array|WP_Error The data that was updated or an error.
*/
public function update_wc_stripe_settings( $args ) {

if ( ! class_exists( '\WC_Payment_Gateways' ) ) {
if ( ! class_exists( 'WC_Payment_Gateways' ) ) {
return false;
}
$gateways = WC_Payment_Gateways::instance()->payment_gateways();
if ( ! isset( $gateways['stripe'] ) ) {

// Get the Stripe payment gateway instance.
$stripe = self::get_stripe_gateway();

if ( ! $stripe ) {
if ( isset( $args['enabled'] ) && $args['enabled'] ) {
// Stripe is not installed and we want to use it. Install/Activate/Initialize it.
// Stripe gateway is not installed and we want to use it. Install/Activate/Initialize it.
Plugin_Manager::activate( 'woocommerce-gateway-stripe' );
do_action( 'plugins_loaded' );
WC_Payment_Gateways::instance()->init();
$gateways = WC_Payment_Gateways::instance()->payment_gateways();
\WC_Payment_Gateways::instance()->init();
$stripe = self::get_stripe_gateway();
if ( ! $stripe ) {
return new WP_Error( 'newspack_stripe_gateway_error', __( 'Error activating the Stripe payment gateway.', 'newspack-plugin' ) );
}
} else {
// Stripe is not installed and we don't want to use it. No settings needed.
return true;
}
}

$stripe = $gateways['stripe'];
// Update Stripe payment gateway settings.
if ( isset( $args['enabled'] ) ) {
$stripe->update_option( 'enabled', $args['enabled'] ? 'yes' : 'no' );
}
Expand Down
7 changes: 3 additions & 4 deletions includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,9 @@ public static function get_prerequisites_status() {
'reader_revenue' => [
'active' => self::is_reader_revenue_ready(),
'plugins' => [
'newspack-blocks' => class_exists( '\Newspack_Blocks' ),
'woocommerce' => function_exists( 'WC' ),
'woocommerce-subscriptions' => class_exists( 'WC_Subscriptions_Product' ),
'woocommerce-name-your-price' => class_exists( 'WC_Name_Your_Price_Helpers' ),
'newspack-blocks' => class_exists( '\Newspack_Blocks' ),
'woocommerce' => function_exists( 'WC' ),
'woocommerce-subscriptions' => class_exists( 'WC_Subscriptions_Product' ),
],
'label' => __( 'Reader Revenue', 'newspack-plugin' ),
'description' => __( 'Setting suggested donation amounts is required for enabling a streamlined donation experience.', 'newspack-plugin' ),
Expand Down
Loading

0 comments on commit fcfa88c

Please sign in to comment.