unable to show amount with paypal express checkout - paypal

I am using the demo code with my own client id please check the code, i am not able to show the amount and current ,Also it doesnt redirects to the return url.
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<div id="paypal-button-container"></div>
<script>
// Render the PayPal button
paypal.Button.render({
// Set your environment
env: 'sandbox', // sandbox | production
// Specify the style of the button
style: {
label: 'checkout', // checkout || credit
size: 'small', // tiny | small | medium
shape: 'pill', // pill | rect
color: 'blue' // gold | blue | silver
},
// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'AZDxjDScFpQtjWTOUtWKbyN_bDt4OgqaF4eYXlewfBP4-8aqX3PiV8e1GWU6liB2CUXlkA59kJXE7M6R',
production: 'Aco85QiB9jk8Q3GdsidqKVCXuPAAVbnqm0agscHCL2-K2Lu2L6MxDU2AwTZa-ALMn_N0z-s2MXKJBxqJ'
},
// Wait for the PayPal button to be clicked
payment: function() {
// Make a client-side call to the REST api to create the payment
return paypal.rest.payment.create(this.props.env, this.props.client, {
transactions: [
{
amount: { total: '0.01', currency: 'USD' }
}
]
});
},
// Wait for the payment to be authorized by the customer
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function() {
document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
});
}
}, '#paypal-button-container');
</script>
this is the demo provided on their website.I am new to this paypal integration.please help me out!
Thanks

Please add commit: true,
payment: function() {
// Make a client-side call to the REST api to create the payment
return paypal.rest.payment.create(this.props.env, this.props.client, {
transactions: [
{
amount: { total: '0.01', currency: 'USD' }
}
]
});
},
commit: true,

Related

Remove shipping address from paypal express checkout

I am using express checkout in my website. I want to disable shipping address from it while completing the transaction. i am using script on a button. Piece of code that i am using is this.
paypal.Button.render({
env: 'production', // sandbox | production
client: {
sandbox: 'mykey',
production: 'mykey'
},
// Show the buyer a 'Pay Now' button in the checkout flow
commit: true,
// payment() is called when the button is clicked
payment: function(data, actions) {
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '5.00', currency: 'EUR' }
}
]
}
});
},
// onAuthorize() is called when the buyer approves the payment
onAuthorize: function(data, actions) {
// Make a call to the REST api to execute the payment
return actions.payment.execute().then(function() {
window.location = "address";
});
}
}, '#paypal-button-container');
Help will be really appreciated.Thanks
In case you are still looking for a solution the shipment address can be disabled by adding the following lines:
experience: {
input_fields: {
no_shipping: 1
}
}
So your code needs to be adjusted like this:
...
// Make a call to the REST api to create the payment
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '5.00', currency: 'EUR' }
}
],
experience: {
input_fields: {
no_shipping: 1
}
}
}
});
...

Paypal Express Checkout not working with production credentials

I have a paypal express checkout button that is working fine with the sandbox account but when I switch to production the payment is not going through, instead after logging in through paypal it says 'Return to Merchant'. The registered business account is verified and I can see api calls being received in dashboard.
paypal.Button.render({
env: "production", // sandbox | production
client: {
sandbox: /* sandbox client id */
production: /* production client id */
},
payment: function (data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: {total: '1', currency: 'USD'}
}
]
}
});
},
onAuthorize: function (data, actions) {
return actions.payment.get().then(function (data) {
console.log(data)
});
}
}, '#paypal-button-container');

PayPal REST API / paypal-checkout Javascript integration: setting payee, permission denied

