mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-03 08:37:53 +08:00
Fix psalm
This commit is contained in:
parent
e1d84af7f0
commit
eca8a3b09c
21 changed files with 357 additions and 203 deletions
|
@ -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 ),
|
||||
);
|
||||
},
|
||||
|
||||
|
|
|
@ -65,19 +65,24 @@ 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'] ?? '' ) );
|
||||
if ( $plan_id && $price ) {
|
||||
$args = array(
|
||||
$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();
|
||||
$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 );
|
||||
$product->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,29 +77,31 @@ class BillingPlanUpdated implements RequestHandler {
|
|||
)
|
||||
);
|
||||
|
||||
foreach ( $products as $product ) {
|
||||
if ( $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
$plan_name = wc_clean( wp_unslash( $request['resource']['name'] ?? '' ) );
|
||||
if ( $plan_name !== $product->get_meta( '_ppcp_subscription_plan_name' ) ) {
|
||||
$product->update_meta_data( '_ppcp_subscription_plan_name', $plan_name );
|
||||
$product->save();
|
||||
}
|
||||
|
||||
$billing_cycles = wc_clean( wp_unslash( $request['resource']['billing_cycles'] ?? array() ) );
|
||||
if ( $billing_cycles ) {
|
||||
$price = $billing_cycles[0]['pricing_scheme']['fixed_price']['value'] ?? '';
|
||||
if ( $price && round( $price, 2 ) !== round( $product->get_meta( '_subscription_price' ), 2 ) ) {
|
||||
$product->update_meta_data( '_subscription_price', $price );
|
||||
if ( is_array( $products ) ) {
|
||||
foreach ( $products as $product ) {
|
||||
if ( $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
$plan_name = wc_clean( wp_unslash( $request['resource']['name'] ?? '' ) );
|
||||
if ( $plan_name !== $product->get_meta( '_ppcp_subscription_plan_name' ) ) {
|
||||
$product->update_meta_data( '_ppcp_subscription_plan_name', $plan_name );
|
||||
$product->save();
|
||||
}
|
||||
}
|
||||
|
||||
$payment_preferences = wc_clean( wp_unslash( $request['resource']['payment_preferences'] ?? array() ) );
|
||||
if ( $payment_preferences ) {
|
||||
$setup_fee = $payment_preferences['setup_fee']['value'] ?? '';
|
||||
if ( $setup_fee && round( $setup_fee, 2 ) !== round( $product->get_meta( '_subscription_sign_up_fee' ), 2 ) ) {
|
||||
$product->update_meta_data( '_subscription_sign_up_fee', $setup_fee );
|
||||
$product->save();
|
||||
$billing_cycles = wc_clean( wp_unslash( $request['resource']['billing_cycles'] ?? array() ) );
|
||||
if ( $billing_cycles ) {
|
||||
$price = $billing_cycles[0]['pricing_scheme']['fixed_price']['value'] ?? '';
|
||||
if ( $price && round( $price, 2 ) !== round( $product->get_meta( '_subscription_price' ), 2 ) ) {
|
||||
$product->update_meta_data( '_subscription_price', $price );
|
||||
$product->save();
|
||||
}
|
||||
}
|
||||
|
||||
$payment_preferences = wc_clean( wp_unslash( $request['resource']['payment_preferences'] ?? array() ) );
|
||||
if ( $payment_preferences ) {
|
||||
$setup_fee = $payment_preferences['setup_fee']['value'] ?? '';
|
||||
if ( $setup_fee && round( $setup_fee, 2 ) !== round( $product->get_meta( '_subscription_sign_up_fee' ), 2 ) ) {
|
||||
$product->update_meta_data( '_subscription_sign_up_fee', $setup_fee );
|
||||
$product->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 ) {
|
||||
|
|
|
@ -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,21 +77,28 @@ class CatalogProductUpdated implements RequestHandler {
|
|||
);
|
||||
$products = wc_get_products( $args );
|
||||
|
||||
foreach ( $products as $product ) {
|
||||
if (
|
||||
$product->meta_exists( 'ppcp_subscription_product' )
|
||||
&& isset( $product->get_meta( 'ppcp_subscription_product' )['id'] )
|
||||
&& $product->get_meta( 'ppcp_subscription_product' )['id'] === $product_id
|
||||
&& $product->get_title() !== $name
|
||||
) {
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $product->get_id(),
|
||||
'post_title' => $name,
|
||||
)
|
||||
);
|
||||
if ( is_array( $products ) ) {
|
||||
foreach ( $products as $product ) {
|
||||
if (
|
||||
$product->meta_exists( 'ppcp_subscription_product' )
|
||||
&& isset( $product->get_meta( 'ppcp_subscription_product' )['id'] )
|
||||
&& $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(),
|
||||
'post_title' => $name,
|
||||
)
|
||||
);
|
||||
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,7 +92,8 @@ class PaymentSaleCompleted implements RequestHandler {
|
|||
$subscriptions = wcs_get_subscriptions( $args );
|
||||
|
||||
if ( ! $subscriptions ) {
|
||||
$message = "Could not retrieve WC subscriptions for billing agreement: {$billing_agreement_id}";
|
||||
$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;
|
||||
return new WP_REST_Response( $response );
|
||||
|
@ -96,11 +101,13 @@ class PaymentSaleCompleted implements RequestHandler {
|
|||
|
||||
foreach ( $subscriptions as $subscription ) {
|
||||
$renewal_order = wcs_create_renewal_order( $subscription );
|
||||
$renewal_order->payment_complete();
|
||||
if ( is_a( $renewal_order, WC_Order::class ) ) {
|
||||
$renewal_order->payment_complete();
|
||||
|
||||
$transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ) ) ?? '';
|
||||
if ( $transaction_id ) {
|
||||
$this->update_transaction_id( $transaction_id, $renewal_order );
|
||||
$transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
|
||||
if ( $transaction_id && is_string( $transaction_id ) ) {
|
||||
$this->update_transaction_id( $transaction_id, $renewal_order );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue