mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Extrat update subscription status logic to its own class
This commit is contained in:
parent
f57c7683ac
commit
a2d02cd7f4
3 changed files with 111 additions and 48 deletions
|
@ -40,4 +40,10 @@ return array(
|
||||||
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
'paypal-subscriptions.status' => static function ( ContainerInterface $container ): SubscriptionStatus {
|
||||||
|
return new SubscriptionStatus(
|
||||||
|
$container->get( 'api.endpoint.billing-subscriptions' ),
|
||||||
|
$container->get( 'woocommerce.logger.woocommerce' )
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -189,6 +189,9 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
||||||
30
|
30
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executed when updating WC Subscription.
|
||||||
|
*/
|
||||||
add_action(
|
add_action(
|
||||||
'woocommerce_process_shop_subscription_meta',
|
'woocommerce_process_shop_subscription_meta',
|
||||||
/**
|
/**
|
||||||
|
@ -196,65 +199,23 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
|
||||||
*
|
*
|
||||||
* @psalm-suppress MissingClosureParamType
|
* @psalm-suppress MissingClosureParamType
|
||||||
*/
|
*/
|
||||||
function( $id, $post ) use ( $c ) {
|
function( $id ) use ( $c ) {
|
||||||
$subscription = wcs_get_subscription( $id );
|
$subscription = wcs_get_subscription( $id );
|
||||||
if ( ! is_a( $subscription, WC_Subscription::class ) ) {
|
if ( ! is_a( $subscription, WC_Subscription::class ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
||||||
if ( ! $subscription_id ) {
|
if ( ! $subscription_id ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$subscriptions_endpoint = $c->get( 'api.endpoint.billing-subscriptions' );
|
|
||||||
assert( $subscriptions_endpoint instanceof BillingSubscriptions );
|
|
||||||
|
|
||||||
if ( $subscription->get_status() === 'cancelled' ) {
|
$subscription_status = $c->get( 'paypal-subscriptions.status' );
|
||||||
try {
|
assert( $subscription_status instanceof SubscriptionStatus );
|
||||||
$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' );
|
$subscription_status->update_status( $subscription->get_status(), $subscription_id );
|
||||||
$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,
|
20
|
||||||
2
|
|
||||||
);
|
);
|
||||||
|
|
||||||
add_filter(
|
add_filter(
|
||||||
|
|
96
modules/ppcp-paypal-subscriptions/src/SubscriptionStatus.php
Normal file
96
modules/ppcp-paypal-subscriptions/src/SubscriptionStatus.php
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Handles PayPal subscription status.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\WcSubscriptions
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\PayPalSubscriptions;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Billing subscriptions endpoint.
|
||||||
|
*
|
||||||
|
* @var BillingSubscriptions
|
||||||
|
*/
|
||||||
|
private $subscriptions_endpoint ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The logger.
|
||||||
|
*
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
private $logger;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
BillingSubscriptions $subscriptions_endpoint,
|
||||||
|
LoggerInterface $logger
|
||||||
|
) {
|
||||||
|
$this->subscriptions_endpoint = $subscriptions_endpoint;
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates PayPal subscription status from the given WC Subscription status.
|
||||||
|
*
|
||||||
|
* @param string $subscription_status The WC Subscription status.
|
||||||
|
* @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') {
|
||||||
|
try {
|
||||||
|
$this->subscriptions_endpoint->cancel($subscription_id);
|
||||||
|
} catch (RuntimeException $exception) {
|
||||||
|
|
||||||
|
$this->logger->error('Could not cancel subscription product on PayPal. '
|
||||||
|
. $this->get_error($exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($subscription_status === 'pending-cancel') {
|
||||||
|
try {
|
||||||
|
$this->subscriptions_endpoint->suspend($subscription_id);
|
||||||
|
} catch (RuntimeException $exception) {
|
||||||
|
$this->logger->error('Could not suspend subscription product on PayPal. '
|
||||||
|
. $this->get_error($exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($subscription_status === 'active') {
|
||||||
|
try {
|
||||||
|
$current_subscription = $this->subscriptions_endpoint->subscription($subscription_id);
|
||||||
|
if ($current_subscription->status === 'SUSPENDED') {
|
||||||
|
$this->subscriptions_endpoint->activate($subscription_id);
|
||||||
|
}
|
||||||
|
} catch (RuntimeException $exception) {
|
||||||
|
$this->logger->error('Could not reactivate subscription product on PayPal. '
|
||||||
|
. $this->get_error($exception));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get error from exception.
|
||||||
|
*
|
||||||
|
* @param RuntimeException $exception The exception.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function get_error(RuntimeException $exception): string {
|
||||||
|
$error = $exception->getMessage();
|
||||||
|
if (is_a($exception, PayPalApiException::class)) {
|
||||||
|
$error = $exception->get_details($error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $error;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue