Move capture authorized payment into authorized payment processor

This commit is contained in:
dinamiko 2021-10-14 15:45:57 +02:00
parent ab0ed12495
commit 457ebeeb8c
15 changed files with 271 additions and 376 deletions

View file

@ -18,7 +18,7 @@ use Automattic\WooCommerce\Admin\Notes\NoteTraits;
*/
class DeactivateNote {
use NoteTraits;
//use NoteTraits;
/**
* Name of the note for use in the database.

View file

@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
/**
* Class RenewalHandler
@ -240,7 +241,7 @@ class RenewalHandler {
if ( $order->intent() === 'AUTHORIZE' ) {
$this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'false' );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
}
}
}

View file

@ -46,7 +46,6 @@ return array(
$order_processor = $container->get( 'wcgateway.order-processor' );
$settings_renderer = $container->get( 'wcgateway.settings.render' );
$authorized_payments = $container->get( 'wcgateway.processor.authorized-payments' );
$notice = $container->get( 'wcgateway.notice.authorize-order-action' );
$settings = $container->get( 'wcgateway.settings' );
$session_handler = $container->get( 'session.handler' );
$refund_processor = $container->get( 'wcgateway.processor.refunds' );
@ -63,7 +62,6 @@ return array(
$settings_renderer,
$order_processor,
$authorized_payments,
$notice,
$settings,
$session_handler,
$refund_processor,
@ -82,7 +80,6 @@ return array(
$order_processor = $container->get( 'wcgateway.order-processor' );
$settings_renderer = $container->get( 'wcgateway.settings.render' );
$authorized_payments = $container->get( 'wcgateway.processor.authorized-payments' );
$notice = $container->get( 'wcgateway.notice.authorize-order-action' );
$settings = $container->get( 'wcgateway.settings' );
$module_url = $container->get( 'wcgateway.url' );
$session_handler = $container->get( 'session.handler' );
@ -101,7 +98,6 @@ return array(
$settings_renderer,
$order_processor,
$authorized_payments,
$notice,
$settings,
$module_url,
$session_handler,
@ -236,7 +232,8 @@ return array(
$order_endpoint = $container->get( 'api.endpoint.order' );
$payments_endpoint = $container->get( 'api.endpoint.payments' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
return new AuthorizedPaymentsProcessor( $order_endpoint, $payments_endpoint, $logger );
$notice = $container->get( 'wcgateway.notice.authorize-order-action' );
return new AuthorizedPaymentsProcessor( $order_endpoint, $payments_endpoint, $logger, $notice );
},
'wcgateway.admin.render-authorize-action' => static function ( ContainerInterface $container ): RenderAuthorizeAction {

View file

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Admin;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
@ -102,7 +103,7 @@ class OrderTablePaymentStatusColumn {
*/
public function should_render_for_order( \WC_Order $order ): bool {
$intent = $order->get_meta( PayPalGateway::INTENT_META_KEY );
$captured = $order->get_meta( PayPalGateway::CAPTURED_META_KEY );
$captured = $order->get_meta( AuthorizedPaymentsProcessor::CAPTURED_META_KEY );
$status = $order->get_status();
$not_allowed_statuses = array( 'refunded' );
return ! empty( $intent ) && strtoupper( self::INTENT ) === strtoupper( $intent ) &&
@ -118,7 +119,7 @@ class OrderTablePaymentStatusColumn {
* @return bool
*/
public function is_captured( \WC_Order $wc_order ): bool {
$captured = $wc_order->get_meta( PayPalGateway::CAPTURED_META_KEY );
$captured = $wc_order->get_meta( AuthorizedPaymentsProcessor::CAPTURED_META_KEY );
return wc_string_to_bool( $captured );
}

View file

@ -10,6 +10,7 @@ declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Admin;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
/**
* Class RenderAuthorizeAction
@ -45,7 +46,7 @@ class RenderAuthorizeAction {
* @return bool
*/
private function should_render_for_order( \WC_Order $order ) : bool {
$data = $order->get_meta( PayPalGateway::CAPTURED_META_KEY );
$data = $order->get_meta( AuthorizedPaymentsProcessor::CAPTURED_META_KEY );
return in_array( $data, array( 'true', 'false' ), true );
}
}

View file

@ -19,7 +19,6 @@ use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
@ -36,32 +35,32 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
const ID = 'ppcp-credit-card-gateway';
/**
* Service to get transaction url for an order.
* The Settings Renderer.
*
* @var TransactionUrlProvider
* @var SettingsRenderer
*/
protected $transaction_url_provider;
protected $settings_renderer;
/**
* The subscription helper.
* The processor for orders.
*
* @var SubscriptionHelper
* @var OrderProcessor
*/
protected $subscription_helper;
protected $order_processor;
/**
* The logger.
* The processor for authorized payments.
*
* @var LoggerInterface
* @var AuthorizedPaymentsProcessor
*/
protected $logger;
protected $authorized_payments_processor;
/**
* The payments endpoint
* The settings.
*
* @var PaymentsEndpoint
* @var ContainerInterface
*/
protected $payments_endpoint;
protected $config;
/**
* The URL to the module.
@ -70,6 +69,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
private $module_url;
/**
* The Session Handler.
*
* @var SessionHandler
*/
protected $session_handler;
/**
* The refund processor.
*
@ -77,6 +83,20 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
private $refund_processor;
/**
* The state.
*
* @var State
*/
protected $state;
/**
* Service to get transaction url for an order.
*
* @var TransactionUrlProvider
*/
protected $transaction_url_provider;
/**
* The payment token repository.
*
@ -105,6 +125,20 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
private $order_endpoint;
/**
* The subscription helper.
*
* @var SubscriptionHelper
*/
protected $subscription_helper;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* The environment.
*
@ -112,13 +146,19 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
protected $environment;
/**
* The payments endpoint
*
* @var PaymentsEndpoint
*/
protected $payments_endpoint;
/**
* CreditCardGateway 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 Notices.
* @param ContainerInterface $config The settings.
* @param string $module_url The URL to the module.
* @param SessionHandler $session_handler The Session Handler.
@ -138,7 +178,6 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
SettingsRenderer $settings_renderer,
OrderProcessor $order_processor,
AuthorizedPaymentsProcessor $authorized_payments_processor,
AuthorizeOrderActionNotice $notice,
ContainerInterface $config,
string $module_url,
SessionHandler $session_handler,
@ -157,8 +196,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->id = self::ID;
$this->order_processor = $order_processor;
$this->authorized_payments = $authorized_payments_processor;
$this->notice = $notice;
$this->authorized_payments_processor = $authorized_payments_processor;
$this->settings_renderer = $settings_renderer;
$this->config = $config;
$this->session_handler = $session_handler;
@ -223,6 +261,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->subscription_helper = $subscription_helper;
$this->logger = $logger;
$this->payments_endpoint = $payments_endpoint;
$this->state = $state;
}
/**

View file

@ -9,7 +9,6 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
@ -18,7 +17,6 @@ use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor;
@ -34,7 +32,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
use ProcessPaymentTrait;
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';
const ORDER_PAYMENT_MODE_META_KEY = '_ppcp_paypal_payment_mode';
@ -46,20 +43,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/
protected $settings_renderer;
/**
* The processor for authorized payments.
*
* @var AuthorizedPaymentsProcessor
*/
protected $authorized_payments;
/**
* The Authorized Order Action Notice.
*
* @var AuthorizeOrderActionNotice
*/
protected $notice;
/**
* The processor for orders.
*
@ -67,6 +50,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/
protected $order_processor;
/**
* The processor for authorized payments.
*
* @var AuthorizedPaymentsProcessor
*/
protected $authorized_payments_processor;
/**
* The settings.
*
@ -81,6 +71,20 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/
protected $session_handler;
/**
* The Refund Processor.
*
* @var RefundProcessor
*/
private $refund_processor;
/**
* The state.
*
* @var State
*/
protected $state;
/**
* Service able to provide transaction url for an order.
*
@ -110,11 +114,18 @@ class PayPalGateway extends \WC_Payment_Gateway {
protected $logger;
/**
* The Refund Processor.
* The payments endpoint
*
* @var RefundProcessor
* @var PaymentsEndpoint
*/
private $refund_processor;
protected $payments_endpoint;
/**
* The order endpoint.
*
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
* Whether the plugin is in onboarded state.
@ -130,20 +141,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/
protected $page_id;
/**
* The payments endpoint
*
* @var PaymentsEndpoint
*/
protected $payments_endpoint;
/**
* The order endpoint.
*
* @var OrderEndpoint
*/
protected $order_endpoint;
/**
* The environment.
*
@ -157,7 +154,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
* @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.
* @param RefundProcessor $refund_processor The Refund Processor.
@ -175,7 +171,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
SettingsRenderer $settings_renderer,
OrderProcessor $order_processor,
AuthorizedPaymentsProcessor $authorized_payments_processor,
AuthorizeOrderActionNotice $notice,
ContainerInterface $config,
SessionHandler $session_handler,
RefundProcessor $refund_processor,
@ -192,8 +187,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->id = self::ID;
$this->order_processor = $order_processor;
$this->authorized_payments = $authorized_payments_processor;
$this->notice = $notice;
$this->authorized_payments_processor = $authorized_payments_processor;
$this->settings_renderer = $settings_renderer;
$this->config = $config;
$this->session_handler = $session_handler;
@ -250,6 +244,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->logger = $logger;
$this->payments_endpoint = $payments_endpoint;
$this->order_endpoint = $order_endpoint;
$this->state = $state;
}
/**
@ -286,73 +281,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
}
}
/**
* Captures an authorized payment for an WooCommerce order.
*
* @param \WC_Order $wc_order The WooCommerce order.
*
* @return bool
*/
public function capture_authorized_payment( \WC_Order $wc_order ): bool {
$result_status = $this->authorized_payments->process( $wc_order );
$this->render_authorization_message_for_status( $result_status );
if ( AuthorizedPaymentsProcessor::ALREADY_CAPTURED === $result_status ) {
if ( $wc_order->get_status() === 'on-hold' ) {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )
);
}
$wc_order->update_meta_data( self::CAPTURED_META_KEY, 'true' );
$wc_order->save();
$wc_order->payment_complete();
return true;
}
$captures = $this->authorized_payments->captures();
if ( empty( $captures ) ) {
return false;
}
$capture = end( $captures );
$this->handle_capture_status( $capture, $wc_order );
if ( AuthorizedPaymentsProcessor::SUCCESSFUL === $result_status ) {
if ( $capture->status()->is( CaptureStatus::COMPLETED ) ) {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )
);
}
$wc_order->update_meta_data( self::CAPTURED_META_KEY, 'true' );
$wc_order->save();
return true;
}
return false;
}
/**
* Displays the notice for a status.
*
* @param string $status The status.
*/
private function render_authorization_message_for_status( string $status ) {
$message_mapping = array(
AuthorizedPaymentsProcessor::SUCCESSFUL => AuthorizeOrderActionNotice::SUCCESS,
AuthorizedPaymentsProcessor::ALREADY_CAPTURED => AuthorizeOrderActionNotice::ALREADY_CAPTURED,
AuthorizedPaymentsProcessor::INACCESSIBLE => AuthorizeOrderActionNotice::NO_INFO,
AuthorizedPaymentsProcessor::NOT_FOUND => AuthorizeOrderActionNotice::NOT_FOUND,
AuthorizedPaymentsProcessor::BAD_AUTHORIZATION => AuthorizeOrderActionNotice::BAD_AUTHORIZATION,
);
$display_message = ( isset( $message_mapping[ $status ] ) ) ?
$message_mapping[ $status ]
: AuthorizeOrderActionNotice::FAILED;
$this->notice->display_message( $display_message );
}
/**
* Renders the settings.
*

View file

@ -10,10 +10,13 @@ declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use Exception;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
@ -101,7 +104,7 @@ trait ProcessPaymentTrait {
if ( $order->intent() === 'AUTHORIZE' ) {
$order = $this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'false' );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
}
$this->handle_new_order_status( $order, $wc_order );
@ -166,7 +169,7 @@ trait ProcessPaymentTrait {
if ( $tokens ) {
$this->logger->info( "Payment for subscription parent order #{$order_id} was saved correctly." );
$this->capture_authorized_payment( $wc_order );
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
$this->session_handler->destroy_session_data();
return array(
@ -213,6 +216,13 @@ trait ProcessPaymentTrait {
$this->payments_endpoint->void( $authorization );
}
$this->logger->debug(
sprintf(
'Order %1$s voided successfully.',
$order->id()
)
);
$error_message = __( 'Could not process order because it was not possible to save the payment.', 'woocommerce-paypal-payments' );
$wc_order->update_status( 'failed', $error_message );

View file

@ -16,8 +16,10 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Authorization;
use WooCommerce\PayPalCommerce\ApiClient\Entity\AuthorizationStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
/**
* Class AuthorizedPaymentsProcessor
@ -33,6 +35,8 @@ class AuthorizedPaymentsProcessor {
const NOT_FOUND = 'NOT_FOUND';
const BAD_AUTHORIZATION = 'BAD_AUTHORIZATION';
const CAPTURED_META_KEY = '_ppcp_paypal_captured';
/**
* The Order endpoint.
*
@ -61,22 +65,32 @@ class AuthorizedPaymentsProcessor {
*/
private $captures;
/**
* The notice.
*
* @var AuthorizeOrderActionNotice
*/
private $notice;
/**
* AuthorizedPaymentsProcessor constructor.
*
* @param OrderEndpoint $order_endpoint The Order endpoint.
* @param PaymentsEndpoint $payments_endpoint The Payments endpoint.
* @param LoggerInterface $logger The logger.
* @param AuthorizeOrderActionNotice $notice The notice.
*/
public function __construct(
OrderEndpoint $order_endpoint,
PaymentsEndpoint $payments_endpoint,
LoggerInterface $logger
LoggerInterface $logger,
AuthorizeOrderActionNotice $notice
) {
$this->order_endpoint = $order_endpoint;
$this->payments_endpoint = $payments_endpoint;
$this->logger = $logger;
$this->notice = $notice;
}
/**
@ -127,6 +141,73 @@ class AuthorizedPaymentsProcessor {
return $this->captures;
}
/**
* Captures an authorized payment for an WooCommerce order.
*
* @param \WC_Order $wc_order The WooCommerce order.
*
* @return bool
*/
public function capture_authorized_payment( \WC_Order $wc_order ): bool {
$result_status = $this->process( $wc_order );
$this->render_authorization_message_for_status( $result_status );
if ( self::ALREADY_CAPTURED === $result_status ) {
if ( $wc_order->get_status() === 'on-hold' ) {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )
);
}
$wc_order->update_meta_data( self::CAPTURED_META_KEY, 'true' );
$wc_order->save();
$wc_order->payment_complete();
return true;
}
$captures = $this->captures();
if ( empty( $captures ) ) {
return false;
}
$capture = end( $captures );
$this->handle_capture_status( $capture, $wc_order );
if ( self::SUCCESSFUL === $result_status ) {
if ( $capture->status()->is( CaptureStatus::COMPLETED ) ) {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )
);
}
$wc_order->update_meta_data( self::CAPTURED_META_KEY, 'true' );
$wc_order->save();
return true;
}
return false;
}
/**
* Displays the notice for a status.
*
* @param string $status The status.
*/
private function render_authorization_message_for_status( string $status ) {
$message_mapping = array(
self::SUCCESSFUL => AuthorizeOrderActionNotice::SUCCESS,
self::ALREADY_CAPTURED => AuthorizeOrderActionNotice::ALREADY_CAPTURED,
self::INACCESSIBLE => AuthorizeOrderActionNotice::NO_INFO,
self::NOT_FOUND => AuthorizeOrderActionNotice::NOT_FOUND,
self::BAD_AUTHORIZATION => AuthorizeOrderActionNotice::BAD_AUTHORIZATION,
);
$display_message = ( isset( $message_mapping[ $status ] ) ) ?
$message_mapping[ $status ]
: AuthorizeOrderActionNotice::FAILED;
$this->notice->display_message( $display_message );
}
/**
* Returns the PayPal order from a given WooCommerce order.
*

View file

@ -171,7 +171,7 @@ class OrderProcessor {
if ( $order->intent() === 'AUTHORIZE' ) {
$order = $this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'false' );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
}
$transaction_id = $this->get_paypal_order_transaction_id( $order );
@ -186,7 +186,7 @@ class OrderProcessor {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )
);
$wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'true' );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'true' );
$wc_order->update_status( 'processing' );
}
WC()->cart->empty_cart();

View file

@ -25,6 +25,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Notice\DccWithoutPayPalAdminNotice;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SectionsRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Settings\SettingsListener;
@ -259,13 +260,14 @@ class WCGatewayModule implements ModuleInterface {
add_action(
'woocommerce_order_action_ppcp_authorize_order',
static function ( \WC_Order $wc_order ) use ( $container ) {
/**
* The PayPal Gateway.
* The authorized payments processor.
*
* @var PayPalGateway $gateway
* @var AuthorizedPaymentsProcessor $authorized_payments_processor
*/
$gateway = $container->get( 'wcgateway.paypal-gateway' );
$gateway->capture_authorized_payment( $wc_order );
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
$authorized_payments_processor->capture_authorized_payment( $wc_order );
}
);
}

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Webhooks\Handler;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
/**
* Class PaymentCaptureCompleted
@ -118,7 +119,7 @@ class PaymentCaptureCompleted implements RequestHandler {
);
$wc_order->payment_complete();
$wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'true' );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'true' );
$wc_order->save();
$this->logger->log(
'info',

View file

@ -8,8 +8,6 @@ use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use Woocommerce\PayPalCommerce\ApiClient\Entity\Capture;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
@ -53,7 +51,6 @@ class WcGatewayTest extends TestCase
}
);
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
$settings = Mockery::mock(Settings::class);
$sessionHandler = Mockery::mock(SessionHandler::class);
$sessionHandler
@ -85,7 +82,6 @@ class WcGatewayTest extends TestCase
$settingsRenderer,
$orderProcessor,
$authorizedPaymentsProcessor,
$authorizedOrderActionNotice,
$settings,
$sessionHandler,
$refundProcessor,
@ -123,7 +119,6 @@ class WcGatewayTest extends TestCase
$settingsRenderer = Mockery::mock(SettingsRenderer::class);
$orderProcessor = Mockery::mock(OrderProcessor::class);
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
$settings = Mockery::mock(Settings::class);
$settings
->shouldReceive('has')->andReturnFalse();
@ -144,7 +139,6 @@ class WcGatewayTest extends TestCase
$settingsRenderer,
$orderProcessor,
$authorizedPaymentsProcessor,
$authorizedOrderActionNotice,
$settings,
$sessionHandler,
$refundProcessor,
@ -196,7 +190,6 @@ class WcGatewayTest extends TestCase
->expects('last_error')
->andReturn($lastError);
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
$settings = Mockery::mock(Settings::class);
$settings
->shouldReceive('has')->andReturnFalse();
@ -220,7 +213,6 @@ class WcGatewayTest extends TestCase
$settingsRenderer,
$orderProcessor,
$authorizedPaymentsProcessor,
$authorizedOrderActionNotice,
$settings,
$sessionHandler,
$refundProcessor,
@ -256,203 +248,6 @@ class WcGatewayTest extends TestCase
);
}
public function testCaptureAuthorizedPayment() {
expect('is_admin')->andReturn(false);
$wcOrder = Mockery::mock(\WC_Order::class);
$wcOrder
->expects('add_order_note');
$wcOrder
->expects('update_meta_data')
->with(PayPalGateway::CAPTURED_META_KEY, 'true');
$wcOrder
->expects('payment_complete');
$wcOrder
->expects('save');
$settingsRenderer = Mockery::mock(SettingsRenderer::class);
$orderProcessor = Mockery::mock(OrderProcessor::class);
$capture = Mockery::mock(Capture::class);
$capture
->shouldReceive('status')
->andReturn(new CaptureStatus(CaptureStatus::COMPLETED));
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$authorizedPaymentsProcessor
->expects('process')
->with($wcOrder)
->andReturn(AuthorizedPaymentsProcessor::SUCCESSFUL);
$authorizedPaymentsProcessor
->expects('captures')
->andReturn([$capture]);
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
$authorizedOrderActionNotice
->expects('display_message')
->with(AuthorizeOrderActionNotice::SUCCESS);
$settings = Mockery::mock(Settings::class);
$settings
->shouldReceive('has')->andReturnFalse();
$sessionHandler = Mockery::mock(SessionHandler::class);
$refundProcessor = Mockery::mock(RefundProcessor::class);
$state = Mockery::mock(State::class);
$transactionUrlProvider = Mockery::mock(TransactionUrlProvider::class);
$state
->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
$subscriptionHelper = Mockery::mock(SubscriptionHelper::class);
$paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class);
$logger = Mockery::mock(LoggerInterface::class);
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
$testee = new PayPalGateway(
$settingsRenderer,
$orderProcessor,
$authorizedPaymentsProcessor,
$authorizedOrderActionNotice,
$settings,
$sessionHandler,
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper,
PayPalGateway::ID,
$this->environment,
$paymentTokenRepository,
$logger,
$paymentsEndpoint,
$orderEndpoint
);
$this->assertTrue($testee->capture_authorized_payment($wcOrder));
}
public function testCaptureAuthorizedPaymentHasAlreadyBeenCaptured() {
expect('is_admin')->andReturn(false);
$wcOrder = Mockery::mock(\WC_Order::class);
$wcOrder
->expects('get_status')
->andReturn('on-hold');
$wcOrder
->expects('add_order_note');
$wcOrder
->expects('update_meta_data')
->with(PayPalGateway::CAPTURED_META_KEY, 'true');
$wcOrder
->expects('payment_complete');
$wcOrder
->expects('save');
$settingsRenderer = Mockery::mock(SettingsRenderer::class);
$orderProcessor = Mockery::mock(OrderProcessor::class);
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$authorizedPaymentsProcessor
->expects('process')
->with($wcOrder)
->andReturn(AuthorizedPaymentsProcessor::ALREADY_CAPTURED);
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
$authorizedOrderActionNotice
->expects('display_message')
->with(AuthorizeOrderActionNotice::ALREADY_CAPTURED);
$settings = Mockery::mock(Settings::class);
$settings
->shouldReceive('has')->andReturnFalse();
$sessionHandler = Mockery::mock(SessionHandler::class);
$refundProcessor = Mockery::mock(RefundProcessor::class);
$state = Mockery::mock(State::class);
$transactionUrlProvider = Mockery::mock(TransactionUrlProvider::class);
$state
->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
$subscriptionHelper = Mockery::mock(SubscriptionHelper::class);
$paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class);
$logger = Mockery::mock(LoggerInterface::class);
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
$testee = new PayPalGateway(
$settingsRenderer,
$orderProcessor,
$authorizedPaymentsProcessor,
$authorizedOrderActionNotice,
$settings,
$sessionHandler,
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper,
PayPalGateway::ID,
$this->environment,
$paymentTokenRepository,
$logger,
$paymentsEndpoint,
$orderEndpoint
);
$this->assertTrue($testee->capture_authorized_payment($wcOrder));
}
/**
* @dataProvider dataForTestCaptureAuthorizedPaymentNoActionableFailures
*
* @param string $lastStatus
* @param int $expectedMessage
*/
public function testCaptureAuthorizedPaymentNoActionableFailures($lastStatus, $expectedMessage) {
expect('is_admin')->andReturn(false);
$wcOrder = Mockery::mock(\WC_Order::class);
$settingsRenderer = Mockery::mock(SettingsRenderer::class);
$orderProcessor = Mockery::mock(OrderProcessor::class);
$authorizedPaymentsProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$authorizedPaymentsProcessor
->expects('process')
->with($wcOrder)
->andReturn($lastStatus);
$authorizedPaymentsProcessor
->expects('captures')
->andReturn([]);
$authorizedOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
$authorizedOrderActionNotice
->expects('display_message')
->with($expectedMessage);
$settings = Mockery::mock(Settings::class);
$settings
->shouldReceive('has')->andReturnFalse();
$sessionHandler = Mockery::mock(SessionHandler::class);
$refundProcessor = Mockery::mock(RefundProcessor::class);
$state = Mockery::mock(State::class);
$transactionUrlProvider = Mockery::mock(TransactionUrlProvider::class);
$state
->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
$subscriptionHelper = Mockery::mock(SubscriptionHelper::class);
$paymentTokenRepository = Mockery::mock(PaymentTokenRepository::class);
$logger = Mockery::mock(LoggerInterface::class);
$paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$orderEndpoint = Mockery::mock(OrderEndpoint::class);
$testee = new PayPalGateway(
$settingsRenderer,
$orderProcessor,
$authorizedPaymentsProcessor,
$authorizedOrderActionNotice,
$settings,
$sessionHandler,
$refundProcessor,
$state,
$transactionUrlProvider,
$subscriptionHelper,
PayPalGateway::ID,
$this->environment,
$paymentTokenRepository,
$logger,
$paymentsEndpoint,
$orderEndpoint
);
$this->assertFalse($testee->capture_authorized_payment($wcOrder));
}
/**
* @dataProvider dataForTestNeedsSetup
*/
@ -462,7 +257,6 @@ class WcGatewayTest extends TestCase
$settingsRenderer = Mockery::mock(SettingsRenderer::class);
$orderProcessor = Mockery::mock(OrderProcessor::class);
$authorizedOrdersProcessor = Mockery::mock(AuthorizedPaymentsProcessor::class);
$authorizeOrderActionNotice = Mockery::mock(AuthorizeOrderActionNotice::class);
$config = Mockery::mock(ContainerInterface::class);
$config
->shouldReceive('has')
@ -485,7 +279,6 @@ class WcGatewayTest extends TestCase
$settingsRenderer,
$orderProcessor,
$authorizedOrdersProcessor,
$authorizeOrderActionNotice,
$config,
$sessionHandler,
$refundProcessor,

