PayPal Smart Payments: changing the currency code - paypal

I'm trying to set up PayPal Smart Payments on a webpage. I'm using the example they give on here: https://developer.paypal.com/docs/checkout/integrate/
If I have currency_code set to USD it works fine, but if I change it to anything else, such as CAD or GBP the window won't load. What am I doing wrong?
<script src="https://www.paypal.com/sdk/js?client-id=sb"></script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
'purchase_units': [{
'amount': {
'currency_code': 'USD',
'value': '5',
},
}]
})
}
}).render('body')
</script>
For some reason this example won't run here on Stack Overflow, but it runs fine on JSFiddle, so I have made two examples with the currency_code set differently.
'currency_code': 'USD': https://jsfiddle.net/liquidmetalrob/8y3p52fh/
'currency_code': 'GBP': https://jsfiddle.net/liquidmetalrob/8y3p52fh/1
The first example will load the PayPal window, and you need a PayPal Sandbox account to log into it. So if you want to log in you can use the throwaway account that I just created. Username: sb-ilukn1050819#personal.example.com password: pRKCu9.> But the important question is why does the window not even load in the second example?

I found the answer here: https://developer.paypal.com/docs/checkout/reference/customize-sdk/
You have to add the currency code to the script URL instead, and remove it from the JS.
<script
src="https://www.paypal.com/sdk/js?client-id=sb&currency=GBP">
</script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
'purchase_units': [{
'amount': {
'value': '5',
},
}]
})
}
}).render('body')
</script>
Note: The client-id can be set to sb for testing, but in production you use your own one.

Related

PayPal integration with PHP: which API and credentials to use?

I am trying to integrate paypal api with PHP compared to the old button form that I have used to date on my sites. But there is one thing that is not clear to me, is it more correct to integrate paypal with client_id and secret or through the codes provided in the account panel (api username, api password and signature)? I followed the REST API integration guide (version 2) but they require client_id and secret. So what is the data in the account panel for? Anyone can clarify my ideas? Thank you
The API username, password, and signature is used by the classic NVP/SOAP APIs, which are much older than the REST API. They exist only for backwards compatibility with old shopping cart software and such integrations.
The v2/checkout/orders API should be used for current PayPal Checkout integrations. Pair two routes on your server (one for create order, one for capture order) that return/output only JSON data (never any HTML or text) with this JS approval flow.
I would go with JS SDK inline integration - requires client-id only and is more flexible than checkout buttons. Also creates nice user experience as if staying on the page (no redirects to 3rd party site). See all demos here.
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
// note: custom_id (127 chars) will be passed later back in capture
purchase_units: [{
amount: {
value: amtDue.toFixed(2),
currency: 'EUR'
},
description : description,
custom_id : '<?= $encryptedPaymentData ?>'
}]
});
},
onApprove: function(data, actions) {
$("#global-spinner").show();
// set custom capture handler
return actions.order.capture().then(function(details) {
$.ajax({
type: "POST",
url: "/paypal/success",
data: ({
details : details,
ad : amtDue,
desc : description,
_token : '<?= $csrf ?>'
}),
success: function(resp) {
$("#global-spinner").hide();
window.showThankYou(); // some "thank you" function
},
error: function() {
$("#global-spinner").hide();
alert("Connection error.");
}
});
});
},
onError: function (err) {
// some custom function - send error data to server logger
window.handlePaypalError(err, description, amtDue);
}
}).render('#paypal-button-container');

Can i get payments to my paypal account with help of paypal api

Is it possible to sell some non-physical product and get its payment directly into your personal paypal account? Has anyone did something like this?
It is certainly possible, and actually very easy. What kind of integration are you looking for? Maybe one of the buttons at http://www.paypal.com/buttons would be a good start for you.
Here is a sample of a VERY Simple checkout Script using REST API (Client Side Only Code)
First add this line to "head" of your HTML page:
{
<script src="https://www.paypal.com/sdk/js?client-
id=YOUR USER ID GOES HEREg&disable-funding=credit,card"></script>
}
Then add this to where you want your PayPal Button(s) to be on your HTML Page:
'<div style="width:30%" id="paypal-button-container"><p style="font-size:small">
<b>Checkout using your PayPal Acount <br>
or if you do not have a PayPal Account, you can pay with your Debit or Credit
Card (option at bottom of Pop-up Window).</b></p></div>
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
onError: function (err) {
alert('An Error Occured, returning you to the Form. Please Check that you
are not submitting a Zero Amount and have Filled out the Form');
},
style: {
layout: 'horizontal',
color: 'gold',
shape: 'pill',
label: 'checkout',
size: 'responsive',
tagline: 'true',
},
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
description: 'GnG Order',
amount: {
value: cartTotal
}
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name +
'!');
});
if (details.error === 'INSTRUMENT_DECLINED') {
return actions.restart();
};
}
}).render('#paypal-button-container');
</script>'
That should do it for you. Be sure to login to the Developer Panel to set up your API credentials.

