PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Core/Payment/Google.php

https://github.com/michaelmcandrew/citylink
PHP | 274 lines | 159 code | 45 blank | 70 comment | 25 complexity | 5c299f184c46e1f1d545316c1ba29cbf MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 2.2 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. require_once 'CRM/Core/Payment.php';
  34. class CRM_Core_Payment_Google extends CRM_Core_Payment {
  35. /**
  36. * mode of operation: live or test
  37. *
  38. * @var object
  39. * @static
  40. */
  41. static protected $_mode = null;
  42. /**
  43. * Constructor
  44. *
  45. * @param string $mode the mode of operation: live or test
  46. *
  47. * @return void
  48. */
  49. function __construct( $mode, &$paymentProcessor ) {
  50. $this->_mode = $mode;
  51. $this->_paymentProcessor = $paymentProcessor;
  52. }
  53. /**
  54. * This function checks to see if we have the right config values
  55. *
  56. * @return string the error message if any
  57. * @public
  58. */
  59. function checkConfig( ) {
  60. $config =& CRM_Core_Config::singleton( );
  61. $error = array( );
  62. if ( empty( $this->_paymentProcessor['user_name'] ) ) {
  63. $error[] = ts( 'User Name is not set in the Administer CiviCRM &raquo; Payment Processor.' );
  64. }
  65. if ( empty( $this->_paymentProcessor['password'] ) ) {
  66. $error[] = ts( 'Password is not set in the Administer CiviCRM &raquo; Payment Processor.' );
  67. }
  68. if ( ! empty( $error ) ) {
  69. return implode( '<p>', $error );
  70. } else {
  71. return null;
  72. }
  73. }
  74. function doDirectPayment( &$params ) {
  75. CRM_Core_Error::fatal( ts( 'This function is not implemented' ) );
  76. }
  77. /**
  78. * Sets appropriate parameters for checking out to google
  79. *
  80. * @param array $params name value pair of contribution datat
  81. *
  82. * @return void
  83. * @access public
  84. *
  85. */
  86. function doTransferCheckout( &$params, $component ) {
  87. $component = strtolower( $component );
  88. $url =
  89. $this->_paymentProcessor['url_site'] .
  90. 'cws/v2/Merchant/' .
  91. $this->_paymentProcessor['user_name'] .
  92. '/checkout';
  93. //Create a new shopping cart object
  94. $merchant_id = $this->_paymentProcessor['user_name']; // Merchant ID
  95. $merchant_key = $this->_paymentProcessor['password']; // Merchant Key
  96. $server_type = ( $this->_mode == 'test' ) ? 'sandbox' : '';
  97. $cart = new GoogleCart($merchant_id, $merchant_key, $server_type);
  98. $item1 = new GoogleItem($params['item_name'],'', 1, $params['amount'], $params['currencyID']);
  99. $cart->AddItem($item1);
  100. if ( $component == "event" ) {
  101. $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},eventID={$params['eventID']},participantID={$params['participantID']},invoiceID={$params['invoiceID']}";
  102. } elseif ( $component == "contribute" ) {
  103. $privateData = "contactID={$params['contactID']},contributionID={$params['contributionID']},contributionTypeID={$params['contributionTypeID']},invoiceID={$params['invoiceID']}";
  104. $membershipID = CRM_Utils_Array::value( 'membershipID', $params );
  105. if ( $membershipID ) {
  106. $privateData .= ",membershipID=$membershipID";
  107. }
  108. $relatedContactID = CRM_Utils_Array::value( 'related_contact', $params );
  109. if ( $relatedContactID ) {
  110. $privateData .= ",relatedContactID=$relatedContactID";
  111. $onBehalfDupeAlert = CRM_Utils_Array::value( 'onbehalf_dupe_alert', $params );
  112. if ( $onBehalfDupeAlert ) {
  113. $privateData .= ",onBehalfDupeAlert=$onBehalfDupeAlert";
  114. }
  115. }
  116. }
  117. $cart->SetMerchantPrivateData($privateData);
  118. if ( $component == "event" ) {
  119. $returnURL = CRM_Utils_System::url( 'civicrm/event/register',
  120. "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
  121. true, null, false );
  122. } elseif ( $component == "contribute" ) {
  123. $returnURL = CRM_Utils_System::url( 'civicrm/contribute/transact',
  124. "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
  125. true, null, false );
  126. }
  127. $cart->SetContinueShoppingUrl( $returnURL );
  128. $cartVal = base64_encode($cart->GetXML());
  129. $signatureVal = base64_encode($cart->CalcHmacSha1($cart->GetXML()));
  130. $googleParams = array('cart' => $cartVal,
  131. 'signature' => $signatureVal );
  132. require_once 'HTTP/Request.php';
  133. $params = array( 'method' => HTTP_REQUEST_METHOD_POST,
  134. 'allowRedirects' => false );
  135. $request =& new HTTP_Request( $url, $params );
  136. foreach ( $googleParams as $key => $value ) {
  137. $request->addPostData($key, $value);
  138. }
  139. $result = $request->sendRequest( );
  140. if ( PEAR::isError( $result ) ) {
  141. CRM_Core_Error::fatal( $result->getMessage( ) );
  142. }
  143. if ( $request->getResponseCode( ) != 302 ) {
  144. CRM_Core_Error::fatal( ts( 'Invalid response code received from Google Checkout: %1',
  145. array(1 => $request->getResponseCode())) );
  146. }
  147. CRM_Utils_System::redirect( $request->getResponseHeader( 'location' ) );
  148. exit( );
  149. }
  150. /**
  151. * hash_call: Function to perform the API call to PayPal using API signature
  152. * @paymentProcessor is the array of payment processor settings value.
  153. * @searchParamsnvpStr is the array of search params.
  154. * returns an associtive array containing the response from the server.
  155. */
  156. function invokeAPI( $paymentProcessor, $searchParams ) {
  157. $merchantID = $paymentProcessor['user_name'];
  158. $merchantKey = $paymentProcessor['password'];
  159. $siteURL = rtrim(str_replace('https://', '', $paymentProcessor['url_site']), '/');
  160. $url = "https://{$merchantID}:{$merchantKey}@{$siteURL}/api/checkout/v2/reports/Merchant/{$merchantID}";
  161. $xml = self::buildXMLQuery( $searchParams );
  162. if ( !function_exists('curl_init') ) {
  163. CRM_Core_Error::fatal("curl functions NOT available.");
  164. }
  165. $ch = curl_init();
  166. curl_setopt($ch, CURLOPT_URL, $url );
  167. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  168. //turning off the server and peer verification(TrustManager Concept).
  169. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  170. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  171. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  172. curl_setopt($ch, CURLOPT_POST, 1);
  173. //setting the nvpreq as POST FIELD to curl
  174. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  175. //getting response from server
  176. $xmlResponse = curl_exec( $ch );
  177. // strip slashes if needed
  178. if (get_magic_quotes_gpc()) {
  179. $xmlResponse = stripslashes($xmlResponse);
  180. }
  181. if ( curl_errno( $ch ) ) {
  182. $e =& CRM_Core_Error::singleton( );
  183. $e->push( curl_errno( $ch ),
  184. 0, null,
  185. curl_error( $ch ) );
  186. return $e;
  187. } else {
  188. curl_close($ch);
  189. }
  190. return self::getArrayFromXML( $xmlResponse );
  191. }
  192. static function buildXMLQuery( $searchParams ) {
  193. $xml = '<?xml version="1.0" encoding="UTF-8"?>
  194. <notification-history-request xmlns="http://checkout.google.com/schema/2">';
  195. if ( array_key_exists('next-page-token', $searchParams) ) {
  196. $xml .= '
  197. <next-page-token>' . $searchParams['next-page-token'] . '</next-page-token>';
  198. }
  199. if ( array_key_exists('start', $searchParams) ) {
  200. $xml .= '
  201. <start-time>' . $searchParams['start'] . '</start-time>
  202. <end-time>' . $searchParams['end'] . '</end-time>';
  203. }
  204. if ( array_key_exists('notification-types', $searchParams) ) {
  205. $xml .= '
  206. <notification-types>
  207. <notification-type>' . implode($searchParams['notification-types'], '</notification-type>
  208. <notification-type>') . '</notification-type>
  209. </notification-types>';
  210. }
  211. if ( array_key_exists('order-numbers', $searchParams) ) {
  212. $xml .= '
  213. <order-numbers>
  214. <google-order-number>' . implode($searchParams['order-numbers'], '</google-order-number>
  215. <google-order-number>') . '</google-order-number>
  216. </order-numbers>';
  217. }
  218. $xml .= '
  219. </notification-history-request>';
  220. return $xml;
  221. }
  222. static function getArrayFromXML( $xmlData ) {
  223. require_once 'Google/library/xml-processing/xmlparser.php';
  224. $xmlParser = new XmlParser($xmlData);
  225. $root = $xmlParser->GetRoot();
  226. $data = $xmlParser->GetData();
  227. return array( $root, $data );
  228. }
  229. }