I'm having a problem using the REST API along with the
paypal-checkout javascript integration
(https://github.com/paypal/paypal-checkout) to set a third party as the
payment receiver.
According to this
https://devblog.paypal.com/setting-payee/
a third-party payee should be allowed now. Correct?
The heart of the matter seems to be this call:
paypal.rest.payment.create(button_member.env,
button_member.client,
pp_data_member);
where button_member.env is "sandbox"
and button_member.client is
{"sandbox":"Ab**********0O","production":"AW**********38"}
When pp_data_member is
{"intent":"authorize",
"payer":{"payment_method":"paypal"},
"transactions":[
{ "amount":{"total":"5.50",
"currency":"USD",
"details":{
"subtotal":"5.00","tax":"0.00","shipping":"0.50"
}},
"payee":{"email":"development-facilitator#troc*****.com"},
"soft_descriptor":"ZenKitsch",
"notify_url":
"https://www.trocadero.com/_internal/alpha/**********",
"description":"Your Zen Kitsch order of September 16, 2017",
"invoice_number":"CHKT-zenkitsch-479618-SV16M8PSFG",
"item_list":{
"items":[{"sku":"844697","name":"United Airlines Beverage Menu Card","quantity":"1","price":"5.00","currency":"USD"}]
}
}
]
}
all works fine. But when I set the payee to a different sandbox address and
nothing else changes:
...
"payee":{"email":"development-seller#troc*****.com"},
...
while I get the popup as expected, after I log in with the buyer sandbox
account and confirm the purchase, I get a failure back from the API, a
400 status code and a "contingency":"PERMISSION_DENIED" error message:
Request URL: https://www.sandbox.paypal.com/webapps/hermes/api/payment/PAY-9TX329794L770414LLG6YSZQ/execute
Request Method:POST
Status Code:400 Bad Request
Remote Address:173.0.82.77:443
{"ack":"contingency",
"contingency":"PERMISSION_DENIED",
"meta":
{"calc":"e305762ccebe",
"rlog":"7Jz2DIxLg7B25ErQ1yIJI2shyI%2FIqDJZY2fbMMEef%2FVnBUNHVn2YYp4ptK5fN6DoGzjRHFjL6nRZkZqdnHIRwQ_15e8c60e8a8"
},
"server":"6ajNNYfBIhVmBs2ogHWhF35ezBlTaOHlCtx2erpwzk-5XmIY9isE5Hk07x5GBputw5DXmqsjLE71y3mJtJHw6TPBWcgBGzPpOkEXIWpJVOPsgsani23mTTQPfrrhKtd2pLLSFQDXQ0ISqpWOC0vZa4DvUshowfLFfFn_oNyAB_gquO5vWd1wIZOEp8z6EiUAnjsDBhCv1K1xBjn6KsOFyczIThuWeoh86RnPLJnhzpqXWAuSDCof_00kGVXCskQDxDqLpa4-0Ry"
}
Any help greatly appreciated. Thanks!
Edited to add: small code to demo the issue. The first button with payee set to app owner works, the second button fails with an error that can be ssen in the console.
<html><head><title>PP Button Test</title></head>
<body>
<script src="https://www.paypalobjects.com/api/checkout.js" data-version-4></script>
<div id="myContainerElement1"></div>
<hr>
<div id="myContainerElement2"></div>
<script>
paypal.Button.render({
client: {
sandbox: 'AbR*****T0O',
production: 'xxxxxxxxx'
},
payment: function(data, actions) {
return actions.payment.create({
transactions: [
{
amount: {
total: '1.00',
currency: 'USD'
},
payee:{email:'development-facilitator#troc*****.com'},
}
]
});
},
commit: true,
locale: 'en_US',
style: {
size: 'medium', // tiny, small, medium
color: 'blue', // orange, blue, silver
shape: 'pill' // pill, rect
},
env: 'sandbox', // Optional: specify 'sandbox' or 'production'
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function(response) {
console.log('payment 1 completed!');
});
},
onCancel: function(data) {
console.log('payment 1 was cancelled!');
}
}, '#myContainerElement1');
paypal.Button.render({
client: {
sandbox: 'AbRF38w5WaF4qE3nSr_JLsxdGGOEGSpS4wxI8HbW-QYcnISvpmPEc-hJfUemR1k57IrCKwBDNamV9T0O',
production: 'xxxxxxxxx'
},
payment: function(data, actions) {
return actions.payment.create({
transactions: [
{
amount: {
total: '1.00',
currency: 'USD'
},
payee:{email:'development-seller#troc*****.com'},
}
]
});
},
commit: true,
locale: 'en_US',
style: {
size: 'medium', // tiny, small, medium
color: 'blue', // orange, blue, silver
shape: 'pill' // pill, rect
},
env: 'sandbox', // Optional: specify 'sandbox' or 'production'
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function(response) {
console.log('payment 2 completed!');
});
},
onCancel: function(data) {
console.log('payment 2 was cancelled!');
}
}, '#myContainerElement2');
</script>
</body></html>

Paypal Checkout - don't ask for delivery address for non-members?

