mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Merge branch 'trunk' into PCP-1393-update-to-vault-v-3
# Conflicts: # modules/ppcp-api-client/services.php # modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php # tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php
This commit is contained in:
commit
fb1ceeba76
27 changed files with 658 additions and 231 deletions
|
@ -97,7 +97,9 @@ return array(
|
|||
$payment_token_repository,
|
||||
$logger,
|
||||
$api_shop_country,
|
||||
$container->get( 'api.endpoint.order' )
|
||||
$container->get( 'api.endpoint.order' ),
|
||||
$container->get( 'api.factory.paypal-checkout-url' ),
|
||||
$container->get( 'wcgateway.place-order-button-text' )
|
||||
);
|
||||
},
|
||||
'wcgateway.credit-card-gateway' => static function ( ContainerInterface $container ): CreditCardGateway {
|
||||
|
@ -141,7 +143,9 @@ return array(
|
|||
$container->get( 'wcgateway.settings.allow_card_button_gateway.default' ),
|
||||
$container->get( 'onboarding.environment' ),
|
||||
$container->get( 'vaulting.repository.payment-token' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||
$container->get( 'api.factory.paypal-checkout-url' ),
|
||||
$container->get( 'wcgateway.place-order-button-text' )
|
||||
);
|
||||
},
|
||||
'wcgateway.disabler' => static function ( ContainerInterface $container ): DisableGateways {
|
||||
|
@ -209,7 +213,9 @@ return array(
|
|||
static function ( ContainerInterface $container ): Settings {
|
||||
return new Settings(
|
||||
$container->get( 'wcgateway.button.default-locations' ),
|
||||
$container->get( 'wcgateway.settings.dcc-gateway-title.default' )
|
||||
$container->get( 'wcgateway.settings.dcc-gateway-title.default' ),
|
||||
$container->get( 'wcgateway.settings.pay-later.default-button-locations' ),
|
||||
$container->get( 'wcgateway.settings.pay-later.default-messaging-locations' )
|
||||
);
|
||||
}
|
||||
),
|
||||
|
@ -350,7 +356,10 @@ return array(
|
|||
$logger,
|
||||
$environment,
|
||||
$subscription_helper,
|
||||
$order_helper
|
||||
$order_helper,
|
||||
$container->get( 'api.factory.purchase-unit' ),
|
||||
$container->get( 'api.factory.payer' ),
|
||||
$container->get( 'api.factory.shipping-preference' )
|
||||
);
|
||||
},
|
||||
'wcgateway.processor.refunds' => static function ( ContainerInterface $container ): RefundProcessor {
|
||||
|
@ -1156,6 +1165,25 @@ return array(
|
|||
);
|
||||
},
|
||||
|
||||
'wcgateway.use-place-order-button' => function ( ContainerInterface $container ) : bool {
|
||||
/**
|
||||
* Whether to use the standard "Place order" button with redirect to PayPal instead of the PayPal smart buttons.
|
||||
*/
|
||||
return apply_filters(
|
||||
'woocommerce_paypal_payments_use_place_order_button',
|
||||
false
|
||||
);
|
||||
},
|
||||
'wcgateway.place-order-button-text' => function ( ContainerInterface $container ) : string {
|
||||
/**
|
||||
* The text for the standard "Place order" button, when the "Place order" button mode is enabled.
|
||||
*/
|
||||
return apply_filters(
|
||||
'woocommerce_paypal_payments_place_order_button_text',
|
||||
__( 'Pay with PayPal', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
},
|
||||
|
||||
'wcgateway.helper.vaulting-scope' => static function ( ContainerInterface $container ): bool {
|
||||
try {
|
||||
$token = $container->get( 'api.bearer' )->bearer();
|
||||
|
@ -1360,6 +1388,11 @@ return array(
|
|||
'mini-cart' => 'Mini Cart',
|
||||
);
|
||||
},
|
||||
'wcgateway.button.default-locations' => static function( ContainerInterface $container ): array {
|
||||
$button_locations = $container->get( 'wcgateway.button.locations' );
|
||||
unset( $button_locations['mini-cart'] );
|
||||
return array_keys( $button_locations );
|
||||
},
|
||||
'wcgateway.settings.pay-later.messaging-locations' => static function( ContainerInterface $container ): array {
|
||||
$button_locations = $container->get( 'wcgateway.button.locations' );
|
||||
unset( $button_locations['mini-cart'] );
|
||||
|
@ -1371,8 +1404,10 @@ return array(
|
|||
)
|
||||
);
|
||||
},
|
||||
'wcgateway.button.default-locations' => static function( ContainerInterface $container ): array {
|
||||
return array_keys( $container->get( 'wcgateway.settings.pay-later.messaging-locations' ) );
|
||||
'wcgateway.settings.pay-later.default-messaging-locations' => static function( ContainerInterface $container ): array {
|
||||
$locations = $container->get( 'wcgateway.settings.pay-later.messaging-locations' );
|
||||
unset( $locations['home'] );
|
||||
return array_keys( $locations );
|
||||
},
|
||||
'wcgateway.settings.pay-later.button-locations' => static function( ContainerInterface $container ): array {
|
||||
$settings = $container->get( 'wcgateway.settings' );
|
||||
|
@ -1384,6 +1419,9 @@ return array(
|
|||
|
||||
return array_intersect_key( $button_locations, array_flip( $smart_button_selected_locations ) );
|
||||
},
|
||||
'wcgateway.settings.pay-later.default-button-locations' => static function( ContainerInterface $container ): array {
|
||||
return $container->get( 'wcgateway.button.default-locations' );
|
||||
},
|
||||
'wcgateway.ppcp-gateways' => static function ( ContainerInterface $container ): array {
|
||||
return array(
|
||||
PayPalGateway::ID,
|
||||
|
|
|
@ -184,7 +184,7 @@ class SettingsPageAssets {
|
|||
}
|
||||
|
||||
$screen = get_current_screen();
|
||||
if ( $screen->id !== 'woocommerce_page_wc-settings' ) {
|
||||
if ( ! $screen || $screen->id !== 'woocommerce_page_wc-settings' ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,14 +85,18 @@ class ReturnUrlEndpoint {
|
|||
// phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
$order = $this->order_endpoint->order( $token );
|
||||
|
||||
if ( $order->status()->is( OrderStatus::APPROVED )
|
||||
|| $order->status()->is( OrderStatus::COMPLETED )
|
||||
) {
|
||||
$this->session_handler->replace_order( $order );
|
||||
}
|
||||
|
||||
$wc_order_id = (int) $order->purchase_units()[0]->custom_id();
|
||||
if ( ! $wc_order_id ) {
|
||||
// We cannot finish processing here without WC order, but at least go into the continuation mode.
|
||||
if ( $order->status()->is( OrderStatus::APPROVED )
|
||||
|| $order->status()->is( OrderStatus::COMPLETED )
|
||||
) {
|
||||
$this->session_handler->replace_order( $order );
|
||||
|
||||
wp_safe_redirect( wc_get_checkout_url() );
|
||||
exit();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
/**
|
||||
* Thrown when there is no PayPal order during WC order processing.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\WcGateway\Exception
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\WcGateway\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class PayPalOrderMissingException
|
||||
*/
|
||||
class PayPalOrderMissingException extends Exception {
|
||||
}
|
|
@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\WcSubscriptions\FreeTrialHandlerTrait;
|
|||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\GatewayGenericException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
|
@ -126,21 +127,30 @@ class CardButtonGateway extends \WC_Payment_Gateway {
|
|||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* The function return the PayPal checkout URL for the given order ID.
|
||||
*
|
||||
* @var callable(string):string
|
||||
*/
|
||||
private $paypal_checkout_url_factory;
|
||||
|
||||
/**
|
||||
* CardButtonGateway constructor.
|
||||
*
|
||||
* @param SettingsRenderer $settings_renderer The Settings Renderer.
|
||||
* @param OrderProcessor $order_processor The Order Processor.
|
||||
* @param ContainerInterface $config The settings.
|
||||
* @param SessionHandler $session_handler The Session Handler.
|
||||
* @param RefundProcessor $refund_processor The Refund Processor.
|
||||
* @param State $state The state.
|
||||
* @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param bool $default_enabled Whether the gateway should be enabled by default.
|
||||
* @param Environment $environment The environment.
|
||||
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param SettingsRenderer $settings_renderer The Settings Renderer.
|
||||
* @param OrderProcessor $order_processor The Order Processor.
|
||||
* @param ContainerInterface $config The settings.
|
||||
* @param SessionHandler $session_handler The Session Handler.
|
||||
* @param RefundProcessor $refund_processor The Refund Processor.
|
||||
* @param State $state The state.
|
||||
* @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param bool $default_enabled Whether the gateway should be enabled by default.
|
||||
* @param Environment $environment The environment.
|
||||
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID.
|
||||
* @param string $place_order_button_text The text for the standard "Place order" button.
|
||||
*/
|
||||
public function __construct(
|
||||
SettingsRenderer $settings_renderer,
|
||||
|
@ -154,22 +164,26 @@ class CardButtonGateway extends \WC_Payment_Gateway {
|
|||
bool $default_enabled,
|
||||
Environment $environment,
|
||||
PaymentTokenRepository $payment_token_repository,
|
||||
LoggerInterface $logger
|
||||
LoggerInterface $logger,
|
||||
callable $paypal_checkout_url_factory,
|
||||
string $place_order_button_text
|
||||
) {
|
||||
$this->id = self::ID;
|
||||
$this->settings_renderer = $settings_renderer;
|
||||
$this->order_processor = $order_processor;
|
||||
$this->config = $config;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->refund_processor = $refund_processor;
|
||||
$this->state = $state;
|
||||
$this->transaction_url_provider = $transaction_url_provider;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->default_enabled = $default_enabled;
|
||||
$this->environment = $environment;
|
||||
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
|
||||
$this->payment_token_repository = $payment_token_repository;
|
||||
$this->logger = $logger;
|
||||
$this->id = self::ID;
|
||||
$this->settings_renderer = $settings_renderer;
|
||||
$this->order_processor = $order_processor;
|
||||
$this->config = $config;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->refund_processor = $refund_processor;
|
||||
$this->state = $state;
|
||||
$this->transaction_url_provider = $transaction_url_provider;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->default_enabled = $default_enabled;
|
||||
$this->environment = $environment;
|
||||
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
|
||||
$this->payment_token_repository = $payment_token_repository;
|
||||
$this->logger = $logger;
|
||||
$this->paypal_checkout_url_factory = $paypal_checkout_url_factory;
|
||||
$this->order_button_text = $place_order_button_text;
|
||||
|
||||
$this->supports = array(
|
||||
'refunds',
|
||||
|
@ -294,18 +308,20 @@ class CardButtonGateway extends \WC_Payment_Gateway {
|
|||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
try {
|
||||
if ( ! $this->order_processor->process( $wc_order ) ) {
|
||||
return $this->handle_payment_failure(
|
||||
$wc_order,
|
||||
new Exception(
|
||||
$this->order_processor->last_error()
|
||||
)
|
||||
try {
|
||||
$this->order_processor->process( $wc_order );
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
|
||||
|
||||
return $this->handle_payment_success( $wc_order );
|
||||
} catch ( PayPalOrderMissingException $exc ) {
|
||||
$order = $this->order_processor->create_order( $wc_order );
|
||||
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => ( $this->paypal_checkout_url_factory )( $order->id() ),
|
||||
);
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
|
||||
|
||||
return $this->handle_payment_success( $wc_order );
|
||||
} catch ( PayPalApiException $error ) {
|
||||
return $this->handle_payment_failure(
|
||||
$wc_order,
|
||||
|
@ -315,7 +331,7 @@ class CardButtonGateway extends \WC_Payment_Gateway {
|
|||
$error
|
||||
)
|
||||
);
|
||||
} catch ( RuntimeException $error ) {
|
||||
} catch ( Exception $error ) {
|
||||
return $this->handle_payment_failure( $wc_order, $error );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -394,14 +394,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
//phpcs:enable WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
try {
|
||||
if ( ! $this->order_processor->process( $wc_order ) ) {
|
||||
return $this->handle_payment_failure(
|
||||
$wc_order,
|
||||
new Exception(
|
||||
$this->order_processor->last_error()
|
||||
)
|
||||
);
|
||||
}
|
||||
$this->order_processor->process( $wc_order );
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
|
||||
|
||||
|
@ -415,7 +408,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
|||
$error
|
||||
)
|
||||
);
|
||||
} catch ( RuntimeException $error ) {
|
||||
} catch ( Exception $error ) {
|
||||
return $this->handle_payment_failure( $wc_order, $error );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ use WooCommerce\PayPalCommerce\WcSubscriptions\FreeTrialHandlerTrait;
|
|||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\GatewayGenericException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
|
||||
|
@ -163,24 +164,33 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
*/
|
||||
private $order_endpoint;
|
||||
|
||||
/**
|
||||
* The function return the PayPal checkout URL for the given order ID.
|
||||
*
|
||||
* @var callable(string):string
|
||||
*/
|
||||
private $paypal_checkout_url_factory;
|
||||
|
||||
/**
|
||||
* PayPalGateway constructor.
|
||||
*
|
||||
* @param SettingsRenderer $settings_renderer The Settings Renderer.
|
||||
* @param FundingSourceRenderer $funding_source_renderer The funding source renderer.
|
||||
* @param OrderProcessor $order_processor The Order Processor.
|
||||
* @param ContainerInterface $config The settings.
|
||||
* @param SessionHandler $session_handler The Session Handler.
|
||||
* @param RefundProcessor $refund_processor The Refund Processor.
|
||||
* @param State $state The state.
|
||||
* @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page.
|
||||
* @param Environment $environment The environment.
|
||||
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $api_shop_country The api shop country.
|
||||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
||||
* @param SettingsRenderer $settings_renderer The Settings Renderer.
|
||||
* @param FundingSourceRenderer $funding_source_renderer The funding source renderer.
|
||||
* @param OrderProcessor $order_processor The Order Processor.
|
||||
* @param ContainerInterface $config The settings.
|
||||
* @param SessionHandler $session_handler The Session Handler.
|
||||
* @param RefundProcessor $refund_processor The Refund Processor.
|
||||
* @param State $state The state.
|
||||
* @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page.
|
||||
* @param Environment $environment The environment.
|
||||
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param string $api_shop_country The api shop country.
|
||||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
||||
* @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID.
|
||||
* @param string $place_order_button_text The text for the standard "Place order" button.
|
||||
*/
|
||||
public function __construct(
|
||||
SettingsRenderer $settings_renderer,
|
||||
|
@ -197,24 +207,28 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
PaymentTokenRepository $payment_token_repository,
|
||||
LoggerInterface $logger,
|
||||
string $api_shop_country,
|
||||
OrderEndpoint $order_endpoint
|
||||
OrderEndpoint $order_endpoint,
|
||||
callable $paypal_checkout_url_factory,
|
||||
string $place_order_button_text
|
||||
) {
|
||||
$this->id = self::ID;
|
||||
$this->settings_renderer = $settings_renderer;
|
||||
$this->funding_source_renderer = $funding_source_renderer;
|
||||
$this->order_processor = $order_processor;
|
||||
$this->config = $config;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->refund_processor = $refund_processor;
|
||||
$this->state = $state;
|
||||
$this->transaction_url_provider = $transaction_url_provider;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->page_id = $page_id;
|
||||
$this->environment = $environment;
|
||||
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
|
||||
$this->payment_token_repository = $payment_token_repository;
|
||||
$this->logger = $logger;
|
||||
$this->api_shop_country = $api_shop_country;
|
||||
$this->id = self::ID;
|
||||
$this->settings_renderer = $settings_renderer;
|
||||
$this->funding_source_renderer = $funding_source_renderer;
|
||||
$this->order_processor = $order_processor;
|
||||
$this->config = $config;
|
||||
$this->session_handler = $session_handler;
|
||||
$this->refund_processor = $refund_processor;
|
||||
$this->state = $state;
|
||||
$this->transaction_url_provider = $transaction_url_provider;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->page_id = $page_id;
|
||||
$this->environment = $environment;
|
||||
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
|
||||
$this->payment_token_repository = $payment_token_repository;
|
||||
$this->logger = $logger;
|
||||
$this->api_shop_country = $api_shop_country;
|
||||
$this->paypal_checkout_url_factory = $paypal_checkout_url_factory;
|
||||
$this->order_button_text = $place_order_button_text;
|
||||
|
||||
if ( $this->onboarded ) {
|
||||
$this->supports = array( 'refunds', 'tokenization' );
|
||||
|
@ -546,19 +560,20 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
|
||||
return $this->handle_payment_success( $wc_order );
|
||||
}
|
||||
try {
|
||||
$this->order_processor->process( $wc_order );
|
||||
|
||||
if ( ! $this->order_processor->process( $wc_order ) ) {
|
||||
return $this->handle_payment_failure(
|
||||
$wc_order,
|
||||
new Exception(
|
||||
$this->order_processor->last_error()
|
||||
)
|
||||
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
|
||||
|
||||
return $this->handle_payment_success( $wc_order );
|
||||
} catch ( PayPalOrderMissingException $exc ) {
|
||||
$order = $this->order_processor->create_order( $wc_order );
|
||||
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => ( $this->paypal_checkout_url_factory )( $order->id() ),
|
||||
);
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order );
|
||||
|
||||
return $this->handle_payment_success( $wc_order );
|
||||
} catch ( PayPalApiException $error ) {
|
||||
$retry_keys_messages = array(
|
||||
'INSTRUMENT_DECLINED' => __( 'Instrument declined.', 'woocommerce-paypal-payments' ),
|
||||
|
@ -593,12 +608,9 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
);
|
||||
}
|
||||
|
||||
$host = $this->config->has( 'sandbox_on' ) && $this->config->get( 'sandbox_on' ) ?
|
||||
'https://www.sandbox.paypal.com/' : 'https://www.paypal.com/';
|
||||
$url = $host . 'checkoutnow?token=' . $this->session_handler->order()->id();
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => $url,
|
||||
'redirect' => ( $this->paypal_checkout_url_factory )( $this->session_handler->order()->id() ),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -610,7 +622,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
|
|||
$error
|
||||
)
|
||||
);
|
||||
} catch ( RuntimeException $error ) {
|
||||
} catch ( Exception $error ) {
|
||||
return $this->handle_payment_failure( $wc_order, $error );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,20 +9,26 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
|
||||
|
||||
use Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WC_Order;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderHelper;
|
||||
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
|
@ -89,13 +95,6 @@ class OrderProcessor {
|
|||
*/
|
||||
private $settings;
|
||||
|
||||
/**
|
||||
* The last error.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $last_error = '';
|
||||
|
||||
/**
|
||||
* A logger.
|
||||
*
|
||||
|
@ -117,6 +116,27 @@ class OrderProcessor {
|
|||
*/
|
||||
private $order_helper;
|
||||
|
||||
/**
|
||||
* The PurchaseUnit factory.
|
||||
*
|
||||
* @var PurchaseUnitFactory
|
||||
*/
|
||||
private $purchase_unit_factory;
|
||||
|
||||
/**
|
||||
* The payer factory.
|
||||
*
|
||||
* @var PayerFactory
|
||||
*/
|
||||
private $payer_factory;
|
||||
|
||||
/**
|
||||
* The shipping_preference factory.
|
||||
*
|
||||
* @var ShippingPreferenceFactory
|
||||
*/
|
||||
private $shipping_preference_factory;
|
||||
|
||||
/**
|
||||
* Array to store temporary order data changes to restore after processing.
|
||||
*
|
||||
|
@ -137,6 +157,9 @@ class OrderProcessor {
|
|||
* @param Environment $environment The environment.
|
||||
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
||||
* @param OrderHelper $order_helper The order helper.
|
||||
* @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory.
|
||||
* @param PayerFactory $payer_factory The payer factory.
|
||||
* @param ShippingPreferenceFactory $shipping_preference_factory The shipping_preference factory.
|
||||
*/
|
||||
public function __construct(
|
||||
SessionHandler $session_handler,
|
||||
|
@ -148,7 +171,10 @@ class OrderProcessor {
|
|||
LoggerInterface $logger,
|
||||
Environment $environment,
|
||||
SubscriptionHelper $subscription_helper,
|
||||
OrderHelper $order_helper
|
||||
OrderHelper $order_helper,
|
||||
PurchaseUnitFactory $purchase_unit_factory,
|
||||
PayerFactory $payer_factory,
|
||||
ShippingPreferenceFactory $shipping_preference_factory
|
||||
) {
|
||||
|
||||
$this->session_handler = $session_handler;
|
||||
|
@ -161,60 +187,57 @@ class OrderProcessor {
|
|||
$this->logger = $logger;
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->order_helper = $order_helper;
|
||||
$this->purchase_unit_factory = $purchase_unit_factory;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->shipping_preference_factory = $shipping_preference_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a given WooCommerce order and captured/authorizes the connected PayPal orders.
|
||||
*
|
||||
* @param \WC_Order $wc_order The WooCommerce order.
|
||||
* @param WC_Order $wc_order The WooCommerce order.
|
||||
*
|
||||
* @return bool
|
||||
* @throws PayPalOrderMissingException If no PayPal order.
|
||||
* @throws Exception If processing fails.
|
||||
*/
|
||||
public function process( \WC_Order $wc_order ): bool {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification
|
||||
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) );
|
||||
$order = $this->session_handler->order();
|
||||
if ( ! $order && is_string( $order_id ) && $order_id ) {
|
||||
$order = $this->order_endpoint->order( $order_id );
|
||||
}
|
||||
public function process( WC_Order $wc_order ): void {
|
||||
$order = $this->session_handler->order();
|
||||
if ( ! $order ) {
|
||||
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
|
||||
if ( ! $order_id ) {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification
|
||||
$order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) );
|
||||
if ( is_string( $order_id ) && $order_id ) {
|
||||
try {
|
||||
$order = $this->order_endpoint->order( $order_id );
|
||||
} catch ( RuntimeException $exception ) {
|
||||
throw new Exception( __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' ) );
|
||||
}
|
||||
} else {
|
||||
$this->logger->warning(
|
||||
sprintf(
|
||||
'No PayPal order ID found in order #%d meta.',
|
||||
$wc_order->get_id()
|
||||
)
|
||||
);
|
||||
$this->last_error = __( 'Could not retrieve order. Maybe it was already completed or this browser is not supported. Please check your email or try again with a different browser.', 'woocommerce-paypal-payments' );
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$order = $this->order_endpoint->order( $order_id );
|
||||
} catch ( RuntimeException $exception ) {
|
||||
$this->last_error = __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' );
|
||||
return false;
|
||||
throw new PayPalOrderMissingException(
|
||||
__(
|
||||
'Could not retrieve order. Maybe it was already completed or this browser is not supported. Please check your email or try again with a different browser.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->add_paypal_meta( $wc_order, $order, $this->environment );
|
||||
|
||||
$error_message = null;
|
||||
if ( $this->order_helper->contains_physical_goods( $order ) && ! $this->order_is_ready_for_process( $order ) ) {
|
||||
$error_message = __(
|
||||
'The payment is not ready for processing yet.',
|
||||
'woocommerce-paypal-payments'
|
||||
throw new Exception(
|
||||
__(
|
||||
'The payment is not ready for processing yet.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
if ( $error_message ) {
|
||||
$this->last_error = sprintf(
|
||||
// translators: %s is the message of the error.
|
||||
__( 'Payment error: %s', 'woocommerce-paypal-payments' ),
|
||||
$error_message
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$order = $this->patch_order( $wc_order, $order );
|
||||
|
||||
|
@ -245,9 +268,28 @@ class OrderProcessor {
|
|||
}
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_after_order_processor', $wc_order, $order );
|
||||
}
|
||||
|
||||
$this->last_error = '';
|
||||
return true;
|
||||
/**
|
||||
* Creates a PayPal order for the given WC order.
|
||||
*
|
||||
* @param WC_Order $wc_order The WC order.
|
||||
* @return Order
|
||||
* @throws RuntimeException If order creation fails.
|
||||
*/
|
||||
public function create_order( WC_Order $wc_order ): Order {
|
||||
$pu = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
||||
$shipping_preference = $this->shipping_preference_factory->from_state( $pu, 'checkout' );
|
||||
$order = $this->order_endpoint->create(
|
||||
array( $pu ),
|
||||
$shipping_preference,
|
||||
$this->payer_factory->from_wc_order( $wc_order ),
|
||||
null,
|
||||
'',
|
||||
ApplicationContext::USER_ACTION_PAY_NOW
|
||||
);
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -284,25 +326,15 @@ class OrderProcessor {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last error.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function last_error(): string {
|
||||
|
||||
return $this->last_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Patches a given PayPal order with a WooCommerce order.
|
||||
*
|
||||
* @param \WC_Order $wc_order The WooCommerce order.
|
||||
* @param Order $order The PayPal order.
|
||||
* @param WC_Order $wc_order The WooCommerce order.
|
||||
* @param Order $order The PayPal order.
|
||||
*
|
||||
* @return Order
|
||||
*/
|
||||
public function patch_order( \WC_Order $wc_order, Order $order ): Order {
|
||||
public function patch_order( WC_Order $wc_order, Order $order ): Order {
|
||||
$this->apply_outbound_order_filters( $wc_order );
|
||||
$updated_order = $this->order_factory->from_wc_order( $wc_order, $order );
|
||||
$this->restore_order_from_filters( $wc_order );
|
||||
|
|
|
@ -73,7 +73,7 @@ return function ( ContainerInterface $container, array $fields ): array {
|
|||
'type' => 'ppcp-multiselect',
|
||||
'class' => array(),
|
||||
'input_class' => array( 'wc-enhanced-select' ),
|
||||
'default' => $container->get( 'wcgateway.button.default-locations' ),
|
||||
'default' => $container->get( 'wcgateway.settings.pay-later.default-button-locations' ),
|
||||
'desc_tip' => false,
|
||||
'description' => __( 'Select where the Pay Later button should be displayed.', 'woocommerce-paypal-payments' ),
|
||||
'options' => $container->get( 'wcgateway.settings.pay-later.button-locations' ),
|
||||
|
@ -119,7 +119,7 @@ return function ( ContainerInterface $container, array $fields ): array {
|
|||
'type' => 'ppcp-multiselect',
|
||||
'class' => array(),
|
||||
'input_class' => array( 'wc-enhanced-select' ),
|
||||
'default' => $container->get( 'wcgateway.button.default-locations' ),
|
||||
'default' => $container->get( 'wcgateway.settings.pay-later.default-messaging-locations' ),
|
||||
'desc_tip' => false,
|
||||
'description' => __( 'Select where the Pay Later messaging should be displayed.', 'woocommerce-paypal-payments' ),
|
||||
'options' => $container->get( 'wcgateway.settings.pay-later.messaging-locations' ),
|
||||
|
|
|
@ -35,6 +35,20 @@ class Settings implements ContainerInterface {
|
|||
*/
|
||||
protected $default_button_locations;
|
||||
|
||||
/**
|
||||
* The list of selected default pay later button locations.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $default_pay_later_button_locations;
|
||||
|
||||
/**
|
||||
* The list of selected default pay later messaging locations.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $default_pay_later_messaging_locations;
|
||||
|
||||
/**
|
||||
* The default ACDC gateway title.
|
||||
*
|
||||
|
@ -47,10 +61,19 @@ class Settings implements ContainerInterface {
|
|||
*
|
||||
* @param string[] $default_button_locations The list of selected default button locations.
|
||||
* @param string $default_dcc_gateway_title The default ACDC gateway title.
|
||||
* @param string[] $default_pay_later_button_locations The list of selected default pay later button locations.
|
||||
* @param string[] $default_pay_later_messaging_locations The list of selected default pay later messaging locations.
|
||||
*/
|
||||
public function __construct( array $default_button_locations, string $default_dcc_gateway_title ) {
|
||||
$this->default_button_locations = $default_button_locations;
|
||||
$this->default_dcc_gateway_title = $default_dcc_gateway_title;
|
||||
public function __construct(
|
||||
array $default_button_locations,
|
||||
string $default_dcc_gateway_title,
|
||||
array $default_pay_later_button_locations,
|
||||
array $default_pay_later_messaging_locations
|
||||
) {
|
||||
$this->default_button_locations = $default_button_locations;
|
||||
$this->default_dcc_gateway_title = $default_dcc_gateway_title;
|
||||
$this->default_pay_later_button_locations = $default_pay_later_button_locations;
|
||||
$this->default_pay_later_messaging_locations = $default_pay_later_messaging_locations;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -122,8 +145,8 @@ class Settings implements ContainerInterface {
|
|||
'smart_button_enable_styling_per_location' => true,
|
||||
'pay_later_messaging_enabled' => true,
|
||||
'pay_later_button_enabled' => true,
|
||||
'pay_later_button_locations' => $this->default_button_locations,
|
||||
'pay_later_messaging_locations' => $this->default_button_locations,
|
||||
'pay_later_button_locations' => $this->default_pay_later_button_locations,
|
||||
'pay_later_messaging_locations' => $this->default_pay_later_messaging_locations,
|
||||
'brand_name' => get_bloginfo( 'name' ),
|
||||
'dcc_gateway_title' => $this->default_dcc_gateway_title,
|
||||
'dcc_gateway_description' => __(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue