Fix renewal logging

This commit is contained in:
Alex P 2022-01-04 17:30:26 +02:00
parent 21aa8980d6
commit d42cb3e47c
2 changed files with 16 additions and 72 deletions

View file

@ -90,52 +90,23 @@ class RenewalHandler {
* @param \WC_Order $wc_order The WooCommerce order. * @param \WC_Order $wc_order The WooCommerce order.
*/ */
public function renew( \WC_Order $wc_order ) { public function renew( \WC_Order $wc_order ) {
$this->logger->log(
'info',
sprintf(
// translators: %d is the id of the order.
__( 'Start moneytransfer for order %d', 'woocommerce-paypal-payments' ),
(int) $wc_order->get_id()
),
array(
'order' => $wc_order,
)
);
try { try {
$this->process_order( $wc_order ); $this->process_order( $wc_order );
} catch ( \Exception $error ) { } catch ( \Exception $error ) {
$this->logger->log( $this->logger->error(
'error',
sprintf( sprintf(
// translators: %1$d is the order number, %2$s the error message. 'An error occurred while trying to renew the subscription for order %1$d: %2$s',
__( $wc_order->get_id(),
'An error occured while trying to renew the subscription for order %1$d: %2$s',
'woocommerce-paypal-payments'
),
(int) $wc_order->get_id(),
$error->getMessage() $error->getMessage()
),
array(
'order' => $wc_order,
) )
); );
return; return;
} }
$this->logger->log( $this->logger->info(
'info',
sprintf( sprintf(
// translators: %d is the order number. 'Renewal for order %d is completed.',
__( $wc_order->get_id()
'Moneytransfer for order %d is completed.',
'woocommerce-paypal-payments'
),
(int) $wc_order->get_id()
),
array(
'order' => $wc_order,
) )
); );
} }
@ -164,7 +135,10 @@ class RenewalHandler {
$token $token
); );
$this->capture_order( $order, $wc_order ); if ( $order->intent() === 'AUTHORIZE' ) {
$this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
}
} }
/** /**
@ -185,12 +159,8 @@ class RenewalHandler {
if ( ! $tokens ) { if ( ! $tokens ) {
$error_message = sprintf( $error_message = sprintf(
// translators: %d is the customer id. 'Payment failed. No payment tokens found for customer %d.',
__( $customer->get_id()
'Payment failed. No payment tokens found for customer %d.',
'woocommerce-paypal-payments'
),
(int) $customer->get_id()
); );
$wc_order->update_status( $wc_order->update_status(
@ -198,14 +168,7 @@ class RenewalHandler {
$error_message $error_message
); );
$this->logger->log( $this->logger->error( $error_message );
'error',
$error_message,
array(
'customer' => $customer,
'order' => $wc_order,
)
);
} }
$subscription = function_exists( 'wcs_get_subscription' ) ? wcs_get_subscription( $wc_order->get_meta( '_subscription_renewal' ) ) : null; $subscription = function_exists( 'wcs_get_subscription' ) ? wcs_get_subscription( $wc_order->get_meta( '_subscription_renewal' ) ) : null;
@ -223,25 +186,4 @@ class RenewalHandler {
return current( $tokens ); return current( $tokens );
} }
/**
* If the PayPal order is captured/authorized the WooCommerce order gets updated accordingly.
*
* @param Order $order The PayPal order.
* @param \WC_Order $wc_order The related WooCommerce order.
*/
private function capture_order( Order $order, \WC_Order $wc_order ) {
if ( $order->intent() === 'CAPTURE' && $order->status()->is( OrderStatus::COMPLETED ) ) {
$wc_order->update_status(
'processing',
__( 'Payment received.', 'woocommerce-paypal-payments' )
);
}
if ( $order->intent() === 'AUTHORIZE' ) {
$this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
}
}
} }

View file

@ -37,6 +37,9 @@ class RenewalHandlerTest extends TestCase
$this->purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class); $this->purchaseUnitFactory = Mockery::mock(PurchaseUnitFactory::class);
$this->payerFactory = Mockery::mock(PayerFactory::class); $this->payerFactory = Mockery::mock(PayerFactory::class);
$this->logger->shouldReceive('error');
$this->logger->shouldReceive('info');
$this->sut = new RenewalHandler( $this->sut = new RenewalHandler(
$this->logger, $this->logger,
$this->repository, $this->repository,
@ -59,7 +62,6 @@ class RenewalHandlerTest extends TestCase
$payer = Mockery::mock(Payer::class); $payer = Mockery::mock(Payer::class);
$order = Mockery::mock(Order::class); $order = Mockery::mock(Order::class);
$this->logger->shouldReceive('log');
$wcOrder $wcOrder
->shouldReceive('get_id') ->shouldReceive('get_id')
->andReturn(1); ->andReturn(1);