I'm hoping some knowledgeable soul may be able to explain where I'm going wrong with the code below.
I am trying to upgrade from PayPal Encrypted Payment Settings (EPS) to a more current PayPal checkout solution. I've written my shopping cart in PHP and essentially have a final page of checkout where I have everything ready to go, e.g.
$seid \\ session id to uniquely identify session / items in basket and used on the return call to write order to database, update stock levels etc
$txn_value_excl_tax
$shipping
$tax
$grand_total
The relevant part of the subsequent PHP code looks like this:
<?php
include 'createClientToken.php';
?>
<script src="https://www.paypal.com/sdk/js?components=hosted-fields,buttons&client-id=MyClientID¤cy=GBP&disable-funding=credit" data-client-token='<?=$clientToken?>'></script>
<DIV id="paypal-button-container" style="width:120px;"></DIV>
<SCRIPT>
paypal.Buttons({
createOrder:function(data,actions) {
return actions.order.create({
purchase_units:[{
custom_id:"<? echo $seid;?>",
amount:{
...
},
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// only invokes code below if order is successful?
var txn_status = details.status;
var custom_id = details.purchase_units[0].custom_id;
var given_name = details.payer.name.given_name;
alert("Status: "+txn_status);
// need to write order to database & display order confirmation
return fetch('completed.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
});
});
},
onCancel: function (data) {
alert("USER CANCELLED");
}
}).render('#paypal-button-container');
</SCRIPT>
I believe all is well with the above code, as it successfully alerts out the transaction status (txn_status) and doesn't present any errors in the console log as far as I can see.
completed.php looks like this:
<?php
$orderID = json_decode($_POST['orderID']);
$custom_ID = json_decode($_POST['custom_ID']);
session_start();
include_once("myroutines.php"); // connect to db within here and assign to $conn
connect();
$write_txn = "update transactions set checked_out='true' where custom_ID='$custom_ID';";
mysql_query($write_txn, $conn) or die(mysql_error());
?>
But the code in completed.php writes a blank custom_ID
to the database. As far as I can tell, $orderID
is also blank as I've tested updating an existing row in the table with $orderID
, so it looks like I'm failing in how I retrieve the JSON info. Does somebody have a working example of what that specific piece of code should look like, rather than just point me to a generic "parsing JSON for PHP how-to"?
I've spent literally several days now attempting to work through PP docs to implement this, though it would appear one how-to links to another equally abstract how-to which references a PP 404 page. I've also looked at other similar questions here on Stack which has helped me along further, but I'm still missing this critical piece of the jigsaw. The horrendously over-complicated integration (imo) of PP and lack of clear how-to is inspiring me to offer a second checkout option with Square, which I'm hoping will be more intuitive, but I'd like to get this up and running first.