PayPal Express Checkout, error when supplying input_fields - javascript

I'm using PayPal Express Checkout from JavaScript.
This is the code I have and works (I see the PayPal button, I can click it, and the payment window opens fine):
<div id="paypal-button"></div>
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
paypal.Button.render({
env: 'sandbox',
client: {
sandbox: 'xxxxxx',
production: 'yyyyyy'
},
payment: function () {
var env = this.props.env;
var client = this.props.client;
return paypal.rest.payment.create(env, client, {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
});
},
commit: true,
onAuthorize: function (data, actions) {
return actions.payment.execute().then(function() {
document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
});
}
}, '#paypal-button');
</script>
But, if I supply input_fields like the code below, it stops working and throws a console error:
paypal.Button.render({
env: 'sandbox',
client: {
sandbox: 'xxxxxx',
production: 'yyyyyy'
},
payment: function () {
var env = this.props.env;
var client = this.props.client;
return paypal.rest.payment.create(env, client, {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
}, {
input_fields: {
no_shipping: 1
}
});
},
commit: true,
onAuthorize: function (data, actions) {
return actions.payment.execute().then(function() {
document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
});
}
}, '#paypal-button');
This is the error detail:
In this last case, I see the PayPal button, the window opens when I click on it, but after a few seconds the window closes itself and gives the console error provided here.
What am I missing?
UPDATE
As #bluepnume pointed out, if I switch from sandbox to production environment, the problem disappears.
But, if I also supply first_name and last_name in input_fields, I get another console error no matter if I'm in sandbox or production environment.
This is the code snippet:
, {
input_fields: {
no_shipping: 1,
first_name: 'Abc',
last_name: 'Dfg'
}
}
And this is the error message:

This is a known issue that's being tracked. For what it's worth, if you run in production mode, the issue should not be present.
EDIT: For the second issue, looks like this is not a valid request format. You should be able to see in your network tab:
Are you looking for the payer_info field for the payments api? https://developer.paypal.com/docs/api/payments/

Concerning the first issue, it seems everything is working well now. The "no_shipping" option can be used even in test conditions.

Related

Paypal Button returns 'Order could not be captured'

