2020-04-02 08:38:00 +03:00
|
|
|
|
<?php
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* The PayPal Payment Gateway
|
|
|
|
|
*
|
2020-09-11 14:11:10 +03:00
|
|
|
|
* @package WooCommerce\PayPalCommerce\WcGateway\Gateway
|
2020-08-28 08:13:45 +03:00
|
|
|
|
*/
|
2020-04-13 14:25:20 +03:00
|
|
|
|
|
2020-04-02 08:38:00 +03:00
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
|
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
|
2020-04-02 08:38:00 +03:00
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
|
|
|
|
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
|
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
|
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
|
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsRenderer;
|
2020-07-02 14:30:03 +03:00
|
|
|
|
use Psr\Container\ContainerInterface;
|
2020-04-02 08:38:00 +03:00
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* Class PayPalGateway
|
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
|
class PayPalGateway extends \WC_Payment_Gateway {
|
|
|
|
|
|
2020-09-14 14:17:39 +03:00
|
|
|
|
use ProcessPaymentTrait;
|
|
|
|
|
|
2020-09-11 13:38:02 +03:00
|
|
|
|
const ID = 'ppcp-gateway';
|
|
|
|
|
const CAPTURED_META_KEY = '_ppcp_paypal_captured';
|
|
|
|
|
const INTENT_META_KEY = '_ppcp_paypal_intent';
|
|
|
|
|
const ORDER_ID_META_KEY = '_ppcp_paypal_order_id';
|
2020-08-27 11:08:36 +03:00
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* The Settings Renderer.
|
|
|
|
|
*
|
|
|
|
|
* @var SettingsRenderer
|
|
|
|
|
*/
|
|
|
|
|
protected $settings_renderer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The processor for authorized payments.
|
|
|
|
|
*
|
|
|
|
|
* @var AuthorizedPaymentsProcessor
|
|
|
|
|
*/
|
|
|
|
|
protected $authorized_payments;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Authorized Order Action Notice.
|
|
|
|
|
*
|
|
|
|
|
* @var AuthorizeOrderActionNotice
|
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
|
protected $notice;
|
2020-08-28 08:13:45 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The processor for orders.
|
|
|
|
|
*
|
|
|
|
|
* @var OrderProcessor
|
|
|
|
|
*/
|
|
|
|
|
protected $order_processor;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The settings.
|
|
|
|
|
*
|
|
|
|
|
* @var ContainerInterface
|
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
|
protected $config;
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* The Session Handler.
|
|
|
|
|
*
|
|
|
|
|
* @var SessionHandler
|
|
|
|
|
*/
|
|
|
|
|
protected $session_handler;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* PayPalGateway constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param SettingsRenderer $settings_renderer The Settings Renderer.
|
|
|
|
|
* @param OrderProcessor $order_processor The Order Processor.
|
|
|
|
|
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor.
|
|
|
|
|
* @param AuthorizeOrderActionNotice $notice The Order Action Notice object.
|
|
|
|
|
* @param ContainerInterface $config The settings.
|
|
|
|
|
* @param SessionHandler $session_handler The Session Handler.
|
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
|
public function __construct(
|
2020-08-28 08:13:45 +03:00
|
|
|
|
SettingsRenderer $settings_renderer,
|
|
|
|
|
OrderProcessor $order_processor,
|
|
|
|
|
AuthorizedPaymentsProcessor $authorized_payments_processor,
|
2020-08-27 11:08:36 +03:00
|
|
|
|
AuthorizeOrderActionNotice $notice,
|
|
|
|
|
ContainerInterface $config,
|
2020-08-28 08:13:45 +03:00
|
|
|
|
SessionHandler $session_handler
|
2020-08-27 11:08:36 +03:00
|
|
|
|
) {
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$this->id = self::ID;
|
|
|
|
|
$this->order_processor = $order_processor;
|
|
|
|
|
$this->authorized_payments = $authorized_payments_processor;
|
|
|
|
|
$this->notice = $notice;
|
|
|
|
|
$this->settings_renderer = $settings_renderer;
|
|
|
|
|
$this->config = $config;
|
|
|
|
|
$this->session_handler = $session_handler;
|
2020-08-27 11:08:36 +03:00
|
|
|
|
if (
|
|
|
|
|
defined( 'PPCP_FLAG_SUBSCRIPTION' )
|
|
|
|
|
&& PPCP_FLAG_SUBSCRIPTION
|
|
|
|
|
&& $this->config->has( 'vault_enabled' )
|
|
|
|
|
&& $this->config->get( 'vault_enabled' )
|
|
|
|
|
) {
|
|
|
|
|
$this->supports = array(
|
|
|
|
|
'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',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 12:21:13 +03:00
|
|
|
|
$this->method_title = $this->define_method_title();
|
|
|
|
|
$this->method_description = $this->define_method_description();
|
2020-08-27 11:08:36 +03:00
|
|
|
|
$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,
|
|
|
|
|
array(
|
|
|
|
|
$this,
|
|
|
|
|
'process_admin_options',
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* Whether the Gateway needs to be setup.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
|
public function needs_setup(): bool {
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* Initializes the form fields.
|
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
|
public function init_form_fields() {
|
|
|
|
|
$this->form_fields = array(
|
|
|
|
|
'enabled' => array(
|
2020-09-11 10:20:12 +03:00
|
|
|
|
'title' => __( 'Enable/Disable', 'paypal-payments-for-woocommerce' ),
|
2020-09-03 08:17:45 +03:00
|
|
|
|
'type' => 'checkbox',
|
|
|
|
|
'desc_tip' => true,
|
2020-09-11 10:20:12 +03:00
|
|
|
|
'description' => __( 'In order to use PayPal or PayPal Card Processing, you need to enable the Gateway.', 'paypal-payments-for-woocommerce' ),
|
|
|
|
|
'label' => __( 'Enable the PayPal Gateway', 'paypal-payments-for-woocommerce' ),
|
2020-09-03 08:17:45 +03:00
|
|
|
|
'default' => 'no',
|
2020-08-27 11:08:36 +03:00
|
|
|
|
),
|
|
|
|
|
'ppcp' => array(
|
|
|
|
|
'type' => 'ppcp',
|
|
|
|
|
),
|
|
|
|
|
);
|
2020-09-02 13:02:31 +03:00
|
|
|
|
if ( $this->is_credit_card_tab() ) {
|
|
|
|
|
unset( $this->form_fields['enabled'] );
|
|
|
|
|
}
|
2020-08-27 11:08:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
2020-09-03 07:05:50 +03:00
|
|
|
|
* Captures an authorized payment for an WooCommerce order.
|
2020-08-28 08:13:45 +03:00
|
|
|
|
*
|
2020-09-03 07:05:50 +03:00
|
|
|
|
* @param \WC_Order $wc_order The WooCommerce order.
|
2020-08-28 08:13:45 +03:00
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function capture_authorized_payment( \WC_Order $wc_order ): bool {
|
|
|
|
|
$is_processed = $this->authorized_payments->process( $wc_order );
|
|
|
|
|
$this->render_authorization_message_for_status( $this->authorized_payments->last_status() );
|
2020-08-27 11:08:36 +03:00
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
if ( $is_processed ) {
|
|
|
|
|
$wc_order->add_order_note(
|
2020-09-11 10:20:12 +03:00
|
|
|
|
__( 'Payment successfully captured.', 'paypal-payments-for-woocommerce' )
|
2020-08-27 11:08:36 +03:00
|
|
|
|
);
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$wc_order->set_status( 'processing' );
|
|
|
|
|
$wc_order->update_meta_data( self::CAPTURED_META_KEY, 'true' );
|
|
|
|
|
$wc_order->save();
|
2020-08-27 11:08:36 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
if ( $this->authorized_payments->last_status() === AuthorizedPaymentsProcessor::ALREADY_CAPTURED ) {
|
|
|
|
|
if ( $wc_order->get_status() === 'on-hold' ) {
|
|
|
|
|
$wc_order->add_order_note(
|
2020-09-11 10:20:12 +03:00
|
|
|
|
__( 'Payment successfully captured.', 'paypal-payments-for-woocommerce' )
|
2020-08-27 11:08:36 +03:00
|
|
|
|
);
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$wc_order->set_status( 'processing' );
|
2020-08-27 11:08:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$wc_order->update_meta_data( self::CAPTURED_META_KEY, 'true' );
|
|
|
|
|
$wc_order->save();
|
2020-08-27 11:08:36 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* Displays the notice for a status.
|
|
|
|
|
*
|
|
|
|
|
* @param string $status The status.
|
|
|
|
|
*/
|
|
|
|
|
private function render_authorization_message_for_status( string $status ) {
|
2020-08-27 11:08:36 +03:00
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$message_mapping = array(
|
2020-08-27 11:08:36 +03:00
|
|
|
|
AuthorizedPaymentsProcessor::SUCCESSFUL => AuthorizeOrderActionNotice::SUCCESS,
|
|
|
|
|
AuthorizedPaymentsProcessor::ALREADY_CAPTURED => AuthorizeOrderActionNotice::ALREADY_CAPTURED,
|
|
|
|
|
AuthorizedPaymentsProcessor::INACCESSIBLE => AuthorizeOrderActionNotice::NO_INFO,
|
|
|
|
|
AuthorizedPaymentsProcessor::NOT_FOUND => AuthorizeOrderActionNotice::NOT_FOUND,
|
|
|
|
|
);
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$display_message = ( isset( $message_mapping[ $status ] ) ) ?
|
|
|
|
|
$message_mapping[ $status ]
|
2020-08-27 11:08:36 +03:00
|
|
|
|
: AuthorizeOrderActionNotice::FAILED;
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$this->notice->display_message( $display_message );
|
2020-08-27 11:08:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
|
/**
|
|
|
|
|
* Renders the settings.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2020-08-27 11:08:36 +03:00
|
|
|
|
public function generate_ppcp_html(): string {
|
|
|
|
|
|
|
|
|
|
ob_start();
|
2020-08-28 08:13:45 +03:00
|
|
|
|
$this->settings_renderer->render( false );
|
2020-08-27 11:08:36 +03:00
|
|
|
|
$content = ob_get_contents();
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
2020-09-02 12:21:13 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the method title. If we are on the credit card tab in the settings, we want to change this.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function define_method_title(): string {
|
2020-09-02 13:02:31 +03:00
|
|
|
|
if ( $this->is_credit_card_tab() ) {
|
2020-09-11 10:20:12 +03:00
|
|
|
|
return __( 'PayPal Card Processing', 'paypal-payments-for-woocommerce' );
|
2020-09-02 12:21:13 +03:00
|
|
|
|
}
|
2020-09-03 07:16:52 +03:00
|
|
|
|
if ( $this->is_paypal_tab() ) {
|
2020-09-11 10:20:12 +03:00
|
|
|
|
return __( 'PayPal Checkout', 'paypal-payments-for-woocommerce' );
|
2020-09-03 07:16:52 +03:00
|
|
|
|
}
|
2020-09-11 10:20:12 +03:00
|
|
|
|
return __( 'PayPal', 'paypal-payments-for-woocommerce' );
|
2020-09-02 12:21:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the method description. If we are on the credit card tab in the settings, we want to change this.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function define_method_description(): string {
|
2020-09-02 13:02:31 +03:00
|
|
|
|
if ( $this->is_credit_card_tab() ) {
|
2020-09-02 12:21:13 +03:00
|
|
|
|
return __(
|
|
|
|
|
'Accept debit and credit cards, and local payment methods with PayPal’s latest solution.',
|
2020-09-11 10:20:12 +03:00
|
|
|
|
'paypal-payments-for-woocommerce'
|
2020-09-02 12:21:13 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return __(
|
|
|
|
|
'Accept PayPal, PayPal Credit and alternative payment types with PayPal’s latest solution.',
|
2020-09-11 10:20:12 +03:00
|
|
|
|
'paypal-payments-for-woocommerce'
|
2020-09-02 12:21:13 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
2020-09-02 13:02:31 +03:00
|
|
|
|
|
|
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended
|
2020-09-02 14:53:30 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines, whether the current session is on the credit card tab in the admin settings.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function is_credit_card_tab() : bool {
|
2020-09-03 07:16:52 +03:00
|
|
|
|
return is_admin()
|
|
|
|
|
&& isset( $_GET[ SectionsRenderer::KEY ] )
|
|
|
|
|
&& CreditCardGateway::ID === sanitize_text_field( wp_unslash( $_GET[ SectionsRenderer::KEY ] ) );
|
|
|
|
|
|
|
|
|
|
}
|
2020-09-02 13:02:31 +03:00
|
|
|
|
|
2020-09-03 07:16:52 +03:00
|
|
|
|
/**
|
|
|
|
|
* Whether we are on the PayPal settings tab.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function is_paypal_tab() : bool {
|
|
|
|
|
return ! $this->is_credit_card_tab()
|
|
|
|
|
&& is_admin()
|
|
|
|
|
&& isset( $_GET['section'] )
|
|
|
|
|
&& self::ID === sanitize_text_field( wp_unslash( $_GET['section'] ) );
|
2020-09-02 13:02:31 +03:00
|
|
|
|
}
|
2020-09-02 12:41:10 +03:00
|
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
2020-04-06 11:16:18 +03:00
|
|
|
|
}
|