This file is indexed.

/usr/share/php/kohana2/modules/payment/libraries/drivers/Payment/Paypal.php is in libkohana2-modules-php 2.3.4-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Paypal Payment Driver. Express Checkout transactions consist of 3 stages with
 * a separate API call for each: SetExpressCheckout, GetExpressCheckout (optional)
 * and DoExpressCheckout. This class has a corresponding array of fields for each
 * call these are used to construct the required name value pairs for the request
 * to each API call.
 *
 * $Id: Paypal.php 1978 2008-03-25 12:05:32GMT by atomless -
 *
 *
 * @package    Payment
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
*/
class Payment_Paypal_Driver implements Payment_Driver {

	// this array details the required fields within the arrays $set_express_checkout_fields,
	// $get_express_checkout_fields, $do_express_checkout_fields as well as the
	// fields wihtin api_connection and api_authorization
	private $required_fields = array
	(
		'USER'          => FALSE,
		'PWD'           => FALSE,
		'SIGNATURE'     => FALSE,

		'RETURNURL'     => FALSE,
		'CANCELURL'     => FALSE,

		'AMT'           => FALSE, // payment amount
	);


	//-- RESPONSE to setExpressCheckout calls will take the form:
	//-- https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token={20 single byte character timestamped token}
	//-- this token is passed back and forth throughout the stages of the express checkout process

	private $set_express_checkout_fields = array
	(
		//-- REQUIRED --//
		'METHOD'        => 'SetExpressCheckout',

		'RETURNURL'     => '',
		'CANCELURL'     => '',

		'AMT'           => '', // payment amount - MUST include decimal point followed by two further digits

		//-- OPTIONAL --//
		'CURRENCYCODE'  => '', // default is USD - only required if other currency needed
		'MAXAMT'        => '',

		// USERACTION defaults to 'continue'
		// if set to 'commit' the submit button on the paypal site transaction page is labelled 'Pay'
		// instead of 'continue' - meaning that you can just go ahead and call DoExpressCheckout
		// right away on the RETURNURL page on your site without needing a further order review page
		// with a 'pay now' button.
		'USERACTION'    => 'continue',

		'INVNUM'        => '', // Your own unique invoice / tracking number


		//-- set ADDROVERRIDE to '1' if you want to collect the shipping address on your site
		//-- and have that over-ride the user's stored details on paypal
		//-- if set to '1' you will of course also need to pass the shipping details!
		'ADDROVERRIDE'  => '0',

		'SHIPTONAME'         => '',
		'SHIPTOSTREET'       => '',
		'SHIPTOSTREET2'      => '',
		'SHIPTOCITY'         => '',
		'SHIPTOSTATE'        => '',
		'SHIPTOZIP'          => '',
		'SHIPTOCOUNTRYCODE'  => '', // list of country codes here: https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/countrycodes_new.html#1006794

		'LOCALECODE'    => 'US', // Defaults to 'US' - list of country codes here: https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/countrycodes_new.html#1006794

		//-- If YOU WANT TO APPLY CUSTOM STYLING TO PAYAPAL PAGES - see under SetExpressCheckout Request for descriptions here: https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/Appx_fieldreference.html#2557853
		'PAGESTYLE'     => '', //set this to match the name of any page style you set up in the profile subtab of your paypal account

		'HDRIMG'        => '', //header image displayed top left, size: 750px x 90px. defaults to business name in text

	// there are several other optional settings supported
	// see under SetExpressCheckout Request here : https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/Appx_fieldreference.html#2557853
	);

	private $get_express_checkout_fields = array
	(
		'METHOD' => 'GetExpressCheckoutDetails',
		'TOKEN'  => '' // this token is retrieved from the response to the SetExpressCheckout call
	);

