Reputation: 159
I've been struggling with this for hours and can't wrap my head around where/how to call a php function to insert order details into MySQL upon user clicking the Buy button (doing so at this point allows me to track orders that go to Paypal and then are abandoned).
I thought to put it in the <form action=>
tag, (for example, <form action='addToMySQL.php' method='post'>
) but that's where the Paypal url goes, yes? Function must be run server side, so can't use js... So... how to do this?
cURL has been suggested, and I've seen it recommended before, but would the user still be redirected to Paypal's payment page and then returned back to my site? Can anyone suggest a relevant web tutorial?
I'm new to web programming so details would be appreciated.
Function that creates the form to be sent to PayPal is:
function create_paypal_checkout {
return "
<form action='".PAYPAL_URL."' method='post'>
<input type='hidden' name = 'business' value='".PAYPAL_ID."' />
<input type='hidden' name = 'cmd' value='_cart' />
<input type='hidden' name = 'upload' value='1' />
<input type='hidden' name = 'currency_code' value='USD' />
<input type='hidden' name = 'lc' value='US' />
<input type='hidden' name='rm' value='2' />
<input type='hidden' name='cancel_return' value='http://www.mydomain.com/'>
<input type='hidden' name='notify_url' value='http://www.mydomain.com/ipn-listener.php'>
<input type='hidden' name='return' value='http://www.mydomain.com/thankyou.php?custIP=".$custip."' />
" . render_shopping_cart($shopping_cart) . "
<table class='formBuyButton'>
<tr id='sc_total'>
<td> </td>
<td> </td>
<td id='ppbutt'>
<input type='image' name='submit' src='https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_buynowCC_LG.gif' border='0' alt='PayPal - Buy Now' />
</td>
</form></tr></table>" ;
}
Upvotes: 0
Views: 2860
Reputation: 23120
I had the same problem and I solved it using ajax. What I do is to call a php script using ajax before submitting the form to Paypal. Here, I'm using the post function from jquery, since it easier. You need to have jquery somewhere and to point to it. When the user submit the form, mycall() is first called, and if it returns true, then the form it submitted to PayPal.
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function mycall() {
$.post('addToMySQL.php', {...}); // Replace ... with arguments
return true;
}
</script>
<form action='".PAYPAL_URL."' method='post' onSubmit='return mycall();'>
Upvotes: 1