root/donation/optionals/de.easy-coding.wcf.donation.paypal/files/lib/system/event/listener/DonationAddFormPayPalListener.class.php @ 255

Revision 255, 3.3 kB (checked in by d0nut, 5 years ago)

some bigger changes - new beta - no updates

Line 
1<?php
2require_once(WCF_DIR.'lib/system/event/listener/DonationAddFormAbstractListener.class.php');
3
4/**
5 * paypal
6 *
7 * @author      Torben Brodt
8 * @package     de.easy-coding.wcf.donation.paypal
9 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
10 */
11class DonationAddFormPayPalListener extends DonationAddFormAbstractListener {
12        protected $icon = 'paypal48.png';
13        protected $templateName = 'donationPayPal';
14        protected $system = 'paypal';
15       
16        //data
17        protected $arr = array('paypal_enable','paypal_code');
18       
19        /**
20         *
21         */
22        protected function validatePaypal() {
23                // read the post from PayPal system and add 'cmd'
24                $req = 'cmd=_notify-synch';
25
26                $tx_token = $_GET['tx'];
27                $auth_token = "GX_sTf5bW3wxRfFEbgofs88nQxvMQ7nsI8m21rzNESnl_79ccFTWj2aPgQ0";
28                $req .= "&tx=$tx_token&at=$auth_token";
29
30                // post back to PayPal system to validate
31                $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
32                $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
33                $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
34                $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
35                // If possible, securely post back to paypal using HTTPS
36                // Your PHP server will need to be SSL enabled
37                // $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
38
39                if (!$fp) {
40                        // HTTP ERROR
41                } else {
42                        fputs ($fp, $header . $req);
43                        // read the body data
44                        $res = '';
45                        $headerdone = false;
46                        while (!feof($fp)) {
47                                $line = fgets ($fp, 1024);
48                                if (strcmp($line, "\r\n") == 0) {
49                                        // read the header
50                                        $headerdone = true;
51                                }
52                                else if ($headerdone)
53                                {
54                                        // header has been read. now read the contents
55                                        $res .= $line;
56                                }
57                        }
58
59                        // parse the data
60                        $lines = explode("\n", $res);
61                        $keyarray = array();
62                        if (strcmp ($lines[0], "SUCCESS") == 0) {
63                                for ($i=1; $i<count($lines);$i++){
64                                        list($key,$val) = explode("=", $lines[$i]);
65                                        $keyarray[urldecode($key)] = urldecode($val);
66                                }
67                                // check the payment_status is Completed
68                                // check that txn_id has not been previously processed
69                                // check that receiver_email is your Primary PayPal email
70                                // check that payment_amount/payment_currency are correct
71                                // process payment
72                                $firstname = $keyarray['first_name'];
73                                $lastname = $keyarray['last_name'];
74                                $itemname = $keyarray['item_name'];
75                                $amount = $keyarray['payment_gross'];
76                                $currency = $keyarray['mc_currency'];
77                                $text = urldecode($keyarray['custom']);
78
79                                echo ("<p><h3>Thank you for your purchase!</h3></p>");
80
81                                echo ("<b>Payment Details</b><br>\n");
82                                echo ("<li>Name: $firstname $lastname</li>\n");
83                                echo ("<li>Item: $itemname</li>\n");
84                                echo ("<li>Amount: $amount</li>\n");
85                                echo ("");
86
87                                fclose ($fp);           
88                                return true;
89                        }
90                        else if (strcmp ($lines[0], "FAIL") == 0) {
91                                // log for manual investigation
92                        }
93
94                }
95
96                fclose ($fp);
97                return false;
98        }
99
100        /**
101         * @see Form::readFormParameters()
102         */
103        protected function readFormParameters() {
104                //TODO: get information from paypal
105        }
106       
107        /**
108         * @see Form::validate()
109         */
110        protected function validate() {
111                //TODO: did we get an answer from paypal? submission finished?
112                switch($this->eventObj->step) {
113                        case 10: // return page
114                                if($this->validatePaypal()) {
115                                        EventListener::fireEvent($this->eventObj, 'save'); //TODO: EventListener
116                                }
117                               
118                        break;
119                        case 20: // cancel page
120                               
121                        break;
122                }
123        }
124}
125?>
Note: See TracBrowser for help on using the browser.