Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | require_once 'Google/library/googlecart.php'; | |
37 | require_once 'Google/library/googleitem.php'; | |
38 | require_once 'Google/library/googlesubscription.php'; | |
39 | require_once 'Google/library/googlerequest.php'; | |
40 | class CRM_Core_Payment_Google extends CRM_Core_Payment { | |
41 | ||
42 | /** | |
43 | * mode of operation: live or test | |
44 | * | |
45 | * @var object | |
46 | */ | |
47 | protected $_mode = NULL; | |
48 | ||
49 | /** | |
50 | * We only need one instance of this object. So we use the singleton | |
51 | * pattern and cache the instance in this variable | |
52 | * | |
53 | * @var object | |
54 | * @static | |
55 | */ | |
56 | static private $_singleton = NULL; | |
57 | ||
58 | /** | |
59 | * Constructor | |
60 | * | |
61 | * @param string $mode the mode of operation: live or test | |
62 | * | |
63 | * @return void | |
64 | */ | |
65 | function __construct($mode, &$paymentProcessor) { | |
66 | $this->_mode = $mode; | |
67 | $this->_paymentProcessor = $paymentProcessor; | |
68 | $this->_processorName = ts('Google Checkout'); | |
69 | } | |
70 | ||
71 | /** | |
72 | * singleton function used to manage this object | |
73 | * | |
74 | * @param string $mode the mode of operation: live or test | |
75 | * | |
76 | * @return object | |
77 | * @static | |
78 | * | |
79 | */ | |
80 | static function &singleton($mode, &$paymentProcessor) { | |
81 | $processorName = $paymentProcessor['name']; | |
82 | if (!isset(self::$_singleton[$processorName]) || self::$_singleton[$processorName] === NULL) { | |
83 | self::$_singleton[$processorName] = new CRM_Core_Payment_Google($mode, $paymentProcessor); | |
84 | } | |
85 | return self::$_singleton[$processorName]; | |
86 | } | |
87 | ||
88 | /** | |
89 | * This function checks to see if we have the right config values | |
90 | * | |
91 | * @return string the error message if any | |
92 | * @public | |
93 | */ | |
94 | function checkConfig() { | |
95 | $config = CRM_Core_Config::singleton(); | |
96 | ||
97 | $error = array(); | |
98 | ||
99 | if (empty($this->_paymentProcessor['user_name'])) { | |
100 | $error[] = ts('User Name is not set in the Administer CiviCRM » System Settings » Payment Processors.'); | |
101 | } | |
102 | ||
103 | if (empty($this->_paymentProcessor['password'])) { | |
104 | $error[] = ts('Password is not set in the Administer CiviCRM » System Settings » Payment Processors.'); | |
105 | } | |
106 | ||
107 | if (!empty($error)) { | |
108 | return implode('<p>', $error); | |
109 | } | |
110 | else { | |
111 | return NULL; | |
112 | } | |
113 | } | |
114 | ||
115 | function doDirectPayment(&$params) { | |
116 | CRM_Core_Error::fatal(ts('This function is not implemented')); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Sets appropriate parameters for checking out to google | |
121 | * | |
122 | * @param array $params name value pair of contribution datat | |
123 | * | |
124 | * @return void | |
125 | * @access public | |
126 | * | |
127 | */ | |
128 | function doTransferCheckout(&$params, $component) { | |
129 | $component = strtolower($component); | |
130 | ||
131 | if (CRM_Utils_Array::value('is_recur', $params) && | |
132 | $params['contributionRecurID'] | |
133 | ) { | |
134 | return $this->doRecurCheckout($params, $component); | |
135 | } | |
136 | ||
137 | //Create a new shopping cart object | |
138 | // Merchant ID | |
139 | $merchant_id = $this->_paymentProcessor['user_name']; | |
140 | // Merchant Key | |
141 | $merchant_key = $this->_paymentProcessor['password']; | |
142 | $server_type = ($this->_mode == 'test') ? 'sandbox' : ''; | |
143 | ||
144 | $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $params['currencyID']); | |
145 | $item1 = new GoogleItem($params['item_name'], '', 1, $params['amount']); | |
146 | $cart->AddItem($item1); | |
147 | ||
148 | $this->submitPostParams($params, $component, $cart); | |
149 | } | |
150 | ||
151 | function doRecurCheckout(&$params, $component) { | |
152 | $intervalUnit = CRM_Utils_Array::value('frequency_unit', $params); | |
153 | if ($intervalUnit == 'week') { | |
154 | $intervalUnit = 'WEEKLY'; | |
155 | } | |
156 | elseif ($intervalUnit == 'year') { | |
157 | $intervalUnit = 'YEARLY'; | |
158 | } | |
159 | elseif ($intervalUnit == 'day') { | |
160 | $intervalUnit = 'DAILY'; | |
161 | } | |
162 | elseif ($intervalUnit == 'month') { | |
163 | $intervalUnit = 'MONTHLY'; | |
164 | } | |
165 | ||
166 | // Merchant ID | |
167 | $merchant_id = $this->_paymentProcessor['user_name']; | |
168 | // Merchant Key | |
169 | $merchant_key = $this->_paymentProcessor['password']; | |
170 | $server_type = ($this->_mode == 'test') ? 'sandbox' : ''; | |
171 | ||
172 | $itemName = CRM_Utils_Array::value('item_name', $params); | |
173 | $description = CRM_Utils_Array::value('description', $params); | |
174 | $amount = CRM_Utils_Array::value('amount', $params); | |
175 | $installments = CRM_Utils_Array::value('installments', $params); | |
176 | ||
177 | $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $params['currencyID']); | |
178 | $item = new GoogleItem($itemName, $description, 1, $amount); | |
179 | $subscription_item = new GoogleSubscription("merchant", $intervalUnit, $amount, $installments); | |
180 | ||
181 | $item->SetSubscription($subscription_item); | |
182 | $cart->AddItem($item); | |
183 | ||
184 | $this->submitPostParams($params, $component, $cart); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Builds appropriate parameters for checking out to google and submits the post params | |
189 | * | |
190 | * @param array $params name value pair of contribution data | |
191 | * @param string $component event/contribution | |
192 | * @param object $cart object of googel cart | |
193 | * | |
194 | * @return void | |
195 | * @access public | |
196 | * | |
197 | */ | |
198 | function submitPostParams($params, $component, $cart) { | |
199 | $url = rtrim($this->_paymentProcessor['url_site'], '/') . '/cws/v2/Merchant/' . $this->_paymentProcessor['user_name'] . '/checkout'; | |
200 | ||
201 | if ($component == "event") { | |
202 | $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},eventID={$params['eventID']},participantID={$params['participantID']},invoiceID={$params['invoiceID']}"; | |
203 | } | |
204 | elseif ($component == "contribute") { | |
205 | $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},invoiceID={$params['invoiceID']}"; | |
206 | ||
207 | $contributionRecurID = CRM_Utils_Array::value('contributionRecurID', $params); | |
208 | if ($contributionRecurID) { | |
209 | $privateData .= ",contributionRecurID=$contributionRecurID"; | |
210 | } | |
211 | ||
212 | $membershipID = CRM_Utils_Array::value('membershipID', $params); | |
213 | if ($membershipID) { | |
214 | $privateData .= ",membershipID=$membershipID"; | |
215 | } | |
216 | ||
217 | $relatedContactID = CRM_Utils_Array::value('related_contact', $params); | |
218 | if ($relatedContactID) { | |
219 | $privateData .= ",relatedContactID=$relatedContactID"; | |
220 | ||
221 | $onBehalfDupeAlert = CRM_Utils_Array::value('onbehalf_dupe_alert', $params); | |
222 | if ($onBehalfDupeAlert) { | |
223 | $privateData .= ",onBehalfDupeAlert=$onBehalfDupeAlert"; | |
224 | } | |
225 | } | |
226 | } | |
227 | ||
228 | // Allow further manipulation of the arguments via custom hooks .. | |
229 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $privateData); | |
230 | ||
231 | $cart->SetMerchantPrivateData($privateData); | |
232 | ||
233 | if ($component == "event") { | |
234 | $returnURL = CRM_Utils_System::url('civicrm/event/register', | |
235 | "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", | |
236 | TRUE, NULL, FALSE | |
237 | ); | |
238 | } | |
239 | elseif ($component == "contribute") { | |
240 | $returnURL = CRM_Utils_System::url('civicrm/contribute/transact', | |
241 | "_qf_ThankYou_display=1&qfKey={$params['qfKey']}", | |
242 | TRUE, NULL, FALSE | |
243 | ); | |
244 | } | |
245 | $cart->SetContinueShoppingUrl($returnURL); | |
246 | ||
247 | $cartVal = base64_encode($cart->GetXML()); | |
248 | $signatureVal = base64_encode($cart->CalcHmacSha1($cart->GetXML())); | |
249 | ||
250 | $googleParams = array( | |
251 | 'cart' => $cartVal, | |
252 | 'signature' => $signatureVal, | |
253 | ); | |
254 | ||
255 | require_once 'HTTP/Request.php'; | |
256 | $params = array( | |
257 | 'method' => HTTP_REQUEST_METHOD_POST, | |
258 | 'allowRedirects' => FALSE, | |
259 | ); | |
260 | $request = new HTTP_Request($url, $params); | |
261 | foreach ($googleParams as $key => $value) { | |
262 | $request->addPostData($key, $value); | |
263 | } | |
264 | ||
265 | $result = $request->sendRequest(); | |
266 | ||
267 | if (PEAR::isError($result)) { | |
268 | CRM_Core_Error::fatal($result->getMessage()); | |
269 | } | |
270 | ||
271 | if ($request->getResponseCode() != 302) { | |
272 | CRM_Core_Error::fatal(ts('Invalid response code received from Google Checkout: %1', | |
273 | array(1 => $request->getResponseCode()) | |
274 | )); | |
275 | } | |
276 | CRM_Utils_System::redirect($request->getResponseHeader('location')); | |
277 | CRM_Utils_System::civiExit(); | |
278 | } | |
279 | ||
280 | /** | |
281 | * hash_call: Function to perform the API call to PayPal using API signature | |
282 | * @paymentProcessor is the array of payment processor settings value. | |
283 | * @searchParamsnvpStr is the array of search params. | |
284 | * returns an associtive array containing the response from the server. | |
285 | */ | |
286 | function invokeAPI($paymentProcessor, $searchParams) { | |
287 | $merchantID = $paymentProcessor['user_name']; | |
288 | $merchantKey = $paymentProcessor['password']; | |
289 | $siteURL = rtrim(str_replace('https://', '', $paymentProcessor['url_site']), '/'); | |
290 | ||
291 | $url = "https://{$merchantID}:{$merchantKey}@{$siteURL}/api/checkout/v2/reports/Merchant/{$merchantID}"; | |
292 | $xml = self::buildXMLQuery($searchParams); | |
293 | ||
294 | if (!function_exists('curl_init')) { | |
295 | CRM_Core_Error::fatal("curl functions NOT available."); | |
296 | } | |
297 | ||
298 | $ch = curl_init(); | |
299 | curl_setopt($ch, CURLOPT_URL, $url); | |
300 | curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
301 | ||
302 | //turning off the server and peer verification(TrustManager Concept). | |
303 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL')); | |
17c04b52 | 304 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'verifySSL') ? 2 : 0); |
6a488035 TO |
305 | |
306 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
307 | curl_setopt($ch, CURLOPT_POST, 1); | |
308 | ||
309 | //setting the nvpreq as POST FIELD to curl | |
310 | curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); | |
311 | ||
312 | //getting response from server | |
313 | $xmlResponse = curl_exec($ch); | |
314 | ||
315 | // strip slashes if needed | |
316 | if (get_magic_quotes_gpc()) { | |
317 | $xmlResponse = stripslashes($xmlResponse); | |
318 | } | |
319 | ||
320 | if (curl_errno($ch)) { | |
321 | $e = CRM_Core_Error::singleton(); | |
322 | $e->push(curl_errno($ch), | |
323 | 0, NULL, | |
324 | curl_error($ch) | |
325 | ); | |
326 | return $e; | |
327 | } | |
328 | else { | |
329 | curl_close($ch); | |
330 | } | |
331 | ||
332 | return self::getArrayFromXML($xmlResponse); | |
333 | } | |
334 | ||
335 | static function buildXMLQuery($searchParams) { | |
336 | $xml = '<?xml version="1.0" encoding="UTF-8"?> | |
337 | <notification-history-request xmlns="http://checkout.google.com/schema/2">'; | |
338 | ||
339 | if (array_key_exists('next-page-token', $searchParams)) { | |
340 | $xml .= ' | |
341 | <next-page-token>' . $searchParams['next-page-token'] . '</next-page-token>'; | |
342 | } | |
343 | if (array_key_exists('start', $searchParams)) { | |
344 | $xml .= ' | |
345 | <start-time>' . $searchParams['start'] . '</start-time> | |
346 | <end-time>' . $searchParams['end'] . '</end-time>'; | |
347 | } | |
348 | if (array_key_exists('notification-types', $searchParams)) { | |
349 | $xml .= ' | |
350 | <notification-types> | |
351 | <notification-type>' . implode($searchParams['notification-types'], '</notification-type> | |
352 | <notification-type>') . '</notification-type> | |
353 | </notification-types>'; | |
354 | } | |
355 | if (array_key_exists('order-numbers', $searchParams)) { | |
356 | $xml .= ' | |
357 | <order-numbers> | |
358 | <google-order-number>' . implode($searchParams['order-numbers'], '</google-order-number> | |
359 | <google-order-number>') . '</google-order-number> | |
360 | </order-numbers>'; | |
361 | } | |
362 | $xml .= ' | |
363 | </notification-history-request>'; | |
364 | ||
365 | return $xml; | |
366 | } | |
367 | ||
368 | static function getArrayFromXML($xmlData) { | |
369 | require_once 'Google/library/xml-processing/gc_xmlparser.php'; | |
370 | $xmlParser = new gc_XmlParser($xmlData); | |
371 | $root = $xmlParser->GetRoot(); | |
372 | $data = $xmlParser->GetData(); | |
373 | ||
374 | return array($root, $data); | |
375 | } | |
376 | ||
377 | function &error($errorCode = NULL, $errorMessage = NULL) { | |
378 | $e = &CRM_Core_Error::singleton(); | |
379 | if ($errorCode) { | |
380 | $e->push($errorCode, 0, NULL, $errorMessage); | |
381 | } | |
382 | else { | |
383 | $e->push(9001, 0, NULL, 'Unknown System Error.'); | |
384 | } | |
385 | return $e; | |
386 | } | |
387 | ||
388 | function accountLoginURL() { | |
389 | return ($this->_mode == 'test') ? 'https://sandbox.google.com/checkout/sell' : 'https://checkout.google.com/'; | |
390 | } | |
391 | ||
392 | function cancelSubscription(&$message = '', $params = array( | |
393 | )) { | |
394 | $orderNo = CRM_Utils_Array::value('subscriptionId', $params); | |
395 | ||
396 | $merchant_id = $this->_paymentProcessor['user_name']; | |
397 | $merchant_key = $this->_paymentProcessor['password']; | |
398 | $server_type = ($this->_mode == 'test') ? 'sandbox' : ''; | |
399 | ||
400 | $googleRequest = new GoogleRequest($merchant_id, $merchant_key, $server_type); | |
401 | $result = $googleRequest->SendCancelItems($orderNo, array(), 'Cancelled by admin', ''); | |
402 | $message = "{$result[0]}: {$result[1]}"; | |
403 | ||
404 | if ($result[0] != 200) { | |
405 | return self::error($result[0], $result[1]); | |
406 | } | |
407 | return TRUE; | |
408 | } | |
409 | } | |
410 |