Util::receiver_list()   B
last analyzed

Complexity

Conditions 6
Paths 13

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 13
cts 13
cp 1
rs 8.8497
c 0
b 0
f 0
cc 6
nc 13
nop 2
crap 6
1
<?php
2
3
namespace OpenBuildings\PayPal;
4
5
class Util {
6
7 10
	public static function receiver_list($receivers, $chained = FALSE)
8
	{
9 10
		$receiver_list = array();
10
11 10
		foreach ($receivers as $index => $receiver)
12
		{
13 9
			$receiver_list[$index]['amount'] = number_format(abs( (float) $receiver['amount']), 2, '.', '');
14
15 9
			if ( ! empty($receiver['email']))
16
			{
17 8
				$receiver_list[$index]['email'] = $receiver['email'];
18
			}
19
20 9
			if ( ! empty($receiver['accountId']))
21
			{
22 3
				$receiver_list[$index]['accountId'] = $receiver['accountId'];
23
			}
24
25 9
			if ($chained)
26
			{
27 4
				$receiver_list[$index]['primary'] = empty($receiver['primary'])
28 4
					? 'false'
29 2
					: 'true';
30
			}
31
		}
32
33 10
		return $receiver_list;
34
	}
35
36 2
	public static function array_to_nvp(array $array, $key, $prefix)
37
	{
38 2
		$result = array();
39
		
40 2
		foreach ($array[$key] as $index => $values)
41
		{
42 2
			$nvp_key = $key.'.'.$prefix.'('.$index.')';
43
44 2
			foreach ($values as $name => $value)
45
			{
46 2
				$result[$nvp_key.'.'.$name] = $value;
47
			}
48
		}
49
50 2
		return $result;
51
	}
52
53
	/**
54
	 * Parse response string without parse_str()
55
	 * parse_str converts dots and spaces to underscores.
56
	 * The reason is because all keys must be valid PHP variable names.
57
	 * http://php.net/manual/en/function.parse-str.php#76978
58
	 *
59
	 * Notice: It does not work with strings representing nested arrays like:
60
	 *     "first=value&arr[]=foo+bar&arr[]=baz"
61
	 *
62
	 * It is not needed for the purpose of parsing PayPal responses.
63
	 *
64
	 * @param  string $response_string a key value pair like param1=value1&param2=value2
65
	 * @return array
66
	 */
67 6
	public static function parse_str($response_string)
68
	{
69 6
		$response_raw_array = explode('&', $response_string);
70 6
		$response = array();
71 6
		foreach ($response_raw_array as $keyval)
72
		{
73 6
			$keyval = explode('=', $keyval);
74
			
75 6
			if (count($keyval) === 2)
76
			{
77 6
				$response[$keyval[0]] = urldecode($keyval[1]);
78
			}
79
		}
80
81 6
		return $response;
82
	}
83
84
	/**
85
	 * Validates an IPN request from Paypal.
86
	 */
87
	public static function verify_ipn($raw_data = NULL)
88
	{
89
		if ($raw_data === NULL)
90
		{
91
			$raw_data = file_get_contents('php://input');
92
		}
93
	
94
		if (empty($raw_data))
95
			return FALSE;
96
97
		$raw_post_array = explode('&', $raw_data);
98
		$post_data = array();
99
100
		foreach ($raw_post_array as $keyval)
101
		{
102
			$keyval = explode('=', $keyval);
103
			
104
			if (count($keyval) === 2)
105
			{
106
				$post_data[$keyval[0]] = urldecode($keyval[1]);
107
			}
108
		}
109
110
		$request_data = 'cmd=_notify-validate';
111
112
		foreach ($post_data as $key => $value)
113
		{
114
			if (version_compare(PHP_VERSION, '5.4') < 0 AND get_magic_quotes_gpc())
115
			{
116
				$value = stripslashes($value);
117
			}
118
119
			$value = urlencode($value);
120
121
			$request_data .= "&$key=$value";
122
		}
123
124
		$url = Payment::webscr_url();
125
126
		$curl = curl_init($url);
127
		curl_setopt_array($curl, array(
128
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
129
			CURLOPT_POST => 1,
130
			CURLOPT_RETURNTRANSFER => 1,
131
			CURLOPT_POSTFIELDS => $request_data,
132
			CURLOPT_SSL_VERIFYPEER => 1,
133
			CURLOPT_SSL_VERIFYHOST => 2,
134
			CURLOPT_FORBID_REUSE => 1,
135
			CURLOPT_HTTPHEADER => array(
136
				'Connection: Close',
137
			)
138
		));
139
140
		if ( ! ($response = curl_exec($curl)))
141
		{
142
			// Get the error code and message
143
			$code  = curl_errno($curl);
144
			$error = curl_error($curl);
145
146
			// Close curl
147
			curl_close($curl);
148
149
			throw new Request_Exception('PayPal API request for :method failed: :error (:code)', $url, $post_data, array(
150
				':method' => '_notify-validate',
151
				':error' => $error,
152
				':code' => $code
153
			));
154
		}
155
156
		curl_close($curl);
157
158
		if ($response === 'VERIFIED')
159
			return TRUE;
160
161
		if ($response === 'INVALID')
162
			throw new Request_Exception('PayPal request to verify IPN was invalid!', $url, $post_data);
163
164
		return FALSE;
165
	}
166
}
167