#' @export
#' @import httr
confirm <- function(pool = pool::dbPool(
RMariaDB::MariaDB(),
host = getOption("CannaData_host"),
port = as.integer(getOption("CannaData_port")),
user = getOption("CannaData_user"),
password = getOption("CannaData_password"),
db = getOption("CannaData_db")
), base_url = getOption("CannaData_baseUrl"),
canna_pubkey = gsub("\n ", "\n", getOption("canna_pubkey"))) {
Sys.setenv("TWILIO_SID" = getOption("TWILIO_SID"),
"TWILIO_TOKEN" = getOption("TWILIO_TOKEN")
)
ui <- function(req) {
query <- lapply(parseQueryString(req$QUERY_STRING), function(x) {
rawToChar(base64enc::base64decode(x))
})
if (length(query) == 0 || all(vapply(query, nchar, integer(1)) == 0)) {
tags$script(sprintf('location.replace(\"%s\");', paste0(base_url, "menu/")))
} else if (query$u != jose::jwt_decode_sig(query$e, canna_pubkey)$secret) {
tags$script(sprintf('location.replace(\"%s\");', paste0(base_url, "menu/")))
} else if (length(q_m_status(pool, query$idtransaction)) == 0 || (q_m_status(pool, query$idtransaction) != 4)) {
tags$script(sprintf('location.replace(\"%s\");', paste0(base_url, "menu/")))
} else {
fluidPage(
title = "Confirm CannaData Order",
tags$head(HTML('<link rel="icon"
type="image/png"
href="https://s3-us-west-2.amazonaws.com/cannadatacdn/CannaData_Logo.png">')),
tags$script(sprintf(
'Shiny.addCustomMessageHandler("leave", function(param) {
setTimeout(function(){ location.replace("%s") }, 5000);
})'
, paste0(base_url, "menu/"))),
h1("Please confirm your order by clicking the box below"),
tags$style(".recaptcha-btn {
display: none;
}"),
recaptchaUI("humans_only", sitekey = "6LfcTTQUAAAAALcaI602Vjwtw0_4xttGQktQJOxO", class = "recaptcha-btn"),
tags$form(id = "submit-btn",
actionButton("finalizes", "Submit"))
)
}
}
msg_service_sid = tw_msg_service_list()[[1]]$sid
server <- function(input, output, session) {
params <- lapply(parseQueryString(isolate(session$clientData$url_search)), function(x) {
rawToChar(base64enc::base64decode(x))
})
info <- reactive({
req(params$idtransaction)
q_m_confirm(pool, params$idtransaction)
})
human <- callModule(recaptcha, "humans_only", "6LfcTTQUAAAAALsjAdB4YWReWK2h7ctFW07x9i7U")
observeEvent(input$finalizes, {
req(params$u, params$e)
req(human()$success)
req(params$u == jose::jwt_decode_sig(params$e, canna_pubkey)$secret)
u_m_validate(pool, params$idtransaction)
showModal(
modalDialog(
h1("Thank you for confirming!")
)
)
session$sendCustomMessage("leave", list(NULL))
tw_send_message(
to = paste0("+1", info()$phone),
msg_service_id = msg_service_sid,
body = "Thank you for confirming your order. You will be notified when your order is ready. We will also let you know if there are any problems processing your order."
)
if (isTruthy(info()$address)) {
task <- httr::with_verbose(onfleet_post_tasks(
destination = list(address = list(unparsed = paste(info()$address, info()$zip, sep = ", "))),
recipients = data.frame(name = info()$name, phone = paste0("+1", info()$phone))
))
# cat(unlist(task), file = stderr())
u_m_onfleet(pool, params$idtransaction, task$id)
}
})
}
shinyApp(ui, server)
}
recaptchaUI <- function(id, sitekey = Sys.getenv("recaptcha_sitekey"), ...) {
ns <- NS(id)
tagList(tags$div(
shiny::tags$script(
src = "https://www.google.com/recaptcha/api.js",
async = NA,
defer = NA
),
tags$script(
paste0("shinyCaptcha = function(response) {
console.log(response);
Shiny.onInputChange('", ns("recaptcha_response"),"', response);
}"
)),
tags$form(
class = "shinyCAPTCHA-form",
action = "?",
method = "POST",
tags$div(class = "g-recaptcha", `data-sitekey` = sitekey, `data-callback` = I("shinyCaptcha"))
)
)#, actionButton(ns("test"),"TEST")
)
}
recaptcha <- function(input, output, session, secret = Sys.getenv("recaptcha_secret")) {
status <- reactive({
if (isTruthy(input$recaptcha_response)) {
url <- "https://www.google.com/recaptcha/api/siteverify"
resp <- httr::POST(url, body = list(
secret = secret,
response = input$recaptcha_response
))
jsonlite::fromJSON(content(resp, "text"))
} else {
list(success = FALSE)
}
})
return(status)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.