I am receiving data after a PayPal transaction. This data is written to a console.log. What I would like to do is send the customer an confirmation email, however I do not know how to use the data from the console.log to use into a PHP mail script.
The data in the console.log looks as follows:
{
"intent": "CAPTURE",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "GBP",
"value": "7.99",
"breakdown": {
"item_total": {
"currency_code": "GBP",
"value": "7.99"
},
"shipping": {
"currency_code": "GBP",
"value": "0.00"
},
"handling": {
"currency_code": "GBP",
"value": "0.00"
},
"insurance": {
"currency_code": "GBP",
"value": "0.00"
},
"shipping_discount": {
"currency_code": "GBP",
"value": "0.00"
}
}
},
"payee": {
"email_address": "[email protected]",
"merchant_id": "XMPRDEBVATPBG"
},
"description": "JHS Kingsman KSS11 Keyboard Bench in Black ",
"items": [
{
"name": "JHS Kingsman KSS11 Keyboard Bench in Black ",
"unit_amount": {
"currency_code": "GBP",
"value": "5.00"
},
"tax": {
"currency_code": "GBP",
"value": "0.00"
},
"quantity": "1"
},
{
"name": "Postage and Packaging",
"unit_amount": {
"currency_code": "GBP",
"value": "2.99"
},
"tax": {
"currency_code": "GBP",
"value": "0.00"
},
"quantity": "1"
}
],
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "Whittaker House",
"address_line_2": "2 Whittaker Avenue",
"admin_area_2": "Richmond",
"admin_area_1": "Surrey",
"postal_code": "TW9 1EH",
"country_code": "GB"
}
},
"payments": {
"captures": [
{
"id": "**5XA41291M3793842K**",
"status": "COMPLETED",
"amount": {
"currency_code": "GBP",
"value": "7.99"
},
"final_capture": true,
"seller_protection": {
"status": "ELIGIBLE",
"dispute_categories": [
"ITEM_NOT_RECEIVED",
"UNAUTHORIZED_TRANSACTION"
]
},
"create_time": "2024-03-01T13:59:46Z",
"update_time": "2024-03-01T13:59:46Z"
}
]
}
}
],
"payer": {
"name": {
"given_name": "John",
"surname": "Doe"
},
"email_address": "[email protected]",
"payer_id": "VJXBXRRPJU2L4",
"address": {
"country_code": "GB"
}
},
"create_time": "2024-03-01T13:59:01Z",
"update_time": "2024-03-01T13:59:46Z",
"links": [
{
"href": "[https://api.sandbox.paypal.com/v2/checkout/orders/3KJ025674H470494E](https://api.sandbox.paypal.com/v2/checkout/orders/3KJ025674H470494E)",
"rel": "self",
"method": "GET"
}
]
}
I can receive the data as follows:
onApprove: (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));
console.log(data);
const transaction = orderData.purchase_units[0].payments.captures[0];
const detail = orderData.purchase_units[0].items[0];
const payee = orderData.purchase_units[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:
const element = document.getElementById('paypal-button-container');
element.innerHTML = 'Thank you for your payment! <br> Your Order ID is: ';
document.querySelector("#order_data").innerText = transaction.id ;
});
}
This does display the transaction id.
What I would like to do next is to send an email confirmation to the customer that I have received the order with the order Data. My JavaScript is not very good therefore I would like to use a PHP script.
The PHP script I would like to use is as follows:
$emailsubject = 'Order Confirmation';
$webmaster = '[email protected]';
$Transaction = **transaction.id**;
$email = **payee.payee.email_address**;
$Product = **detail.name[]**;
$fromAddress = "From: $webmaster\r\n" .
"Reply-To: $email\r\n" .
"X-Mailer: PHP/" . phpversion();
$Body = <<<EOD
Thank you for your order.
Your transaction id = $Transaction
Transaction Details:
$Product
EOD;
$headers = "From: $email\r\n" .
"Reply-To: $webmaster\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .="content-type: text/html\r\n";
$succes = mail($webmaster, $emailsubject, $Body, $fromAddress);
The problem is that I don't know how to use the data from the console.log (see bold text in the PHP script) in the PHP mail script. Sorry if this is this a very stupid question however I do not know where to start looking. Any help welcome
actions.order.create() and actions.order.capture() are both deprecated and should not be used for any new integration. They will be sunset (support removed in the future).
Change your code to create and capture orders from a backend. See the standard integration guide for details. Its samples use node.js for the backend implementation, since JS the most widely-understood language, but everything the backend does can of course be implemented in any language/envrionment including PHP.
Confirmation emails should be sent from the backend after a successful capture only.