Schedule checking for saved payment (WIP)

This commit is contained in:
dinamiko 2022-02-28 17:31:15 +01:00
parent e9a8d864da
commit a7290f9ce0
2 changed files with 19 additions and 88 deletions

View file

@ -173,96 +173,15 @@ trait ProcessPaymentTrait {
try {
if ( $this->order_processor->process( $wc_order ) ) {
if ( $this->subscription_helper->has_subscription( $order_id ) ) {
$this->logger->info( "Trying to save payment for subscription parent order #{$order_id}." );
$tokens = $this->payment_token_repository->all_for_user_id( $wc_order->get_customer_id() );
if ( $tokens ) {
$this->logger->info( "Payment for subscription parent order #{$order_id} was saved correctly." );
if ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
}
$this->session_handler->destroy_session_data();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
$this->logger->error( "Payment for subscription parent order #{$order_id} was not saved." );
$paypal_order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY );
if ( ! $paypal_order_id ) {
throw new RuntimeException( 'PayPal order ID not found in meta.' );
}
$order = $this->order_endpoint->order( $paypal_order_id );
$purchase_units = $order->purchase_units();
if ( ! $purchase_units ) {
throw new RuntimeException( 'No purchase units.' );
}
$payments = $purchase_units[0]->payments();
if ( ! $payments ) {
throw new RuntimeException( 'No payments.' );
}
$this->logger->debug(
sprintf(
'Trying to void order %1$s, payments: %2$s.',
$order->id(),
wp_json_encode( $payments->to_array() )
)
as_schedule_single_action(
time() + (1 * MINUTE_IN_SECONDS),
'woocommerce_paypal_payments_check_saved_payment',
array(
'order_id' => $order_id,
'customer_id' => $wc_order->get_customer_id(),
)
);
$voidable_authorizations = array_filter(
$payments->authorizations(),
function ( Authorization $authorization ): bool {
return $authorization->is_voidable();
}
);
if ( ! $voidable_authorizations ) {
throw new RuntimeException( 'No voidable authorizations.' );
}
foreach ( $voidable_authorizations as $authorization ) {
$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 );
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
foreach ( $subscriptions as $key => $subscription ) {
if ( $subscription->get_parent_id() === $order_id ) {
try {
$subscription->update_status( 'cancelled' );
break;
} catch ( Exception $exception ) {
$this->logger->error( "Could not update cancelled status on subscription #{$subscription->get_id()} " . $exception->getMessage() );
}
}
}
// Adds retry counter meta to avoid duplicate invoice id error on consequent tries.
$wc_order->update_meta_data( 'ppcp-retry', (int) $wc_order->get_meta( 'ppcp-retry' ) + 1 );
$wc_order->save_meta_data();
$this->session_handler->destroy_session_data();
wc_add_notice( $error_message, 'error' );
return $failure_data;
}
WC()->cart->empty_cart();