Skip to content

WPS Migration: Fix capture failure #60136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function capture_authorized_payment( $order ) {
}

// Skip if the payment is already captured.
if ( 'COMPLETED' === $order->get_meta( '_paypal_status', true ) ) {
if ( 'completed' === $order->get_meta( '_paypal_status', true ) ) {
WC_Gateway_Paypal::log( 'PayPal payment is already captured. Skipping capture. Order ID: ' . $order->get_id() );
return;
}
Expand All @@ -272,12 +272,18 @@ public function capture_authorized_payment( $order ) {
return;
}

$http_code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$http_code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
$response_data = json_decode( $body, true );

if ( 200 !== $http_code ) {
WC_Gateway_Paypal::log( 'PayPal capture payment failed. Response status: ' . $http_code . '. Response body: ' . $body );
}

if ( isset( $response_data['status'] ) ) {
$order->update_meta_data( '_paypal_status', strtolower( $response_data['status'] ) );
$order->save();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ private function process_checkout_order_approved( $event ) {
$paypal_order_id = $event['resource']['id'] ?? null;
if ( 'APPROVED' === $status ) {
WC_Gateway_Paypal::log( 'PayPal payment approved. Order ID: ' . $order->get_id() );
$order->update_meta_data( '_paypal_status', $status );
$order->update_meta_data( '_paypal_status', strtolower( $status ) );
$order->add_order_note(
sprintf(
/* translators: %1$s: PayPal order ID */
__( 'PayPal payment approved. PayPal Order ID: %1$s', 'woocommerce' ),
$paypal_order_id
)
);
$order->save();

// Authorize or capture the payment after approval.
$action = 'CAPTURE' === $event['resource']['intent'] ? 'capture' : 'authorize';
Expand Down Expand Up @@ -105,7 +106,7 @@ private function process_payment_capture_completed( $event ) {
}

$order->set_transaction_id( $event['resource']['id'] );
$order->update_meta_data( '_paypal_status', $event['resource']['status'] );
$order->update_meta_data( '_paypal_status', strtolower( $event['resource']['status'] ) );
$order->payment_complete();
$order->add_order_note(
sprintf(
Expand Down Expand Up @@ -178,7 +179,6 @@ private function get_wc_order( $custom_id ) {
* @return void
*/
private function authorize_or_capture_payment( $order, $links, $action ) {
wc_get_logger()->info( 'called from webhook handler: ' . $action );
$action_url = null;
foreach ( $links as $link ) {
if ( $action === $link['rel'] && 'POST' === $link['method'] && filter_var( $link['href'], FILTER_VALIDATE_URL ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ public function capture_authorized_payment( WP_REST_Request $request ) {
$authorization_id = $request->get_param( 'authorization_id' );
$request_data = $request->get_json_params();

wc_get_logger()->info( '(Proxy) PayPal capture authorized payment request received for auth ID: ' . $authorization_id );
wc_get_logger()->info( '(Proxy) PayPal capture authorized payment request received for request data: ' . wc_print_r( $request_data, true ) );

error_log( '(Proxy) PayPal capture authorized payment request received for auth ID: ' . $authorization_id );

if ( empty( $authorization_id ) || ! isset( $request_data['testmode'] ) ) {
Expand All @@ -340,12 +337,15 @@ public function capture_authorized_payment( WP_REST_Request $request ) {
);
}

if ( ! isset( $request_data['testmode'] ) ) {
error_log( '(Proxy) PayPal authorize payment request missing required param: ' . $param . '.' );
return new WP_REST_Response(
array( 'error' => 'PayPal capture authorized payment request missing required param: ' . $param . '.' ),
400
);
$required_params = array( 'testmode' );
foreach ( $required_params as $param ) {
if ( ! isset( $request_data[ $param ] ) ) {
error_log( '(Proxy) PayPal capture authorized payment request missing required param: ' . $param . '.' );
return new WP_REST_Response(
array( 'error' => 'PayPal capture authorized payment request missing required param: ' . $param . '.' ),
400
);
}
}

$access_token = $this->get_paypal_access_token( $request_data['testmode'] );
Expand Down Expand Up @@ -374,8 +374,6 @@ public function capture_authorized_payment( WP_REST_Request $request ) {
$response_data = json_decode( $body, true );
error_log( '(Proxy) PayPal capture authorized payment response: ' . wc_print_r( $response_data, true ) );

wc_get_logger()->info( '(Proxy) PayPal capture authorized payment response: ' . wc_print_r( $response_data, true ) );

if ( in_array( $http_code, array( 200, 201 ), true ) ) {
return new WP_REST_Response(
$response_data,
Expand Down
Loading