PayPal Express Checkout Shipping Calculations

I would like to:
Have the user log into paypal
Fetch the users information (billing&shipping addresses)
Then calculate the shipping fee
Place the payment
But, how can I receive the address and calculate the fee before I place the payment?
<script>
paypal.Button.render({
// Configure environment
env: '<?php echo $paypal->paypalEnv; ?>',
client: {
sandbox: '<?php echo $paypal->paypalClientID; ?>',
production: '<?php echo $paypal->paypalClientID; ?>'
},
// Customize button (optional)
locale: 'de_DE',
style: {
size: 'small',
color: 'gold',
shape: 'rect',
},
// Set up a payment
payment: function (data, actions) {
return actions.payment.create({
transactions: [{
amount: {
total: '10',
currency: 'EUR'
}
}]
});
},
// Execute the payment
onAuthorize: function (data, actions) {
return actions.payment.execute()
.then(function () {
window.location = "process.php?paymentID="+data.paymentID+"&token="+data.paymentToken+"&payerID="+data.payerID+"&pid=<?php echo $productID; ?>";
});
}
}, '#paypal-button');
</script>
Instead of "return actions.payment.execute()" calling the execute directly, I would like to fetch the address, then show the final basket with all the costs and then click on "pay now" - how can I do this?
Your sample is an older version of PayPal's checkout.js
Start by switching to the latest version of the PayPal Checkout: https://developer.paypal.com/docs/checkout/
Demo of client-side pattern: https://developer.paypal.com/demo/checkout/#/pattern/client
In the onApprove function, you can use the data object to get the address and do Javascript calculations about adding shipping before the capture() call with a modified total.
If you want to do the calculations on your server rather than in Javascript (more secure/reliable), use a server-side pattern:
https://developer.paypal.com/demo/checkout/#/pattern/server
thank you! mhhh okay but what has to be in the serverside part? i dont see any example of the actions to receive the buyers adress information before checkout and afterwards. ?
/demo/checkout/api/paypal/order/create

How to dynamically change currency & pass other data in PayPal Express Checkout

I want to accept payment in Multiple currencies & also autofill the Address/Name in PayPal form using Express Checkout method
I have tried going through various posts, paypal getting started, paypal community, but not able to find if it possible.
Currently, using Express Checkout I'm able to receive payment on changing the get parameter of the script.
<script src="https://www.paypal.com/sdk/js?client-id=valueOfClientID&currency=GBP"> </script>
Is is possible to pass the currency & name/address details in below code?
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '24'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
// Call your server to save the transaction
return fetch('/paypal-transaction-complete', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
});
});
}
}).render('#paypal-button-container');
</script>
Based on the currency dynamically inject the script to the page and pass the address object as mentioned in the link

paypal Javascript Integration - What's form Action Parameter?

What is "/checkout" in the below line ?. Actually it referring localpath.
Can you please suggest me the appropriate action URL ?
Also, Can you suggest me where do I give the IPN URL / Syntax ?
<form id="myContainer" method="post" action="/checkout"></form>
<script>
window.paypalCheckoutReady = function () {
paypal.checkout.setup('<Your-Merchant-ID>', {
environment: 'sandbox',
container: 'myContainer'
});
};
</script>
[paypal Integration] https://developer.paypal.com/docs/classic/express-checkout/in-context/integration/
Thanks,
Raja K
This describes it:
A basic Express Checkout integration assumes that you are sending the
API calls from your own server using a <form> or <a>.
Essentially, your existing call to SetExpressCheckout (that would obtain a Paypal token) that you use to redirect (in the "standard" Paypal Express Checkout flow).
The linked sample should hopefully clear things up - you'll see the form action (and a link) POST (GET for the a) to some server implementation (the ip address in the sample) of SetExpressCheckout.
Hth..
I can help you with single page PayPal pay now button actually I use this one of my projects
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXXXXXXXXX">
</script>
<div id="paypal-button-container"> </div>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '1230'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed');
// Call your server to save the transaction
return fetch('codes/paypalapi.php?invo=123', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID,
amount: data.amount
})
});
});
}
}).render('#paypal-button-container');
</script>
change codes/paypalapi.php?invo=123 with your callback url