diff --git a/modules/ppcp-webhooks/services.php b/modules/ppcp-webhooks/services.php index d6fa99706..dacb722ce 100644 --- a/modules/ppcp-webhooks/services.php +++ b/modules/ppcp-webhooks/services.php @@ -17,6 +17,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookFactory; use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint; use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulateEndpoint; use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulationStateEndpoint; +use WooCommerce\PayPalCommerce\Webhooks\Handler\BillingPlanPricingChangeActivated; use WooCommerce\PayPalCommerce\Webhooks\Handler\BillingSubscriptionCancelled; use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderApproved; use WooCommerce\PayPalCommerce\Webhooks\Handler\CheckoutOrderCompleted; @@ -89,6 +90,7 @@ return array( new PaymentCapturePending( $logger ), new PaymentSaleCompleted( $logger ), new BillingSubscriptionCancelled( $logger ), + new BillingPlanPricingChangeActivated( $logger ), ); }, diff --git a/modules/ppcp-webhooks/src/Handler/BillingPlanPricingChangeActivated.php b/modules/ppcp-webhooks/src/Handler/BillingPlanPricingChangeActivated.php new file mode 100644 index 000000000..1735b3254 --- /dev/null +++ b/modules/ppcp-webhooks/src/Handler/BillingPlanPricingChangeActivated.php @@ -0,0 +1,88 @@ +logger = $logger; + } + + /** + * The event types a handler handles. + * + * @return string[] + */ + public function event_types(): array { + return array( + 'BILLING.PLAN.PRICING-CHANGE.ACTIVATED', + ); + } + + /** + * Whether a handler is responsible for a given request or not. + * + * @param WP_REST_Request $request The request. + * + * @return bool + */ + public function responsible_for_request( WP_REST_Request $request ): bool { + return in_array( $request['event_type'], $this->event_types(), true ); + } + + /** + * Responsible for handling the request. + * + * @param WP_REST_Request $request The request. + * + * @return WP_REST_Response + */ + public function handle_request( WP_REST_Request $request ): WP_REST_Response { + $response = array( 'success' => false ); + + $plan_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) ); + $price = wc_clean( wp_unslash( $request['resource']['billing_cycles'][0]['pricing_scheme']['fixed_price']['value'] ?? '' ) ); + if ( $plan_id && $price ) { + $args = array( + 'meta_key' => 'ppcp_subscription_plan', + ); + $products = wc_get_products( $args ); + + foreach ( $products as $product ) { + if ( $product->get_meta( 'ppcp_subscription_plan' )->id === $plan_id ) { + $product->update_meta_data( '_subscription_price', $price ); + $product->save(); + } + } + } + + $response['success'] = true; + return new WP_REST_Response( $response ); + } +}