I have integrated Paypal smart button to my page and it works. 3 days ago Do not pass Pay-xxx directly gave an error and told me to send a token instead. This time, when I make a reference to it, it gives an error: Expected an order id to be passed. What should I do?
The following code raises an error: Expected an order id to be passed
var CREATE_PAYMENT_URL = '/api/create-payment';
var checkBox = document.getElementById("ship_to_different");
var note = $("#ordernote").val();
if (checkBox.checked == true) {
var body = $("#checkoutt, #data").serializeArray();
} else {
$('input[name=note]').val(note);
var body = $("#data").serializeArray();
}
$("#wait").show();
return fetch(CREATE_PAYMENT_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
body: body
})
}).then(function (res) {
return res.json();
}).then(function (data) {
console.log(data);
let token;
for (let link of data.links) {
if (link.rel === 'approval_url') {
token = link.href.match(/EC-\w+/)[0];
}
}
return data.token;
});
The following code raises an error: Do not pass PAY-XXX or PAYID-XXX directly into createOrder. Pass the EC-XXX token instead
var CREATE_PAYMENT_URL = '/api/create-payment';
var checkBox = document.getElementById("ship_to_different");
var note = $("#ordernote").val();
if (checkBox.checked == true) {
var body = $("#checkoutt, #data").serializeArray();
} else {
$('input[name=note]').val(note);
var body = $("#data").serializeArray();
}
$("#wait").show();
return fetch(CREATE_PAYMENT_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
body: body
})
}).then(function (res) {
return res.json();
}).then(function (data) {
console.log(data);
let token;
for (let link of data.links) {
if (link.rel === 'approval_url') {
token = link.href.match(/EC-\w+/)[0];
}
}
return data.id;
});
I am not able to understand what is going on.
I have integrated Paypal smart button to my page and it works. 3 days ago Do not pass Pay-xxx directly gave an error and told me to send a token instead. This time, when I make a reference to it, it gives an error: Expected an order id to be passed. What should I do?
The following code raises an error: Expected an order id to be passed
var CREATE_PAYMENT_URL = '/api/create-payment';
var checkBox = document.getElementById("ship_to_different");
var note = $("#ordernote").val();
if (checkBox.checked == true) {
var body = $("#checkoutt, #data").serializeArray();
} else {
$('input[name=note]').val(note);
var body = $("#data").serializeArray();
}
$("#wait").show();
return fetch(CREATE_PAYMENT_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
body: body
})
}).then(function (res) {
return res.json();
}).then(function (data) {
console.log(data);
let token;
for (let link of data.links) {
if (link.rel === 'approval_url') {
token = link.href.match(/EC-\w+/)[0];
}
}
return data.token;
});
The following code raises an error: Do not pass PAY-XXX or PAYID-XXX directly into createOrder. Pass the EC-XXX token instead
var CREATE_PAYMENT_URL = '/api/create-payment';
var checkBox = document.getElementById("ship_to_different");
var note = $("#ordernote").val();
if (checkBox.checked == true) {
var body = $("#checkoutt, #data").serializeArray();
} else {
$('input[name=note]').val(note);
var body = $("#data").serializeArray();
}
$("#wait").show();
return fetch(CREATE_PAYMENT_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
body: body
})
}).then(function (res) {
return res.json();
}).then(function (data) {
console.log(data);
let token;
for (let link of data.links) {
if (link.rel === 'approval_url') {
token = link.href.match(/EC-\w+/)[0];
}
}
return data.id;
});
I am not able to understand what is going on.
Share Improve this question edited Nov 28, 2022 at 21:54 Mr. Stash 3,1404 gold badges11 silver badges25 bronze badges asked Jul 25, 2019 at 8:49 Energy40Energy40 1971 gold badge1 silver badge11 bronze badges8 Answers
Reset to default 3I also faced this issue, and I resolved easily.
paypal.Buttons({
createOrder: function () {
return fetch('/api/create_payment/', {
method: "POST",
headers: {
'content-type': 'application/json',
'X-CSRFToken': $('[name=csrfmiddlewaretoken]').val()
}
}
)
.then(function(res) {
return res.json();
}).then(function(res) {
return res['orderId'];
});
},
onApprove: function (data, actions) {
// Do something after approved...
}
}).render('#paypal-container');
Here important is, on the back-end, I created order with Paypal V2 api endpoint - https://developer.paypal.com/docs/api/orders/v2/#orders_create
If you are using .net, then in your create payment api call, return an object that contains the order id instead of returning the BraintreeHttp.HttpResponse
[HttpPost]
public async Task<Order> Post()
{
try
{
var request = new OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(BuildRequestBody());
//3. Call PayPal to set up a transaction
var response = await PayPalClient.client().Execute(request);
Order order = new Order();
var result = response.Result<Order>();
if (result?.Status?.Trim()?.ToLower() == "created")
{
order.OrderId = result.Id;
}
return order;
}
catch (Exception ex)
{
string m = ex.Message;
throw;
}
}
then on my js
paypal.Buttons({
createOrder: function () {
return fetch('/api/paypal', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function (data) {
return data.orderId; // the data is the order object returned from the api call, its not the BrainTree.Response object
});
}
Sorry for sloppy code, I was still in the process of trying to figure out the problem. Hope you get the gist of the solution
You need to send the Token of the payment and not the ID of the payment. If you use php, use this :
// Instance of your ApiPayment
$payment = new ApiPayment();
$payment->getToken();
Your first code should work. My subjection is to check your browser console whether there are any errors or not.
Ref: https://developer.paypal.com/docs/checkout/reference/upgrade-integration/?mark=Ec%20token#4-set-up-the-transaction
Code:
createOrder: function(data, actions) {
return fetch('{{ route('create-payment') }}', {
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
method: 'post',
body: JSON.stringify({
body:body
})
}).then(function(res) {
return res.json();
}).then(function(orderData) {
console.log(orderData);
// If you don't use ES2015 or higher replace 'let' with 'var'
var token;
for (link of orderData.links) {
if (link.rel === 'approval_url') {
token = link.href.match(/EC-\w+/)[0];
}
}
return token;
});
},
If you are using paypal sdk then make sure you are returning the order
createOrder={(data, actions) => {
return this.createOrder(cartState, actions)
}}
In your first JS code example, try returning the token variable instead of data.token. Data.token in that example is just going to return an object (which when I ran your code, it return the entire payment object). If you return simply the token variable, it will return the required orderID value (EC-XXXXX). I've adjusted your code example below.
var CREATE_PAYMENT_URL = '/api/create-payment';
var checkBox = document.getElementById("ship_to_different");
var note = $("#ordernote").val();
if (checkBox.checked == true){
var body = $("#checkoutt, #data").serializeArray();
}else{
$('input[name=note]').val(note);
var body = $("#data").serializeArray();
}
$("#wait").show();
return fetch(CREATE_PAYMENT_URL, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
body:body
})
}).then(function (res) {
return res.json();
}).then(function (data) {
console.log(data);
let token;
for (let link of data.links) {
if (link.rel === 'approval_url') {
token = link.href.match(/EC-\w+/)[0];
}
}
return token;
});
},
I was getting this because of a return false
inside of the createorder
function. Cart value input was not there, missed uploading a file.
Might be helpful :)
createOrder: function (data, actions) {
var cartVal = $("body").find("#cartVal").val();
if (typeof cartVal === 'undefined')
return false;
});
Just in case, I'm not replying to the question but might be useful. I'm using this package but with a node server and I was sending to the server a very long number like $25.000 and I got the error "expected an order id".
$25.00 worked