cancel
Showing results for 
Search instead for 
Did you mean: 

How to obtain the Order ID once the order is created?

Options
Go to solution
samebenezer
Contributor
Posted on

I am using the orders API to create orders... But, I'm stuck with the onApprove function... How do you retrieve the order details on the server side?

 

CLIENT SIDE:

onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name);
return fetch('/paypal-transaction-complete', {
method: 'post',
headers: {'content-type': 'application/json'},
body: JSON.stringify({orderID: data.orderID})});
});
}

 

ON THE SERVER SIDE(using php)

GetOrder::getOrder('REPLACE-WITH-ORDER-ID', true);

 

What should I pass in place of 'REPLACE-WITH-ORDER-ID' ?

Basically how do i obtain the order ID from the JSON

1 ACCEPTED SOLUTION

Accepted Solutions
Solved

How to obtain the Order ID once the order is created?

Options
Go to solution
Frigidman
Contributor

Verifying is pretty much just doing a GET on the orderID, and looking over the response to see if the user has "CONFIRMED" (approved) the amount to be paid. Or if the order has already been captured, you check to see if its status is "COMPLETED".

 

Capturing is actually telling the PayPal API to take those funds from the user (the payer) and dump it into your merchant account (the payee) after they have confirmed (onApprove). Once you do this, its a good time to store all of this information in a database (if you have not already)... so fulfilling the purchase can be handled and tracked.

 

Most of the examples I see skip the step of looking at the order first, and instead goes straight into capturing, and then looking at the order. Like their example for the js sdk "onApprove" does a capture immediately instead of verifying the order first. Its a flow choice, but at some point you would want to do a GET on that orderID to check that everything checks out and to store details from it into a database.

View solution in original post

4 REPLIES 4

How to obtain the Order ID once the order is created?

Options
Go to solution
Frigidman
Contributor

You can get your data by reading the file: php://input, for example using file_get_contents('php://input') and then decode that input with json_decode().

 

$json = file_get_contents('php://input');
$data = json_decode($json);
$orderid = $data->orderID;

How to obtain the Order ID once the order is created?

Options
Go to solution
samebenezer
Contributor

Could you tell me the difference between capturing an order and verifying an order ?

How to capture an order and verify it at the same time ?

I'm really confused...

 

Solved

How to obtain the Order ID once the order is created?

Options
Go to solution
Frigidman
Contributor

Verifying is pretty much just doing a GET on the orderID, and looking over the response to see if the user has "CONFIRMED" (approved) the amount to be paid. Or if the order has already been captured, you check to see if its status is "COMPLETED".

 

Capturing is actually telling the PayPal API to take those funds from the user (the payer) and dump it into your merchant account (the payee) after they have confirmed (onApprove). Once you do this, its a good time to store all of this information in a database (if you have not already)... so fulfilling the purchase can be handled and tracked.

 

Most of the examples I see skip the step of looking at the order first, and instead goes straight into capturing, and then looking at the order. Like their example for the js sdk "onApprove" does a capture immediately instead of verifying the order first. Its a flow choice, but at some point you would want to do a GET on that orderID to check that everything checks out and to store details from it into a database.

View solution in original post

How to obtain the Order ID once the order is created?

Options
Go to solution
samebenezer
Contributor

Thanks for that🙏 , it really helped clarify my doubts😊... I feel like their documentation isn't adequately explanatory enough ...

My server integration isn't working for some reason😢....

 

Could you help identify what i'm doing wrong?

Client side

onApprove: function(data) {
return fetch('/server-side-file-name', {
  headers: {
    'content-type': 'application/json'
  },
  body: JSON.stringify({
    orderID: data.orderID
  })
}).then(function(res) {
  return res.json();
}).then(function(details) {
  alert('Transaction funds captured from ' + details.payer_given_name);
})
}

 ON the SERVER SIDE

if (!count(debug_backtrace()))
{
  $json = file_get_contents('php://input');
$data = json_decode($json);
$orderid = $data->orderID;
  CaptureOrder::captureOrder($orderid, true);
}

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.