3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
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
{
43 * mode of operation: live or test
47 protected $_mode = NULL;
50 * We only need one instance of this object. So we use the singleton
51 * pattern and cache the instance in this variable
56 static private $_singleton = NULL;
61 * @param string $mode the mode of operation: live or test
65 function __construct($mode, &$paymentProcessor) {
67 $this->_paymentProcessor
= $paymentProcessor;
68 $this->_processorName
= ts('Google Checkout');
72 * singleton function used to manage this object
74 * @param string $mode the mode of operation: live or test
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);
85 return self
::$_singleton[$processorName];
89 * This function checks to see if we have the right config values
91 * @return string the error message if any
94 function checkConfig() {
95 $config = CRM_Core_Config
::singleton();
99 if (empty($this->_paymentProcessor
['user_name'])) {
100 $error[] = ts('User Name is not set in the Administer CiviCRM » System Settings » Payment Processors.');
103 if (empty($this->_paymentProcessor
['password'])) {
104 $error[] = ts('Password is not set in the Administer CiviCRM » System Settings » Payment Processors.');
107 if (!empty($error)) {
108 return implode('<p>', $error);
115 function doDirectPayment(&$params) {
116 CRM_Core_Error
::fatal(ts('This function is not implemented'));
120 * Sets appropriate parameters for checking out to google
122 * @param array $params name value pair of contribution datat
128 function doTransferCheckout(&$params, $component) {
129 $component = strtolower($component);
131 if (CRM_Utils_Array
::value('is_recur', $params) &&
132 $params['contributionRecurID']
134 return $this->doRecurCheckout($params, $component);
137 //Create a new shopping cart object
139 $merchant_id = $this->_paymentProcessor
['user_name'];
141 $merchant_key = $this->_paymentProcessor
['password'];
142 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
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);
148 $this->submitPostParams($params, $component, $cart);
151 function doRecurCheckout(&$params, $component) {
152 $intervalUnit = CRM_Utils_Array
::value('frequency_unit', $params);
153 if ($intervalUnit == 'week') {
154 $intervalUnit = 'WEEKLY';
156 elseif ($intervalUnit == 'year') {
157 $intervalUnit = 'YEARLY';
159 elseif ($intervalUnit == 'day') {
160 $intervalUnit = 'DAILY';
162 elseif ($intervalUnit == 'month') {
163 $intervalUnit = 'MONTHLY';
167 $merchant_id = $this->_paymentProcessor
['user_name'];
169 $merchant_key = $this->_paymentProcessor
['password'];
170 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
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);
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);
181 $item->SetSubscription($subscription_item);
182 $cart->AddItem($item);
184 $this->submitPostParams($params, $component, $cart);
188 * Builds appropriate parameters for checking out to google and submits the post params
190 * @param array $params name value pair of contribution data
191 * @param string $component event/contribution
192 * @param object $cart object of googel cart
198 function submitPostParams($params, $component, $cart) {
199 $url = rtrim($this->_paymentProcessor
['url_site'], '/') . '/cws/v2/Merchant/' . $this->_paymentProcessor
['user_name'] . '/checkout';
201 if ($component == "event") {
202 $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},eventID={$params['eventID']},participantID={$params['participantID']},invoiceID={$params['invoiceID']}";
204 elseif ($component == "contribute") {
205 $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},invoiceID={$params['invoiceID']}";
207 $contributionRecurID = CRM_Utils_Array
::value('contributionRecurID', $params);
208 if ($contributionRecurID) {
209 $privateData .= ",contributionRecurID=$contributionRecurID";
212 $membershipID = CRM_Utils_Array
::value('membershipID', $params);
214 $privateData .= ",membershipID=$membershipID";
217 $relatedContactID = CRM_Utils_Array
::value('related_contact', $params);
218 if ($relatedContactID) {
219 $privateData .= ",relatedContactID=$relatedContactID";
221 $onBehalfDupeAlert = CRM_Utils_Array
::value('onbehalf_dupe_alert', $params);
222 if ($onBehalfDupeAlert) {
223 $privateData .= ",onBehalfDupeAlert=$onBehalfDupeAlert";
228 // Allow further manipulation of the arguments via custom hooks ..
229 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $privateData);
231 $cart->SetMerchantPrivateData($privateData);
233 if ($component == "event") {
234 $returnURL = CRM_Utils_System
::url('civicrm/event/register',
235 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
239 elseif ($component == "contribute") {
240 $returnURL = CRM_Utils_System
::url('civicrm/contribute/transact',
241 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
245 $cart->SetContinueShoppingUrl($returnURL);
247 $cartVal = base64_encode($cart->GetXML());
248 $signatureVal = base64_encode($cart->CalcHmacSha1($cart->GetXML()));
250 $googleParams = array(
252 'signature' => $signatureVal,
255 require_once 'HTTP/Request.php';
257 'method' => HTTP_REQUEST_METHOD_POST
,
258 'allowRedirects' => FALSE,
260 $request = new HTTP_Request($url, $params);
261 foreach ($googleParams as $key => $value) {
262 $request->addPostData($key, $value);
265 $result = $request->sendRequest();
267 if (PEAR
::isError($result)) {
268 CRM_Core_Error
::fatal($result->getMessage());
271 if ($request->getResponseCode() != 302) {
272 CRM_Core_Error
::fatal(ts('Invalid response code received from Google Checkout: %1',
273 array(1 => $request->getResponseCode())
276 CRM_Utils_System
::redirect($request->getResponseHeader('location'));
277 CRM_Utils_System
::civiExit();
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.
286 function invokeAPI($paymentProcessor, $searchParams) {
287 $merchantID = $paymentProcessor['user_name'];
288 $merchantKey = $paymentProcessor['password'];
289 $siteURL = rtrim(str_replace('https://', '', $paymentProcessor['url_site']), '/');
291 $url = "https://{$merchantID}:{$merchantKey}@{$siteURL}/api/checkout/v2/reports/Merchant/{$merchantID}";
292 $xml = self
::buildXMLQuery($searchParams);
294 if (!function_exists('curl_init')) {
295 CRM_Core_Error
::fatal("curl functions NOT available.");
299 curl_setopt($ch, CURLOPT_URL
, $url);
300 curl_setopt($ch, CURLOPT_VERBOSE
, 1);
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'));
304 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL') ?
2 : 0);
306 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
307 curl_setopt($ch, CURLOPT_POST
, 1);
309 //setting the nvpreq as POST FIELD to curl
310 curl_setopt($ch, CURLOPT_POSTFIELDS
, $xml);
312 //getting response from server
313 $xmlResponse = curl_exec($ch);
315 // strip slashes if needed
316 if (get_magic_quotes_gpc()) {
317 $xmlResponse = stripslashes($xmlResponse);
320 if (curl_errno($ch)) {
321 $e = CRM_Core_Error
::singleton();
322 $e->push(curl_errno($ch),
332 return self
::getArrayFromXML($xmlResponse);
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">';
339 if (array_key_exists('next-page-token', $searchParams)) {
341 <next-page-token>' . $searchParams['next-page-token'] . '</next-page-token>';
343 if (array_key_exists('start', $searchParams)) {
345 <start-time>' . $searchParams['start'] . '</start-time>
346 <end-time>' . $searchParams['end'] . '</end-time>';
348 if (array_key_exists('notification-types', $searchParams)) {
351 <notification-type>' . implode($searchParams['notification-types'], '</notification-type>
352 <notification-type>') . '</notification-type>
353 </notification-types>';
355 if (array_key_exists('order-numbers', $searchParams)) {
358 <google-order-number>' . implode($searchParams['order-numbers'], '</google-order-number>
359 <google-order-number>') . '</google-order-number>
363 </notification-history-request>';
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();
374 return array($root, $data);
377 function &error($errorCode = NULL, $errorMessage = NULL) {
378 $e = &CRM_Core_Error
::singleton();
380 $e->push($errorCode, 0, NULL, $errorMessage);
383 $e->push(9001, 0, NULL, 'Unknown System Error.');
388 function accountLoginURL() {
389 return ($this->_mode
== 'test') ?
'https://sandbox.google.com/checkout/sell' : 'https://checkout.google.com/';
392 function cancelSubscription(&$message = '', $params = array(
394 $orderNo = CRM_Utils_Array
::value('subscriptionId', $params);
396 $merchant_id = $this->_paymentProcessor
['user_name'];
397 $merchant_key = $this->_paymentProcessor
['password'];
398 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
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]}";
404 if ($result[0] != 200) {
405 return self
::error($result[0], $result[1]);