Skip to content
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

Implement error logging in ECE #9108

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions changelog/add-9049-error-logging-in-ece
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Add error logging to ECE critical endpoints.
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,38 @@ public function init() {

/**
* Create order. Security is handled by WC.
*
* @throws Exception If cart is empty.
cesarcosta99 marked this conversation as resolved.
Show resolved Hide resolved
*/
public function ajax_create_order() {
if ( WC()->cart->is_empty() ) {
wp_send_json_error( __( 'Empty cart', 'woocommerce-payments' ), 400 );
}
try {
if ( WC()->cart->is_empty() ) {
throw new Exception( __( 'Empty cart', 'woocommerce-payments' ) );
}

if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
define( 'WOOCOMMERCE_CHECKOUT', true );
}
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
define( 'WOOCOMMERCE_CHECKOUT', true );
}

if ( ! defined( 'WCPAY_ECE_CHECKOUT' ) ) {
define( 'WCPAY_ECE_CHECKOUT', true );
}
if ( ! defined( 'WCPAY_ECE_CHECKOUT' ) ) {
define( 'WCPAY_ECE_CHECKOUT', true );
}

// In case the state is required, but is missing, add a more descriptive error notice.
$this->express_checkout_button_helper->validate_state();
// In case the state is required, but is missing, add a more descriptive error notice.
$this->express_checkout_button_helper->validate_state();

$this->express_checkout_button_helper->normalize_state();
$this->express_checkout_button_helper->normalize_state();

WC()->checkout()->process_checkout();
WC()->checkout()->process_checkout();
} catch ( Exception $e ) {
Logger::error( 'Failed to process express checkout payment: ' . $e );

$response = [
'result' => 'error',
'messages' => $e->getMessage(),
];
wp_send_json( $response, 400 );
Comment on lines +77 to +81
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that this is the response expected in the frontend.

}

die( 0 );
}
Expand All @@ -80,22 +92,16 @@ public function ajax_create_order() {
public function ajax_pay_for_order() {
check_ajax_referer( 'pay_for_order' );

if (
! isset( $_POST['payment_method'] ) || 'woocommerce_payments' !== $_POST['payment_method']
|| ! isset( $_POST['order'] ) || ! intval( $_POST['order'] )
|| ! isset( $_POST['wcpay-payment-method'] ) || empty( $_POST['wcpay-payment-method'] )
) {
// Incomplete request.
$response = [
'result' => 'error',
'messages' => __( 'Invalid request', 'woocommerce-payments' ),
];
wp_send_json( $response, 400 );

return;
}

try {
if (
! isset( $_POST['payment_method'] ) || 'woocommerce_payments' !== $_POST['payment_method']
|| ! isset( $_POST['order'] ) || ! intval( $_POST['order'] )
|| ! isset( $_POST['wcpay-payment-method'] ) || empty( $_POST['wcpay-payment-method'] )
) {
// Incomplete request.
throw new Exception( __( 'Invalid request', 'woocommerce-payments' ) );
}

// Set up an environment, similar to core checkout.
wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
wc_set_time_limit( 0 );
Expand Down Expand Up @@ -128,14 +134,18 @@ public function ajax_pay_for_order() {
$result['order_id'] = $order_id;

$result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );

wp_send_json( $result );
} catch ( Exception $e ) {
$order_message = isset( $order_id ) ? "order #$order_id" : 'invalid order';
Logger::error( 'Failed to process express checkout payment for ' . $order_message . ': ' . $e );

$result = [
'result' => 'error',
'messages' => $e->getMessage(),
];
wp_send_json( $result, 400 );
}

wp_send_json( $result );
}

/**
Expand Down
Loading