I've just started playing with this module:
https://github.com/paypal/paypal-checkout
I'm trying to work out how to can turn off the shipping address for clients. I know in order versions you could do &NOSHIPPING=1 in the URL, but I can't find anything about the API 4 version. My code is:
paypal.Button.render({
// Pass the client ids to use to create your transaction on sandbox and production environments
locale: 'fr_FR',
//env: 'production',
env: 'sandbox',
client: {
sandbox: "...",
production: "..."
},
// Pass the payment details for your transaction
// See https://developer.paypal.com/docs/api/payments/#payment_create for the expected json parameters
payment: function() {
return paypal.rest.payment.create(this.props.env, this.props.client, {
transactions: [
{
amount: {
total: window.my_config.grand_total,
currency: 'EUR',
details: {
"subtotal": window.my_config.price,
"tax": window.my_config.vat_amount
}
},
}
]
});
},
// Display a "Pay Now" button rather than a "Continue" button
commit: true,
// Pass a function to be called when the customer completes the payment
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function() {
console.log('The payment was completed!');
console.log(data, actions)
if (error === 'INSTRUMENT_DECLINED') {
actions.restart();
}
});
},
// Pass a function to be called when the customer cancels the payment
onCancel: function(data) {
console.log('The payment was cancelled!');
},
style: {
shape: 'rect',
size: "medium"
}
}, '#paypalContainerEl');
Use "shipping_preference: 'NO_SHIPPING'."
createOrder: function(data, actions) {
$('#paypalmsg').html('<b>' + 'WAITING ON AUTHORIZATION TO RETURN...' + '</b>');
$('#chkoutmsg').hide()
return actions.order.create({
purchase_units: [{
description: 'GnG Order',
amount: {
value: cartTotal
}
}],
application_context: {
shipping_preference: 'NO_SHIPPING'
}
});
},
You need to pass the no_shipping option under experience in the payment function, like so:
return actions.payment.create(
{
payment:
{
transactions: [
{
amount:
{
total: "10",
currency: 'EUR'
}
}]
},
experience:
{
input_fields:
{
no_shipping: 1
}
}
});
In the docs, here and here. A quick note though, guests will still be asked for their billing address, even though their shipping address will no longer be asked.
For those of you integrating via PayPal REST API in PHP, to set the no_shipping attribute:
apiContext = $this->apiContext;
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$inputFields = new \PayPal\Api\InputFields();
$inputFields->setNoShipping(1); //<-- NO SHIPPING!!!!!!!!!!
$webProfile = new \PayPal\Api\WebProfile();
$webProfile->setName($uid); // <-- UNIQUE NAME FOR THE TRANSACTION
$webProfile->setInputFields($inputFields);
$createProfileResponse = $webProfile->create($apiContext);
$webProfile = \PayPal\Api\WebProfile::get($createProfileResponse->getId(), $apiContext);
$amount = new \PayPal\Api\Amount();
$amount->setCurrency('EUR')
->setTotal($this->deposit_eur);
$transaction = new \PayPal\Api\Transaction();
$transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl($this->return_url)
->setCancelUrl($this->cancel_url);
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction))
->setExperienceProfileId($webProfile->getId()); //<-- SET EXPERIENCE PROFILE
try{
$payment->create($apiContext);
} catch (\Exception $ex) {
debug($ex);
exit;
}
$approvalUrl = $payment->getApprovalLink();
For the unlucky lads integrating this via PayPal REST API, using C#, this is a bit trickier.
You create a WebProfile as in the Paypal Repo Example.
var experienceProfile = new WebProfile()
{
name = Guid.NewGuid().ToString(), // required field
input_fields = new InputFields()
{
no_shipping = 1
}
};
var experienceId = experienceProfile .Create(_apiContext).id;
new Payment
{
intent = "sale",
payer = new Payer
{
payment_method = "paypal"
},
transactions = new List<Transaction>
{
// ...
},
redirect_urls = new RedirectUrls
{
return_url = "..",
cancel_url = ".."
},
experience_profile_id = experienceId
};
If anyone runs into this problem with the v2 API REST approach, setting application_context with shipping_preference on create order in the body did work for me.
https://developer.paypal.com/docs/api/orders/v2/#definition-experience_context_base
const response = await fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
intent: 'CAPTURE',
application_context: {
shipping_preference: 'NO_SHIPPING',
},
purchase_units: []
})
For the new API you need to set the parameter no_shipping=1
https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/

Remove shipping address option in PayPal Express Checkout

I am using the JS script recommended by PayPal. It's working well, however it is showing a "Ship to" address of the buyers.
I am trying to search the internet and found that https://api.sandbox.paypal.com/v1/payment-experience/web-profiles/ requested with "no_shipping": 1, can do the trick. But for that we need to make a curl request before the payment.create, so that we can pass it returned id in the function.
Is this possible in JS?
Or is there a much better and simpler way to remove it using the following JS?
<script src="https://www.paypalobjects.com/api/checkout.js" data-version-4></script>
<script>
paypal.Button.render({
env: 'sandbox', // Optional: specify 'sandbox' or 'production'
client: {
sandbox: '{{$data['SandboxId']}}',
production: '{{$data['ProductionId']}}'
},
payment: function() {
var amount = document.getElementById("amount").value;
var env = this.props.env;
var client = this.props.client;
return paypal.rest.payment.create(env, client, {
transactions: [
{
amount: {
total: amount,
currency: "USD",
details: {
subtotal: amount,
tax: "0.00",
shipping: "0.00"
}
},
description: "This is payment description.",
item_list: {
items:[
{
quantity:"1",
name:"Orders",
price:amount,
sku:"product12345",
currency:"USD"
}
],
},
}],
});
},
commit: false, // Optional: show a 'Pay Now' button in the checkout flow
onAuthorize: function(data, actions) {
console.log(data);
alert('confirmation here');
// Optional: display a confirmation page here
return actions.payment.execute().then(function() {
alert('Success here');
// Show a success page to the buyer
});
},
}, '#paypal-button');
</script><div id="paypal-button" ></div>
To expand on Bluepnume's answer, here is a complete example:
payment: function(data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
},
experience: {
input_fields: {
no_shipping: 1
}
}
});
},
You can pass an experience options like so:
paypal.rest.payment.create({
// payment options here
}, {
// experience options here
});
This is how it needs to be done in ngx-paypal version 11
application_context:
{
shipping_preference: "NO_SHIPPING"
}
ngx-paypal: "^11.0.0"