Fix merge conflicts

This commit is contained in:
dinamiko 2021-10-20 15:01:53 +02:00
commit e3a793e3e5
12 changed files with 236 additions and 113 deletions

View file

@ -16,6 +16,21 @@ use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
* Class RenderAuthorizeAction
*/
class RenderAuthorizeAction {
/**
* The capture info column.
*
* @var OrderTablePaymentStatusColumn
*/
private $column;
/**
* PaymentStatusOrderDetail constructor.
*
* @param OrderTablePaymentStatusColumn $column The capture info column.
*/
public function __construct( OrderTablePaymentStatusColumn $column ) {
$this->column = $column;
}
/**
* Renders the action into the $order_actions array based on the WooCommerce order.
@ -46,7 +61,10 @@ class RenderAuthorizeAction {
* @return bool
*/
private function should_render_for_order( \WC_Order $order ) : bool {
$data = $order->get_meta( AuthorizedPaymentsProcessor::CAPTURED_META_KEY );
return in_array( $data, array( 'true', 'false' ), true );
$status = $order->get_status();
$not_allowed_statuses = array( 'refunded', 'cancelled', 'failed' );
return $this->column->should_render_for_order( $order ) &&
! $this->column->is_captured( $order ) &&
! in_array( $status, $not_allowed_statuses, true );
}
}

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
@ -106,13 +107,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/
protected $payment_token_repository;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* The payments endpoint
*
@ -148,6 +142,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
*/
protected $environment;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* PayPalGateway constructor.
*
@ -166,6 +167,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
* @param LoggerInterface $logger The logger.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
SettingsRenderer $settings_renderer,
@ -196,6 +198,18 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->page_id = $page_id;
$this->environment = $environment;
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
$this->id = self::ID;
$this->order_processor = $order_processor;
$this->authorized_payments = $authorized_payments_processor;
$this->settings_renderer = $settings_renderer;
$this->config = $config;
$this->session_handler = $session_handler;
$this->refund_processor = $refund_processor;
$this->transaction_url_provider = $transaction_url_provider;
$this->page_id = $page_id;
$this->environment = $environment;
$this->logger = $logger;
$this->onboarded = $state->current_state() === State::STATE_ONBOARDED;
if ( $this->onboarded ) {
$this->supports = array( 'refunds' );

View file

@ -19,13 +19,14 @@ use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
/**
* Trait ProcessPaymentTrait
*/
trait ProcessPaymentTrait {
use OrderMetaTrait, PaymentsStatusHandlingTrait;
use OrderMetaTrait, PaymentsStatusHandlingTrait, TransactionIdHandlingTrait;
/**
* Process a payment for an WooCommerce order.
@ -107,6 +108,11 @@ trait ProcessPaymentTrait {
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
}
$transaction_id = $this->get_paypal_order_transaction_id( $order );
if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );
}
$this->handle_new_order_status( $order, $wc_order );
if ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {

View file

@ -26,7 +26,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
*/
class OrderProcessor {
use OrderMetaTrait, PaymentsStatusHandlingTrait;
use OrderMetaTrait, PaymentsStatusHandlingTrait, TransactionIdHandlingTrait;
/**
* The environment.
@ -176,8 +176,8 @@ class OrderProcessor {
$transaction_id = $this->get_paypal_order_transaction_id( $order );
if ( '' !== $transaction_id ) {
$this->set_order_transaction_id( $transaction_id, $wc_order );
if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );
}
$this->handle_new_order_status( $order, $wc_order );
@ -195,55 +195,6 @@ class OrderProcessor {
return true;
}
/**
* Set transaction id to WC order meta data.
*
* @param string $transaction_id Transaction id to set.
* @param \WC_Order $wc_order Order to set transaction ID to.
*/
public function set_order_transaction_id( string $transaction_id, \WC_Order $wc_order ) {
try {
$wc_order->set_transaction_id( $transaction_id );
} catch ( \WC_Data_Exception $exception ) {
$this->logger->log(
'warning',
sprintf(
'Failed to set transaction ID. Exception caught when tried: %1$s',
$exception->getMessage()
)
);
}
}
/**
* Retrieve transaction id from PayPal order.
*
* @param Order $order Order to get transaction id from.
*
* @return string
*/
private function get_paypal_order_transaction_id( Order $order ): string {
$purchase_units = $order->purchase_units();
if ( ! isset( $purchase_units[0] ) ) {
return '';
}
$payments = $purchase_units[0]->payments();
if ( null === $payments ) {
return '';
}
$captures = $payments->captures();
if ( isset( $captures[0] ) ) {
return $captures[0]->id();
}
return '';
}
/**
* Returns if an order should be captured immediately.
*

View file

@ -0,0 +1,85 @@
<?php
/**
* Functions for retrieving/saving order transaction ID.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Processor
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
/**
* Trait PaymentsStatusHandlingTrait.
*/
trait TransactionIdHandlingTrait {
/**
* Sets transaction ID to the WC order.
*
* @param string $transaction_id The transaction ID to set.
* @param WC_Order $wc_order The order to set transaction ID to.
* @param LoggerInterface|null $logger The logger to log errors.
*
* @return bool
*/
protected function update_transaction_id(
string $transaction_id,
WC_Order $wc_order,
LoggerInterface $logger = null
): bool {
try {
$wc_order->set_transaction_id( $transaction_id );
$wc_order->save();
return true;
} catch ( Exception $exception ) {
if ( $logger ) {
$logger->warning(
sprintf(
'Failed to set transaction ID %1$s. %2$s',
$transaction_id,
$exception->getMessage()
)
);
}
return false;
}
}
/**
* Retrieves transaction id from PayPal order.
*
* @param Order $order The order to get transaction id from.
*
* @return string|null
*/
protected function get_paypal_order_transaction_id( Order $order ): ?string {
$purchase_unit = $order->purchase_units()[0] ?? null;
if ( ! $purchase_unit ) {
return null;
}
$payments = $purchase_unit->payments();
if ( null === $payments ) {
return null;
}
$capture = $payments->captures()[0] ?? null;
if ( $capture ) {
return $capture->id();
}
$authorization = $payments->authorizations()[0] ?? null;
if ( $authorization ) {
return $authorization->id();
}
return null;
}
}