成功完成贝宝交易R Shiny后如何下载excel文件

时间:2020-05-23 12:30:17

标签: r paypal shiny shinydashboard

我正在尝试将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。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的挑战,希望我闪亮的应用程序的一部分对成功的贝宝付款做出反应。我在这里找到了我的解决方案:

https://shiny.rstudio.com/articles/communicating-with-js.html

在包含用于添加 PayPal 结帐按钮的代码的 UI 中,我使用“setInputValue”函数添加了一行:

window.alert('Thank you for your purchase!');
Shiny.setInputValue('payment', 'success');

然后在服务器端,我使用事件观察器来显示两种可能的输出之一:

  observeEvent(input$payment,{
      if(input$payment!='success'){
        output$mymap <- renderLeaflet({
          Option A
        })
       } else if(input$payment=='success'){
        output$mymap <- renderLeaflet({
          Option B
        })
      }
   })