Fix psalm

This commit is contained in:
Emili Castells Guasch 2023-05-02 12:35:46 +02:00
parent e1d84af7f0
commit eca8a3b09c
21 changed files with 357 additions and 203 deletions

View file

@ -120,8 +120,8 @@ class BillingAgreementsEndpoint {
*/
public function reference_transaction_enabled(): bool {
try {
$reference_transaction_enabled = get_transient('ppcp_reference_transaction_enabled') ?? '';
if($reference_transaction_enabled === true) {
$reference_transaction_enabled = get_transient( 'ppcp_reference_transaction_enabled' ) ?? '';
if ( $reference_transaction_enabled === true ) {
return true;
}
@ -135,12 +135,12 @@ class BillingAgreementsEndpoint {
);
} finally {
$this->is_request_logging_enabled = true;
set_transient('ppcp_reference_transaction_enabled', true, 3 * MONTH_IN_SECONDS);
set_transient( 'ppcp_reference_transaction_enabled', true, 3 * MONTH_IN_SECONDS );
}
return true;
} catch ( PayPalApiException $exception ) {
delete_transient('ppcp_reference_transaction_enabled');
delete_transient( 'ppcp_reference_transaction_enabled' );
return false;
}
}

View file

@ -98,7 +98,7 @@ class BillingPlans {
'name' => $name,
'product_id' => $product_id,
'billing_cycles' => $billing_cycles,
'payment_preferences' => $payment_preferences
'payment_preferences' => $payment_preferences,
);
$bearer = $this->bearer->bearer();
@ -108,7 +108,7 @@ class BillingPlans {
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation'
'Prefer' => 'return=representation',
),
'body' => wp_json_encode( $data ),
);
@ -128,17 +128,17 @@ class BillingPlans {
);
}
return $this->plan_factory->from_paypal_response($json);
return $this->plan_factory->from_paypal_response( $json );
}
public function plan(string $id): Plan {
public function plan( string $id ): Plan {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/billing/plans/' . $id;
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation'
'Prefer' => 'return=representation',
),
);
@ -156,15 +156,15 @@ class BillingPlans {
);
}
return $this->plan_factory->from_paypal_response($json);
return $this->plan_factory->from_paypal_response( $json );
}
public function update_pricing(string $id, BillingCycle $billing_cycle): void {
public function update_pricing( string $id, BillingCycle $billing_cycle ): void {
$data = array(
"pricing_schemes" => array(
(object)array(
"billing_cycle_sequence" => 1,
"pricing_scheme" => $billing_cycle->pricing_scheme(),
'pricing_schemes' => array(
(object) array(
'billing_cycle_sequence' => 1,
'pricing_scheme' => $billing_cycle->pricing_scheme(),
),
),
);

View file

@ -179,14 +179,14 @@ class BillingSubscriptions {
* @param string $id Subscription ID.
* @return stdClass
*/
public function subscription(string $id): stdClass {
public function subscription( string $id ): stdClass {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/billing/subscriptions/' . $id;
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation'
'Prefer' => 'return=representation',
),
);

View file

@ -77,12 +77,12 @@ class CatalogProducts {
* @throws RuntimeException If the request fails.
* @throws PayPalApiException If the request fails.
*/
public function create(string $name, string $description): Product {
public function create( string $name, string $description ): Product {
$data = array(
'name' => $name,
);
if($description) {
if ( $description ) {
$data['description'] = $description;
}
@ -93,7 +93,7 @@ class CatalogProducts {
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation'
'Prefer' => 'return=representation',
),
'body' => wp_json_encode( $data ),
);
@ -113,7 +113,7 @@ class CatalogProducts {
);
}
return $this->product_factory->from_paypal_response($json);
return $this->product_factory->from_paypal_response( $json );
}
/**
@ -123,7 +123,7 @@ class CatalogProducts {
* @param array $data Data to update.
* @return void
*/
public function update(string $id, array $data): void {
public function update( string $id, array $data ): void {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/catalogs/products/' . $id;
$args = array(
@ -131,7 +131,7 @@ class CatalogProducts {
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation'
'Prefer' => 'return=representation',
),
'body' => wp_json_encode( $data ),
);
@ -157,14 +157,14 @@ class CatalogProducts {
* @param string $id Product ID.
* @return Product
*/
public function product(string $id): Product {
public function product( string $id ): Product {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/catalogs/products/' . $id;
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $bearer->token(),
'Content-Type' => 'application/json',
'Prefer' => 'return=representation'
'Prefer' => 'return=representation',
),
);
@ -182,6 +182,6 @@ class CatalogProducts {
);
}
return $this->product_factory->from_paypal_response($json);
return $this->product_factory->from_paypal_response( $json );
}
}

View file

@ -6,8 +6,8 @@ use stdClass;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Plan;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
class PlanFactory
{
class PlanFactory {
/**
* @var BillingCycleFactory
*/
@ -20,12 +20,12 @@ class PlanFactory
public function __construct(
BillingCycleFactory $billing_cycle_factory,
PaymentPreferencesFactory $payment_preferences_factory
){
) {
$this->billing_cycle_factory = $billing_cycle_factory;
$this->payment_preferences_factory = $payment_preferences_factory;
}
public function from_paypal_response(stdClass $data): Plan {
public function from_paypal_response( stdClass $data ): Plan {
if ( ! isset( $data->id ) ) {
throw new RuntimeException(
__( 'No id for given plan', 'woocommerce-paypal-payments' )
@ -48,11 +48,11 @@ class PlanFactory
}
$billing_cycles = array();
foreach ($data->billing_cycles as $billing_cycle) {
$billing_cycles[] = $this->billing_cycle_factory->from_paypal_response($billing_cycle);
foreach ( $data->billing_cycles as $billing_cycle ) {
$billing_cycles[] = $this->billing_cycle_factory->from_paypal_response( $billing_cycle );
}
$payment_preferences = $this->payment_preferences_factory->from_paypal_response($data->payment_preferences);
$payment_preferences = $this->payment_preferences_factory->from_paypal_response( $data->payment_preferences );
return new Plan(
$data->id,

View file

@ -23,7 +23,7 @@ class ProductFactory {
* @return Product
* @throws RuntimeException When JSON object is malformed.
*/
public function from_paypal_response(stdClass $data): Product {
public function from_paypal_response( stdClass $data ): Product {
if ( ! isset( $data->id ) ) {
throw new RuntimeException(
__( 'No id for product given', 'woocommerce-paypal-payments' )

View file

@ -1380,14 +1380,13 @@ class SmartButton implements SmartButtonInterface {
* @return string
*/
private function paypal_subscription_id(): string {
if($this->subscription_helper->current_product_is_subscription() ) {
if ( $this->subscription_helper->current_product_is_subscription() ) {
$product = wc_get_product();
assert( $product instanceof WC_Product );
if ( $product->get_type() === 'subscription' && $product->meta_exists( 'ppcp_subscription_plan' ) ) {
return $product->get_meta( 'ppcp_subscription_plan' )['id'];
}
}
$items = WC()->cart->get_cart_contents();

View file

@ -43,14 +43,14 @@ return array(
$endpoint = $container->get( 'api.endpoint.payment-token' );
return new PaymentTokenRepository( $factory, $endpoint );
},
'subscription.api-handler' => static function(ContainerInterface $container): SubscriptionsApiHandler {
'subscription.api-handler' => static function( ContainerInterface $container ): SubscriptionsApiHandler {
return new SubscriptionsApiHandler(
$container->get('api.endpoint.catalog-products'),
$container->get('api.factory.product'),
$container->get('api.endpoint.billing-plans'),
$container->get('api.factory.billing-cycle'),
$container->get('api.factory.payment-preferences'),
$container->get( 'api.endpoint.catalog-products' ),
$container->get( 'api.factory.product' ),
$container->get( 'api.endpoint.billing-plans' ),
$container->get( 'api.factory.billing-cycle' ),
$container->get( 'api.factory.payment-preferences' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
}
},
);

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Subscription;
use WC_Product;
use WC_Product_Subscription;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingSubscriptions;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
@ -142,6 +143,11 @@ class SubscriptionModule implements ModuleInterface {
add_action(
'save_post',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $product_id ) use ( $c ) {
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
@ -152,14 +158,19 @@ class SubscriptionModule implements ModuleInterface {
return;
}
$nonce = wc_clean(wp_unslash($_POST['_wcsnonce'])) ?? '';
$nonce = wc_clean( wp_unslash( $_POST['_wcsnonce'] ?? '' ) );
if (
$subscriptions_mode !== 'subscriptions_api'
|| ! is_string( $nonce )
|| ! wp_verify_nonce( $nonce, 'wcs_subscription_meta' ) ) {
return;
}
$product = wc_get_product( $product_id );
if ( ! is_a( $product, WC_Product::class ) ) {
return;
}
$enable_subscription_product = wc_clean( wp_unslash( $_POST['_ppcp_enable_subscription_product'] ?? '' ) );
$product->update_meta_data( '_ppcp_enable_subscription_product', $enable_subscription_product );
$product->save();
@ -180,6 +191,10 @@ class SubscriptionModule implements ModuleInterface {
if ( $product->meta_exists( 'ppcp_subscription_product' ) && ! $product->meta_exists( 'ppcp_subscription_plan' ) ) {
$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();
@ -192,7 +207,12 @@ class SubscriptionModule implements ModuleInterface {
add_filter(
'woocommerce_order_data_store_cpt_get_orders_query',
function( $query, $query_vars ) {
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $query, $query_vars ): array {
if ( ! empty( $query_vars['ppcp_subscription'] ) ) {
$query['meta_query'][] = array(
'key' => 'ppcp_subscription',
@ -208,6 +228,11 @@ class SubscriptionModule implements ModuleInterface {
add_action(
'woocommerce_customer_changed_subscription_to_cancelled',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $subscription ) use ( $c ) {
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
if ( $subscription_id ) {
@ -231,6 +256,11 @@ class SubscriptionModule implements ModuleInterface {
add_action(
'woocommerce_customer_changed_subscription_to_active',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $subscription ) use ( $c ) {
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
if ( $subscription_id ) {
@ -261,8 +291,17 @@ class SubscriptionModule implements ModuleInterface {
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;
}
$enable_subscription_product = $product->get_meta( '_ppcp_enable_subscription_product' );
$subscription_plan_name = $product->get_meta( '_ppcp_subscription_plan_name' );
@ -289,6 +328,11 @@ class SubscriptionModule implements ModuleInterface {
add_action(
'woocommerce_subscription_before_actions',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $subscription ) use ( $c ) {
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
if ( $subscription_id ) {
@ -308,7 +352,16 @@ class SubscriptionModule implements ModuleInterface {
add_filter(
'wcs_view_subscription_actions',
function( $actions, $subscription ) {
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $actions, $subscription ): array {
if ( ! is_a( $subscription, WC_Subscription::class ) ) {
return $actions;
}
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
if ( $subscription_id && $subscription->get_status() === 'active' ) {
$url = wp_nonce_url(
@ -342,18 +395,22 @@ class SubscriptionModule implements ModuleInterface {
add_action(
'wp_loaded',
function() use ( $c ) {
if(! function_exists('wcs_get_subscription')) {
if ( ! function_exists( 'wcs_get_subscription' ) ) {
return;
}
$cancel_subscription_id = wc_clean( wp_unslash( $_GET['ppcp_cancel_subscription'] ?? '' ) );
$subscription = wcs_get_subscription( absint( $cancel_subscription_id ) );
if ( ! wcs_is_subscription( $subscription ) ) {
if ( ! wcs_is_subscription( $subscription ) || $subscription === false ) {
return;
}
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
$nonce = wc_clean( wp_unslash( $_GET['_wpnonce'] ?? '' ) );
if ( ! is_string( $nonce ) ) {
return;
}
if (
$subscription_id
&& $cancel_subscription_id
@ -393,7 +450,16 @@ class SubscriptionModule implements ModuleInterface {
add_filter(
'woocommerce_order_actions',
function( $actions, $subscription ) {
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $actions, $subscription ): array {
if ( ! is_a( $subscription, WC_Subscription::class ) ) {
return $actions;
}
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
if ( $subscription_id && isset( $actions['wcs_process_renewal'] ) ) {
unset( $actions['wcs_process_renewal'] );
@ -407,6 +473,11 @@ class SubscriptionModule implements ModuleInterface {
add_action(
'woocommerce_process_shop_subscription_meta',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
function( $id, $subscription ) use ( $c ) {
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
if ( $subscription_id ) {

View file

@ -1,4 +1,11 @@
<?php
/**
* The subscription module.
*
* @package WooCommerce\PayPalCommerce\Subscription
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Subscription;
@ -12,38 +19,63 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\BillingCycleFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentPreferencesFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ProductFactory;
/**
* Class SubscriptionsApiHandler
*/
class SubscriptionsApiHandler {
/**
* Catalog products.
*
* @var CatalogProducts
*/
private $products_endpoint;
/**
* Product factory.
*
* @var ProductFactory
*/
private $product_factory;
/**
* Billing plans.
*
* @var BillingPlans
*/
private $billing_plans_endpoint;
/**
* Billing cycle factory.
*
* @var BillingCycleFactory
*/
private $billing_cycle_factory;
/**
* Payment preferences factory.
*
* @var PaymentPreferencesFactory
*/
private $payment_preferences_factory;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* SubscriptionsApiHandler constructor.
*
* @param CatalogProducts $products_endpoint Products endpoint.
* @param ProductFactory $product_factory Product factory.
* @param BillingPlans $billing_plans_endpoint Billing plans endpoint.
* @param BillingCycleFactory $billing_cycle_factory Billing cycle factory.
* @param PaymentPreferencesFactory $payment_preferences_factory Payment preferences factory.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
CatalogProducts $products_endpoint,
ProductFactory $product_factory,
@ -63,12 +95,12 @@ class SubscriptionsApiHandler {
/**
* Creates a Catalog Product and adds it as WC product meta.
*
* @param WC_Product $product
* @param WC_Product $product The WC product.
* @return void
*/
public function create_product( WC_Product $product ) {
try {
$subscription_product = $this->products_endpoint->create( $product->get_title(), $product->get_description());
$subscription_product = $this->products_endpoint->create( $product->get_title(), $product->get_description() );
$product->update_meta_data( 'ppcp_subscription_product', $subscription_product->to_array() );
$product->save();
} catch ( RuntimeException $exception ) {
@ -81,13 +113,20 @@ class SubscriptionsApiHandler {
}
}
public function create_plan( string $plan_name, WC_Product $product ) {
/**
* Creates a subscription plan.
*
* @param string $plan_name The plan name.
* @param WC_Product $product The WC product.
* @return void
*/
public function create_plan( string $plan_name, WC_Product $product ): void {
try {
$subscription_plan = $this->billing_plans_endpoint->create(
$plan_name,
$product->get_meta( 'ppcp_subscription_product' )['id'] ?? '',
array($this->billing_cycle_factory->from_wc_product($product)->to_array()),
$this->payment_preferences_factory->from_wc_product($product)->to_array()
array( $this->billing_cycle_factory->from_wc_product( $product )->to_array() ),
$this->payment_preferences_factory->from_wc_product( $product )->to_array()
);
$product->update_meta_data( 'ppcp_subscription_plan', $subscription_plan->to_array() );
@ -102,23 +141,29 @@ class SubscriptionsApiHandler {
}
}
public function update_product(WC_Product $product) {
/**
* Updates a product.
*
* @param WC_Product $product The WC product.
* @return void
*/
public function update_product( WC_Product $product ): void {
try {
$catalog_product_id = $product->get_meta( 'ppcp_subscription_product' )['id'] ?? '';
if($catalog_product_id) {
$catalog_product = $this->products_endpoint->product($catalog_product_id);
$catalog_product_name = $catalog_product->name() ?? '';
$catalog_product_description = $catalog_product->description() ?? '';
if($catalog_product_name !== $product->get_title() || $catalog_product_description !== $product->get_description()) {
if ( $catalog_product_id ) {
$catalog_product = $this->products_endpoint->product( $catalog_product_id );
$catalog_product_name = $catalog_product->name() ?: '';
$catalog_product_description = $catalog_product->description() ?: '';
if ( $catalog_product_name !== $product->get_title() || $catalog_product_description !== $product->get_description() ) {
$data = array();
if($catalog_product_name !== $product->get_title()) {
if ( $catalog_product_name !== $product->get_title() ) {
$data[] = (object) array(
'op' => 'replace',
'path' => '/name',
'value' => $product->get_title(),
);
}
if($catalog_product_description !== $product->get_description()) {
if ( $catalog_product_description !== $product->get_description() ) {
$data[] = (object) array(
'op' => 'replace',
'path' => '/description',
@ -126,11 +171,10 @@ class SubscriptionsApiHandler {
);
}
$this->products_endpoint->update($catalog_product_id, $data);
$this->products_endpoint->update( $catalog_product_id, $data );
}
}
} catch (RuntimeException $exception) {
} catch ( RuntimeException $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) ) {
$error = $exception->get_details( $error );
@ -140,21 +184,27 @@ class SubscriptionsApiHandler {
}
}
public function update_plan(WC_Product $product) {
/**
* Updates a plan.
*
* @param WC_Product $product The WC product.
* @return void
*/
public function update_plan( WC_Product $product ): void {
try {
$subscription_plan_id = $product->get_meta('ppcp_subscription_plan')['id'] ?? '';
if ($subscription_plan_id) {
$subscription_plan = $this->billing_plans_endpoint->plan($subscription_plan_id);
$subscription_plan_id = $product->get_meta( 'ppcp_subscription_plan' )['id'] ?? '';
if ( $subscription_plan_id ) {
$subscription_plan = $this->billing_plans_endpoint->plan( $subscription_plan_id );
$price = $subscription_plan->billing_cycles()[0]->pricing_scheme()['fixed_price']['value'] ?? '';
if($price && round($price, 2) !== round($product->get_price(), 2)) {
if ( $price && round( $price, 2 ) !== round( (float) $product->get_price(), 2 ) ) {
$this->billing_plans_endpoint->update_pricing(
$subscription_plan_id,
$this->billing_cycle_factory->from_wc_product($product)
$this->billing_cycle_factory->from_wc_product( $product )
);
}
}
} catch (RuntimeException $exception) {
} catch ( RuntimeException $exception ) {
$error = $exception->getMessage();
if ( is_a( $exception, PayPalApiException::class ) ) {
$error = $exception->get_details( $error );

View file

@ -338,9 +338,9 @@ class SettingsListener {
$this->dcc_status_cache->delete( DCCProductStatus::DCC_STATUS_CACHE_KEY );
}
$ppcp_reference_transaction_enabled = get_transient('ppcp_reference_transaction_enabled') ?? '';
if($ppcp_reference_transaction_enabled) {
delete_transient('ppcp_reference_transaction_enabled');
$ppcp_reference_transaction_enabled = get_transient( 'ppcp_reference_transaction_enabled' ) ?? '';
if ( $ppcp_reference_transaction_enabled ) {
delete_transient( 'ppcp_reference_transaction_enabled' );
}
$redirect_url = false;

View file

@ -92,8 +92,8 @@ return array(
new PaymentSaleCompleted( $logger ),
new BillingSubscriptionCancelled( $logger ),
new BillingPlanPricingChangeActivated( $logger ),
new CatalogProductUpdated($logger),
new BillingPlanUpdated($logger),
new CatalogProductUpdated( $logger ),
new BillingPlanUpdated( $logger ),
);
},

View file

@ -65,6 +65,9 @@ class BillingPlanPricingChangeActivated implements RequestHandler {
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
$response = array( 'success' => false );
if ( is_null( $request['resource'] ) ) {
return new WP_REST_Response( $response );
}
$plan_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
$price = wc_clean( wp_unslash( $request['resource']['billing_cycles'][0]['pricing_scheme']['fixed_price']['value'] ?? '' ) );
@ -72,8 +75,9 @@ class BillingPlanPricingChangeActivated implements RequestHandler {
$args = array(
'meta_key' => 'ppcp_subscription_plan',
);
$products = wc_get_products( $args );
$products = wc_get_products( $args );
if ( is_array( $products ) ) {
foreach ( $products as $product ) {
if ( $product->get_meta( 'ppcp_subscription_plan' )->id === $plan_id ) {
$product->update_meta_data( '_subscription_price', $price );
@ -81,6 +85,7 @@ class BillingPlanPricingChangeActivated implements RequestHandler {
}
}
}
}
$response['success'] = true;
return new WP_REST_Response( $response );

View file

@ -64,6 +64,11 @@ class BillingPlanUpdated implements RequestHandler {
* @return WP_REST_Response
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
$response = array( 'success' => false );
if ( is_null( $request['resource'] ) ) {
return new WP_REST_Response( $response );
}
$plan_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
if ( $plan_id ) {
$products = wc_get_products(
@ -72,6 +77,7 @@ class BillingPlanUpdated implements RequestHandler {
)
);
if ( is_array( $products ) ) {
foreach ( $products as $product ) {
if ( $product->meta_exists( 'ppcp_subscription_plan' ) ) {
$plan_name = wc_clean( wp_unslash( $request['resource']['name'] ?? '' ) );
@ -100,6 +106,7 @@ class BillingPlanUpdated implements RequestHandler {
}
}
}
}
$response['success'] = true;
return new WP_REST_Response( $response );

View file

@ -65,6 +65,9 @@ class BillingSubscriptionCancelled implements RequestHandler {
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
$response = array( 'success' => false );
if ( is_null( $request['resource'] ) ) {
return new WP_REST_Response( $response );
}
$subscription_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
if ( $subscription_id ) {

View file

@ -64,6 +64,11 @@ class CatalogProductUpdated implements RequestHandler {
* @return WP_REST_Response
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
$response = array( 'success' => false );
if ( is_null( $request['resource'] ) ) {
return new WP_REST_Response( $response );
}
$product_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
$name = wc_clean( wp_unslash( $request['resource']['name'] ?? '' ) );
if ( $product_id && $name ) {
@ -72,6 +77,7 @@ class CatalogProductUpdated implements RequestHandler {
);
$products = wc_get_products( $args );
if ( is_array( $products ) ) {
foreach ( $products as $product ) {
if (
$product->meta_exists( 'ppcp_subscription_product' )
@ -79,6 +85,11 @@ class CatalogProductUpdated implements RequestHandler {
&& $product->get_meta( 'ppcp_subscription_product' )['id'] === $product_id
&& $product->get_title() !== $name
) {
/**
* Suppress ArgumentTypeCoercion
*
* @psalm-suppress ArgumentTypeCoercion
*/
wp_update_post(
array(
'ID' => $product->get_id(),
@ -90,6 +101,7 @@ class CatalogProductUpdated implements RequestHandler {
}
}
}
}
$response['success'] = true;
return new WP_REST_Response( $response );

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
use WP_REST_Request;
use WP_REST_Response;
@ -67,8 +68,11 @@ class PaymentSaleCompleted implements RequestHandler {
*/
public function handle_request( WP_REST_Request $request ): WP_REST_Response {
$response = array( 'success' => false );
if ( is_null( $request['resource'] ) ) {
return new WP_REST_Response( $response );
}
$billing_agreement_id = wc_clean( wp_unslash( $request['resource']['billing_agreement_id'] ) ) ?? '';
$billing_agreement_id = wc_clean( wp_unslash( $request['resource']['billing_agreement_id'] ?? '' ) );
if ( ! $billing_agreement_id ) {
$message = 'Could not retrieve billing agreement id for subscription.';
$this->logger->warning( $message, array( 'request' => $request ) );
@ -88,6 +92,7 @@ class PaymentSaleCompleted implements RequestHandler {
$subscriptions = wcs_get_subscriptions( $args );
if ( ! $subscriptions ) {
$billing_agreement_id = is_string( $billing_agreement_id ) ? $billing_agreement_id : '';
$message = "Could not retrieve WC subscriptions for billing agreement: {$billing_agreement_id}";
$this->logger->warning( $message, array( 'request' => $request ) );
$response['message'] = $message;
@ -96,13 +101,15 @@ class PaymentSaleCompleted implements RequestHandler {
foreach ( $subscriptions as $subscription ) {
$renewal_order = wcs_create_renewal_order( $subscription );
if ( is_a( $renewal_order, WC_Order::class ) ) {
$renewal_order->payment_complete();
$transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ) ) ?? '';
if ( $transaction_id ) {
$transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
if ( $transaction_id && is_string( $transaction_id ) ) {
$this->update_transaction_id( $transaction_id, $renewal_order );
}
}
}
$response['success'] = true;
return new WP_REST_Response( $response );