├── Paypal_lib.php ├── README.md └── paypallib_config.php /Paypal_lib.php: -------------------------------------------------------------------------------- 1 | 43 | * @copyright Copyright (c) 2006, http://aroussi.com/ci/ 44 | * 45 | */ 46 | 47 | // ------------------------------------------------------------------------ 48 | 49 | class paypal_lib { 50 | 51 | var $last_error; // holds the last error encountered 52 | var $ipn_log; // bool: log IPN results to text file? 53 | 54 | var $ipn_log_file; // filename of the IPN log 55 | var $ipn_response; // holds the IPN response from paypal 56 | var $ipn_data = array(); // array contains the POST values for IPN 57 | var $fields = array(); // array holds the fields to submit to paypal 58 | 59 | var $submit_btn = ''; // Image/Form button 60 | var $button_path = ''; // The path of the buttons 61 | 62 | var $CI; 63 | 64 | function __construct() 65 | { 66 | $this->CI =& get_instance(); 67 | $this->CI->load->helper('url'); 68 | $this->CI->load->helper('form'); 69 | $this->CI->load->config('paypallib_config'); 70 | 71 | $sanbox = $this->CI->config->item('sandbox'); 72 | $this->paypal_url = ($sanbox == TRUE)?'https://www.sandbox.paypal.com/cgi-bin/webscr':'https://www.paypal.com/cgi-bin/webscr'; 73 | 74 | $this->last_error = ''; 75 | $this->ipn_response = ''; 76 | 77 | $this->ipn_log_file = $this->CI->config->item('paypal_lib_ipn_log_file'); 78 | $this->ipn_log = $this->CI->config->item('paypal_lib_ipn_log'); 79 | 80 | $this->button_path = $this->CI->config->item('paypal_lib_button_path'); 81 | 82 | // populate $fields array with a few default values. See the paypal 83 | // documentation for a list of fields and their data types. These defaul 84 | // values can be overwritten by the calling script. 85 | $businessEmail = $this->CI->config->item('business'); 86 | $this->add_field('business',$businessEmail); 87 | $this->add_field('rm','2'); // Return method = POST 88 | $this->add_field('cmd','_xclick'); 89 | 90 | $this->add_field('currency_code', $this->CI->config->item('paypal_lib_currency_code')); 91 | $this->add_field('quantity', '1'); 92 | $this->button('Pay Now!'); 93 | } 94 | 95 | function button($value) 96 | { 97 | // changes the default caption of the submit button 98 | $this->submit_btn = form_submit('pp_submit', $value); 99 | } 100 | 101 | function image($file) 102 | { 103 | $this->submit_btn = ''; 104 | } 105 | 106 | 107 | function add_field($field, $value) 108 | { 109 | // adds a key=>value pair to the fields array, which is what will be 110 | // sent to paypal as POST variables. If the value is already in the 111 | // array, it will be overwritten. 112 | $this->fields[$field] = $value; 113 | } 114 | 115 | function paypal_auto_form() 116 | { 117 | // this function actually generates an entire HTML page consisting of 118 | // a form with hidden elements which is submitted to paypal via the 119 | // BODY element's onLoad attribute. We do this so that you can validate 120 | // any POST vars from you custom form before submitting to paypal. So 121 | // basically, you'll have your own form which is submitted to your script 122 | // to validate the data, which in turn calls this function to create 123 | // another hidden form and submit to paypal. 124 | 125 | $this->button('Click here if you\'re not automatically redirected...'); 126 | 127 | echo '' . "\n"; 128 | echo 'Processing Payment...' . "\n"; 129 | echo '' . "\n"; 130 | echo '

Please wait, your order is being processed and you will be redirected to the paypal website.

' . "\n"; 131 | echo $this->paypal_form('paypal_auto_form'); 132 | echo ''; 133 | } 134 | 135 | function paypal_form($form_name='paypal_form') 136 | { 137 | $str = ''; 138 | $str .= '
' . "\n"; 139 | foreach ($this->fields as $name => $value) 140 | $str .= form_hidden($name, $value) . "\n"; 141 | $str .= '

'. $this->submit_btn . '

