tal_code' => $billing_info->billing_zip_code ?? '', 'country' => $country, 'phone_number' => $billing_info->billing_phone ?? '', 'email' => $billing_info->billing_email ?? '', ); $customer_info = $shipping_and_billing; return (object) array( 'type' => 'recurring', 'previous_payload' => $order_data->payment_payloads, 'total_amount' => floatval( $amount ), 'sub_total_amount' => floatval( $amount ), 'currency' => (object) array( 'code' => $currency_code, 'symbol' => $currency_symbol, 'name' => $currency_info['name'] ?? '', 'locale' => $currency_info['locale'] ?? '', 'numeric_code' => $currency_info['numeric_code'] ?? '', ), 'order_id' => $order_id, 'customer' => (object) $customer_info, 'shipping_address' => (object) $shipping_and_billing, ); } /** * Proceed to payment * * @since 3.0.0 * * @param mixed $payment_data Payment data for making order. * @param string $payment_method Payment method name. * @param string $order_type Order type. * * @throws \Throwable Throw throwable if error occur. * @throws \Exception Throw exception if payment gateway is invalid. * * @return void */ public function proceed_to_payment( $payment_data, $payment_method, $order_type ) { $payment_gateways = apply_filters( 'tutor_gateways_with_class', Ecommerce::payment_gateways_with_ref(), $payment_method ); $payment_gateway_class = isset( $payment_gateways[ $payment_method ] ) ? $payment_gateways[ $payment_method ]['gateway_class'] : null; if ( $payment_gateway_class ) { try { add_filter( 'tutor_ecommerce_webhook_url', function ( $url ) use ( $payment_method ) { $url = add_query_arg( array( 'payment_method' => $payment_method ), $url ); return $url; } ); add_filter( 'tutor_ecommerce_payment_success_url_args', function ( $args ) use ( $payment_data ) { $args['order_id'] = $payment_data->order_id; return $args; } ); add_filter( 'tutor_ecommerce_payment_cancelled_url_args', function ( $args ) use ( $payment_data ) { $args['order_id'] = $payment_data->order_id; return $args; } ); $gateway_instance = Ecommerce::get_payment_gateway_object( $payment_gateway_class ); $gateway_instance->setup_payment_and_redirect( $payment_data ); } catch ( \Throwable $th ) { throw $th; } } else { throw new \Exception( 'Invalid payment gateway class' ); } } /** * Restrict checkout page * * @return void */ public function restrict_checkout_page() { $page_id = self::get_page_id(); if ( ! $page_id || ! is_page( $page_id ) ) { return; } $cart_page_url = CartController::get_page_url(); if ( ! is_user_logged_in() && ! apply_filters( 'tutor_is_guest_checkout_enabled', false ) ) { wp_safe_redirect( $cart_page_url ); exit; } $user_id = tutils()->get_user_id(); $cart_model = new CartModel(); $has_cart_item = $cart_model->has_item_in_cart( $user_id ); $buy_now = Settings::is_buy_now_enabled(); $plan_id = Input::get( 'plan', 0, Input::TYPE_INT ); $order_id = Input::get( 'order_id', 0, Input::TYPE_INT ); if ( ! $has_cart_item && ! $buy_now && ! $plan_id && ! $order_id ) { wp_safe_redirect( $cart_page_url ); exit; } } /** * Set alert message on the session based on * order data * * @since 3.0.0 * * @param mixed $order_data Order data or null. If order * data is falsy then failed message will be set. * * @return void */ private function set_pay_now_alert_msg( $order_data ) { $user_id = $order_data ? $order_data['user_id'] : get_current_user_id(); if ( empty( $order_data ) ) { set_transient( self::PAY_NOW_ALERT_MSG_TRANSIENT_KEY . $user_id, array( 'alert' => 'danger', 'message' => __( 'Failed to place order!', 'tutor' ), ), ); } else { set_transient( self::PAY_NOW_ALERT_MSG_TRANSIENT_KEY . $user_id, array( 'alert' => 'success', 'message' => __( 'Your order has been placed successfully!', 'tutor' ), ), ); } } /** * Pay for the incomplete order * * Redirect to the payment gateway to complete the order * After completing the process it will redirect user to * order placement page * * @since 3.0.0 * * @return void */ public function pay_incomplete_order() { // Authentication check. if ( ! is_user_logged_in() ) { tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, 0, __( 'Please log in first', 'tutor' ) ); } $order_id = Input::post( 'order_id', 0, Input::TYPE_INT ); $payment_method = Input::post( 'payment_method', '' ); $request = Input::sanitize_array( $_POST ); //phpcs:ignore -- $POST sanitized $billing_model = new BillingModel(); $billing_fillable_fields = array_intersect_key( $request, array_flip( $billing_model->get_fillable_fields() ) ); if ( ! tutor_utils()->is_nonce_verified() ) { tutor_utils()->redirect_to( tutor_utils()->tutor_dashboard_url( 'purchase_history' ), tutor_utils()->error_message( 'nonce' ), 'error' ); exit; } if ( $order_id ) { $order_model = new OrderModel(); $order_data = $order_model->get_order_by_id( $order_id ); if ( $order_data && get_current_user_id() === (int) $order_data->user_id ) { try { if ( ! empty( $payment_method ) && OrderModel::PAYMENT_METHOD_MANUAL === $order_data->payment_method ) { $billing_info = $billing_model->get_info( $order_data->user_id ); if ( $billing_info ) { $update_billing = $billing_model->update( $billing_fillable_fields, array( 'user_id' => $order_data->user_id ) ); if ( ! $update_billing ) { tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, $order_data->id, __( 'Billing information update failed!', 'tutor' ) ); } } else { // Save billing info. $billing_fillable_fields['user_id'] = $order_data->user_id; $save = $billing_model->insert( $billing_fillable_fields ); if ( ! $save ) { tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, $order_data->id, __( 'Billing info save failed!', 'tutor' ) ); } } $update_order_data = $order_model->get_recalculated_order_tax_data( $order_id ); $update_order_data['payment_method'] = $payment_method; $updated = $order_model->update_order( $order_data->id, $update_order_data ); if ( $updated ) { $order_data = $order_model->get_order_by_id( $order_id ); } } $payment_data = $this->prepare_payment_data( (array) $order_data, $payment_method ? $payment_method : $order_data->payment_method, $order_data->order_type ); $this->proceed_to_payment( $payment_data, $payment_method ? $payment_method : $order_data->payment_method, $order_data->order_type ); } catch ( \Throwable $th ) { tutor_log( $th ); tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, $order_data->id, $th->getMessage() ); } } else { $error_msg = __( 'Order not found!', 'tutor' ); tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, $order_id, $error_msg ); } } else { $error_msg = __( 'Invalid order ID!', 'tutor' ); tutor_redirect_after_payment( OrderModel::ORDER_PLACEMENT_FAILED, $order_id, $error_msg ); } } /** * Validate pay now request * * @since 3.0.0 * * @param array $data The data array to validate. * * @return object The validation result. It returns validation object. */ protected function validate_pay_now_req( array $data ) { $order_types = array( OrderModel::TYPE_SINGLE_ORDER, OrderModel::TYPE_SUBSCRIPTION, OrderModel::TYPE_RENEWAL, ); $order_types = implode( ',', $order_types ); $validation_rules = array( 'object_ids' => 'required', 'order_type' => "required|match_string:{$order_types}", 'payment_method' => 'required', ); // Skip validation rules for not available fields in data. foreach ( $validation_rules as $key => $value ) { if ( ! array_key_exists( $key, $data ) ) { unset( $validation_rules[ $key ] ); } } return ValidationHelper::validate( $validation_rules, $data ); } /** * Retrieve course data for a given set of order items. * * @since 3.9.0 * * @param array $order_items Array of order item objects. * @return array{ * courses: array{ * total_count: int, * results: \WP_Post[] * } * } */ public function get_courses_data_by_order_items( $order_items ): array { $results = array(); foreach ( $order_items as $item ) { $course = get_post( $item->id ); if ( $course instanceof \WP_Post ) { $results[] = $course; } } return array( 'courses' => array( 'total_count' => count( $results ), 'results' => $results, ), ); } }