3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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';
42 * Class CRM_Core_Payment_Google
44 class CRM_Core_Payment_Google
extends CRM_Core_Payment
{
47 * Mode of operation: live or test
51 protected $_mode = NULL;
54 * We only need one instance of this object. So we use the singleton
55 * pattern and cache the instance in this variable
59 static private $_singleton = NULL;
65 * The mode of operation: live or test.
67 * @param $paymentProcessor
69 * @return \CRM_Core_Payment_Google
71 public function __construct($mode, &$paymentProcessor) {
73 $this->_paymentProcessor
= $paymentProcessor;
74 $this->_processorName
= ts('Google Checkout');
78 * This function checks to see if we have the right config values
81 * the error message if any
83 public function checkConfig() {
84 $config = CRM_Core_Config
::singleton();
88 if (empty($this->_paymentProcessor
['user_name'])) {
89 $error[] = ts('User Name is not set in the Administer CiviCRM » System Settings » Payment Processors.');
92 if (empty($this->_paymentProcessor
['password'])) {
93 $error[] = ts('Password is not set in the Administer CiviCRM » System Settings » Payment Processors.');
97 return implode('<p>', $error);
105 * This function collects all the information from a web/api form and invokes
106 * the relevant payment processor specific functions to perform the transaction
108 * @param array $params
109 * Assoc array of input parameters for this transaction.
112 * the result in an nice formatted array (or an error object)
115 public 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
123 * Name value pair of contribution datat.
129 public function doTransferCheckout(&$params, $component) {
130 $component = strtolower($component);
132 if (!empty($params['is_recur']) &&
133 $params['contributionRecurID']
135 return $this->doRecurCheckout($params, $component);
138 //Create a new shopping cart object
140 $merchant_id = $this->_paymentProcessor
['user_name'];
142 $merchant_key = $this->_paymentProcessor
['password'];
143 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
145 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $params['currencyID']);
146 $item1 = new GoogleItem($params['item_name'], '', 1, $params['amount']);
147 $cart->AddItem($item1);
149 $this->submitPostParams($params, $component, $cart);
153 * @param array $params
156 public function doRecurCheckout(&$params, $component) {
157 $intervalUnit = CRM_Utils_Array
::value('frequency_unit', $params);
158 if ($intervalUnit == 'week') {
159 $intervalUnit = 'WEEKLY';
161 elseif ($intervalUnit == 'year') {
162 $intervalUnit = 'YEARLY';
164 elseif ($intervalUnit == 'day') {
165 $intervalUnit = 'DAILY';
167 elseif ($intervalUnit == 'month') {
168 $intervalUnit = 'MONTHLY';
172 $merchant_id = $this->_paymentProcessor
['user_name'];
174 $merchant_key = $this->_paymentProcessor
['password'];
175 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
177 $itemName = CRM_Utils_Array
::value('item_name', $params);
178 $description = CRM_Utils_Array
::value('description', $params);
179 $amount = CRM_Utils_Array
::value('amount', $params);
180 $installments = CRM_Utils_Array
::value('installments', $params);
182 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $params['currencyID']);
183 $item = new GoogleItem($itemName, $description, 1, $amount);
184 $subscription_item = new GoogleSubscription("merchant", $intervalUnit, $amount, $installments);
186 $item->SetSubscription($subscription_item);
187 $cart->AddItem($item);
189 $this->submitPostParams($params, $component, $cart);
193 * Builds appropriate parameters for checking out to google and submits the post params
195 * @param array $params
196 * Name value pair of contribution data.
197 * @param string $component
198 * Event/contribution.
199 * @param object $cart
200 * Object of googel cart.
204 public function submitPostParams($params, $component, $cart) {
205 $url = rtrim($this->_paymentProcessor
['url_site'], '/') . '/cws/v2/Merchant/' . $this->_paymentProcessor
['user_name'] . '/checkout';
207 if ($component == "event") {
208 $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},eventID={$params['eventID']},participantID={$params['participantID']},invoiceID={$params['invoiceID']}";
210 elseif ($component == "contribute") {
211 $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},invoiceID={$params['invoiceID']}";
213 $contributionRecurID = CRM_Utils_Array
::value('contributionRecurID', $params);
214 if ($contributionRecurID) {
215 $privateData .= ",contributionRecurID=$contributionRecurID";
218 $membershipID = CRM_Utils_Array
::value('membershipID', $params);
220 $privateData .= ",membershipID=$membershipID";
223 $relatedContactID = CRM_Utils_Array
::value('related_contact', $params);
224 if ($relatedContactID) {
225 $privateData .= ",relatedContactID=$relatedContactID";
227 $onBehalfDupeAlert = CRM_Utils_Array
::value('onbehalf_dupe_alert', $params);
228 if ($onBehalfDupeAlert) {
229 $privateData .= ",onBehalfDupeAlert=$onBehalfDupeAlert";
234 // Allow further manipulation of the arguments via custom hooks ..
235 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $privateData);
237 $cart->SetMerchantPrivateData($privateData);
239 if ($component == "event") {
240 $returnURL = CRM_Utils_System
::url('civicrm/event/register',
241 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
245 elseif ($component == "contribute") {
246 $returnURL = CRM_Utils_System
::url('civicrm/contribute/transact',
247 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
251 $cart->SetContinueShoppingUrl($returnURL);
253 $cartVal = base64_encode($cart->GetXML());
254 $signatureVal = base64_encode($cart->CalcHmacSha1($cart->GetXML()));
256 $googleParams = array(
258 'signature' => $signatureVal,
261 require_once 'HTTP/Request.php';
263 'method' => HTTP_REQUEST_METHOD_POST
,
264 'allowRedirects' => FALSE,
266 $request = new HTTP_Request($url, $params);
267 foreach ($googleParams as $key => $value) {
268 $request->addPostData($key, $value);
271 $result = $request->sendRequest();
273 if (PEAR
::isError($result)) {
274 CRM_Core_Error
::fatal($result->getMessage());
277 if ($request->getResponseCode() != 302) {
278 CRM_Core_Error
::fatal(ts('Invalid response code received from Google Checkout: %1',
279 array(1 => $request->getResponseCode())
282 CRM_Utils_System
::redirect($request->getResponseHeader('location'));
283 CRM_Utils_System
::civiExit();
287 * Hash_call: Function to perform the API call to PayPal using API signature
288 * @paymentProcessor is the array of payment processor settings value.
289 * @searchParamsnvpStr is the array of search params.
290 * returns an associtive array containing the response from the server.
292 public function invokeAPI($paymentProcessor, $searchParams) {
293 $merchantID = $paymentProcessor['user_name'];
294 $merchantKey = $paymentProcessor['password'];
295 $siteURL = rtrim(str_replace('https://', '', $paymentProcessor['url_site']), '/');
297 $url = "https://{$merchantID}:{$merchantKey}@{$siteURL}/api/checkout/v2/reports/Merchant/{$merchantID}";
298 $xml = self
::buildXMLQuery($searchParams);
300 if (!function_exists('curl_init')) {
301 CRM_Core_Error
::fatal("curl functions NOT available.");
305 curl_setopt($ch, CURLOPT_URL
, $url);
306 curl_setopt($ch, CURLOPT_VERBOSE
, 1);
308 //turning off the server and peer verification(TrustManager Concept).
309 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
310 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL') ?
2 : 0);
312 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
313 curl_setopt($ch, CURLOPT_POST
, 1);
315 //setting the nvpreq as POST FIELD to curl
316 curl_setopt($ch, CURLOPT_POSTFIELDS
, $xml);
318 //getting response from server
319 $xmlResponse = curl_exec($ch);
321 // strip slashes if needed
322 if (get_magic_quotes_gpc()) {
323 $xmlResponse = stripslashes($xmlResponse);
326 if (curl_errno($ch)) {
327 $e = CRM_Core_Error
::singleton();
328 $e->push(curl_errno($ch),
338 return self
::getArrayFromXML($xmlResponse);
342 * @param array $searchParams
346 public static function buildXMLQuery($searchParams) {
347 $xml = '<?xml version="1.0" encoding="UTF-8"?>
348 <notification-history-request xmlns="http://checkout.google.com/schema/2">';
350 if (array_key_exists('next-page-token', $searchParams)) {
352 <next-page-token>' . $searchParams['next-page-token'] . '</next-page-token>';
354 if (array_key_exists('start', $searchParams)) {
356 <start-time>' . $searchParams['start'] . '</start-time>
357 <end-time>' . $searchParams['end'] . '</end-time>';
359 if (array_key_exists('notification-types', $searchParams)) {
362 <notification-type>' . implode($searchParams['notification-types'], '</notification-type>
363 <notification-type>') . '</notification-type>
364 </notification-types>';
366 if (array_key_exists('order-numbers', $searchParams)) {
369 <google-order-number>' . implode($searchParams['order-numbers'], '</google-order-number>
370 <google-order-number>') . '</google-order-number>
374 </notification-history-request>';
384 public static function getArrayFromXML($xmlData) {
385 require_once 'Google/library/xml-processing/gc_xmlparser.php';
386 $xmlParser = new gc_XmlParser($xmlData);
387 $root = $xmlParser->GetRoot();
388 $data = $xmlParser->GetData();
390 return array($root, $data);
394 * @param null $errorCode
395 * @param null $errorMessage
399 public function &error($errorCode = NULL, $errorMessage = NULL) {
400 $e = &CRM_Core_Error
::singleton();
402 $e->push($errorCode, 0, NULL, $errorMessage);
405 $e->push(9001, 0, NULL, 'Unknown System Error.');
413 public function accountLoginURL() {
414 return ($this->_mode
== 'test') ?
'https://sandbox.google.com/checkout/sell' : 'https://checkout.google.com/';
418 * @param string $message
419 * @param array $params
421 * @return bool|object
423 public function cancelSubscription(&$message = '', $params = array()) {
424 $orderNo = CRM_Utils_Array
::value('subscriptionId', $params);
426 $merchant_id = $this->_paymentProcessor
['user_name'];
427 $merchant_key = $this->_paymentProcessor
['password'];
428 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
430 $googleRequest = new GoogleRequest($merchant_id, $merchant_key, $server_type);
431 $result = $googleRequest->SendCancelItems($orderNo, array(), 'Cancelled by admin', '');
432 $message = "{$result[0]}: {$result[1]}";
434 if ($result[0] != 200) {
435 return self
::error($result[0], $result[1]);