'api_key', 'label' => __( 'API Key', 'adev-otp-login' ), 'type' => 'text', 'desc' => __( 'SMS.ir panel API key (x-api-key)', 'adev-otp-login' ), ), array( 'key' => 'send_mode', 'label' => __( 'Send mode', 'adev-otp-login' ), 'type' => 'select', 'options' => array( 'verify' => __( 'Verify (template)', 'adev-otp-login' ), 'simple' => __( 'Simple send (bulk)', 'adev-otp-login' ), ), ), array( 'key' => 'template_id', 'label' => __( 'Template ID', 'adev-otp-login' ), 'type' => 'text', 'desc' => __( 'Numeric template ID from SMS.ir → Developers → Ultra Fast Send', 'adev-otp-login' ), ), array( 'key' => 'parameter_name', 'label' => __( 'Template parameter name', 'adev-otp-login' ), 'type' => 'text', 'desc' => __( 'Variable name in the template without # (e.g. CODE for #CODE#)', 'adev-otp-login' ), ), array( 'key' => 'line_number', 'label' => __( 'Line number', 'adev-otp-login' ), 'type' => 'text', 'desc' => __( 'Required for simple send mode (your SMS.ir line number).', 'adev-otp-login' ), ), array( 'key' => 'message', 'label' => __( 'Message text (simple send)', 'adev-otp-login' ), 'type' => 'textarea', 'desc' => __( 'Use {OTP} as a placeholder for the code.', 'adev-otp-login' ), ), ); } /** * {@inheritdoc} */ public function send_otp( string $phone, string $otp, array $context = array() ): array { $cfg = Plugin::get_setting( 'gateways', array() ); $cfg = isset( $cfg['smsir'] ) && is_array( $cfg['smsir'] ) ? $cfg['smsir'] : array(); $api_key = isset( $cfg['api_key'] ) ? trim( (string) $cfg['api_key'] ) : ''; if ( '' === $api_key ) { return array( 'success' => false, 'message' => __( 'SMS.ir API key is not configured.', 'adev-otp-login' ), ); } $mode = isset( $cfg['send_mode'] ) ? $cfg['send_mode'] : 'verify'; $mobile = $this->to_local( $phone ); if ( 'verify' === $mode ) { $payload = $this->build_verify_payload( $cfg, $mobile, $otp ); if ( isset( $payload['error'] ) ) { return $payload['error']; } $url = 'https://api.sms.ir/v1/send/verify'; $body = $payload['body']; } else { $payload = $this->build_bulk_payload( $cfg, $mobile, $otp ); if ( isset( $payload['error'] ) ) { return $payload['error']; } $url = 'https://api.sms.ir/v1/send/bulk'; $body = $payload['body']; } $response = wp_remote_post( $url, array( 'timeout' => 30, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'text/plain', 'x-api-key' => $api_key, ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $response ) ) { return array( 'success' => false, 'message' => $response->get_error_message(), ); } $http = (int) wp_remote_retrieve_response_code( $response ); $raw = wp_remote_retrieve_body( $response ); $data = json_decode( $raw, true ); if ( ! is_array( $data ) ) { return array( 'success' => false, 'message' => sprintf( /* translators: 1: HTTP code, 2: body */ __( 'Invalid SMS.ir response (HTTP %1$d): %2$s', 'adev-otp-login' ), $http, $raw ? mb_substr( $raw, 0, 200 ) : '-' ), 'raw' => $raw, ); } $status = isset( $data['status'] ) ? (int) $data['status'] : 0; $message = isset( $data['message'] ) ? (string) $data['message'] : ''; if ( 1 === $status ) { return array( 'success' => true, 'message' => $message ? $message : __( 'Sent successfully', 'adev-otp-login' ), 'raw' => $data, ); } $mapped = $this->map_status_message( $status ); $msg = $mapped ? $mapped : ( $message ? $message : __( 'SMS.ir error', 'adev-otp-login' ) ); return array( 'success' => false, 'message' => $msg, 'raw' => $data, ); } /** * Build verify (template) request body. * * @param array $cfg Gateway config. * @param string $mobile Local phone. * @param string $otp OTP code. * @return array{body?:array,error?:array{success:bool,message:string}} */ private function build_verify_payload( array $cfg, string $mobile, string $otp ): array { $template_id = isset( $cfg['template_id'] ) ? trim( (string) $cfg['template_id'] ) : ''; if ( '' === $template_id || ! ctype_digit( $template_id ) ) { return array( 'error' => array( 'success' => false, 'message' => __( 'SMS.ir template ID is not configured.', 'adev-otp-login' ), ), ); } $param = isset( $cfg['parameter_name'] ) ? trim( (string) $cfg['parameter_name'] ) : 'CODE'; $param = trim( $param, "# \t\n\r\0\x0B" ); if ( '' === $param ) { $param = 'CODE'; } return array( 'body' => array( 'mobile' => $mobile, 'templateId' => (int) $template_id, 'parameters' => array( array( 'name' => $param, 'value' => $otp, ), ), ), ); } /** * Build bulk (simple) request body. * * @param array $cfg Gateway config. * @param string $mobile Local phone. * @param string $otp OTP code. * @return array{body?:array,error?:array{success:bool,message:string}} */ private function build_bulk_payload( array $cfg, string $mobile, string $otp ): array { $line = isset( $cfg['line_number'] ) ? preg_replace( '/\D+/', '', (string) $cfg['line_number'] ) : ''; if ( ! is_string( $line ) || '' === $line ) { return array( 'error' => array( 'success' => false, 'message' => __( 'Line number is required for simple send.', 'adev-otp-login' ), ), ); } $message = isset( $cfg['message'] ) ? (string) $cfg['message'] : 'Your verification code: {OTP}'; $message = str_replace( '{OTP}', $otp, $message ); if ( '' === trim( $message ) ) { return array( 'error' => array( 'success' => false, 'message' => __( 'Message is empty.', 'adev-otp-login' ), ), ); } return array( 'body' => array( 'lineNumber' => (int) $line, 'messageText' => $message, 'mobiles' => array( $mobile ), ), ); } /** * Map SMS.ir status codes to readable messages. * * @param int $status API status. */ private function map_status_message( int $status ): string { $map = array( 0 => __( 'Your request failed.', 'adev-otp-login' ), 10 => __( 'API key is invalid.', 'adev-otp-login' ), 11 => __( 'API key is disabled.', 'adev-otp-login' ), 12 => __( 'API key is restricted to defined IPs.', 'adev-otp-login' ), 13 => __( 'User account is inactive.', 'adev-otp-login' ), 14 => __( 'User account is suspended.', 'adev-otp-login' ), 15 => __( 'Upgrade your plan to use the web service.', 'adev-otp-login' ), 16 => __( 'Submitted parameter value is invalid.', 'adev-otp-login' ), 20 => __( 'Request rate limit exceeded.', 'adev-otp-login' ), 101 => __( 'Line number is invalid.', 'adev-otp-login' ), 102 => __( 'Insufficient panel credit.', 'adev-otp-login' ), 103 => __( 'Message is empty.', 'adev-otp-login' ), 104 => __( 'Recipient number is invalid.', 'adev-otp-login' ), 113 => __( 'Template was not found.', 'adev-otp-login' ), 114 => __( 'Parameter value exceeds the 25-character limit.', 'adev-otp-login' ), 115 => __( 'Mobile number is on the system blacklist.', 'adev-otp-login' ), ); return isset( $map[ $status ] ) ? $map[ $status ] : ''; } /** * Local 09… format for SMS.ir. * * @param string $phone Phone. */ private function to_local( string $phone ): string { $digits = preg_replace( '/\D+/', '', $phone ); if ( ! is_string( $digits ) ) { return ''; } if ( 0 === strpos( $digits, '98' ) && 12 === strlen( $digits ) ) { return '0' . substr( $digits, 2 ); } if ( 0 === strpos( $digits, '9' ) && 10 === strlen( $digits ) ) { return '0' . $digits; } return $digits; } }