Hi I have not been able to solve this problem for 3 days. I have integrated Paypal smart button to my page and it works. 3 days ago Do not pass Pay-xxx directly gave an erro
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();
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
I 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
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;
});
},