diff --git a/modules/ppcp-api-client/src/Endpoint/BillingSubscriptions.php b/modules/ppcp-api-client/src/Endpoint/BillingSubscriptions.php index 520720f54..c9244e2dd 100644 --- a/modules/ppcp-api-client/src/Endpoint/BillingSubscriptions.php +++ b/modules/ppcp-api-client/src/Endpoint/BillingSubscriptions.php @@ -165,4 +165,32 @@ class BillingSubscriptions { ); } } + + public function subscription(string $id) { + $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' + ), + ); + + $response = $this->request( $url, $args ); + if ( is_wp_error( $response ) || ! is_array( $response ) ) { + throw new RuntimeException( 'Not able to get subscription.' ); + } + + $json = json_decode( $response['body'] ); + $status_code = (int) wp_remote_retrieve_response_code( $response ); + if ( 200 !== $status_code ) { + throw new PayPalApiException( + $json, + $status_code + ); + } + + return $json; + } } diff --git a/modules/ppcp-subscription/src/SubscriptionModule.php b/modules/ppcp-subscription/src/SubscriptionModule.php index cee98b995..4ea8b6b98 100644 --- a/modules/ppcp-subscription/src/SubscriptionModule.php +++ b/modules/ppcp-subscription/src/SubscriptionModule.php @@ -400,6 +400,64 @@ class SubscriptionModule implements ModuleInterface { 20, 2 ); + + add_action( + 'woocommerce_process_shop_subscription_meta', + function( $id, $subscription ) use ( $c ) { + $subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? ''; + if ( $subscription_id ) { + $subscriptions_endpoint = $c->get( 'api.endpoint.billing-subscriptions' ); + assert( $subscriptions_endpoint instanceof BillingSubscriptions ); + + if ( $subscription->get_status() === 'cancelled' ) { + try { + $subscriptions_endpoint->cancel( $subscription_id ); + } catch ( RuntimeException $exception ) { + $error = $exception->getMessage(); + if ( is_a( $exception, PayPalApiException::class ) ) { + $error = $exception->get_details( $error ); + } + + $logger = $c->get( 'woocommerce.logger.woocommerce' ); + $logger->error( 'Could not cancel subscription product on PayPal. ' . $error ); + } + } + + if ( $subscription->get_status() === 'pending-cancel' ) { + try { + $subscriptions_endpoint->suspend( $subscription_id ); + } catch ( RuntimeException $exception ) { + $error = $exception->getMessage(); + if ( is_a( $exception, PayPalApiException::class ) ) { + $error = $exception->get_details( $error ); + } + + $logger = $c->get( 'woocommerce.logger.woocommerce' ); + $logger->error( 'Could not suspend subscription product on PayPal. ' . $error ); + } + } + + if ( $subscription->get_status() === 'active' ) { + try { + $current_subscription = $subscriptions_endpoint->subscription( $subscription_id ); + if ( $current_subscription->status === 'SUSPENDED' ) { + $subscriptions_endpoint->activate( $subscription_id ); + } + } catch ( RuntimeException $exception ) { + $error = $exception->getMessage(); + if ( is_a( $exception, PayPalApiException::class ) ) { + $error = $exception->get_details( $error ); + } + + $logger = $c->get( 'woocommerce.logger.woocommerce' ); + $logger->error( 'Could not reactivate subscription product on PayPal. ' . $error ); + } + } + } + }, + 20, + 2 + ); } /**