2020-07-28 12:27:42 +03:00
|
|
|
<?php
|
2020-08-27 11:50:10 +03:00
|
|
|
/**
|
|
|
|
* The subscription module.
|
|
|
|
*
|
2020-09-11 14:11:10 +03:00
|
|
|
* @package WooCommerce\PayPalCommerce\Subscription
|
2020-08-27 11:50:10 +03:00
|
|
|
*/
|
2020-07-28 12:27:42 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
namespace WooCommerce\PayPalCommerce\Subscription;
|
2020-07-28 12:27:42 +03:00
|
|
|
|
|
|
|
use Dhii\Container\ServiceProvider;
|
|
|
|
use Dhii\Modular\Module\ModuleInterface;
|
2021-05-10 17:12:46 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2021-03-01 12:37:13 +01:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
2021-05-10 17:12:46 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Subscription\Repository\PaymentTokenRepository;
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
2021-03-01 12:37:13 +01:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
2020-07-28 12:27:42 +03:00
|
|
|
use Interop\Container\ServiceProviderInterface;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
2020-08-27 11:50:10 +03:00
|
|
|
/**
|
|
|
|
* Class SubscriptionModule
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
class SubscriptionModule implements ModuleInterface {
|
2020-07-28 12:27:42 +03:00
|
|
|
|
2020-08-27 11:50:10 +03:00
|
|
|
/**
|
|
|
|
* Setup the module.
|
|
|
|
*
|
|
|
|
* @return ServiceProviderInterface
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
public function setup(): ServiceProviderInterface {
|
|
|
|
return new ServiceProvider(
|
|
|
|
require __DIR__ . '/../services.php',
|
|
|
|
require __DIR__ . '/../extensions.php'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-27 11:50:10 +03:00
|
|
|
* Runs the module.
|
|
|
|
*
|
2020-09-16 10:18:45 +03:00
|
|
|
* @param ContainerInterface|null $container The container.
|
2020-08-27 11:08:36 +03:00
|
|
|
*/
|
2020-09-16 10:18:45 +03:00
|
|
|
public function run( ContainerInterface $container = null ) {
|
2020-08-27 11:08:36 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_scheduled_subscription_payment_' . PayPalGateway::ID,
|
2021-03-01 12:37:13 +01:00
|
|
|
function ( $amount, $order ) use ( $container ) {
|
|
|
|
$this->renew( $order, $container );
|
2020-08-27 11:08:36 +03:00
|
|
|
},
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
2021-03-01 12:37:13 +01:00
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_scheduled_subscription_payment_' . CreditCardGateway::ID,
|
|
|
|
function ( $amount, $order ) use ( $container ) {
|
|
|
|
$this->renew( $order, $container );
|
|
|
|
},
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
2021-05-10 17:12:46 +02:00
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_subscription_payment_complete',
|
|
|
|
function ( $subscription ) use ( $container ) {
|
|
|
|
$payment_token_repository = $container->get( 'subscription.repository.payment-token' );
|
|
|
|
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
|
|
|
|
|
|
|
$this->add_payment_token_id( $subscription, $payment_token_repository, $logger );
|
|
|
|
}
|
|
|
|
);
|
2021-03-01 12:37:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-10 12:55:35 +01:00
|
|
|
* Handles a Subscription product renewal.
|
|
|
|
*
|
|
|
|
* @param \WC_Order $order WooCommerce order.
|
|
|
|
* @param ContainerInterface|null $container The container.
|
2021-03-01 12:37:13 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2021-03-10 12:55:35 +01:00
|
|
|
protected function renew( $order, $container ) {
|
2021-03-01 12:37:13 +01:00
|
|
|
if ( ! is_a( $order, \WC_Order::class ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$handler = $container->get( 'subscription.renewal-handler' );
|
|
|
|
$handler->renew( $order );
|
2020-08-27 11:08:36 +03:00
|
|
|
}
|
2020-09-16 10:18:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the key for the module.
|
|
|
|
*
|
|
|
|
* @return string|void
|
|
|
|
*/
|
|
|
|
public function getKey() {
|
|
|
|
}
|
2021-05-10 17:12:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds Payment token ID to subscription.
|
|
|
|
*
|
|
|
|
* @param \WC_Subscription $subscription The subscription.
|
|
|
|
* @param PaymentTokenRepository $payment_token_repository The payment repository.
|
|
|
|
* @param LoggerInterface $logger The logger.
|
|
|
|
*/
|
|
|
|
private function add_payment_token_id(
|
|
|
|
\WC_Subscription $subscription,
|
|
|
|
PaymentTokenRepository $payment_token_repository,
|
|
|
|
LoggerInterface $logger
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
$tokens = $payment_token_repository->all_for_user_id( $subscription->get_customer_id() );
|
|
|
|
if ( $tokens ) {
|
|
|
|
$subscription_id = $subscription->get_id();
|
|
|
|
$latest_token_id = end( $tokens )->id() ?: '';
|
|
|
|
update_post_meta( $subscription_id, 'payment_token_id', $latest_token_id, true );
|
|
|
|
}
|
|
|
|
} catch ( RuntimeException $error ) {
|
|
|
|
$message = sprintf(
|
|
|
|
// translators: %1$s is the payment token Id, %2$s is the error message.
|
|
|
|
__(
|
|
|
|
'Could not add token Id to subscription %1$s: %2$s',
|
|
|
|
'woocommerce-paypal-payments'
|
|
|
|
),
|
|
|
|
$subscription->get_id(),
|
|
|
|
$error->getMessage()
|
|
|
|
);
|
|
|
|
|
|
|
|
$logger->log( 'warning', $message );
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 12:27:42 +03:00
|
|
|
}
|