3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 require_once 'Google/library/googlecart.php';
35 require_once 'Google/library/googleitem.php';
36 require_once 'Google/library/googlesubscription.php';
37 require_once 'Google/library/googlerequest.php';
40 * Class CRM_Core_Payment_Google.
42 * Possibly not functional.
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 * Sets appropriate parameters for checking out to google.
107 * @param array $params
108 * Name value pair of contribution data.
110 * @param string $component
112 public function doTransferCheckout(&$params, $component) {
113 $component = strtolower($component);
115 if (!empty($params['is_recur']) &&
116 $params['contributionRecurID']
118 return $this->doRecurCheckout($params, $component);
121 //Create a new shopping cart object
123 $merchant_id = $this->_paymentProcessor
['user_name'];
125 $merchant_key = $this->_paymentProcessor
['password'];
126 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
128 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $params['currencyID']);
129 $item1 = new GoogleItem($params['item_name'], '', 1, $params['amount']);
130 $cart->AddItem($item1);
132 $this->submitPostParams($params, $component, $cart);
136 * @param array $params
139 public function doRecurCheckout(&$params, $component) {
140 $intervalUnit = CRM_Utils_Array
::value('frequency_unit', $params);
141 if ($intervalUnit == 'week') {
142 $intervalUnit = 'WEEKLY';
144 elseif ($intervalUnit == 'year') {
145 $intervalUnit = 'YEARLY';
147 elseif ($intervalUnit == 'day') {
148 $intervalUnit = 'DAILY';
150 elseif ($intervalUnit == 'month') {
151 $intervalUnit = 'MONTHLY';
155 $merchant_id = $this->_paymentProcessor
['user_name'];
157 $merchant_key = $this->_paymentProcessor
['password'];
158 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
160 $itemName = CRM_Utils_Array
::value('item_name', $params);
161 $description = CRM_Utils_Array
::value('description', $params);
162 $amount = CRM_Utils_Array
::value('amount', $params);
163 $installments = CRM_Utils_Array
::value('installments', $params);
165 $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $params['currencyID']);
166 $item = new GoogleItem($itemName, $description, 1, $amount);
167 $subscription_item = new GoogleSubscription("merchant", $intervalUnit, $amount, $installments);
169 $item->SetSubscription($subscription_item);
170 $cart->AddItem($item);
172 $this->submitPostParams($params, $component, $cart);
176 * Builds appropriate parameters for checking out to google and submits the post params.
178 * @param array $params
179 * Name value pair of contribution data.
180 * @param string $component
181 * Event/contribution.
182 * @param object $cart
183 * Object of google cart.
185 public function submitPostParams($params, $component, $cart) {
186 $url = rtrim($this->_paymentProcessor
['url_site'], '/') . '/cws/v2/Merchant/' . $this->_paymentProcessor
['user_name'] . '/checkout';
188 if ($component == "event") {
189 $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},eventID={$params['eventID']},participantID={$params['participantID']},invoiceID={$params['invoiceID']}";
191 elseif ($component == "contribute") {
192 $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},invoiceID={$params['invoiceID']}";
194 $contributionRecurID = CRM_Utils_Array
::value('contributionRecurID', $params);
195 if ($contributionRecurID) {
196 $privateData .= ",contributionRecurID=$contributionRecurID";
199 $membershipID = CRM_Utils_Array
::value('membershipID', $params);
201 $privateData .= ",membershipID=$membershipID";
204 $relatedContactID = CRM_Utils_Array
::value('related_contact', $params);
205 if ($relatedContactID) {
206 $privateData .= ",relatedContactID=$relatedContactID";
208 $onBehalfDupeAlert = CRM_Utils_Array
::value('onbehalf_dupe_alert', $params);
209 if ($onBehalfDupeAlert) {
210 $privateData .= ",onBehalfDupeAlert=$onBehalfDupeAlert";
215 // Allow further manipulation of the arguments via custom hooks ..
216 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $privateData);
218 $cart->SetMerchantPrivateData($privateData);
220 if ($component == "event") {
221 $returnURL = CRM_Utils_System
::url('civicrm/event/register',
222 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
226 elseif ($component == "contribute") {
227 $returnURL = CRM_Utils_System
::url('civicrm/contribute/transact',
228 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
232 $cart->SetContinueShoppingUrl($returnURL);
234 $cartVal = base64_encode($cart->GetXML());
235 $signatureVal = base64_encode($cart->CalcHmacSha1($cart->GetXML()));
237 $googleParams = array(
239 'signature' => $signatureVal,
242 require_once 'HTTP/Request.php';
244 'method' => HTTP_REQUEST_METHOD_POST
,
245 'allowRedirects' => FALSE,
247 $request = new HTTP_Request($url, $params);
248 foreach ($googleParams as $key => $value) {
249 $request->addPostData($key, $value);
252 $result = $request->sendRequest();
254 if (PEAR
::isError($result)) {
255 CRM_Core_Error
::fatal($result->getMessage());
258 if ($request->getResponseCode() != 302) {
259 CRM_Core_Error
::fatal(ts('Invalid response code received from Google Checkout: %1',
260 array(1 => $request->getResponseCode())
263 CRM_Utils_System
::redirect($request->getResponseHeader('location'));
264 CRM_Utils_System
::civiExit();
268 * Hash_call: Function to perform the API call to PayPal using API signature
270 * @paymentProcessor is the array of payment processor settings value.
271 * @searchParamsnvpStr is the array of search params.
272 * returns an associtive array containing the response from the server.
274 * @param $paymentProcessor
275 * @param $searchParams
277 * @return array|object
280 public function invokeAPI($paymentProcessor, $searchParams) {
281 $merchantID = $paymentProcessor['user_name'];
282 $merchantKey = $paymentProcessor['password'];
283 $siteURL = rtrim(str_replace('https://', '', $paymentProcessor['url_site']), '/');
285 $url = "https://{$merchantID}:{$merchantKey}@{$siteURL}/api/checkout/v2/reports/Merchant/{$merchantID}";
286 $xml = self
::buildXMLQuery($searchParams);
288 if (!function_exists('curl_init')) {
289 CRM_Core_Error
::fatal("curl functions NOT available.");
293 curl_setopt($ch, CURLOPT_URL
, $url);
294 curl_setopt($ch, CURLOPT_VERBOSE
, 1);
296 //turning off the server and peer verification(TrustManager Concept).
297 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, Civi
::settings()->get('verifySSL'));
298 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, Civi
::settings()->get('verifySSL') ?
2 : 0);
300 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
301 curl_setopt($ch, CURLOPT_POST
, 1);
303 //setting the nvpreq as POST FIELD to curl
304 curl_setopt($ch, CURLOPT_POSTFIELDS
, $xml);
306 //getting response from server
307 $xmlResponse = curl_exec($ch);
309 // strip slashes if needed
310 if (get_magic_quotes_gpc()) {
311 $xmlResponse = stripslashes($xmlResponse);
314 if (curl_errno($ch)) {
315 $e = CRM_Core_Error
::singleton();
316 $e->push(curl_errno($ch),
326 return self
::getArrayFromXML($xmlResponse);
330 * @param array $searchParams
334 public static function buildXMLQuery($searchParams) {
335 $xml = '<?xml version="1.0" encoding="UTF-8"?>
336 <notification-history-request xmlns="http://checkout.google.com/schema/2">';
338 if (array_key_exists('next-page-token', $searchParams)) {
340 <next-page-token>' . $searchParams['next-page-token'] . '</next-page-token>';
342 if (array_key_exists('start', $searchParams)) {
344 <start-time>' . $searchParams['start'] . '</start-time>
345 <end-time>' . $searchParams['end'] . '</end-time>';
347 if (array_key_exists('notification-types', $searchParams)) {
350 <notification-type>' . implode($searchParams['notification-types'], '</notification-type>
351 <notification-type>') . '</notification-type>
352 </notification-types>';
354 if (array_key_exists('order-numbers', $searchParams)) {
357 <google-order-number>' . implode($searchParams['order-numbers'], '</google-order-number>
358 <google-order-number>') . '</google-order-number>
362 </notification-history-request>';
372 public static function getArrayFromXML($xmlData) {
373 require_once 'Google/library/xml-processing/gc_xmlparser.php';
374 $xmlParser = new gc_XmlParser($xmlData);
375 $root = $xmlParser->GetRoot();
376 $data = $xmlParser->GetData();
378 return array($root, $data);
382 * @param null $errorCode
383 * @param null $errorMessage
387 public function &error($errorCode = NULL, $errorMessage = NULL) {
388 $e = &CRM_Core_Error
::singleton();
390 $e->push($errorCode, 0, NULL, $errorMessage);
393 $e->push(9001, 0, NULL, 'Unknown System Error.');
401 public function accountLoginURL() {
402 return ($this->_mode
== 'test') ?
'https://sandbox.google.com/checkout/sell' : 'https://checkout.google.com/';
406 * @param string $message
407 * @param array $params
409 * @return bool|object
411 public function cancelSubscription(&$message = '', $params = array()) {
412 $orderNo = CRM_Utils_Array
::value('subscriptionId', $params);
414 $merchant_id = $this->_paymentProcessor
['user_name'];
415 $merchant_key = $this->_paymentProcessor
['password'];
416 $server_type = ($this->_mode
== 'test') ?
'sandbox' : '';
418 $googleRequest = new GoogleRequest($merchant_id, $merchant_key, $server_type);
419 $result = $googleRequest->SendCancelItems($orderNo, array(), 'Cancelled by admin', '');
420 $message = "{$result[0]}: {$result[1]}";
422 if ($result[0] != 200) {
423 return self
::error($result[0], $result[1]);