I'm using following js code to show paypal button on a page. I add two radio button to select amount of payment, but I can't set it. I save amount in variable named importoSelezionato. But if I replace 50 with variabled,it not work. How replace correctly number in purchaseunit with variable? Thanks
<script>
function initPayPalButton() {
// Radiobutton IMPORTO $(document.getElementsByName('CustImporto')).on('click',function(){
var getSelectedValue = document.querySelector( 'input[name="CustImporto"]:checked');
//alert("importo selezionato: euro "+getSelectedValue.value);
var importoSelezionato = getSelectedValue.value;
});
// Radiobutton IMPORTO
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'horizontal',
label: 'pay',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"description":"Ricarica standard account","amount":{"currency_code":"EUR","value":50}}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Full available details
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
// Show a success message within this page, e.g.
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
//aggiungere un log in una tabella di database
// Or go to another URL: actions.redirect('thank_you.html');
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
<div class="row">
<div class="col-lg-6 in-gp-tl">
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="CustImporto" required="" value="25" aria-label="...">
</span>
<input type="text" class="form-control" readonly="readonly" value="25" aria-label="...">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
<div class="col-lg-6 in-gp-tb">
<div class="input-group">
<span class="input-group-addon">
<input type="radio" name="CustImporto" required="" value="50" aria-label="...">
</span>
<input type="text" class="form-control" readonly="readonly" value="50" aria-label="...">
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
Query the DOM from createOrder
, which runs when a PayPal button is clicked. No need for an onclick listener...
2024 edit: actions.order.create and actions.order.capture are now deprecated and should not be used in any new integration. The code in this question/answer is obsolete, only create and capture orders via API from a server. DOM querying can still be used, but amounts should be sent to a server via a body
payload or similar where it can validate their appropriateness.
<script>
function initPayPalButton() {
function getAmount() {
var getSelectedValue = document.querySelector( 'input[name="CustImporto"]:checked');
//alert("importo selezionato: euro "+getSelectedValue.value);
return getSelectedValue.value;
};
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'horizontal',
label: 'pay',
},
createOrder: function(data, actions) {
return actions.order.create({
"purchase_units": [
{
"amount": {
"currency_code": "EUR",
"value": getAmount()
},
"description": "Ricarica standard account"
}
]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Full available details
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
// Show a success message within this page, e.g.
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
//aggiungere un log in una tabella di database
// Or go to another URL: actions.redirect('thank_you.html');
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>