	//-- associative array filled by the paypal api response to a call to the GetExpressCheckout method
	private $get_express_checkout_response = array();
	// responses contain these fields:
	// TOKEN
	// EMAIL
	// PAYERID     - paypal customer id -  13 single byte alpha numeric
	// PAYERSTATUS - verified or unverified
	// SALUTATION  - 20 single byte characters
	// FIRSTNAME   - 25 single byte characters
	// LASTNAME    - 25 single byte characters
	// MIDDLENAME  - 25 single byte characters
	// SUFFIX      - payer's suffix - 12 single byte character
	// COUNTRYCODE - list of country codes here: https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/countrycodes_new.html#1006794
	// BUSINESS    - payer's business name
	// SHIPTONAME
	// SHIPTOSTREET
	// SHIPTOSTREET2
	// SHIPTOCITY
	// SHIPTOSTATE
	// SHIPTOZIP
	// SHIPTOCOUNTRYCODE
	// ADDRESSSTATUS - status of the street address on file with paypal
	// CUSTOM        - freeform field as optionally set by you in the setExpressCheckout call
	// INVNUM        - invoice tracking number as optionally set by you in the setExpressCheckout call
	// PHONENUM      -  Note: PayPal returns a contact telephone number only if your Merchant account profile settings require that the buyer enter one.
	// REDIRECTREQUIRED - flag to indicate whether you need to redirect the customer to back to PayPal after completing the transaction.

	private $do_express_checkout_fields = array
	(
		//-- REQUIRED --
		'METHOD'        => 'DoExpressCheckoutPayment',
		'TOKEN'         => '', // this token is retrieved from the response to the setExpressCheckout call
		'PAYERID'       => '',
		'AMT'           => '', // payment amount - MUST include decimal point followed by two further digits
		'PAYMENTACTION' => 'Sale',

		//-- OPTIONAL --
		'CURRENCYCODE'  => '', // default is USD - only required if other currency needed
		'INVNUM'        => '',
		'ITEMAMT'       => '', // sum cost of all items in order not including shipping or handling
		'SHIPPINGAMT'   => '',
		'HANDLINGAMT'   => '',
		'TAXAMT'        => '',

		//-- OPTIONAL ORDER CONTENTS INFO
		/* these fileds would obviously need setting dynamically but shown here as an example

		'L_NAME0'   => '', // max 127 single-byte characters product/item name
		'L_NUMBER0' => '', // max 127 single-byte characters product/item number
		'L_QTY0'    => '', // positive integer
		'L_TAXAMT0'  => '', // item sales tax amount
		'L_AMT0'     => '', // cost of item

		'L_NAME1'...

		'L_NAME2'...

		*/
	);

	private $api_authroization_fields = array
	(
		'USER'          => '',
		'PWD'           => '',
		'SIGNATURE'     => '',
		'VERSION'       => '3.2',
	);


	private $api_connection_fields = array
	(
		'ENDPOINT'      => 'https://api-3t.paypal.com/nvp',
		'PAYPALURL'     => 'https://www.paypal.com/webscr&cmd=_express-checkout&token=',
		'ERRORURL'      => '',
		'GETDETAILS'    => TRUE
	);

	private $array_of_arrays;

	private $test_mode = TRUE;

	private $nvp_response_array = array();
	// after successful transaction $nvp_response_array will contain
	// TOKEN           - The timestamped token value that was returned by SetExpressCheckout
	// TRANSACTIONID   - Unique transaction ID of the payment. 19 single-byte characters
	// TRANSACTIONTYPE - possible values: 'cart' or 'express-checkout'
	// PAYMENTTYPE     - Indicates whether the payment is instant or delayed. possible values: 'non', 'echeck', 'instant'
	// ORDERTIME       - Time/date stamp of payment
	// AMT             - The final amount charged, including any generic shipping and taxes set in your Merchant Profile.
	// CURRENCYCODE    - 3 char currency code:  https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/Appx_fieldreference.html#2557565
	// FEEAMT          - PayPal fee amount charged for the transaction
	// SETTLEAMT       - Amount deposited in your PayPal account after a currency conversion.
	// TAXAMT          - Tax charged on the transaction.
	// EXCHANGERATE    - Exchange rate if a currency conversion occurred.
	// PAYMENTSTATUS   - possible values: 'Completed' or 'Pending'
	// PENDINGREASON   - possible values: 'none', 'address', 'echeck', 'int1', 'multi-currency', 'verify', 'other'
	// REASONCODE      - The reason for a reversal if TransactionType is reversal. possible values: 'none', 'chargeback', 'guarantee', 'buyer-complaint', 'refund', 'other'
	// for more info see : https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/Appx_fieldreference.html#2557853

