Reputation: 5197
I'm trying to figure out how to get the transaction ID for a payment made using the PayPal SDK. Here is what I have in my JS:
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: parseFloat( window.my_config.amount ),
currency: 'USD',
},
description: window.my_config.description
}],
commit: true
});
},
onApprove: function(data, actions) {
console.dir({ data: data, actions:actions });
return actions.order.capture().then(function(details) {
console.log({ details: details });
// This function shows a transaction success message to your buyer.
alert('Transaction completed by ' + details.payer.name.given_name);
});
$('#AJAXloadingWrapper').show();
}
}).render('#paypalWrapper');
This is what my debug comes back with:
I can't see the transaction ID in there anywhere. It should be 7CA44490MT363621L (going by the transid in my PayPal account)
How does one go about getting that? In the old system I used to use:
curl https://api.paypal.com/v1/payments/payment/L8EM97CXSYBJ2 -H "Content-Type: application/json" -H "Authorization: Bearer tokenstring"
But that doesn't work. I've tried just changing it to v2:
curl https://api.paypal.com/v2/payments/payment/L8EM97CXSYBJ2 -H "Content-Type: application/json" -H "Authorization: Bearer xxxx"
...but all I get in response to that, is nothing (no error, but no output)
I have tried something based on:
sub get_proper_trans_id {
my $token = encode_base64(qq|$config->{$config->{mode}}->{system_client_id}:$config->{$config->{mode}}->system_secret}|);
$token =~ s/\n//g;
my $trans_id = '50D311011N3466050';
print qq|curl https://$config->{$config->{mode}}->{transaction_endpoint}/v2/payments/payment/$trans_id -H "Content-Type: application/json" -H "Authorization: Bearer $token"\n|;
my $results = `curl https://$config->{$config->{mode}}->{transaction_endpoint}/v2/payments/payment/$trans_id -H "Content-Type: application/json" -H "Authorization: Bearer $token"`;
my $json_vals = decode_json($results);
use Data::Dumper;
print Dumper($json_vals);
}
Which doesn't output anything (no errors either)
UPDATE: As suggested, I'm trying to use purchase_units -> payments -> captures -> id
from the outputted results. The issue is that this seems wrong / different to what I'm expecting. When I log into my PayPal, I get:
0NP90326GE709501L
Yet the value in the returned data comes up as:
891170207E054363D
Why are these differently? FWIW I'm looking at this in my personal PayPal account (not the business it was sent to). The trans ID's should be the same for both the merchant and customer though, surely?
UPDATE 2: So after all this, I've found a thread that says PayPal intentionally give the buyer and seller different transaction ID's. What a PITA. So the alternative is to pass in a custom invoice_id, which we can cross reference with. All good - apart from I can't seem to do that either!
I've tried:
return actions.order.create({
purchase_units: [{
amount: {
value: parseFloat( window.my_config.amount ),
currency: 'USD',
},
description: window.my_config.description,
}],
invoice_id: invoice_id,
commit: true
});
},
and also:
return actions.order.create({
purchase_units: [{
amount: {
value: parseFloat( window.my_config.amount ),
currency: 'USD',
},
description: window.my_config.description,
invoice_id: invoice_id
}],
commit: true
});
},
But while they go through the process of making the payment ok, it comes back with:
Error: Order could not be captured
..when trying to run return actions.order.capture().then(function(details) {
UPDATE 3:
I still can't get this to work. Is this how you pass a custom_id?
return actions.order.create({
purchase_units: [{
amount: {
value: parseFloat( window.my_config.amount ),
currency: 'USD',
custom_id: invoice_id
},
description: window.my_config.description
}],
commit: true
});
I've tried it there, and also as below commit: true
, but it still doesn't work :( ("Order could not be captured" right after confirming payment)
Upvotes: 0
Views: 835
Reputation: 137
Note: This is the transaction id stored when you look at a paypal transaction in the paypal app. The following code shows you how to get the transaction ID using paypal javascript sdk. Go to the onApprove function and read the code:
<script>
paypal.Buttons({
// Sets up the transaction when a payment button is clicked
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '1'
},
}],
application_context: {
shipping_preference: "NO_SHIPPING",
}
});
},
// Finalize the transaction after payer approval
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
// console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
//The transaction ID is below this comment in the alert
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// When ready to go live, remove the alert and show a success message within this page. For example:
// var element = document.getElementById('paypal-button-container');
// element.innerHTML = '';
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
actions.redirect('https://wwwwebsite.com/att_thanks.php?id=1640035591');
});
}
}).render('#paypal-button-container');
</script>
Upvotes: 0
Reputation: 30379
It's in purchase_units -> payments -> captures -> id
See a sample response at https://developer.paypal.com/docs/api/orders/v2/#orders-capture-response
Once knowing the id, there's no reason you would need to do a separate GET request for this same data about it, but if you did the API call would be:
https://developer.paypal.com/docs/api/payments/v2/#captures_get
Upvotes: 2