【发布时间】:2020-09-10 07:11:49
【问题描述】:
我正在尝试将 Paypal 集成到 R Shiny 应用程序中。我已成功设置沙盒并确认付款。但是,我不太清楚如何让应用程序立即下载 csv 文件或在成功付款后显示一个新按钮以允许下载。基本上,我想在每次下载我清理的数据集作为 csv 时收取便宜的费用。以下是 ui/server 部分,包括我尝试过的一些反应性的东西(我一直在尝试使用 shinyJS 来绕过它但无济于事)。
#ui
fluidRow(
column(width = 12,
boxPlus(
title = 'Bulk Financial Download - By Ticker',
closable = FALSE,
width = 12,
status = "info",
solidHeader = FALSE,
collapsible = TRUE,
enable_dropdown = FALSE,
fluidRow(
column(width = 3,
actionButton("Calculate",
label = ui <- fluidPage(
tags$script(src = "https://www.paypalobjects.com/api/checkout.js "),
tags$script("paypal.Button.render({
// Configure environment
env: 'sandbox',
client: {
sandbox: 'hidden',
production: 'demo_production_client_id'
},
// Customize button (optional)
locale: 'en_US',
style: {
size: 'small',
color: 'gold',
shape: 'pill',
},
// Set up a payment
payment: function (data, actions) {
return actions.payment.create({
transactions: [{
amount: {
total: '0.01',
currency: 'USD'
}
}]
});
},
// Execute the payment
onAuthorize: function (data, actions) {
return actions.payment.execute()
.then(function () {
// Show a confirmation message to the buyer
window.alert('Thank you for your purchase!');
});
}
}, '#Calculate');"),
tags$div(id = "Calculate")))
),
column(
width =3,
hidden(div(
id='actions',
downloadButton("downloadData", "Download")
))
)
)
)
)
)
#server
shinyjs::hide('actions')
observeEvent(input$Calculate, {
shinyjs::hide('Calculate')
shinyjs::show('actions')
})
observeEvent(input$ticker, {
shinyjs::hide('actions')
shinyjs::show('Calculate')
})
output$downloadData <- downloadHandler(
filename = function() {
paste(stockInfo()$symbol, "financials.csv", sep = "")
},
content = function(file) {
write.csv(tables(), file, row.names = FALSE)
}
)
到目前为止,在我的尝试中,Paypal 按钮周围有一个按钮可以激活shinyjs 显示或隐藏,但是当我直接单击 Paypal 按钮而不是实际付款时,什么也没有发生。我也在使用 shinydashboard 和 shinydashboardPlus。
【问题讨论】:
-
我认为一种方法是将自定义消息放入 onAuthorize 回调函数中。然后,让 Shiny 收听,如果成功,使用 RenderUI 或条件显示下载按钮。但如果有人能想出一个完整的解决方案,那就太好了!
标签: r paypal shiny shinydashboard