Return status instead of bool + last_status

This commit is contained in:
Alex P 2021-10-04 18:36:30 +03:00
parent c6229c9944
commit 07928c9935
5 changed files with 21 additions and 58 deletions

View file

@ -239,10 +239,10 @@ class PayPalGateway extends \WC_Payment_Gateway {
* @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() );
$result_status = $this->authorized_payments->process( $wc_order );
$this->render_authorization_message_for_status( $result_status );
if ( $is_processed ) {
if ( AuthorizedPaymentsProcessor::SUCCESSFUL === $result_status ) {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )
);
@ -252,7 +252,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
return true;
}
if ( $this->authorized_payments->last_status() === AuthorizedPaymentsProcessor::ALREADY_CAPTURED ) {
if ( AuthorizedPaymentsProcessor::ALREADY_CAPTURED === $result_status ) {
if ( $wc_order->get_status() === 'on-hold' ) {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )

View file

@ -42,13 +42,6 @@ class AuthorizedPaymentsProcessor {
*/
private $payments_endpoint;
/**
* The last status.
*
* @var string
*/
private $last_status = '';
/**
* AuthorizedPaymentsProcessor constructor.
*
@ -69,46 +62,31 @@ class AuthorizedPaymentsProcessor {
*
* @param \WC_Order $wc_order The WooCommerce order.
*
* @return bool
* @return string One of the AuthorizedPaymentsProcessor status constants.
*/
public function process( \WC_Order $wc_order ): bool {
public function process( \WC_Order $wc_order ): string {
try {
$order = $this->paypal_order_from_wc_order( $wc_order );
} catch ( Exception $exception ) {
if ( $exception->getCode() === 404 ) {
$this->last_status = self::NOT_FOUND;
return false;
return self::NOT_FOUND;
}
$this->last_status = self::INACCESSIBLE;
return false;
return self::INACCESSIBLE;
}
$authorizations = $this->all_authorizations( $order );
if ( ! $this->are_authorzations_to_capture( ...$authorizations ) ) {
$this->last_status = self::ALREADY_CAPTURED;
return false;
return self::ALREADY_CAPTURED;
}
try {
$this->capture_authorizations( ...$authorizations );
} catch ( Exception $exception ) {
$this->last_status = self::FAILED;
return false;
return self::FAILED;
}
$this->last_status = self::SUCCESSFUL;
return true;
}
/**
* Returns the last status.
*
* @return string
*/
public function last_status(): string {
return $this->last_status;
return self::SUCCESSFUL;
}
/**

View file

@ -188,7 +188,7 @@ class OrderProcessor {
$wc_order->payment_complete();
}
if ( $this->capture_authorized_downloads( $order ) && $this->authorized_payments_processor->process( $wc_order ) ) {
if ( $this->capture_authorized_downloads( $order ) && AuthorizedPaymentsProcessor::SUCCESSFUL === $this->authorized_payments_processor->process( $wc_order ) ) {
$wc_order->add_order_note(
__( 'Payment successfully captured.', 'woocommerce-paypal-payments' )
);