Sometimes it’s hard to implement a little snipped of code… Last time when I tried to use the IPN-Handler from PayPal. But at the end, it works pretty good. For that reason I will share my code with you.
First, set this in the Controller (in the beforeFilter):
| 1 | if ($this->action == 'payPalHandler') { |
| 2 | $this->Security->validatePost = false; |
| 3 | $this->Security->csrfCheck = false; |
| 4 | } |
If you are using the security component, you have to set these things to false, otherwise nothing will work correctly because the handler works as a form.
Then you have to build the PayPal-Handler in the Controller. Here we go:
| 01 | public function payPalHandler() |
| 02 | { |
| 03 | // sandbox: |
| 04 | $paypalspecs = array('url' => 'ssl://www.sandbox.paypal.com', 'primaryemail' => 'email_sandbox@example.com'); |
| 05 | // live: |
| 06 | //$paypalspecs = array('url' => 'ssl://www.paypal.com', 'primaryemail' => 'email@example.com'); |
| 07 | |
| 08 | // read the post from PayPal system and add 'cmd' |
| 09 | $req = 'cmd=_notify-validate'; |
| 10 | |
| 11 | foreach ($_POST as $key => $value) { |
| 12 | $value = urlencode(stripslashes($value)); |
| 13 | $req .= "&$key=$value"; |
| 14 | } |
| 15 | |
| 16 | // post back to PayPal system to validate |
| 17 | $header = "POST /cgi-bin/webscr HTTP/1.0\r\n"; |
| 18 | $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
| 19 | $header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; |
| 20 | |
| 21 | $fp = fsockopen ($paypalspecs['url'], 443, $errno, $errstr, 30); |
| 22 | |
| 23 | if (!$fp) |
| 24 | { |
| 25 | // HTTP ERROR |
| 26 | } |
| 27 | else |
| 28 | { |
| 29 | fputs ($fp, $header . $req); |
| 30 | while (!feof($fp)) |
| 31 | { |
| 32 | $res = fgets ($fp, 1024); |
| 33 | $fgts = $fgts.$res; |
| 34 | if (strcmp ($res, "VERIFIED") == 0) |
| 35 | { |
| 36 | // check the payment_status is Completed -> done |
| 37 | // check that txn_id has not been previously processed -> done, will be checked in the model |
| 38 | // check that receiver_email is your Primary PayPal email -> done |
| 39 | // check that payment_amount/payment_currency are correct -> NOT done... |
| 40 | // process payment |
| 41 | |
| 42 | $item_name = $_POST['item_name']; |
| 43 | $id = $_POST['item_number']; // -> the item_number is the id of the user in my case |
| 44 | $receiver_email = $_POST['receiver_email']; |
| 45 | $txn_id = $_POST['txn_id']; |
| 46 | $mc_gross = $_POST['mc_gross']; |
| 47 | $payment_status = $_POST['payment_status']; |
| 48 | |
| 49 | if($item_name == 'credits' && $payment_status == 'Completed' && $receiver_email == $paypalspecs['primaryemail']) |
| 50 | { |
| 51 | if(!empty($id)) |
| 52 | { |
| 53 | $lastAmmount = $this->Credit->checkCredits($id); |
| 54 | $amount = (int) $_POST['mc_gross']; |
| 55 | $debugvar['response'] = $res; |
| 56 | $debugvar['post'] = $_POST; |
| 57 | $debugvar['fgets'] = $fgts; |
| 58 | $paypalvars = print_r($debugvar, true); |
| 59 | $data = array('Credit' => array( |
| 60 | 'count' => $amount, |
| 61 | 'action' => 'Payment via Paypal', |
| 62 | 'cumulated' => $lastAmmount+$amount, |
| 63 | 'user_id' => $id, |
| 64 | 'txn_id' => $txn_id, |
| 65 | 'mc_gross' => $mc_gross, |
| 66 | 'paypal_vars_array' => $paypalvars, |
| 67 | 'payment_status' => $payment_status |
| 68 | )); |
| 69 | $this->Credit->save($data); |
| 70 | } else { |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | else if (strcmp ($res, "INVALID") == 0) |
| 75 | { |
| 76 | // log for manual investigation |
| 77 | } |
| 78 | } |
| 79 | fclose ($fp); |
| 80 | } |
| 81 | $this->autoRender = false; |
| 82 | |
| 83 | } |
Most of the code is from the official IPN-Handler. But some things I want to explain:
Line 03 to 06: Here we go to set the credentials. You should know one important thing: If you are testing your handler via the PayPal Sandbox Instant Payment Notification (IPN) simulator, then make sure using the sandbox credentials. Otherwise the thing will never give a proper response.
Line 49: Now we have to check if the information retrieved is ok or not
Line 53: I get some other information from the model
Line 59 to 69: Here we are going to save the information
Line 81: At the end, we don’t use a view nor a layout, so we set autoRender as false.
Other comments: In this version I’m not checking the value and the currency. If you have suggestions or questions about this snippet, leave a comment!