	/**
	* Sets the config for the class.
	*
	* @param  array  config passed from the library
	*/
	public function __construct($config)
	{
		$this->array_of_arrays = array
		(
			&$this->set_express_checkout_fields,
			&$this->get_express_checkout_fields,
			&$this->do_express_checkout_fields,
			&$this->api_authroization_fields,
			&$this->api_connection_fields
		);

		$this->set_fields($config);

		$this->test_mode = $config['test_mode'];

		if ($this->test_mode)
		{
			$this->api_authroization_fields['USER']      = $config['SANDBOX_USER'];
			$this->api_authroization_fields['PWD']       = $config['SANDBOX_PWD'];
			$this->api_authroization_fields['SIGNATURE'] = $config['SANDBOX_SIGNATURE'];

			$this->api_connection_fields['ENDPOINT']     = $config['SANDBOX_ENDPOINT'];
			$this->api_connection_fields['PAYPALURL']    = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=';
			$this->api_connection_fields['ENDPOINT']     = 'https://api-3t.sandbox.paypal.com/nvp';
		}

		$this->curl_config = $config['curl_config'];

		$this->session = Session::instance();

		Kohana::log('debug', 'PayPal Payment Driver Initialized');
	}

	/**
	*@desc set fields for nvp string
	*/
	public function set_fields($config)
	{

		foreach ($config as $key => $val)
		{
			// Handle any necessary field name translation
			switch ($key)
			{
				case 'amount':
					$key = 'AMT';
					break;
				default:
			}

			if (array_key_exists($key, $this->required_fields) AND !empty($val))
			{
				$this->required_fields[$key] = TRUE;
			}

			foreach ($this->array_of_arrays as &$arr)
			{
				if (array_key_exists($key, $arr))
				{
					$arr[$key] = $val;
				}
			}
		}
	}

	public function process()
	{
		// Check for required fields
		if (in_array(FALSE, $this->required_fields))
		{
			$fields = array();
			foreach ($this->required_fields as $key => $field)
			{
				if ( ! $field)
				{
					$fields[] = $key;
				}
			}

			throw new Kohana_Exception('payment.required', implode(', ', $fields));
		}

		// stage 1 - if no token yet set then we know we just need to run set_express_checkout
		$paypal_token = $this->session->get('paypal_token', FALSE);
		if ( ! $paypal_token)
		{
			$this->set_express_checkout();
			return FALSE;
		}
		else
		{
			$this->set_fields(array('TOKEN' => $paypal_token));
		}

		// stage 2 (optional) retrieve the user info from paypal and store it in the get_express_checkout_response array
		// --------------------------------------------------------------------------------
		// *note: if you don't wish to record the user info (shipping address etc)
		//        then you can skip this stage by setting GETDETAILS to FALSE
		//        like so:
		//        $this->payment = new Payment('Paypal');
		//        $this->payment->GETDETAILS = FALSE;
		// IMPORTANT - if you do choose to skip this step you will need to extract
		//             the PayerID from the $_GET array in the method targeted by RETURNURL
		//             and use the value to set the PAYERID value of your payment object
		//             eg:
		//             $this->payment = new Payment('Paypal');
		//             $this->payment->PAYERID = $this->input->get('PayerID');
		// --------------------------------------------------------------------------------
		if ($this->api_connection_fields['GETDETAILS'])
		{
			$this->get_express_checkout();
		}

		// stage 3
		if (empty($this->do_express_checkout_fields['PAYERID']))
		{
			throw new Kohana_Exception('payment.required', 'PAYERID');
		}

		$this->do_express_checkout_payment();

		return (strtoupper($this->nvp_response_array['ACK']) == 'Success') ? TRUE : array_merge($this->nvp_response_array, $this->get_express_checkout_response);
	}

