mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Merge pull request #1650 from woocommerce/PCP-1968-subscriptions-api-renewals
PayPal Subscriptions API renewals (1968)
This commit is contained in:
commit
2961132aec
2 changed files with 51 additions and 17 deletions
|
@ -9,6 +9,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Subscription;
|
||||
|
||||
use ActionScheduler_Store;
|
||||
use Exception;
|
||||
use WC_Product;
|
||||
use WC_Product_Subscription_Variation;
|
||||
|
@ -288,6 +289,35 @@ class SubscriptionModule implements ModuleInterface {
|
|||
30,
|
||||
2
|
||||
);
|
||||
|
||||
add_action(
|
||||
'action_scheduler_before_execute',
|
||||
/**
|
||||
* Param types removed to avoid third-party issues.
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
function( $action_id ) {
|
||||
/**
|
||||
* Class exist in WooCommerce.
|
||||
*
|
||||
* @psalm-suppress UndefinedClass
|
||||
*/
|
||||
$store = ActionScheduler_Store::instance();
|
||||
$action = $store->fetch_action( $action_id );
|
||||
|
||||
$subscription_id = $action->get_args()['subscription_id'] ?? null;
|
||||
if ( $subscription_id ) {
|
||||
$subscription = wcs_get_subscription( $subscription_id );
|
||||
if ( is_a( $subscription, WC_Subscription::class ) ) {
|
||||
$paypal_subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
||||
if ( $paypal_subscription_id ) {
|
||||
as_unschedule_action( $action->get_hook(), $action->get_args() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -527,7 +557,8 @@ class SubscriptionModule implements ModuleInterface {
|
|||
$subscriptions_api_handler = $c->get( 'subscription.api-handler' );
|
||||
assert( $subscriptions_api_handler instanceof SubscriptionsApiHandler );
|
||||
$this->update_subscription_product_meta( $product, $subscriptions_api_handler );
|
||||
}
|
||||
},
|
||||
30
|
||||
);
|
||||
|
||||
add_action(
|
||||
|
|
|
@ -80,6 +80,11 @@ class PaymentSaleCompleted implements RequestHandler {
|
|||
return $this->failure_response( 'Could not retrieve billing agreement id for subscription.' );
|
||||
}
|
||||
|
||||
$transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
|
||||
if ( ! $transaction_id || ! is_string( $transaction_id ) ) {
|
||||
return $this->failure_response( 'Could not retrieve transaction id for subscription.' );
|
||||
}
|
||||
|
||||
$args = array(
|
||||
// phpcs:ignore WordPress.DB.SlowDBQuery
|
||||
'meta_query' => array(
|
||||
|
@ -92,12 +97,11 @@ class PaymentSaleCompleted implements RequestHandler {
|
|||
);
|
||||
$subscriptions = wcs_get_subscriptions( $args );
|
||||
foreach ( $subscriptions as $subscription ) {
|
||||
$transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) );
|
||||
if ( $transaction_id && is_string( $transaction_id ) ) {
|
||||
$is_renewal = $subscription->get_meta( '_ppcp_is_subscription_renewal' ) ?? '';
|
||||
if ( $is_renewal ) {
|
||||
$renewal_order = wcs_create_renewal_order( $subscription );
|
||||
if ( is_a( $renewal_order, WC_Order::class ) ) {
|
||||
$renewal_order->set_payment_method( $subscription->get_payment_method() );
|
||||
$renewal_order->payment_complete();
|
||||
$this->update_transaction_id( $transaction_id, $renewal_order, $this->logger );
|
||||
break;
|
||||
|
@ -111,7 +115,6 @@ class PaymentSaleCompleted implements RequestHandler {
|
|||
$this->update_transaction_id( $transaction_id, $parent_order, $this->logger );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success_response();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue