wrap throwing code in the try-catch block

This commit is contained in:
Kirill Braslavsky 2021-02-22 14:08:43 +02:00
parent 5d5f395016
commit 62f4146387

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Processor;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
@ -94,6 +95,10 @@ class OrderProcessor {
* @var string
*/
private $last_error = '';
/**
* @var LoggerInterface
*/
private $logger;
/**
* OrderProcessor constructor.
@ -106,6 +111,7 @@ class OrderProcessor {
* @param ThreeDSecure $three_d_secure The ThreeDSecure Helper.
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The Authorized Payments Processor.
* @param Settings $settings The Settings.
* @param LoggerInterface $logger A logger service.
* @param bool $sandbox_mode Whether sandbox mode enabled.
*/
public function __construct(
@ -117,6 +123,7 @@ class OrderProcessor {
ThreeDSecure $three_d_secure,
AuthorizedPaymentsProcessor $authorized_payments_processor,
Settings $settings,
LoggerInterface $logger,
bool $sandbox_mode
) {
@ -129,6 +136,7 @@ class OrderProcessor {
$this->authorized_payments_processor = $authorized_payments_processor;
$this->settings = $settings;
$this->sandbox_mode = $sandbox_mode;
$this->logger = $logger;
}
/**
@ -180,7 +188,7 @@ class OrderProcessor {
$transaction_id = $this->get_paypal_order_transaction_id( $order );
if ( '' !== $transaction_id ) {
$wc_order->set_transaction_id( $transaction_id );
$this->set_order_transaction_id( $transaction_id, $wc_order );
}
$wc_order->update_status(
@ -207,6 +215,26 @@ 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.
*