use super::*;
impl ElementConfig {
pub fn new(client_secret: String) -> Self {
Self {
client_secret,
kind: ElementKind::Payment,
locale: String::new(),
}
}
pub fn with_kind(mut self, kind: ElementKind) -> Self {
self.set_kind(kind);
self
}
pub fn with_locale(mut self, locale: String) -> Self {
self.set_locale(locale);
self
}
pub fn effective_locale(&self) -> &str {
let requested: &str = self.get_locale();
if requested.is_empty() {
DEFAULT_LOCALE
} else {
requested
}
}
pub fn is_mountable(&self) -> bool {
!self.get_client_secret().trim().is_empty()
}
pub fn build_options(&self) -> Result<String, ElementError> {
if !self.is_mountable() {
return Err(ElementError::MissingClientSecret);
}
let trimmed: String = format!(
"{{{}:{}{}{}}}",
CLIENT_SECRET_FIELD,
String::from("\""),
self.get_client_secret(),
String::from("\""),
);
Ok(format!(
"{},{}:{}\"{}\"{}}}",
trimmed.strip_suffix('}').unwrap_or(trimmed.as_str()),
LOCALE_FIELD,
String::from("\""),
self.effective_locale(),
String::from("\""),
))
}
pub fn require_stripe_js(available: bool) -> Result<(), ElementError> {
if available {
return Ok(());
}
Err(ElementError::StripeJsUnavailable)
}
pub fn preflight(
&self,
has_mount_target: bool,
stripe_js_loaded: bool,
) -> Result<(), ElementError> {
if !self.is_mountable() {
return Err(ElementError::MissingClientSecret);
}
if !has_mount_target {
return Err(ElementError::MissingMountTarget);
}
Self::require_stripe_js(stripe_js_loaded)
}
}