I'm creating a integration with a payment service. The payment service provides me a form with a script tag inside.
My question is a continuation from Insert a script tag inside template Vue
The form with checkout of payment service:
<form action="http://localhost:8081/api/v1/payment/" method="POST">
<script
src="https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js"
data-public-key="KEY"
data-transaction-amount="14.90">
</script>
</form>
I can make the next on "mounted()" of vuejs:
<form ref="myform">
...
</form>
mounted() {
let foo = document.createElement('script');
foo.setAttribute("src","https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js");
foo.setAttribute("data-transaction-amount", this.newAmount)
this.$refs.myform.appendChild(foo);
}
But, my problem is that after the view has been mounted. the user can change "data-transaction-amount".
To solve it , I tried:
data:()=>({
newAmount:0
})
watch: {
newAmount() {
this.modifyScript();
},
},
methods: {
modifyScript() {
let scripts = document.getElementsByTagName("script");
for (let i = 0; i < scripts.length; i++) {
let script = scripts[i];
if (script.getAttribute("src") == 'https://www.mercadopago.com.br/integrations/v1/web-tokenize-checkout.js') {
// we've got a match
script.setAttribute("data-transaction-amount", this.newAmount);
}
}
},
The "data-transaction-amount" is changing to new value, but the window checkout of payment service shows the original value used in "mounted()".
scripts[i].setAttribute("data-transaction-amount", this.newAmount);?