From 087a532023d72090149d311d0925be9be05ea520 Mon Sep 17 00:00:00 2001 From: David Remer Date: Tue, 18 Aug 2020 09:04:58 +0300 Subject: [PATCH] add paypal credit card gateway --- modules.local/ppcp-wc-gateway/services.php | 77 +++++++++--------- .../src/Gateway/CreditCardGateway.php | 79 +++++++++++++++++++ .../src/Gateway/PayPalGateway.php | 12 +-- .../src/Settings/SettingsListener.php | 9 ++- .../src/Settings/SettingsRenderer.php | 8 +- .../ppcp-wc-gateway/src/WcGatewayModule.php | 1 + 6 files changed, 141 insertions(+), 45 deletions(-) create mode 100644 modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php diff --git a/modules.local/ppcp-wc-gateway/services.php b/modules.local/ppcp-wc-gateway/services.php index 6b860e59a..85d4ceab9 100644 --- a/modules.local/ppcp-wc-gateway/services.php +++ b/modules.local/ppcp-wc-gateway/services.php @@ -11,6 +11,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways; +use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; @@ -37,6 +38,21 @@ return [ $settings ); }, + 'wcgateway.credit-card-gateway' => static function (ContainerInterface $container): CreditCardGateway { + $orderProcessor = $container->get('wcgateway.order-processor'); + $settingsRenderer = $container->get('wcgateway.settings.render'); + $authorizedPayments = $container->get('wcgateway.processor.authorized-payments'); + $notice = $container->get('wcgateway.notice.authorize-order-action'); + $settings = $container->get('wcgateway.settings'); + + return new CreditCardGateway( + $settingsRenderer, + $orderProcessor, + $authorizedPayments, + $notice, + $settings + ); + }, 'wcgateway.disabler' => static function (ContainerInterface $container): DisableGateways { $sessionHandler = $container->get('session.handler'); $settings = $container->get('wcgateway.settings'); @@ -136,6 +152,7 @@ return [ State::STATE_PROGRESSIVE, ], 'requirements' => [], + 'gateway' => 'all', ], 'sandbox_on' => [ 'title' => __('Sandbox', 'woocommerce-paypal-commerce-gateway'), @@ -146,6 +163,7 @@ return [ State::STATE_START, ], 'requirements' => [], + 'gateway' => 'all', ], 'sandbox_on_info' => [ 'title' => __('Sandbox', 'woocommerce-paypal-commerce-gateway'), @@ -157,6 +175,7 @@ return [ ], 'hidden' => 'sandbox_on', 'requirements' => [], + 'gateway' => 'all', ], 'merchant_email' => [ 'title' => __('Email address', 'woocommerce-paypal-commerce-gateway'), @@ -169,6 +188,7 @@ return [ State::STATE_START, ], 'requirements' => [], + 'gateway' => 'all', ], 'merchant_email_info' => [ 'title' => __('Email address', 'woocommerce-paypal-commerce-gateway'), @@ -180,6 +200,7 @@ return [ ], 'hidden' => 'merchant_email', 'requirements' => [], + 'gateway' => 'all', ], 'toggle_manual_input' => [ 'type' => 'ppcp-text', @@ -191,6 +212,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'all', ], 'client_id' => [ 'title' => __('Client Id', 'woocommerce-paypal-commerce-gateway'), @@ -204,6 +226,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'all', ], 'client_secret' => [ 'title' => __('Secret Key', 'woocommerce-paypal-commerce-gateway'), @@ -217,6 +240,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'all', ], 'title' => [ 'title' => __('Title', 'woocommerce-paypal-commerce-gateway'), @@ -232,6 +256,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'description' => [ 'title' => __('Description', 'woocommerce-paypal-commerce-gateway'), @@ -250,6 +275,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'intent' => [ 'title' => __('Intent', 'woocommerce-paypal-commerce-gateway'), @@ -269,6 +295,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'button_single_product_enabled' => [ 'title' => __('Buttons on Single Product', 'woocommerce-paypal-commerce-gateway'), @@ -280,6 +307,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'button_mini_cart_enabled' => [ 'title' => __('Buttons on Mini Cart', 'woocommerce-paypal-commerce-gateway'), @@ -291,6 +319,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'button_cart_enabled' => [ 'title' => __('Buttons on Cart', 'woocommerce-paypal-commerce-gateway'), @@ -302,6 +331,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'button_label' => [ 'title' => __('Button Label', 'woocommerce-paypal-commerce-gateway'), @@ -324,6 +354,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'brand_name' => [ 'title' => __('Brand Name', 'woocommerce-paypal-commerce-gateway'), @@ -339,6 +370,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'landing_page' => [ 'title' => __('Landing Page', 'woocommerce-paypal-commerce-gateway'), @@ -359,6 +391,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'button_color' => [ 'title' => __('Color', 'woocommerce-paypal-commerce-gateway'), @@ -381,6 +414,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'button_shape' => [ 'title' => __('Shape', 'woocommerce-paypal-commerce-gateway'), @@ -401,6 +435,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'disable_funding' => [ 'title' => __('Disable funding sources', 'woocommerce-paypal-commerce-gateway'), @@ -429,6 +464,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'paypal', ], 'vault_enabled' => [ 'title' => __('Vaulting', 'woocommerce-paypal-commerce-gateway'), @@ -441,30 +477,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], - ], - 'dcc_cart_enabled' => [ - 'title' => __('Enable credit card on cart', 'woocommerce-paypal-commerce-gateway'), - 'type' => 'checkbox', - 'label' => __('Allow your customers to pay with credit card directly in your cart.', 'woocommerce-paypal-commerce-gateway'), - 'default' => true, - 'screens' => [ - State::STATE_ONBOARDED, - ], - 'requirements' => [ - 'dcc', - ], - ], - 'dcc_mini_cart_enabled' => [ - 'title' => __('Enable credit card on mini cart', 'woocommerce-paypal-commerce-gateway'), - 'type' => 'checkbox', - 'label' => __('Allow your customers to pay with credit card directly in your mini cart.', 'woocommerce-paypal-commerce-gateway'), - 'default' => true, - 'screens' => [ - State::STATE_ONBOARDED, - ], - 'requirements' => [ - 'dcc', - ], + 'gateway' => 'paypal', ], 'dcc_checkout_enabled' => [ 'title' => __('Enable credit card on checkout', 'woocommerce-paypal-commerce-gateway'), @@ -477,18 +490,7 @@ return [ 'requirements' => [ 'dcc', ], - ], - 'dcc_single_product_enabled' => [ - 'title' => __('Enable credit card on products', 'woocommerce-paypal-commerce-gateway'), - 'type' => 'checkbox', - 'label' => __('Allow your customers to pay with credit card instantly on the product page.', 'woocommerce-paypal-commerce-gateway'), - 'default' => true, - 'screens' => [ - State::STATE_ONBOARDED, - ], - 'requirements' => [ - 'dcc', - ], + 'gateway' => 'dcc', ], 'disable_cards' => [ 'title' => __('Disable specific credit cards', 'woocommerce-paypal-commerce-gateway'), @@ -515,6 +517,7 @@ return [ 'requirements' => [ 'dcc', ], + 'gateway' => 'dcc', ], 'logging_enabled' => [ 'title' => __('Logging', 'woocommerce-paypal-commerce-gateway'), @@ -529,6 +532,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'all', ], 'prefix' => [ 'title' => __('Installation prefix', 'woocommerce-paypal-commerce-gateway'), @@ -542,6 +546,7 @@ return [ State::STATE_ONBOARDED, ], 'requirements' => [], + 'gateway' => 'all', ], ]; }, diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php new file mode 100644 index 000000000..85d69d349 --- /dev/null +++ b/modules.local/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php @@ -0,0 +1,79 @@ +id = self::ID; + $this->orderProcessor = $orderProcessor; + $this->authorizedPayments = $authorizedPayments; + $this->notice = $notice; + $this->settingsRenderer = $settingsRenderer; + $this->config = $config; + if ($this->config->has('vault_enabled') && $this->config->get('vault_enabled')) { + $this->supports = [ + 'products', + 'subscriptions', + 'subscription_cancellation', + 'subscription_suspension', + 'subscription_reactivation', + 'subscription_amount_changes', + 'subscription_date_changes', + 'subscription_payment_method_change', + 'subscription_payment_method_change_customer', + 'subscription_payment_method_change_admin', + 'multiple_subscriptions', + ]; + } + + $this->method_title = __('Credit Card Payments', 'woocommerce-paypal-commerce-gateway'); + $this->method_description = __( + 'Provide your customers with the option to pay with credit card.', + 'woocommerce-paypal-commerce-gateway' + ); + $this->title = $this->config->has('title') ? + $this->config->get('title') : $this->method_title; + $this->description = $this->config->has('description') ? + $this->config->get('description') : $this->method_description; + + $this->init_form_fields(); + $this->init_settings(); + + add_action( + 'woocommerce_update_options_payment_gateways_' . $this->id, + [ + $this, + 'process_admin_options', + ] + ); + } + public function generate_ppcp_html(): string + { + + ob_start(); + $this->settingsRenderer->render(true); + $content = ob_get_contents(); + ob_end_clean(); + return $content; + } + +} \ No newline at end of file diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 68dc2c651..2a9d9f71b 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -30,11 +30,11 @@ class PayPalGateway extends \WC_Payment_Gateway public const INTENT_META_KEY = '_ppcp_paypal_intent'; public const ORDER_ID_META_KEY = '_ppcp_paypal_order_id'; - private $settingsRenderer; - private $authorizedPayments; - private $notice; - private $orderProcessor; - private $config; + protected $settingsRenderer; + protected $authorizedPayments; + protected $notice; + protected $orderProcessor; + protected $config; public function __construct( SettingsRenderer $settingsRenderer, @@ -178,7 +178,7 @@ class PayPalGateway extends \WC_Payment_Gateway { ob_start(); - $this->settingsRenderer->render(); + $this->settingsRenderer->render(false); $content = ob_get_contents(); ob_end_clean(); return $content; diff --git a/modules.local/ppcp-wc-gateway/src/Settings/SettingsListener.php b/modules.local/ppcp-wc-gateway/src/Settings/SettingsListener.php index 7cfea8be4..b929767fb 100644 --- a/modules.local/ppcp-wc-gateway/src/Settings/SettingsListener.php +++ b/modules.local/ppcp-wc-gateway/src/Settings/SettingsListener.php @@ -60,8 +60,13 @@ class SettingsListener */ $rawData = (isset($_POST['ppcp'])) ? (array) wp_unslash($_POST['ppcp']) : []; $settings = $this->retrieveSettingsFromRawData($rawData); - $settings['enabled'] = isset($_POST['woocommerce_ppcp-gateway_enabled']) - && absint($_POST['woocommerce_ppcp-gateway_enabled']) === 1; + $settings['enabled'] = ( + isset($_POST['woocommerce_ppcp-gateway_enabled']) + && absint($_POST['woocommerce_ppcp-gateway_enabled']) === 1 + ) || ( + isset($_POST['woocommerce_ppcp-credit-card-gateway_enabled']) + && absint($_POST['woocommerce_ppcp-credit-card-gateway_enabled']) === 1 + ); foreach ($settings as $id => $value) { $this->settings->set($id, $value); } diff --git a/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php b/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php index 411b59d75..05f35967d 100644 --- a/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php +++ b/modules.local/ppcp-wc-gateway/src/Settings/SettingsRenderer.php @@ -106,7 +106,7 @@ class SettingsRenderer } //phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType - public function render() + public function render(bool $isDcc) { $nonce = wp_create_nonce(SettingsListener::NONCE); @@ -117,6 +117,12 @@ class SettingsRenderer if (! in_array($this->state->currentState(), $config['screens'], true)) { continue; } + if ($isDcc && ! in_array($config['gateway'], ['all', 'dcc'],true)) { + continue; + } + if (! $isDcc && ! in_array($config['gateway'], ['all', 'paypal'],true)) { + continue; + } if (in_array('dcc', $config['requirements'], true) && ! $this->dccApplies->forCountryCurrency()) { continue; } diff --git a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php index 40f166d23..25d33504d 100644 --- a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php +++ b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php @@ -99,6 +99,7 @@ class WcGatewayModule implements ModuleInterface 'woocommerce_payment_gateways', static function ($methods) use ($container): array { $methods[] = $container->get('wcgateway.paypal-gateway'); + $methods[] = $container->get('wcgateway.credit-card-gateway'); return (array)$methods; } );