Extract is renewal logic to method

This commit is contained in:
Emili Castells Guasch 2024-09-16 14:58:25 +02:00
parent 17cff6bd09
commit 80e6457502
2 changed files with 35 additions and 22 deletions

View file

@ -48,8 +48,7 @@ class RenewalHandler {
*/
public function process( array $subscriptions, string $transaction_id ): void {
foreach ( $subscriptions as $subscription ) {
$is_renewal = $subscription->get_meta( '_ppcp_is_subscription_renewal' ) ?? '';
if ( $is_renewal ) {
if ( $this->is_renewal( $subscription ) ) {
$renewal_order = wcs_create_renewal_order( $subscription );
if ( is_a( $renewal_order, WC_Order::class ) ) {
$renewal_order->set_payment_method( $subscription->get_payment_method() );
@ -67,4 +66,19 @@ class RenewalHandler {
}
}
}
/**
* Checks whether subscription order is for renewal or not.
*
* @param WC_Subscription $subscription WC Subscription.
* @return bool
*/
private function is_renewal( WC_Subscription $subscription ): bool {
$subscription_renewal_meta = $subscription->get_meta( '_ppcp_is_subscription_renewal' ) ?? '';
if ( $subscription_renewal_meta === 'true' ) {
return true;
}
return false;
}
}

View file

@ -2,34 +2,16 @@
namespace WooCommerce\PayPalCommerce\Tests\E2e;
use WC_Subscriptions_Product;
use WooCommerce\PayPalCommerce\PayPalSubscriptions\RenewalHandler;
class PayPalSubscriptionsRenewalTest extends TestCase
{
public function test_process()
public function test_is_renewal_by_meta()
{
$c = $this->getContainer();
$handler = new RenewalHandler($c->get('woocommerce.logger.woocommerce'));
$order = wc_create_order();
$order->set_customer_id( 1 );
$order->save();
$subscription = wcs_create_subscription(
array(
'order_id' => $order->get_id(),
'status' => 'active',
'billing_period' => WC_Subscriptions_Product::get_period( $_ENV['PAYPAL_SUBSCRIPTIONS_PRODUCT_ID'] ),
'billing_interval' => WC_Subscriptions_Product::get_interval( $_ENV['PAYPAL_SUBSCRIPTIONS_PRODUCT_ID'] ),
'customer_id' => $order->get_customer_id(),
)
);
$parent = $subscription->get_related_orders( 'ids', array( 'parent' ) );
$this->assertEquals(count($parent), 1);
$renewal = $subscription->get_related_orders( 'ids', array( 'renewal' ) );
$this->assertEquals(count($renewal), 0);
$subscription = $this->createSubscription();
$handler->process([$subscription], 'TRANSACTION-ID');
$renewal = $subscription->get_related_orders( 'ids', array( 'renewal' ) );
@ -39,4 +21,21 @@ class PayPalSubscriptionsRenewalTest extends TestCase
$renewal = $subscription->get_related_orders( 'ids', array( 'renewal' ) );
$this->assertEquals(count($renewal), 1);
}
private function createSubscription()
{
$order = wc_create_order();
$order->set_customer_id(1);
$order->save();
return wcs_create_subscription(
array(
'order_id' => $order->get_id(),
'status' => 'active',
'billing_period' => 'day',
'billing_interval' => '1',
'customer_id' => $order->get_customer_id(),
)
);
}
}