How to pass templated variables to server-side handler functions in HTML files using Google Apps Script?

时间秒杀一切 提交于 2020-04-30 06:25:26

问题


Follow up to this answer. https://stackoverflow.com/a/60073413/1640892

I want to pass a templated object variable to a server side function in GAS. I expect to see the buttons render, then after filling out the payment details and clicking pay, I expect to see the handleTxn() function successfully execute using the content object as one of its arguments.

But, instead, nothing happens because the buttons do not even render. It fails silently.

var content = <?!= content ?>;
google.script.run.handleTxn(data, details, content);

How do I now access the content object to pass it to a server function?

Code.gs
[...]
var template = HtmlService.createTemplateFromFile(HTML_FILENAME);
template.content = values;
var htmlOutput = template.evaluate();
[...]

function handleTxn(data, details, content) {
  // do stuff
}
index.html
<!DOCTYPE html>
  <!-- source: https://developer.paypal.com/docs/checkout/integrate/ -->
  <html>
    <body>
      <div class="container">
        Price: $<?= content.price ?></td>
        <div id="paypal-button-container"></div>
      </div>
      <script
        src="https://www.paypal.com/sdk/js?client-id=SB_CLIENT_ID">
      </script> 
      <script>
        // This function displays Smart Payment Buttons on your web page.
        paypal.Buttons({
          createOrder: function(data, actions) {
            // This function sets up the details of the transaction, including the amount and line item details.
            return actions.order.create({
              purchase_units: [{
                amount: {
                  value: <?!= content.price ?>
                }
              }]
            });
          },
          onApprove: function(data, actions) {
            // This function captures the funds from the transaction.
            return actions.order.capture().then(function(details) {
              var alertText = (
                '\nWe charged you: $'
                +
                <?!= content.price ?>
              );
              alert( alertText );
              var content = <?!= content ?>;
              google.script.run.handleTxn(data, details, content);
            });
          }
        }).render('#paypal-button-container');
      </script>
    </body>
  </html>

来源:https://stackoverflow.com/questions/60074230/how-to-pass-templated-variables-to-server-side-handler-functions-in-html-files-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!