Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Set order status to pending when no payment is needed (#4186)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley authored and grogou committed Aug 20, 2021
1 parent 8ed63f7 commit b9b66f9
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/StoreApi/Routes/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ protected function get_route_post_response( WP_REST_Request $request ) {
/**
* Process the payment and return the results.
*/
$payment_result = $this->order->needs_payment() ? $this->process_payment( $request ) : $this->process_without_payment( $request );
$payment_result = new PaymentResult();

if ( $this->order->needs_payment() ) {
$this->process_payment( $request, $payment_result );
} else {
$this->process_without_payment( $request, $payment_result );
}

return $this->prepare_item_for_response(
(object) [
Expand Down Expand Up @@ -478,53 +484,52 @@ private function update_order_from_request( WP_REST_Request $request ) {
* For orders which do not require payment, just update status.
*
* @param WP_REST_Request $request Request object.
* @return PaymentResult
* @param PaymentResult $payment_result Payment result object.
*/
private function process_without_payment( WP_REST_Request $request ) {
private function process_without_payment( WP_REST_Request $request, PaymentResult $payment_result ) {
// Transition the order to pending, and then completed. This ensures transactional emails fire for pending_to_complete events.
$this->order->update_status( 'pending' );
$this->order->payment_complete();

$result = new PaymentResult( 'success' );
$result->set_redirect_url( $this->order->get_checkout_order_received_url() );

return $result;
// Mark the payment as successful.
$payment_result->set_status( 'success' );
$payment_result->set_redirect_url( $this->order->get_checkout_order_received_url() );
}

/**
* Fires an action hook instructing active payment gateways to process the payment for an order and provide a result.
*
* @throws RouteException On error.
*
* @param WP_REST_Request $request Request object.
* @return PaymentResult
* @param PaymentResult $payment_result Payment result object.
*/
private function process_payment( WP_REST_Request $request ) {
private function process_payment( WP_REST_Request $request, PaymentResult $payment_result ) {
try {
$result = new PaymentResult();
// Transition the order to pending before making payment.
$this->order->update_status( 'pending' );

// Prepare the payment context object to pass through payment hooks.
$context = new PaymentContext();
$context->set_payment_method( $this->get_request_payment_method_id( $request ) );
$context->set_payment_data( $this->get_request_payment_data( $request ) );

// Orders are made pending before attempting payment.
$this->order->update_status( 'pending' );
$context->set_order( $this->order );

/**
* Process payment with context.
*
* @hook woocommerce_rest_checkout_process_payment_with_context
*
* @throws Exception If there is an error taking payment, an Exception object can be thrown
* with an error message.
* @throws Exception If there is an error taking payment, an Exception object can be thrown with an error message.
*
* @param PaymentContext $context Holds context for the payment, including order ID and payment method.
* @param PaymentResult $result Result object for the transaction.
* @param PaymentContext $context Holds context for the payment, including order ID and payment method.
* @param PaymentResult $payment_result Result object for the transaction.
*/
do_action_ref_array( 'woocommerce_rest_checkout_process_payment_with_context', [ $context, &$result ] );
do_action_ref_array( 'woocommerce_rest_checkout_process_payment_with_context', [ $context, &$payment_result ] );

if ( ! $result instanceof PaymentResult ) {
if ( ! $payment_result instanceof PaymentResult ) {
throw new RouteException( 'woocommerce_rest_checkout_invalid_payment_result', __( 'Invalid payment result received from payment method.', 'woo-gutenberg-products-block' ), 500 );
}

return $result;
} catch ( Exception $e ) {
throw new RouteException( 'woocommerce_rest_checkout_process_payment_error', $e->getMessage(), 400 );
}
Expand Down

0 comments on commit b9b66f9

Please sign in to comment.