Reputation: 217
I managed to integrate a custom website, with paypal...when creating an account on this website you need to pay a fee, trough paypall... So i redirect a user after registering, to paypall , where after he pays, he is redirected to a success page on my website, with a token key in the url as a parameter(i entered that success page from the merchant account). The only question is, how could i get the email of the customer who just registered and payed, from the paypal response, using this token key.
If I create a custom form like the following:
<form id="paypalForm" method="POST" action="https://www.paypal.com/cgi-bin/webscr">
<input type="hidden" name="cmd" value="_notify-synch">
<input type="hidden" name="tx" value="<?php echo $tokenKey; ?>">
<input type="hidden" name="at" value="<?php echo $merchantPayPalltoken; ?>">
<input type="submit">
</form>
and if i click on submit, i am redirected to a paypall screen where i get the info that i want, listed.
But i tried to make this post call trough jQuery.ajax, like following:
var postVars = jQuery('#paypalForm').serialize();
var url = 'https://www.paypal.com/cgi-bin/webscr';
jQuery.ajax({
type: "POST",
url: url,
data: postVars,
success: function(data){
alert("success email = "+data.payer_email);
},
error:function(xhr, ajaxOptions, thrownError){
alert('status = '+xhr.status+' thrownError = '+thrownError+ ' response = '+xhr.responseText);
}
});
but it always enters in the error part of the response, not in success.
I would need that email info from pay-pal to activate that user account on my website, after he payed.
Or should i make this part by creating a session variable with the users email right before redirecting him to paypall for payment, and when the user is redirected back to success page, to get the email from session. Would this be a safe way to do it?
Many thanks
Upvotes: 2
Views: 1608
Reputation: 7319
There are 2 different ways you can get that information back to your site. You can use either PDT or IPN. Both will return the information back to your site. IPN will post the information back to your site regardless if the buyer returns or not. This is a better method to use encase the buyer does not return, such as their browser crashes or they close the window. This way your system will still be updated. The other method PDT, which looks like what you are trying to use depends on the buyer returning back to your site. When they are redirected back to your site, they are sent back with some of the information. You do a post back to validate the information with PayPal, and then PayPal replies with all of the information.
You can find more on PDT at https://www.x.com/developers/paypal/documentation-tools/ipn/integration-guide/IPNPDTAnAlternativetoIPN There are docs, and samples located on that page. You can also find a link on that page for more information on IPN or you can go to https://www.x.com/developers/paypal/documentation-tools/ipn/integration-guide/IPNOperations It is up to you which method you use. I usually use both in conjunction with one another. I just have the IPN and PDT scripts check to make sure that I haven't already received the post from the other script before writing it to the database. If I have, then I ignore the second post. I use the PDT return page more for creating a dynamic receipt, and rely more on IPN to do the updates in my system.
The nice thing about IPN as well is that you can resend a post to your system, if for some reason there was an error when you received it originally or a network issue. With PDT, you do not have a way to resend this information. So you would have to manually update your database.
Hope this helps!! :)
Upvotes: 4