问题
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?
[...]
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