'POST', 'callback' => [ $this, 'validate_promo' ], 'permission_callback' => [ $this, 'check_permission' ], 'args' => [ 'code' => [ 'required' => true, 'type' => 'string', ], 'plan' => [ 'required' => true, 'type' => 'string', ], 'plan_variant' => [ 'required' => false, 'type' => 'string', ], ], ] ); } /** * POST /mintall/v1/promo/validate */ public function validate_promo( $request ) { $api_key = $this->get_api_key(); if ( is_wp_error( $api_key ) ) { return $api_key; } $code = sanitize_text_field( $request->get_param( 'code' ) ); $plan = sanitize_text_field( $request->get_param( 'plan' ) ); $plan_variant = sanitize_text_field( $request->get_param( 'plan_variant' ) ); $result = Mintall_Promo::validate( $api_key, [ 'code' => $code, 'plan' => $plan, 'plan_variant' => $plan_variant, 'currency' => get_woocommerce_currency(), 'shop_domain' => wp_parse_url( home_url(), PHP_URL_HOST ), ] ); if ( is_wp_error( $result ) ) { $error_data = $result->get_error_data(); $detail = $result->get_error_message(); $error_messages = [ 'PROMO_CODE_INVALID' => 'Promo code not found.', 'PROMO_CODE_INACTIVE' => 'Promo code is not active.', 'PROMO_CODE_EXPIRED' => 'Promo code has expired.', 'PROMO_CODE_NOT_APPLICABLE' => 'Promo code does not apply to this plan.', 'PROMO_CODE_LIMIT_REACHED' => 'Promo code redemption limit reached.', 'PLATFORM_ACCOUNT_UNAUTHORIZED' => 'Account not authorized for promo codes.', ]; $message = $error_messages[ $detail ] ?? $detail ?: 'Promo code is invalid.'; return $this->error_response( $message ); } return new \WP_REST_Response( $result ); } }