'; 142 | $str .= form_close() . "\n"; 143 | 144 | return $str; 145 | } 146 | 147 | function validate_ipn() 148 | { 149 | // parse the paypal URL 150 | $url_parsed = parse_url($this->paypal_url); 151 | 152 | // generate the post string from the _POST vars aswell as load the 153 | // _POST vars into an arry so we can play with them from the calling 154 | // script. 155 | $post_string = ''; 156 | if ($this->CI->input->post()) 157 | { 158 | foreach ($this->CI->input->post() as $field=>$value) 159 | { 160 | $this->ipn_data[$field] = $value; 161 | $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; 162 | } 163 | } 164 | 165 | $post_string.="cmd=_notify-validate"; // append ipn command 166 | 167 | // open the connection to paypal 168 | $fp = fsockopen($url_parsed['host'],"80",$err_num,$err_str,30); 169 | if(!$fp) 170 | { 171 | // could not open the connection. If loggin is on, the error message 172 | // will be in the log. 173 | $this->last_error = "fsockopen error no. $errnum: $errstr"; 174 | $this->log_ipn_results(false); 175 | return false; 176 | } 177 | else 178 | { 179 | // Post the data back to paypal 180 | fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n"); 181 | fputs($fp, "Host: $url_parsed[host]\r\n"); 182 | fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 183 | fputs($fp, "Content-length: ".strlen($post_string)."\r\n"); 184 | fputs($fp, "Connection: close\r\n\r\n"); 185 | fputs($fp, $post_string . "\r\n\r\n"); 186 | 187 | // loop through the response from the server and append to variable 188 | while(!feof($fp)) 189 | $this->ipn_response .= fgets($fp, 1024); 190 | 191 | fclose($fp); // close connection 192 | } 193 | 194 | if (preg_match("/VERIFIED/",$this->ipn_response)) 195 | { 196 | // Valid IPN transaction. 197 | $this->log_ipn_results(true); 198 | return true; 199 | } 200 | else 201 | { 202 | // Invalid IPN transaction. Check the log for details. 203 | $this->last_error = 'IPN Validation Failed.'; 204 | $this->log_ipn_results(false); 205 | return false; 206 | } 207 | } 208 | 209 | function log_ipn_results($success) 210 | { 211 | if (!$this->ipn_log) return; // is logging turned off? 212 | 213 | // Timestamp 214 | $text = '['.date('m/d/Y g:i A').'] - '; 215 | 216 | // Success or failure being logged? 217 | if ($success) $text .= "SUCCESS!\n"; 218 | else $text .= 'FAIL: '.$this->last_error."\n"; 219 | 220 | // Log the POST variables 221 | $text .= "IPN POST Vars from Paypal:\n"; 222 | foreach ($this->ipn_data as $key=>$value) 223 | $text .= "$key=$value, "; 224 | 225 | // Log the response from the paypal server 226 | $text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response; 227 | 228 | // Write to log 229 | $fp=fopen($this->ipn_log_file,'a'); 230 | fwrite($fp, $text . "\n\n"); 231 | 232 | fclose($fp); // close file 233 | } 234 | 235 | 236 | function dump() 237 | { 238 | // Used for debugging, this function will output all the field/value pairs 239 | // that are currently defined in the instance of the class using the 240 | // add_field() function. 241 | 242 | ksort($this->fields); 243 | echo '

ppal->dump() Output:

' . "\n"; 244 | echo '' . "\n"; 245 | foreach ($this->fields as $key => $value) echo ''. $key .': '. urldecode($value) .'
'; 246 | echo "
\n"; 247 | } 248 | 249 | 250 | function curlPost($paypalurl,$paypalreturnarr) 251 | { 252 | 253 | $req = 'cmd=_notify-validate'; 254 | foreach($paypalreturnarr as $key => $value) 255 | { 256 | $value = urlencode(stripslashes($value)); 257 | $req .= "&$key=$value"; 258 | } 259 | 260 | $ipnsiteurl=$paypalurl; 261 | $ch = curl_init(); 262 | curl_setopt($ch, CURLOPT_URL, $ipnsiteurl); 263 | curl_setopt($ch, CURLOPT_HEADER, false); 264 | curl_setopt($ch, CURLOPT_POST, 1); 265 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 266 | curl_setopt($ch, CURLOPT_POSTFIELDS, $req); 267 | $result = curl_exec($ch); 268 | curl_close($ch); 269 | 270 | return $result; 271 | } 272 | 273 | 274 | 275 | } 276 | 277 | ?> 278 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # paypal-library-for-codeigniter 2 | This repository contains PayPal library with the config file for helping to integrate PayPal Payment Gateway in Codeigniter. 3 | 4 | #Usage: 5 | Paypal_lib library has a dependency of a config file called paypallib_config.php. paypal_lib.php file will be placed in the "application/libraries/" directory and paypallib_config.php file will be placed in the "application/config/" directory. 6 | -------------------------------------------------------------------------------- /paypallib_config.php: -------------------------------------------------------------------------------- 1 | 24 | --------------------------------------------------------------------------------