View file

@ -19,19 +19,17 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\TestCase;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Mockery;
use WooCommerce\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
class AuthorizedPaymentsProcessorTest extends TestCase
{
private $wcOrder;
private $paypalOrderId = 'abc';
private $authorizationId = 'qwe';
private $paypalOrder;
private $orderEndpoint;
private $paymentsEndpoint;
private $notice;
private $testee;
public function setUp(): void {
@ -52,7 +50,15 @@ class AuthorizedPaymentsProcessorTest extends TestCase
$this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$this->testee = new AuthorizedPaymentsProcessor($this->orderEndpoint, $this->paymentsEndpoint, new NullLogger());
$this->notice = Mockery::mock(AuthorizeOrderActionNotice::class);
$this->notice->shouldReceive('display_message');
$this->testee = new AuthorizedPaymentsProcessor(
$this->orderEndpoint,
$this->paymentsEndpoint,
new NullLogger(),
$this->notice
);
}
public function testSuccess() {
@ -125,6 +131,41 @@ class AuthorizedPaymentsProcessorTest extends TestCase
$this->assertEquals(AuthorizedPaymentsProcessor::BAD_AUTHORIZATION, $this->testee->process($this->wcOrder));
}
public function testCaptureAuthorizedPayment()
{
$this->orderEndpoint->shouldReceive('order')->andReturn($this->paypalOrder);
$this->paymentsEndpoint
->expects('capture')
->with($this->authorizationId)
->andReturn($this->createCapture(CaptureStatus::COMPLETED));
$this->wcOrder->shouldReceive('payment_complete')->andReturn(true);
$this->wcOrder->expects('add_order_note');
$this->wcOrder->expects('update_meta_data');
$this->wcOrder->expects('save');
$this->assertTrue(
$this->testee->capture_authorized_payment($this->wcOrder)
);
}
public function testCaptureAuthorizedPaymentAlreadyCaptured()
{
$paypalOrder = $this->createPaypalOrder([$this->createAuthorization($this->authorizationId, AuthorizationStatus::CAPTURED)]);
$this->orderEndpoint->shouldReceive('order')->andReturn($paypalOrder);
$this->wcOrder->shouldReceive('get_status')->andReturn('on-hold');
$this->wcOrder->expects('add_order_note');
$this->wcOrder->expects('update_meta_data');
$this->wcOrder->expects('save');
$this->wcOrder->expects('payment_complete');
$this->assertTrue(
$this->testee->capture_authorized_payment($this->wcOrder)
);
}
private function createWcOrder(string $paypalOrderId): WC_Order {
$wcOrder = Mockery::mock(WC_Order::class);
$wcOrder

View file

@ -147,7 +147,7 @@ class OrderProcessorTest extends TestCase
$wcOrder
->expects('update_meta_data')
->with(
PayPalGateway::CAPTURED_META_KEY,
AuthorizedPaymentsProcessor::CAPTURED_META_KEY,
'false'
);
$wcOrder