WC_Checkout::process_order_payment
Process an order that does require payment.
Method of the class: WC_Checkout{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->process_order_payment( $order_id, $payment_method );
- $order_id(int) (required)
- Order ID.
- $payment_method(string) (required)
- Payment method.
Changelog
| Since 3.0.0 | Introduced. |
WC_Checkout::process_order_payment() WC Checkout::process order payment code WC 10.6.2
protected function process_order_payment( $order_id, $payment_method ) {
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( ! isset( $available_gateways[ $payment_method ] ) ) {
return;
}
// Store Order ID in session, so it can be re-used after payment failure.
WC()->session->set( 'order_awaiting_payment', $order_id );
// We save the session early because if the payment gateway hangs
// the request will never finish, thus the session data will never be saved,
// and this can lead to duplicate orders if the user submits the order again.
WC()->session->save_data();
// Process Payment.
$result = $available_gateways[ $payment_method ]->process_payment( $order_id );
// Redirect to success/confirmation/payment page.
if ( isset( $result['result'] ) && 'success' === $result['result'] ) {
$result['order_id'] = $order_id;
$result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
wc_log_order_step(
'[Shortcode #6A] Order payment processed successfully',
array(
'order_id' => $order_id,
'payment_method' => $payment_method,
'redirected' => ! wp_doing_ajax() ? 'yes' : 'no',
),
true
);
if ( ! wp_doing_ajax() ) {
// phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
wp_redirect( $result['redirect'] );
exit;
}
// Using wp_send_json will gracefully handle any problem encoding data.
wp_send_json( $result );
}
}