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-19 15:39:33 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
2021-09-17 17:11:36 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Vaulting\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;
|
2021-05-19 15:39:33 +02:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
2020-07-28 12:27:42 +03:00
|
|
|
|
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
|
|
|
*/
|
2021-07-14 14:58:23 +02:00
|
|
|
public function run( ContainerInterface $container ): void {
|
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 ) {
|
2021-09-17 17:11:36 +02:00
|
|
|
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
|
2021-05-10 17:12:46 +02:00
|
|
|
$logger = $container->get( 'woocommerce.logger.woocommerce' );
|
|
|
|
|
|
|
|
$this->add_payment_token_id( $subscription, $payment_token_repository, $logger );
|
|
|
|
}
|
|
|
|
);
|
2021-05-19 15:39:33 +02:00
|
|
|
|
|
|
|
add_filter(
|
|
|
|
'woocommerce_gateway_description',
|
|
|
|
function ( $description, $id ) use ( $container ) {
|
2021-09-17 17:11:36 +02:00
|
|
|
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
|
2021-05-19 15:39:33 +02:00
|
|
|
$settings = $container->get( 'wcgateway.settings' );
|
|
|
|
$subscription_helper = $container->get( 'subscription.helper' );
|
|
|
|
|
2021-08-05 09:52:24 +02:00
|
|
|
return $this->display_saved_paypal_payments( $settings, (string) $id, $payment_token_repository, (string) $description, $subscription_helper );
|
2021-05-19 15:39:33 +02:00
|
|
|
},
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
|
|
|
add_filter(
|
|
|
|
'woocommerce_credit_card_form_fields',
|
|
|
|
function ( $default_fields, $id ) use ( $container ) {
|
2021-09-17 17:11:36 +02:00
|
|
|
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
|
2021-05-19 15:39:33 +02:00
|
|
|
$settings = $container->get( 'wcgateway.settings' );
|
|
|
|
$subscription_helper = $container->get( 'subscription.helper' );
|
|
|
|
|
|
|
|
return $this->display_saved_credit_cards( $settings, $id, $payment_token_repository, $default_fields, $subscription_helper );
|
|
|
|
},
|
|
|
|
20,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the key for the module.
|
|
|
|
*
|
|
|
|
* @return string|void
|
|
|
|
*/
|
|
|
|
public function getKey() {
|
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
|
|
|
|
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.
|
|
|
|
*/
|
2021-05-19 15:39:33 +02:00
|
|
|
protected function add_payment_token_id(
|
2021-05-10 17:12:46 +02:00
|
|
|
\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();
|
2021-05-17 11:00:57 +02:00
|
|
|
$latest_token_id = end( $tokens )->id() ? end( $tokens )->id() : '';
|
2021-05-10 17:12:46 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
2021-05-19 15:39:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays saved PayPal payments.
|
|
|
|
*
|
|
|
|
* @param Settings $settings The settings.
|
|
|
|
* @param string $id The payment gateway Id.
|
|
|
|
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
|
|
|
|
* @param string $description The payment gateway description.
|
|
|
|
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function display_saved_paypal_payments(
|
|
|
|
Settings $settings,
|
|
|
|
string $id,
|
|
|
|
PaymentTokenRepository $payment_token_repository,
|
|
|
|
string $description,
|
|
|
|
SubscriptionHelper $subscription_helper
|
|
|
|
): string {
|
|
|
|
if ( $settings->has( 'vault_enabled' )
|
|
|
|
&& $settings->get( 'vault_enabled' )
|
|
|
|
&& PayPalGateway::ID === $id
|
|
|
|
&& $subscription_helper->is_subscription_change_payment()
|
|
|
|
) {
|
|
|
|
$tokens = $payment_token_repository->all_for_user_id( get_current_user_id() );
|
|
|
|
if ( ! $tokens || ! $payment_token_repository->tokens_contains_paypal( $tokens ) ) {
|
|
|
|
return esc_html__(
|
|
|
|
'No PayPal payments saved, in order to use a saved payment you first need to create it through a purchase.',
|
|
|
|
'woocommerce-paypal-payments'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = sprintf(
|
|
|
|
'<p class="form-row form-row-wide"><label>%1$s</label><select id="saved-paypal-payment" name="saved_paypal_payment">',
|
|
|
|
esc_html__( 'Select a saved PayPal payment', 'woocommerce-paypal-payments' )
|
|
|
|
);
|
|
|
|
foreach ( $tokens as $token ) {
|
|
|
|
if ( isset( $token->source()->paypal ) ) {
|
|
|
|
$output .= sprintf(
|
|
|
|
'<option value="%1$s">%2$s</option>',
|
|
|
|
$token->id(),
|
|
|
|
$token->source()->paypal->payer->email_address
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output .= '</select></p>';
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $description;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays saved credit cards.
|
|
|
|
*
|
|
|
|
* @param Settings $settings The settings.
|
|
|
|
* @param string $id The payment gateway Id.
|
|
|
|
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
|
|
|
|
* @param array $default_fields Default payment gateway fields.
|
|
|
|
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
|
|
|
* @return array|mixed|string
|
|
|
|
* @throws NotFoundException When setting was not found.
|
|
|
|
*/
|
|
|
|
protected function display_saved_credit_cards(
|
|
|
|
Settings $settings,
|
|
|
|
string $id,
|
|
|
|
PaymentTokenRepository $payment_token_repository,
|
|
|
|
array $default_fields,
|
|
|
|
SubscriptionHelper $subscription_helper
|
|
|
|
) {
|
|
|
|
|
|
|
|
if ( $settings->has( 'vault_enabled' )
|
|
|
|
&& $settings->get( 'vault_enabled' )
|
|
|
|
&& $subscription_helper->is_subscription_change_payment()
|
|
|
|
&& CreditCardGateway::ID === $id
|
|
|
|
) {
|
|
|
|
$tokens = $payment_token_repository->all_for_user_id( get_current_user_id() );
|
|
|
|
if ( ! $tokens || ! $payment_token_repository->tokens_contains_card( $tokens ) ) {
|
|
|
|
$default_fields = array();
|
|
|
|
$default_fields['saved-credit-card'] = esc_html__(
|
|
|
|
'No Credit Card saved, in order to use a saved Credit Card you first need to create it through a purchase.',
|
|
|
|
'woocommerce-paypal-payments'
|
|
|
|
);
|
|
|
|
return $default_fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = sprintf(
|
|
|
|
'<p class="form-row form-row-wide"><label>%1$s</label><select id="saved-credit-card" name="saved_credit_card">',
|
|
|
|
esc_html__( 'Select a saved Credit Card payment', 'woocommerce-paypal-payments' )
|
|
|
|
);
|
|
|
|
foreach ( $tokens as $token ) {
|
|
|
|
if ( isset( $token->source()->card ) ) {
|
|
|
|
$output .= sprintf(
|
|
|
|
'<option value="%1$s">%2$s ...%3$s</option>',
|
|
|
|
$token->id(),
|
|
|
|
$token->source()->card->brand,
|
|
|
|
$token->source()->card->last_digits
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output .= '</select></p>';
|
|
|
|
|
|
|
|
$default_fields = array();
|
|
|
|
$default_fields['saved-credit-card'] = $output;
|
|
|
|
return $default_fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default_fields;
|
|
|
|
}
|
2020-07-28 12:27:42 +03:00
|
|
|
}
|