I am using paypal Buttons SDK. The Code activating the button is:-
paypal.Buttons({
createOrder: ( data, actions ) => {
return actions.order.create({
purchase_units: [{
amount: {
value: this.amount.toFixed(2),
currency_code: "GBP",
}
}]
})
},
onApprove: ( data, actions ) => {
return actions.order.capture().then(details => {
console.log('details',details);
})
},
onError: ( error ) => {
console.log('error',error);
}
}).render('#paypal-button-container')
The User Interface operates as expected, there is then a long pause before the error is returned. The client_id used in the script tag is for a sandbox account. I can find not documentation describing possible cause for the error...
error Error: Order could not be captured
Any advice greatly appreciated.
Paypal.. https://developer.paypal.com/docs/checkout/integrate/#1-get-paypal-rest-api-credentials
As suggested in the comment try to do a curl with this URL :
https://www.sandbox.paypal.com/smart/api/order/ODER_ID/capture
And it replies with code 401 and did some research and end up finding that I was using a wrong account to make payments.
I refresh the PayPal login and login with the correct sandbox buyer account and make the payment and It works.
Probably paypal should give correct errors messages.
Can you check using CURL which returns the Paypal server?
This is a comment but I do not have 50pkt S / O. Sorry.
If you get a Xss message in the console, just try in private navigation, disconnect from your paypal buyer account.
I have had the same "Order could not be captured" error at "actions.order.capture()" in the onApprove callback.
In my case, it worked on the first run but not the subsequent calls. I found my order always had the same invoice_id. I removed invoice_id and Paypal stopped complaining.
It should be good if the invoice_id was always unique.
Same problem with Nuxt , checkout works but catch error response : Error 500 order-could-not-be-captured
<template>
<no-ssr>
<v-layout row wrap>
<div ref="paypal"></div>
</v-layout>
</no-ssr>
</template>
Script
mounted() {
const script = document.createElement("script");
script.src =
"https://www.paypal.com/sdk/js?client-id=MyKeyID";
script.addEventListener("load", this.setLoaded);
document.body.appendChild(script);
},
methods: {
setLoaded: function() {
this.loaded = true;
window.paypal
.Buttons({
createOrder: (data, actions) => {
return actions.order.create({
purchase_units: [
{
description: "Test description",
amount: {
currency_code: "USD",
value: 1
}
}
]
});
},
onApprove: async (data, actions) => {
const order = await actions.order.capture();
this.paidFor = true;
console.log(order);
},
onError: err => {
console.log(err);
}
})
.render(this.$refs.paypal);
}

Paypal purchase not happening in HTML with ASP.NET

I have the little Paypal window working in the sandbox and production mode. The onAuthorize function gets called and my message window pops up thanking you for the purchase and giving the purchased credits to the customer.
paypal.Button.render({
<% if (useBankAccount) { %>
env: 'production', // Or 'sandbox', 'production'
<% } else { %>
env: 'sandbox', // Or 'sandbox', 'production'
<% }%>
client: {
sandbox: 'Axxx',
production: 'Axxx'
},
commit: true,
style: {
size: 'responsive',
color: 'gold',
shape: 'pill',
label: 'pay'
},
payment: function (data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: currentPayment, currency: 'USD' }
}
]
}
});
},
onAuthorize: function (data, actions) {
if (window.ga) { // Google Analytics
window.ga('send', 'event', 'Credits', 'Purchase', 'Payment', currentPayment);
window.ga('send', 'event', 'Credits', 'Purchase', 'Credits', currentCredits);
}
document.getElementById("<%= PurchaseDone.ClientID %>").value = "Yes";
document.getElementById("<%= PurchaseAmount.ClientID %>").value = currentPayment;
TheForm.submit();
},
onCancel: function (data, actions) {
OpenMessageWindow("The payment was cancelled.");
},
onError: function (err) {
var result = err.message.toString().indexOf("Amount cannot be zero")
if (result > 0) {
OpenMessageWindow('Payment Error: The purchase amount must be more than zero');
return;
}
OpenMessageWindow('Payment Error:<br/>' + err.message)
}
}, '#paypal-button');
Looking the data passed into OnAuthorize()
{paymentToken: "EC-xxx",
orderID: "EC-xxx",
payerID: "FTxxx",
paymentID: "PAY-xxx",
intent: "sale", …}
intent:"sale" orderID:"EC-xxx"
payerID:"FTxxx"
paymentID:"PAY-xxx"
paymentToken:"EC-xxx"
returnUrl:
"https://www.paypal.com/?paymentId=PAY-0Kxxx&token=EC-xx&PayerID=FTxxx"
__proto__ :Object
actions is
{close: ƒ, redirect: ƒ, payment: {…}, order: {…}, restart: ƒ, …}
To see those values, I commented out the TheForm.Submit(), which could have been the problem but the payment still did not happen.
The problem is, in production, the customer purchase does not happen. The customer (me) logs into their Paypal account, presses Pay Now, the window disappears calling onAuthorize.
No payment registers on the customer account. No payment transaction happens and my software gives the credits for free.
Ideas? What can I look at? Paypal telephone support does not know anything about development.
Thank you for any help.
The solution is to call actions.payment.execute() in the onAuthorize callback. It causes a second busy indicator that does the payment. It's like the onAuthorize says the user accepts, now do you accept?
Now, this is my working code.
paypal.Button.render({
<% if (useBankAccount) { %>
env: 'production', // Or 'sandbox', 'production'
<% } else { %>
env: 'sandbox', // Or 'sandbox', 'production'
<% }%>
client: {
sandbox: 'xxx',
production: 'xxx'
},
commit: true,
payment: function (data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: currentPayment, currency: 'USD' }
}
]
}
});
},
onAuthorize: function (data, actions) {
return actions.payment.execute().then(function (payment) {
// setup the data to post
TheForm.submit();
});
},
onCancel: function (data, actions) {
OpenMessageWindow("The payment was cancelled.");
},
onError: function (err) {
var result = err.message.toString().indexOf("Amount cannot be zero")
if (result > 0) {
OpenMessageWindow('Payment Error: The purchase amount must be more than zero');
return;
}
OpenMessageWindow('Payment Error:<br/>' + err.message)
}
}, '#paypal-button');
I added two things from the PayPal example to get it working.
The "commit: true," is needed to change the Continue button to Pay Now. Unbelievably, the default is to have a button called Continue that pays.
The second is calling actions.payment.execute() in onAuthorize. Not something you could figure out except by being told.
I am quite incredulous that the PayPal example does not include these two items providing an example that simply does not work. AND you don't know about it until you test a live purchase, sandbox seems fine.
Additionally, Stack Overflow is PayPal's development support, why was this question never answered? I actually found the solution to the problem on a web page explaining how bad PayPal is compared to Stripe. The page gave a nice example of the complicated PayPal code that had the missing function call.

