Unable to set currency using PayPal JavaScript SDK - javascript

I am trying to set the currency for the PayPal API and I am unable to do so. When you click on the PayPal button, It shows the total amount in USD (next to the cart icon). Under pay with, it shows the amount converted to CAD which is not what I want. I want the amount set in createOrder to be in the currency I set.
<script src="https://www.paypal.com/sdk/js?currency=CAD&client-id="></script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
intent: "CAPTURE",
purchase_units: [{
amount: {
value: "150"
}
}]
});
},
onError: function (err) {
alert(err);
}
}).render('#paypal-button-container');
</script>
I have tried to put currency_code: "CAD" in amount but I get:
Error: Unexpected currency: CAD passed to order.create. Please ensure you are passing /sdk/js?currency=CAD in the PayPal script tag.

Not sure where you're going wrong, since your code would work if it were complete. The following is a working example:
<script src="https://www.paypal.com/sdk/js?currency=CAD&client-id=sb"></script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
intent: "CAPTURE",
purchase_units: [{
amount: {
value: "150",
currency_code: "CAD"
}
}]
});
},
onError: function (err) {
alert(err);
}
}).render('body');
</script>

Related

Paypal checkout javascript integration Debit and credit card option not work

I want to create PayPal payment using javascript SDK. I have followed PayPal documentation https://developer.paypal.com/sdk/js/reference/. Below is the code that I have implemented. The code is working for PayPal payment but when clicking in "Debit or Credit Card", and filling all the billing information, it's not working. When I looked into network request, to URL(https://www.paypal.com/graphql?fetch_credit_form_submit) and it's showing 429 HTTP code with no any response. I have looked lot of videos where this code is working but its not working for me. Any help is really appreciated.
<script src="https://www.paypal.com/sdk/js?client-id=AQTjGimz0db5Gqx2NLBvgXfc-dckdeKGJzJ5KY1JBsv7K5OWYsjipJuh8MX25IoohCWNl09RL4hA24uN"></script>
<script>
paypal.Buttons({
createOrder:function(data,actions){
return actions.order.create({
purchase_units:[{
amount:{
value:'40.99'
}
}]
})
}
}).render('#paypal-button-container');
</script>
<script src="https://www.paypal.com/sdk/js?client-id=AQTjGimz0db5Gqx2NLBvgXfc-dckdeKGJzJ5KY1JBsv7K5OWYsjipJuh8MX25IoohCWNl09RL4hA24uN"></script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// Set up the transaction
return actions.order.create({
purchase_units: [{
amount: {
value: 49.0
}
}]
});
},
onApprove: function(data, actions) {
///.........
}).render('#paypal-button-container');
</script>

Sound not displaying inside script

I want to display a sound inside this script but it is not playing anything.
this is my code:
<script>
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({
application_context: {
shipping_preference: 'NO_SHIPPING',
},
purchase_units: [{
amount: {
value: '1.99'
}
}]
});
},
onApprove: function(data, actions) {
var bleep = new Audio();
bleep.src = "static/payment_success.m4a";
bleep.currentTime=0;
bleep.play();
some more code.....
}).render('#paypal-button-container');
</script>
I'm a bit noob with javascript and don't really know how this could work. The sound is ok, it tested it and works perfectly, but not inside this code. Thanks in advance
var bleep = new Audio();
bleep.setAttribute("src", "static/payment_success.m4a");
bleep.currentTime=0;
bleep.play();

programmatically set Paypal purchase 'value'

I'm trying to build my first paypal shopping payment system. Paypal helpfully provide some base code to aid with this:
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=[MY-CLIENT-ID]&currency=GBP" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
style: {
shape: 'rect',
color: 'white',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '1'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>
I can't seem to figure out how to set the 'value' label inside the function inside of:
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '1'
}
}]
});
My website is in bootstrap 4.5 with jQuery. On the site's index.html page I have 2 buttons that can be clicked to purchase two different services. Depending on which one the user selects, I use javascript to set the value in localStorage as follows: localStorage.setItem('serviceCost', $('#standard-cost').text()); This grabs a value from a span tag with ID 'standard-cost'. This is then used to set the text on the purchase.html page confirming for the user their selected item. I want to get the value from localStorage.getItem(serviceCost) and use it to set the cost for the 'value' key in the above function but everything I attempt is unsuccessful. The paypal script is embedded in a html page and the jQuery is in a separate file. The Paypal site doesn't offer any insight into this and I'm assuming this is a fairly common process, so how do you get a value from localStorage and pass it to the value variable in Paypal's generated code?
amount: {
value: localStorage.getItem('serviceCost')
}
Yep

Paypal smart button checkout without shipping

I'm using Paypal's Smart Payment Buttons with the following code:
window.paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: this.price,
currency: "USD"
}
}]
});
},
...
}
Which works fine except for the fact that it asks the user for a shipping address on the checkout window, which I want to get rid of as this is a digital good and not a physical one. How can I achieve that? I've tried doing no_shipping: 1 and similar to no avail.
You can use application_context and shipping_preference like this
return actions.order.create({
"application_context": {
shipping_preference: "NO_SHIPPING",
},
purchase_units: [{
amount: {value: "0.1"},
},],
});
Figured it out, you need to create a payment instead of an order in the server side using a payment experience you have previously created.

Customizing Paypal Checkout Variables

Hi i have been searching around with how I can customize the paypal variable for checkoutJS more specifically with this function here
payment: function (data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: 'cost', currency: 'PHP' }
}
]
}
});
},
I need to be able to edit the 'cost' based on a text of my div.
i tried doing this ( forgive me if this is stupid ) :
var cost = document.getElementById('cost').InnerText;
payment: function (data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: cost, currency: 'PHP' }
}
]
}
});
},
Now, when i click the "Buy now" button a popup window appears with a spinner then closes.
I am trying to this in ASP MVC if it is of any relevance.
Help would be very much appreciated!

Categories

Resources