fluxus
fluxus

Reputation: 559

How to send custom data to Paypal and receive it back

I know that similar questions exist, but since there is no clear answer(at least I did not find any) and Paypal's documentation is a bit of a mess, I am asking it here.

I need to POST custom data(entered by user via web form) along with all the variables that are sent by default with Paypal's button.

I need that custom data because I am generating PDF's with FPDI/TFPDF based on those values. Also I am sending emails that make use of those variables.

I have it working with Stripe. This is part of charge.js that makes the POST request. formData contains Stripe token + custom data.

// grab the form action url
const backendUrl = form.getAttribute('action');
// Make ajax call
fetch(backendUrl, {
method: "POST",
mode: "same-origin",
credentials: "same-origin",
body: formData
})
.then(function(response) {
return response.json();
})
.then(function(jsonData) {
let data = jsonData;
if(data.success == true){
// make those pdf's available to user immediately
}
else{
// error handling
}
});

Next stop is charge.php where I make a charge. If the charge is successful, PDF's are generated and emails are sent.

Here is a little excerpt:

try
{
$charge = \Stripe\Charge::create(array(
'source'  => $token,
'amount'  => $amount,
'currency' => $currency,
'description' => $item_name,
'metadata' => array(
  'customer_name' => $recipient_name,
  'customer_email' => $recipient_email,
  'order_id' => $order_id,
  'order_type' => $order
 )
));
}
catch(\Stripe\Error\Card $e) {
$success = false;
$err = "Declined - $e";
}

if($charge->status == 'succeeded') {
// Generate PDF's and send emails
} 

// send back stuff that might be useful for thank you page, etc.
 echo json_encode(array('success' => $success, 'err' => $err, 
'recipient_name' => $recipient_name, 'recipient_email' => 
 $recipient_email, etc.));

What is the correct way of doing the same thing with Paypal? In which part of the process should I generate PDF's and send emails?

From my understanding that place would be the IPN listener script? But Paypal says to use webhooks with REST API.

P.S. I am a designer that likes to code, but it is not my forte.

Upvotes: 1

Views: 2566

Answers (1)

Drew Angell
Drew Angell

Reputation: 26056

If you're using the REST API you would indeed use Webhooks instead of IPN. This will send a POST of the transaction data to your listener script that you provide when you register the webhook.

Within that script, you can receive all of this data and process it however you want. This is where you can generate custom branded email notifications, hit 3rd party APIs, update your own database, or anything else you might want to automate using the transaction data.

PayPal doesn't have a "metadata" object, though. Instead, you could save all of the data you need in your local database with an order ID or record ID of some sort. Then pass that order ID in the PayPal request, and this value will come back in the Webhook.

So, from within your script, you could then pull all that data back out of your database using the order ID, and pick up where you need with all of the data available at that point.

Hope that helps!

Upvotes: 2

Related Questions