Paypal express checkout button IPN

I have an express checkout button on my webpage which looks somethin like that:
<body>
<div id="paypal-button"></div>
<script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>
paypal.Button.render({
env: 'sandbox', // Or 'sandbox'
client: {
sandbox: '<censored>',
production: 'xxxxxxxxx'
},
commit: true, // Show a 'Pay Now' button
payment: function(data, actions) {
return actions.payment.create({
payment: {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
}
});
},
onAuthorize: function(data, actions) {
return actions.payment.execute().then(function(payment) {
console.log(data);
});
}
}, '#paypal-button');
</script>
I also have an IPN handler on my rails backend that i tested with the IPN simulator at https://developer.paypal.com/developer/ipnSimulator/ and selected express checkout and it works fine. The only thing I couldn't figure out is how to setup paypal to send the IPNs to my server. I tried setting up a paypal business account and entering url there for IPNs but i don't recieve anything on the server nor on the IPNs history website.
I see button code is for sandbox.
Sandbox is a test environment, you should setup IPN in your sandbox account to receive IPN notifications.
You can login to sandbox account in this URL: www.sandbox.paypal.com
If you are using live account, you'll need to change "env" and "client" to production.

Paypal Checkout. I can't login with the paypal account accesses

I'm trying to implement the payment process with Paypal Checkout
This is my code
<div class='wrapper-packages'>
<div id="paypal-button"></div>
</div>
<script>
paypal.Button.render({
env: 'sandbox',
client: {
sandbox: 'xxxxxxxx',
production: 'xxxxxxxx'
},
payment: function() {
var env = this.props.env;
var client = this.props.client;
return paypal.rest.payment.create(env, client, {
transactions: [
{
amount: { total: '1.00', currency: 'USD' }
}
]
});
},
commit: true, // Optional: show a 'Pay Now' button in the checkout flow
onAuthorize: function(data, actions) {
// Optional: display a confirmation page here
return actions.payment.execute().then(function() {
alert('ok');
});
}
}, '#paypal-button')
</script>
When I click on Paypal Checkout button, it opens the modal for log in paypal account, but I can't do login
The login credentials are ok, I've checked it.
So, what can be the problem?
When using PayPal's Sandbox, you need to use Sandbox Test Accounts. You can read more about them in the Docs

PaymentRequest API not defined

I am trying to run an example of the Payment request API as shown on https://www.youtube.com/watch?v=yelPlCVZLEE .
I have followed the process as they describe and i Have also run the following code:
function go() {
console.log('Pay');
var request = new PaymentRequest([{
supportedMethods:['urn:payment:visa','urn:payment:mc','urn:payment:amex']
}],
{
total: {
label: "Total due",
amount: { currencyCode: "USD", value: "60.00" }, // US$60.00
}
}
);
request.show()
.then(function(response) {
// process transaction response here
return response.complete(true);
})
.then(function() {
alert("Buy!");
})
.catch(function(e) {
alert(e.name);
});
}
and I get the following error: Uncaught ReferenceError: PaymentRequest is not defined.
If I run the test from :
http://github.adrianba.net/paymentrequest-demo/tests/payment-tests.html
It's says it's defined.
What I am doing wrong?
The site you linked, http://github.adrianba.net/paymentrequest-demo/tests/payment-tests.html, pulls in a file:
<script src="../lib/paymentrequest.js"></script>
which defines its own implementation of PaymentRequest:
function PaymentRequest(methodData,details,options) {
// Constructor code
if(!Array.isArray(methodData) || methodData.length===0) throw new TypeError("methodData must be a non-empty sequence of PaymentMethodData");
methodData.forEach(d => {
...
http://github.adrianba.net/paymentrequest-demo/lib/paymentrequest.js
To get PaymentRequest in Chrome, you have to enable it in chrome://flags/#enable-experimental-web-platform-features

Categories