Skip to content

Commit d94ea68

Browse files
committed
Refactor to integrate with YITH subscriptions
Remove traits, decouple Woo subscriptions, prepare for first payment and renew
1 parent 1bd928e commit d94ea68

11 files changed

+596
-288
lines changed

class-woocommerce-gateway-monei.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ private function includes() {
140140
include_once 'includes/woocommerce-gateway-monei-core-functions.php';
141141
include_once 'includes/class-wc-monei-ipn.php';
142142
include_once 'includes/class-wc-monei-logger.php';
143-
include_once 'includes/addons/trait-wc-monei-addons-helper.php';
144-
include_once 'includes/addons/trait-wc-monei-subscriptions.php';
145143

146144
if ( $this->is_request( 'admin' ) ) {
147145
include_once 'includes/class-wc-monei-pre-auth.php';

includes/addons/class-wc-monei-addons-redirect-hooks.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use Monei\Features\Subscriptions\SubscriptionService;
4+
use Monei\Features\Subscriptions\WooCommerceSubscriptionsHandler;
5+
use Monei\Features\Subscriptions\YithSubscriptionPluginHandler;
36
use Monei\Services\ApiKeyService;
47
use Monei\Services\payment\MoneiPaymentServices;
58
use Monei\Services\sdk\MoneiSdkClientFactory;
@@ -16,11 +19,6 @@
1619
*/
1720
class WC_Monei_Addons_Redirect_Hooks {
1821

19-
/**
20-
* Use Subscription trait.
21-
*/
22-
use WC_Monei_Subscriptions_Trait;
23-
2422
private MoneiPaymentServices $moneiPaymentServices;
2523

2624
/**
@@ -32,7 +30,10 @@ public function __construct() {
3230
//TODO use the container
3331
$apiKeyService = new ApiKeyService();
3432
$sdkClient = new MoneiSdkClientFactory( $apiKeyService );
33+
$wooHandler = new WooCommerceSubscriptionsHandler( $sdkClient );
34+
$yithHandler = new YithSubscriptionPluginHandler( $sdkClient );
3535
$this->moneiPaymentServices = new MoneiPaymentServices( $sdkClient );
36+
$this->subscriptionService = new SubscriptionService( $wooHandler, $yithHandler );
3637
}
3738

3839
/**
@@ -48,6 +49,7 @@ public function subscriptions_save_sequence_id_on_payment_method_change() {
4849
if ( ! isset( $_GET['id'] ) ) {
4950
return;
5051
}
52+
WC_Monei_Logger::log( 'Changing the method, updating the sequence id for subscriptions' );
5153

5254
$payment_id = filter_input( INPUT_GET, 'id', FILTER_CALLBACK, array( 'options' => 'sanitize_text_field' ) );
5355
$order_id = filter_input( INPUT_GET, 'orderId', FILTER_CALLBACK, array( 'options' => 'sanitize_text_field' ) );
@@ -60,7 +62,8 @@ public function subscriptions_save_sequence_id_on_payment_method_change() {
6062
}
6163

6264
$order_id = $verification_order_id[0];
63-
if ( ! $this->is_order_subscription( $order_id ) ) {
65+
$handler = $this->subscriptionService->getHandler();
66+
if ( ! $handler || ! $handler->is_subscription_order( $order_id ) ) {
6467
return;
6568
}
6669

@@ -69,12 +72,9 @@ public function subscriptions_save_sequence_id_on_payment_method_change() {
6972
* We need to update parent from subscription, where sequence id is stored.
7073
*/
7174
$payment = $this->moneiPaymentServices->get_payment( $payment_id );
72-
$subscription = new WC_Subscription( $order_id );
75+
$subscriptions = $handler->get_subscriptions_for_order( $order_id);
76+
$handler->update_subscription_meta_data($subscriptions, $payment);
7377

74-
$subscription->update_meta_data( '_monei_sequence_id', $payment->getSequenceId() );
75-
$subscription->update_meta_data( '_monei_payment_method_brand', $payment->getPaymentMethod()->getCard()->getBrand() );
76-
$subscription->update_meta_data( '_monei_payment_method_4_last_digits', $payment->getPaymentMethod()->getCard()->getLast4() );
77-
$subscription->save_meta_data();
7878
} catch ( Exception $e ) {
7979
wc_add_notice( __( 'Error while saving sequence id. Please contact admin. Payment ID: ', 'monei' ) . $payment_id, 'error' );
8080
WC_Monei_Logger::log( $e->getMessage(), 'error' );
@@ -100,27 +100,19 @@ public function subscriptions_save_sequence_id() {
100100
/**
101101
* Bail when not subscription.
102102
*/
103-
if ( ! $this->is_order_subscription( $order_id ) ) {
103+
$handler = $this->subscriptionService->getHandler();
104+
if ( ! $handler || ! $handler->is_subscription_order( $order_id ) ) {
104105
return;
105106
}
106107

107108
try {
108-
109-
$subscriptions = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => array( 'any' ) ) );
109+
$subscriptions = $handler->get_subscriptions_for_order( $order_id );
110110
if ( ! $subscriptions ) {
111111
return;
112112
}
113113

114114
$payment = $this->moneiPaymentServices->get_payment( $payment_id );
115-
/**
116-
* Iterate all subscriptions contained in the order, and add sequence id and cc data individually.
117-
*/
118-
foreach ( $subscriptions as $subscription_id => $subscription ) {
119-
$subscription->update_meta_data( '_monei_sequence_id', $payment->getSequenceId() );
120-
$subscription->update_meta_data( '_monei_payment_method_brand', $payment->getPaymentMethod()->getCard()->getBrand() );
121-
$subscription->update_meta_data( '_monei_payment_method_4_last_digits', $payment->getPaymentMethod()->getCard()->getLast4() );
122-
$subscription->save_meta_data();
123-
}
115+
$handler->update_subscription_meta_data( $subscriptions, $payment );
124116
} catch ( Exception $e ) {
125117
wc_add_notice( __( 'Error while saving sequence id. Please contact admin. Payment ID: ', 'monei' ) . $payment_id, 'error' );
126118
WC_Monei_Logger::log( $e->getMessage(), 'error' );

includes/addons/trait-wc-monei-addons-helper.php

Lines changed: 0 additions & 134 deletions
This file was deleted.

src/Core/container-definitions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use Monei\Features\Subscriptions\SubscriptionService;
4+
use Monei\Features\Subscriptions\WooCommerceSubscriptionsHandler;
5+
use Monei\Features\Subscriptions\YithSubscriptionPluginHandler;
36
use Monei\Repositories\PaymentMethodsRepository;
47
use Monei\Services\ApiKeyService;
58
use Monei\Services\BlockSupportService;
@@ -58,6 +61,16 @@ function () {
5861
ApiKeyService::class => DI\autowire( ApiKeyService::class ),
5962
MoneiSdkClientFactory::class => DI\autowire( MoneiSdkClientFactory::class )
6063
->constructor( DI\get( ApiKeyService::class ) ),
64+
WooCommerceSubscriptionsHandler::class => \DI\create(
65+
WooCommerceSubscriptionsHandler::class,
66+
)->constructor(
67+
DI\get( MoneiSdkClientFactory::class )
68+
),
69+
YithSubscriptionPluginHandler::class => \DI\autowire(YithSubscriptionPluginHandler::class),
70+
71+
SubscriptionService::class => \DI\autowire(SubscriptionService::class)
72+
->constructorParameter('wooHandler', \DI\get(WooCommerceSubscriptionsHandler::class))
73+
->constructorParameter('yithHandler', \DI\get(YithSubscriptionPluginHandler::class)),
6174
);
6275

6376
// Dynamically load all gateway classes in the folder
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Monei\Features\Subscriptions;
4+
5+
use WC_Order;
6+
7+
interface SubscriptionHandlerInterface
8+
{
9+
public function is_subscriptions_addon_enabled():bool;
10+
public function is_subscription_order(int $order_id): bool;
11+
public function create_subscription_payload(WC_Order $order, $payment_method, array $payload): array;
12+
public function scheduled_subscription_payment($amount_to_charge, WC_Order $renewal_order): void;
13+
public function init_subscriptions(array $suports, string $gateway_id): array;
14+
public function add_extra_info_to_subscriptions_payment_method_title(string $payment_method_to_display, $subscription): string;
15+
public function subscription_after_payment_success($confirm_payload, $confirm_payment, WC_Order $order): void;
16+
public function get_subscriptions_for_order(int $order_id):array;
17+
public function update_subscription_meta_data( $subscriptions, $payment ): void;
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Monei\Features\Subscriptions;
4+
5+
use WC_Order;
6+
7+
class SubscriptionService
8+
{
9+
private $wooHandler;
10+
private $yithHandler;
11+
12+
public function __construct(WooCommerceSubscriptionsHandler $wooHandler, YithSubscriptionPluginHandler $yithHandler)
13+
{
14+
$this->wooHandler = $wooHandler;
15+
$this->yithHandler = $yithHandler;
16+
}
17+
18+
public function getHandler(): ?SubscriptionHandlerInterface
19+
{
20+
if ($this->wooHandler->is_subscriptions_addon_enabled()) {
21+
return $this->wooHandler;
22+
}
23+
24+
if ($this->yithHandler->is_subscriptions_addon_enabled()) {
25+
return $this->yithHandler;
26+
}
27+
28+
return null;
29+
}
30+
}

0 commit comments

Comments
 (0)