Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions includes/integrations/stripe/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function init() {
add_action( 'update_option_wpum_settings', array( $this, 'flush_product_cache' ) );
add_action( 'wp_ajax_wpum_stripe_connect_account_info', array( $this, 'stripe_connect_account_info_ajax_response' ) );
add_action( 'admin_init', array( $this, 'handle_stripe_connect_disconnect' ) );
add_action( 'admin_init', array( $this, 'handle_fetch_stripe_products' ) );
}

/**
Expand Down Expand Up @@ -256,11 +257,22 @@ public function register_settings( $settings ) {
'type' => 'hidden',
);

$fetch_products_url = add_query_arg(
array(
'page' => 'wpum-settings',
'fetch-products' => true,
),
admin_url( 'users.php' )
);

$fetch_products_url = wp_nonce_url( $fetch_products_url, 'wpum-stripe-fetch-products' );
$fetch_product_btn = sprintf( '<p><a href="%s#/stripe" class="button button-secondary">Fetch Stripe Products</a></p>', esc_url( $fetch_products_url ) );

if ( $this->products && $this->products->totalRecurringProducts() > 1 ) {
$settings['stripe'][] = array(
'id' => 'test_stripe_products',
'name' => __( 'Eligible Products', 'wp-user-manager' ),
'desc' => sprintf( 'Select the product prices users can subscribe to on the account page. This should be the same as the products defined in the <a target="_blank" href="%s">Stripe Customer Portal Subscription settings</a>.', 'https://wpusermanager.com/article/337-recurring-subscriptions/#configure-eligible-products' ),
'desc' => sprintf( 'Select the product prices users can subscribe to on the account page. This should be the same as the products defined in the <a target="_blank" href="%s">Stripe Customer Portal Subscription settings</a>. %s', 'https://wpusermanager.com/article/337-recurring-subscriptions/#configure-eligible-products ', $fetch_product_btn ),
'type' => 'multiselect',
'multiple' => true,
'options' => $this->products->get_plans(),
Expand All @@ -280,7 +292,7 @@ public function register_settings( $settings ) {
$settings['stripe'][] = array(
'id' => 'live_stripe_products',
'name' => __( 'Eligible Products', 'wp-user-manager' ),
'desc' => sprintf( 'Select the product prices users can subscribe to on the account page. This should be the same as the products defined in the <a target="_blank" href="%s">Stripe Customer Portal Subscription settings</a>.', 'https://wpusermanager.com/article/337-recurring-subscriptions/#configure-eligible-products' ),
'desc' => sprintf( 'Select the product prices users can subscribe to on the account page. This should be the same as the products defined in the <a target="_blank" href="%s">Stripe Customer Portal Subscription settings</a>. %s', 'https://wpusermanager.com/article/337-recurring-subscriptions/#configure-eligible-products', $fetch_product_btn ),
'type' => 'multiselect',
'multiple' => true,
'options' => $this->products->get_plans(),
Expand Down Expand Up @@ -602,4 +614,58 @@ public function handle_stripe_connect_disconnect() {

return wp_safe_redirect( esc_url_raw( $redirect ) );
}

/**
* Fetch Stripe Products
*/
public function handle_fetch_stripe_products() {
$page = filter_input( INPUT_GET, 'page', FILTER_UNSAFE_RAW );
$page = sanitize_text_field( $page );

if ( empty( $page ) ) {
return;
}

if ( 'wpum-settings' !== $page ) {
return;
}

$fetch_products = filter_input( INPUT_GET, 'fetch-products', FILTER_UNSAFE_RAW );
$fetch_products = sanitize_text_field( $fetch_products );

if ( empty( $fetch_products ) ) {
return;
}

// Current user cannot handle this request, bail.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

$nonce = filter_input( INPUT_GET, '_wpnonce', FILTER_UNSAFE_RAW );
$nonce = sanitize_text_field( $nonce );

if ( empty( $nonce ) ) {
return;
}

if ( ! wp_verify_nonce( $nonce, 'wpum-stripe-fetch-products' ) ) {
return;
}

// Clear product cache
$this->flush_product_cache();

$products = new Products( $this->connect->get_stripe_secret(), $this->connect->get_gateway_mode() );
$products->all( true );

$redirect = remove_query_arg(
array(
'_wpnonce',
'fetch-products',
)
);

return wp_safe_redirect( esc_url_raw( $redirect ) );
}
}
64 changes: 64 additions & 0 deletions includes/integrations/stripe/StripeWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use WPUserManager\Stripe\Controllers\Subscriptions;
use WPUserManager\Stripe\Models\Product;
use WPUserManager\Stripe\Models\User;
use WPUserManager\Stripe\Controllers\products;

/**
* StripeWebhookController
Expand All @@ -38,6 +39,16 @@ class StripeWebhookController {
*/
protected $invoices;

/**
* @var string
*/
protected $gateway_mode;

/**
* @var string
*/
protected $secret_key;

/**
* StripeWebhookController constructor.
*
Expand All @@ -53,6 +64,8 @@ public function __construct( $secret_key, $webhook_secret, $gateway_mode ) {
$this->subscriptions = new Subscriptions( $gateway_mode );
$this->invoices = new Invoices( $gateway_mode );
$this->webhook_secret = $webhook_secret;
$this->gateway_mode = $gateway_mode;
$this->secret_key = $secret_key;
}

/**
Expand Down Expand Up @@ -310,4 +323,55 @@ protected function handleInvoicePaymentSucceeded( $payload ) {

return new \WP_REST_Response( 'Webhook handled', 200 );
}

/**
* Handle the product.created webhook to update the cached Stripe products.
*
* @param array $payload
*
* @return \WP_REST_Response
* @throws \Exception
*/
protected function handleProductCreated( $payload ) {
$products = new Products( $this->secret_key, $this->gateway_mode );
$products->all( true );

do_action( 'wpum_stripe_webhook_product_updated', $subscription );

return new \WP_REST_Response( 'Webhook handled', 200 );
}

/**
* Handle the product.deleted webhook to update the cached Stripe products.
*
* @param array $payload
*
* @return \WP_REST_Response
* @throws \Exception
*/
protected function handleProductDeleted( $payload ) {
$products = new Products( $this->secret_key, $this->gateway_mode );
$products->all( true );

do_action( 'wpum_stripe_webhook_product_updated', $subscription );

return new \WP_REST_Response( 'Webhook handled', 200 );
}

/**
* Handle the product.updated webhook to update the cached Stripe products.
*
* @param array $payload
*
* @return \WP_REST_Response
* @throws \Exception
*/
protected function handleProductUpdated( $payload ) {
$products = new Products( $this->secret_key, $this->gateway_mode );
$products->all( true );

do_action( 'wpum_stripe_webhook_product_updated', $subscription );

return new \WP_REST_Response( 'Webhook handled', 200 );
}
}
Loading