How can the status "Payment completed" be generated automatically?

If you use payment services that run automatically (PayPal, Klarna,..), i.e. do not require a manual check, as is the case with prepayment, you would probably like to have the status of the payment automatically set to “Completed”. This is made possible by the following snippet.

add_action( 'woocommerce_payment_complete', 'wpdesk_set_completed_for_paid_orders' );
function wpdesk_set_completed_for_paid_orders( $order_id ) {
    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' ); 
}