mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Updates subscription status from subscriptions list page
This commit is contained in:
parent
a2d02cd7f4
commit
6518ae0655
2 changed files with 105 additions and 30 deletions
|
@ -200,13 +200,9 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
|||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
function( $id ) use ( $c ) {
|
||||
$subscription = wcs_get_subscription( $id );
|
||||
if ( ! is_a( $subscription, WC_Subscription::class ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscription = wcs_get_subscription( $id );
|
||||
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
||||
if ( ! $subscription_id ) {
|
||||
if ( ! is_a( $subscription, WC_Subscription::class ) || ! $subscription_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -218,6 +214,42 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
|||
20
|
||||
);
|
||||
|
||||
/**
|
||||
* Update status to pending-cancel from WC Subscriptions list page action link.
|
||||
*/
|
||||
add_action(
|
||||
'woocommerce_subscription_status_pending-cancel',
|
||||
function( WC_Subscription $subscription ) use ( $c ) {
|
||||
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
||||
if ( ! $subscription_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscription_status = $c->get( 'paypal-subscriptions.status' );
|
||||
assert( $subscription_status instanceof SubscriptionStatus );
|
||||
|
||||
$subscription_status->update_status( $subscription->get_status(), $subscription_id );
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Update status to cancelled from WC Subscriptions list page action link.
|
||||
*/
|
||||
add_action(
|
||||
'woocommerce_subscription_status_cancelled',
|
||||
function( WC_Subscription $subscription ) use ( $c ) {
|
||||
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
||||
if ( ! $subscription_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subscription_status = $c->get( 'paypal-subscriptions.status' );
|
||||
assert( $subscription_status instanceof SubscriptionStatus );
|
||||
|
||||
$subscription_status->update_status( $subscription->get_status(), $subscription_id );
|
||||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'woocommerce_order_actions',
|
||||
/**
|
||||
|
|
|
@ -13,8 +13,10 @@ use Psr\Log\LoggerInterface;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingSubscriptions;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Class SubscriptionStatus
|
||||
*/
|
||||
class SubscriptionStatus {
|
||||
|
||||
/**
|
||||
|
@ -22,7 +24,7 @@ class SubscriptionStatus {
|
|||
*
|
||||
* @var BillingSubscriptions
|
||||
*/
|
||||
private $subscriptions_endpoint ;
|
||||
private $subscriptions_endpoint;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
|
@ -31,12 +33,18 @@ class SubscriptionStatus {
|
|||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* SubscriptionStatus constructor.
|
||||
*
|
||||
* @param BillingSubscriptions $subscriptions_endpoint Billing subscriptions endpoint.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
*/
|
||||
public function __construct(
|
||||
BillingSubscriptions $subscriptions_endpoint,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->subscriptions_endpoint = $subscriptions_endpoint;
|
||||
$this->logger = $logger;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,35 +54,70 @@ class SubscriptionStatus {
|
|||
* @param string $subscription_id The PayPal Subscription ID.
|
||||
* @return void
|
||||
*/
|
||||
public function update_status(string $subscription_status, string $subscription_id): void {
|
||||
if ($subscription_status === 'cancelled') {
|
||||
public function update_status( string $subscription_status, string $subscription_id ): void {
|
||||
if ( $subscription_status === 'cancelled' ) {
|
||||
try {
|
||||
$this->subscriptions_endpoint->cancel($subscription_id);
|
||||
} catch (RuntimeException $exception) {
|
||||
$this->logger->info(
|
||||
sprintf(
|
||||
'Canceling PayPal subscription #%s.',
|
||||
$subscription_id
|
||||
)
|
||||
);
|
||||
|
||||
$this->logger->error('Could not cancel subscription product on PayPal. '
|
||||
. $this->get_error($exception));
|
||||
$this->subscriptions_endpoint->cancel( $subscription_id );
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$this->logger->error(
|
||||
sprintf(
|
||||
'Could not cancel PayPal subscription #%s. %s',
|
||||
$subscription_id,
|
||||
$this->get_error( $exception )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($subscription_status === 'pending-cancel') {
|
||||
if ( $subscription_status === 'pending-cancel' || $subscription_status === 'on-hold' ) {
|
||||
try {
|
||||
$this->subscriptions_endpoint->suspend($subscription_id);
|
||||
} catch (RuntimeException $exception) {
|
||||
$this->logger->error('Could not suspend subscription product on PayPal. '
|
||||
. $this->get_error($exception));
|
||||
$this->logger->info(
|
||||
sprintf(
|
||||
'Suspending PayPal subscription #%s.',
|
||||
$subscription_id
|
||||
)
|
||||
);
|
||||
|
||||
$this->subscriptions_endpoint->suspend( $subscription_id );
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$this->logger->error(
|
||||
sprintf(
|
||||
'Could not suspend PayPal subscription #%s. %s',
|
||||
$subscription_id,
|
||||
$this->get_error( $exception )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($subscription_status === 'active') {
|
||||
if ( $subscription_status === 'active' ) {
|
||||
try {
|
||||
$current_subscription = $this->subscriptions_endpoint->subscription($subscription_id);
|
||||
if ($current_subscription->status === 'SUSPENDED') {
|
||||
$this->subscriptions_endpoint->activate($subscription_id);
|
||||
$current_subscription = $this->subscriptions_endpoint->subscription( $subscription_id );
|
||||
if ( $current_subscription->status === 'SUSPENDED' ) {
|
||||
$this->logger->info(
|
||||
sprintf(
|
||||
'Activating suspended PayPal subscription #%s.',
|
||||
$subscription_id
|
||||
)
|
||||
);
|
||||
|
||||
$this->subscriptions_endpoint->activate( $subscription_id );
|
||||
}
|
||||
} catch (RuntimeException $exception) {
|
||||
$this->logger->error('Could not reactivate subscription product on PayPal. '
|
||||
. $this->get_error($exception));
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$this->logger->error(
|
||||
sprintf(
|
||||
'Could not reactivate PayPal subscription #%s. %s',
|
||||
$subscription_id,
|
||||
$this->get_error( $exception )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,10 +128,10 @@ class SubscriptionStatus {
|
|||
* @param RuntimeException $exception The exception.
|
||||
* @return string
|
||||
*/
|
||||
private function get_error(RuntimeException $exception): string {
|
||||
private function get_error( RuntimeException $exception ): string {
|
||||
$error = $exception->getMessage();
|
||||
if (is_a($exception, PayPalApiException::class)) {
|
||||
$error = $exception->get_details($error);
|
||||
if ( is_a( $exception, PayPalApiException::class ) ) {
|
||||
$error = $exception->get_details( $error );
|
||||
}
|
||||
|
||||
return $error;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue