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
|
|
|
|
2023-01-09 13:07:33 +01:00
|
|
|
use WC_Product_Subscription;
|
2023-03-06 16:41:18 +01:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingSubscriptions;
|
2023-01-30 16:10:21 +01:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
2022-11-09 10:11:31 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
2021-05-10 17:12:46 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-09-13 15:41:46 +02:00
|
|
|
use WC_Order;
|
2022-09-13 15:53:05 +02:00
|
|
|
use WC_Subscription;
|
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;
|
2022-11-09 10:11:31 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
|
|
|
|
use WooCommerce\PayPalCommerce\Vendor\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
|
|
|
/**
|
2021-08-30 08:08:41 +02:00
|
|
|
* {@inheritDoc}
|
2020-08-27 11:50:10 +03:00
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
public function setup(): ServiceProviderInterface {
|
|
|
|
return new ServiceProvider(
|
|
|
|
require __DIR__ . '/../services.php',
|
|
|
|
require __DIR__ . '/../extensions.php'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-30 08:08:41 +02:00
|
|
|
* {@inheritDoc}
|
2020-08-27 11:08:36 +03:00
|
|
|
*/
|
2021-08-30 08:10:43 +02:00
|
|
|
public function run( ContainerInterface $c ): void {
|
2020-08-27 11:08:36 +03:00
|
|
|
add_action(
|
|
|
|
'woocommerce_scheduled_subscription_payment_' . PayPalGateway::ID,
|
2021-08-30 08:10:43 +02:00
|
|
|
function ( $amount, $order ) use ( $c ) {
|
|
|
|
$this->renew( $order, $c );
|
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,
|
2021-08-30 08:10:43 +02:00
|
|
|
function ( $amount, $order ) use ( $c ) {
|
|
|
|
$this->renew( $order, $c );
|
2021-03-01 12:37:13 +01:00
|
|
|
},
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
2021-05-10 17:12:46 +02:00
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_subscription_payment_complete',
|
2021-08-30 08:10:43 +02:00
|
|
|
function ( $subscription ) use ( $c ) {
|
2021-09-30 12:52:15 +02:00
|
|
|
$payment_token_repository = $c->get( 'vaulting.repository.payment-token' );
|
2021-08-30 08:10:43 +02:00
|
|
|
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
2021-05-10 17:12:46 +02:00
|
|
|
|
|
|
|
$this->add_payment_token_id( $subscription, $payment_token_repository, $logger );
|
|
|
|
}
|
|
|
|
);
|
2021-05-19 15:39:33 +02:00
|
|
|
|
|
|
|
add_filter(
|
|
|
|
'woocommerce_gateway_description',
|
2021-08-30 08:10:43 +02:00
|
|
|
function ( $description, $id ) use ( $c ) {
|
2021-09-30 12:52:15 +02:00
|
|
|
$payment_token_repository = $c->get( 'vaulting.repository.payment-token' );
|
2021-08-30 08:10:43 +02:00
|
|
|
$settings = $c->get( 'wcgateway.settings' );
|
|
|
|
$subscription_helper = $c->get( 'subscription.helper' );
|
2021-05-19 15:39:33 +02:00
|
|
|
|
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',
|
2021-08-30 08:10:43 +02:00
|
|
|
function ( $default_fields, $id ) use ( $c ) {
|
2021-09-30 12:52:15 +02:00
|
|
|
$payment_token_repository = $c->get( 'vaulting.repository.payment-token' );
|
2021-08-30 08:10:43 +02:00
|
|
|
$settings = $c->get( 'wcgateway.settings' );
|
|
|
|
$subscription_helper = $c->get( 'subscription.helper' );
|
2021-05-19 15:39:33 +02:00
|
|
|
|
|
|
|
return $this->display_saved_credit_cards( $settings, $id, $payment_token_repository, $default_fields, $subscription_helper );
|
|
|
|
},
|
|
|
|
20,
|
|
|
|
2
|
|
|
|
);
|
2022-09-13 15:41:46 +02:00
|
|
|
|
|
|
|
add_filter(
|
|
|
|
'ppcp_create_order_request_body_data',
|
2022-09-13 15:53:05 +02:00
|
|
|
function( array $data ) use ( $c ) {
|
2022-10-20 15:48:29 +02:00
|
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
2022-10-18 15:59:11 +02:00
|
|
|
$wc_order_action = wc_clean( wp_unslash( $_POST['wc_order_action'] ?? '' ) );
|
2022-09-13 16:15:58 +02:00
|
|
|
if (
|
|
|
|
$wc_order_action === 'wcs_process_renewal'
|
|
|
|
&& isset( $data['payment_source']['token'] ) && $data['payment_source']['token']['type'] === 'PAYMENT_METHOD_TOKEN'
|
|
|
|
&& isset( $data['payment_source']['token']['source']->card )
|
|
|
|
) {
|
|
|
|
$renewal_order_id = absint( $data['purchase_units'][0]['custom_id'] );
|
|
|
|
$subscriptions = wcs_get_subscriptions_for_renewal_order( $renewal_order_id );
|
|
|
|
$subscriptions_values = array_values( $subscriptions );
|
|
|
|
$latest_subscription = array_shift( $subscriptions_values );
|
|
|
|
if ( is_a( $latest_subscription, WC_Subscription::class ) ) {
|
|
|
|
$related_renewal_orders = $latest_subscription->get_related_orders( 'ids', 'renewal' );
|
|
|
|
$latest_order_id_with_transaction = array_slice( $related_renewal_orders, 1, 1, false );
|
2022-09-13 17:25:25 +02:00
|
|
|
$order_id = ! empty( $latest_order_id_with_transaction ) ? $latest_order_id_with_transaction[0] : 0;
|
|
|
|
if ( count( $related_renewal_orders ) === 1 ) {
|
|
|
|
$order_id = $latest_subscription->get_parent_id();
|
|
|
|
}
|
2022-09-13 16:15:58 +02:00
|
|
|
|
2022-09-13 17:25:25 +02:00
|
|
|
$wc_order = wc_get_order( $order_id );
|
2022-09-13 16:15:58 +02:00
|
|
|
if ( is_a( $wc_order, WC_Order::class ) ) {
|
|
|
|
$transaction_id = $wc_order->get_transaction_id();
|
|
|
|
$data['application_context']['stored_payment_source'] = array(
|
|
|
|
'payment_initiator' => 'MERCHANT',
|
|
|
|
'payment_type' => 'RECURRING',
|
|
|
|
'usage' => 'SUBSEQUENT',
|
|
|
|
'previous_transaction_reference' => $transaction_id,
|
|
|
|
);
|
2022-09-13 15:41:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
);
|
2023-01-05 17:33:03 +01:00
|
|
|
|
2023-01-09 12:37:37 +01:00
|
|
|
add_action(
|
|
|
|
'save_post',
|
|
|
|
function( $product_id ) use ( $c ) {
|
2023-01-16 12:37:16 +01:00
|
|
|
$settings = $c->get( 'wcgateway.settings' );
|
|
|
|
assert( $settings instanceof Settings );
|
|
|
|
|
|
|
|
try {
|
2023-02-01 16:30:39 +01:00
|
|
|
$subscriptions_mode = $settings->get( 'subscriptions_mode' );
|
2023-01-16 12:37:16 +01:00
|
|
|
} catch ( NotFoundException $exception ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-02-01 16:30:39 +01:00
|
|
|
$subscriptions_mode !== 'subscriptions_api'
|
2023-01-16 12:37:16 +01:00
|
|
|
|| empty( $_POST['_wcsnonce'] )
|
|
|
|
|| ! wp_verify_nonce( wc_clean( wp_unslash( $_POST['_wcsnonce'] ) ), 'wcs_subscription_meta' ) ) {
|
2023-01-09 12:37:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$product = wc_get_product( $product_id );
|
2023-03-27 15:24:44 +02:00
|
|
|
$enable_subscription_product = wc_clean(wp_unslash($_POST['_ppcp_enable_subscription_product'] ?? ''));
|
|
|
|
$product->update_meta_data('_ppcp_enable_subscription_product', $enable_subscription_product);
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
if ( $product->get_type() === 'subscription' && $enable_subscription_product === 'yes' ) {
|
2023-03-17 16:06:26 +01:00
|
|
|
$subscriptions_api_handler = $c->get('subscription.api-handler');
|
|
|
|
assert($subscriptions_api_handler instanceof SubscriptionsApiHandler);
|
2023-03-01 16:12:26 +01:00
|
|
|
|
2023-03-06 16:41:18 +01:00
|
|
|
if ( $product->meta_exists( 'ppcp_subscription_product' ) && $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
2023-03-27 11:28:45 +02:00
|
|
|
$subscriptions_api_handler->update_product($product);
|
2023-03-28 12:38:43 +02:00
|
|
|
$subscriptions_api_handler->update_plan($product);
|
2023-03-01 16:12:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $product->meta_exists( 'ppcp_subscription_product' ) ) {
|
2023-03-17 16:06:26 +01:00
|
|
|
$subscriptions_api_handler->create_product($product);
|
2023-01-09 12:37:37 +01:00
|
|
|
}
|
|
|
|
|
2023-03-27 11:28:45 +02:00
|
|
|
if ( $product->meta_exists( 'ppcp_subscription_product' ) && ! $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
2023-03-27 15:24:44 +02:00
|
|
|
$subscription_plan_name = wc_clean(wp_unslash($_POST['_ppcp_subscription_plan_name'] ?? ''));
|
|
|
|
$product->update_meta_data('_ppcp_subscription_plan_name', $subscription_plan_name);
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
$subscriptions_api_handler->create_plan($subscription_plan_name, $product);
|
2023-01-09 12:37:37 +01:00
|
|
|
}
|
2023-01-05 17:33:03 +01:00
|
|
|
}
|
2023-01-09 12:37:37 +01:00
|
|
|
},
|
|
|
|
12
|
|
|
|
);
|
2023-01-09 13:07:33 +01:00
|
|
|
|
2023-02-10 17:25:51 +01:00
|
|
|
add_filter(
|
|
|
|
'woocommerce_order_data_store_cpt_get_orders_query',
|
|
|
|
function( $query, $query_vars ) {
|
|
|
|
if ( ! empty( $query_vars['ppcp_subscription'] ) ) {
|
|
|
|
$query['meta_query'][] = array(
|
|
|
|
'key' => 'ppcp_subscription',
|
|
|
|
'value' => esc_attr( $query_vars['ppcp_subscription'] ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
},
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
2023-03-06 16:41:18 +01:00
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_customer_changed_subscription_to_cancelled',
|
|
|
|
function( $subscription ) use ( $c ) {
|
|
|
|
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
|
|
|
if ( $subscription_id ) {
|
|
|
|
$subscriptions_endpoint = $c->get( 'api.endpoint.billing-subscriptions' );
|
|
|
|
assert( $subscriptions_endpoint instanceof BillingSubscriptions );
|
|
|
|
|
|
|
|
try {
|
|
|
|
$subscriptions_endpoint->suspend( $subscription_id );
|
|
|
|
} catch ( RuntimeException $exception ) {
|
|
|
|
$error = $exception->getMessage();
|
|
|
|
if ( is_a( $exception, PayPalApiException::class ) ) {
|
|
|
|
$error = $exception->get_details( $error );
|
|
|
|
}
|
|
|
|
|
|
|
|
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
|
|
|
$logger->error( 'Could not suspend subscription product on PayPal. ' . $error );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
add_action(
|
|
|
|
'woocommerce_customer_changed_subscription_to_active',
|
|
|
|
function( $subscription ) use ( $c ) {
|
|
|
|
$subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? '';
|
|
|
|
if ( $subscription_id ) {
|
|
|
|
$subscriptions_endpoint = $c->get( 'api.endpoint.billing-subscriptions' );
|
|
|
|
assert( $subscriptions_endpoint instanceof BillingSubscriptions );
|
|
|
|
|
|
|
|
try {
|
|
|
|
$subscriptions_endpoint->activate( $subscription_id );
|
|
|
|
} catch ( RuntimeException $exception ) {
|
|
|
|
$error = $exception->getMessage();
|
|
|
|
if ( is_a( $exception, PayPalApiException::class ) ) {
|
|
|
|
$error = $exception->get_details( $error );
|
|
|
|
}
|
|
|
|
|
|
|
|
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
|
|
|
$logger->error( 'Could not active subscription product on PayPal. ' . $error );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-03-27 15:24:44 +02:00
|
|
|
|
|
|
|
add_action( 'woocommerce_product_options_general_product_data', function() use($c) {
|
|
|
|
$settings = $c->get( 'wcgateway.settings' );
|
|
|
|
assert( $settings instanceof Settings );
|
|
|
|
|
|
|
|
try {
|
|
|
|
$subscriptions_mode = $settings->get( 'subscriptions_mode' );
|
|
|
|
if($subscriptions_mode === 'subscriptions_api') {
|
|
|
|
global $post;
|
|
|
|
$product = wc_get_product( $post->ID );
|
|
|
|
$enable_subscription_product = $product->get_meta('_ppcp_enable_subscription_product');
|
|
|
|
$subscription_plan_name = $product->get_meta('_ppcp_subscription_plan_name');
|
|
|
|
|
|
|
|
echo '<div class="options_group subscription_pricing show_if_subscription hidden">';
|
2023-03-28 12:38:43 +02:00
|
|
|
echo '<p class="form-field"><label for="_ppcp_enable_subscription_product">Connect to PayPal</label><input type="checkbox" id="ppcp_enable_subscription_product" name="_ppcp_enable_subscription_product" value="yes" '.checked($enable_subscription_product, 'yes', false).'/><span class="description">Connect Product to PayPal Subscriptions Plan</span></p>';
|
2023-03-27 16:09:16 +02:00
|
|
|
|
|
|
|
$subscription_product = $product->get_meta( 'ppcp_subscription_product' );
|
|
|
|
$subscription_plan = $product->get_meta( 'ppcp_subscription_plan' );
|
|
|
|
if($subscription_product && $subscription_plan) {
|
|
|
|
echo '<p class="form-field"><label>Product</label><a href="'.esc_url('https://www.sandbox.paypal.com/billing/plans/products/' . $subscription_product['id']).'" target="_blank">'.esc_attr($subscription_product['id']).'</a></p>';
|
|
|
|
echo '<p class="form-field"><label>Plan</label><a href="'.esc_url('https://www.sandbox.paypal.com/billing/plans/' . $subscription_plan['id']).'" target="_blank">'.esc_attr($subscription_plan['id']).'</a></p>';
|
|
|
|
} else {
|
2023-03-28 12:38:43 +02:00
|
|
|
echo '<p class="form-field"><label for="_ppcp_subscription_plan_name">Plan Name</label><input type="text" class="short" id="ppcp_subscription_plan_name" name="_ppcp_subscription_plan_name" value="'.esc_attr($subscription_plan_name).'"></p>';
|
2023-03-27 16:09:16 +02:00
|
|
|
}
|
|
|
|
echo '</div>';
|
2023-03-27 15:24:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch ( NotFoundException $exception ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} );
|
2021-05-19 15:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-11-03 10:20:39 +02:00
|
|
|
if ( ! ( $order instanceof \WC_Order ) ) {
|
2021-03-01 12:37:13 +01:00
|
|
|
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 ) {
|
2021-05-17 11:00:57 +02:00
|
|
|
$latest_token_id = end( $tokens )->id() ? end( $tokens )->id() : '';
|
2022-10-24 12:35:31 +02:00
|
|
|
$subscription->update_meta_data( 'payment_token_id', $latest_token_id );
|
|
|
|
$subscription->save();
|
2021-05-10 17:12:46 +02:00
|
|
|
}
|
|
|
|
} 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()
|
|
|
|
) {
|
2022-10-19 11:50:15 +02:00
|
|
|
$tokens = $payment_token_repository->all_for_user_id( get_current_user_id() );
|
2021-05-19 15:39:33 +02:00
|
|
|
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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-19 11:50:15 +02:00
|
|
|
$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' )
|
|
|
|
);
|
2021-05-19 15:39:33 +02:00
|
|
|
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
|
|
|
|
) {
|
|
|
|
|
2023-02-03 17:07:51 +01:00
|
|
|
if ( $settings->has( 'vault_enabled_dcc' )
|
|
|
|
&& $settings->get( 'vault_enabled_dcc' )
|
2021-05-19 15:39:33 +02:00
|
|
|
&& $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
|
|
|
}
|