	/**
	* (stage 1)
	* Runs paypal authentication and sets up express checkout options (stage 1)
	*/
	protected function set_express_checkout()
	{
		$nvp_str = http_build_query($this->remove_empty_optional_fields($this->set_express_checkout_fields));

		$response = $this->make_paypal_api_request($nvp_str);

		parse_str(urldecode($response),$response_array);

		if (strtoupper($response_array['ACK']) == 'SUCCESS')
		{
			$paypal_token = $response_array['TOKEN'];

			// Redirect to paypal.com here
			$this->session->set('paypal_token', urldecode($paypal_token));

			// We are off to paypal to login!
			if ($this->set_express_checkout_fields['USERACTION']=='commit')
			{
				url::redirect($this->api_connection_fields['PAYPALURL'].$paypal_token.'&useraction=commit');
			}
			else
			{
				url::redirect($this->api_connection_fields['PAYPALURL'].$paypal_token);
			}
		}
		else // Something went terribly wrong...
		{
			throw new Kohana_User_Exception('SetExpressCheckout ERROR', Kohana::debug($response_array));

			Kohana::log('error', Kohana::debug('SetExpressCheckout response:'.$response_array));
			//url::redirect($this->api_connection_fields['ERRORURL']);
		}
	}

	/**
	* (stage 2)
	* Retrieves all the user info from paypal and stores it in the get_express_checkout_response array
	*
	*/
	protected function get_express_checkout()
	{
		$nvp_str = http_build_query($this->get_express_checkout_fields);

		$response = $this->make_paypal_api_request($nvp_str);

		parse_str(urldecode($response),$this->get_express_checkout_response);

		if (strtoupper($this->get_express_checkout_response['ACK']) == 'SUCCESS')
		{
			$this->set_fields(array('PAYERID' => $this->get_express_checkout_response['PAYERID']));
		}
		else // Something went terribly wrong...
		{
			throw new Kohana_User_Exception('GetExpressCheckout ERROR', Kohana::debug($this->get_express_checkout_response));

			Kohana::log('error', Kohana::debug('GetExpressCheckout response:'.$response));
			url::redirect($this->api_connection_fields['ERRORURL']);
		}
	}

	/**
	* (stage 3)
	* complete paypal transaction - store response in nvp_response_array
	*
	*/
	protected function do_express_checkout_payment()
	{
		$nvp_qstr = http_build_query($this->remove_empty_optional_fields($this->do_express_checkout_fields));

		$response = $this->make_paypal_api_request($nvp_qstr);

		parse_str(urldecode($response),$this->nvp_response_array);

		if (strtoupper($this->nvp_response_array['ACK']) != 'SUCCESS')
		{
			throw new Kohana_User_Exception('DoExpressCheckoutPayment ERROR', Kohana::debug($this->nvp_response_array));

			Kohana::log('error', Kohana::debug('GetExpressCheckout response:'.$response));
			url::redirect($this->api_connection_fields['ERRORURL']);
		}
	}

	/**
	* Runs the CURL methods to communicate with paypal.
	*
	* @param   string  paypal API method to run
	* @param   string  any additional name-value-pair query string data to send to paypal
	* @return  mixed
	*/
	protected function make_paypal_api_request($nvp_str)
	{
		$postdata = http_build_query($this->api_authroization_fields).'&'.$nvp_str;

		parse_str(urldecode($postdata),$nvpstr);

		Kohana::log('debug', 'Connecting to '.$this->api_connection_fields['ENDPOINT']);

		$ch = curl_init($this->api_connection_fields['ENDPOINT']);

		// Set custom curl options
		curl_setopt_array($ch, $this->curl_config);

		// Setting the nvpreq as POST FIELD to curl
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

		// Getting response from server
		$response = curl_exec($ch);

		if (curl_errno($ch))
		{
			throw new Kohana_User_Exception('CURL ERROR', Kohana::debug(array('curl_error_no' => curl_errno($ch), 'curl_error_msg' => curl_error($ch))));

			// Moving to error page to display curl errors
			$this->session->set_flash(array('curl_error_no' => curl_errno($ch), 'curl_error_msg' => curl_error($ch)));
			url::redirect($this->api_connection_fields['ERRORURL']);
		}
		else
		{
			curl_close($ch);
		}

		return $response;
	}

	/**
	* What is says on the tin
	* @param array
	* @return edited array
	*
	*/
	protected function remove_empty_optional_fields($arr)
	{
		foreach ($arr as $key => $val)
		{
			// don't include unset optional fields in the name-value pair request string
			if ($val==='') unset($arr[$key]);
		}
		return $arr;
	}

} // End Payment_Paypal_Driver Class