成功完成Paypal交易后如何下载Excel文件 - R Shiny

5

我正在尝试将Paypal集成到R Shiny应用程序中。 我已成功设置了沙盒并确认了付款。 但是,我无法弄清楚如何使应用程序在成功付款后立即下载CSV文件或显示一个新按钮以允许下载。 基本上,我想按每次下载我的清理数据集的便宜价格收费,并将其保存为CSV格式。以下是UI /服务器部分,包括我尝试过的某些反应性东西(我一直在尝试使用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或条件语句来显示下载按钮。但真正伟大的是,如果有人能够找出完整的解决方案! - dca
1个回答

0

我有一个类似的挑战,希望我的Shiny应用程序的一部分能够响应成功的Paypal付款。我在这里找到了解决方案:

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

在我的用户界面中,我包含了添加PayPal结帐按钮的代码,并使用“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
        })
      }
   })

我也在寻找解决这个问题的办法。我喜欢这个解决方案,但我担心它不安全。对于了解JS的人来说,难道不是很容易调用Shiny.setInputValue('payment', 'success');这一行而不付款吗?我对JS的了解不多,所以可能是我错了。 - TheDza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接