3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | This file is a part of CiviCRM. |
8 | CiviCRM is free software; you can copy, modify, and distribute it |
9 | under the terms of the GNU Affero General Public License |
10 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
12 | CiviCRM is distributed in the hope that it will be useful, but |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15 | See the GNU Affero General Public License for more details. |
17 | You should have received a copy of the GNU Affero General Public |
18 | License and the CiviCRM Licensing Exception along |
19 | with this program; if not, contact CiviCRM LLC |
20 | at info[AT]civicrm[DOT]org. If you have questions about the |
21 | GNU Affero General Public License or the licensing of CiviCRM, |
22 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
23 +--------------------------------------------------------------------+
28 * PxPay Functionality Copyright (C) 2008 Lucas Baker, Logistic Information Systems Limited (Logis)
29 * PxAccess Functionality Copyright (C) 2008 Eileen McNaughton
30 * Licensed to CiviCRM under the Academic Free License version 3.0.
32 * Grateful acknowledgements go to Donald Lobo for invaluable assistance
33 * in creating this payment processor module
35 class CRM_Core_Payment_PaymentExpress
extends CRM_Core_Payment
{
36 CONST CHARSET
= 'iso-8859-1';
38 protected $_mode = NULL;
41 * We only need one instance of this object. So we use the singleton
42 * pattern and cache the instance in this variable
47 static private $_singleton = NULL;
52 * @param string $mode the mode of operation: live or test
54 * @param $paymentProcessor
56 * @return \CRM_Core_Payment_PaymentExpress
58 function __construct($mode, &$paymentProcessor) {
61 $this->_paymentProcessor
= $paymentProcessor;
62 $this->_processorName
= ts('DPS Payment Express');
66 * singleton function used to manage this object
68 * @param string $mode the mode of operation: live or test
70 * @param object $paymentProcessor
71 * @param null $paymentForm
77 static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
78 $processorName = $paymentProcessor['name'];
79 if (self
::$_singleton[$processorName] === NULL) {
80 self
::$_singleton[$processorName] = new CRM_Core_Payment_PaymentExpress($mode, $paymentProcessor);
82 return self
::$_singleton[$processorName];
85 function checkConfig() {
86 $config = CRM_Core_Config
::singleton();
90 if (empty($this->_paymentProcessor
['user_name'])) {
91 $error[] = ts('UserID is not set in the Administer » System Settings » Payment Processors');
94 if (empty($this->_paymentProcessor
['password'])) {
95 $error[] = ts('pxAccess / pxPay Key is not set in the Administer » System Settings » Payment Processors');
99 return implode('<p>', $error);
106 function setExpressCheckOut(&$params) {
107 CRM_Core_Error
::fatal(ts('This function is not implemented'));
110 function getExpressCheckoutDetails($token) {
111 CRM_Core_Error
::fatal(ts('This function is not implemented'));
114 function doExpressCheckout(&$params) {
115 CRM_Core_Error
::fatal(ts('This function is not implemented'));
118 function doDirectPayment(&$params) {
119 CRM_Core_Error
::fatal(ts('This function is not implemented'));
123 * Main transaction function
125 * @param array $params name value pair of contribution data
132 function doTransferCheckout(&$params, $component) {
133 $component = strtolower($component);
134 $config = CRM_Core_Config
::singleton();
135 if ($component != 'contribute' && $component != 'event') {
136 CRM_Core_Error
::fatal(ts('Component is invalid'));
139 $url = $config->userFrameworkResourceURL
. "extern/pxIPN.php";
141 if ($component == 'event') {
142 $cancelURL = CRM_Utils_System
::url('civicrm/event/register',
143 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
147 elseif ($component == 'contribute') {
148 $cancelURL = CRM_Utils_System
::url('civicrm/contribute/transact',
149 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
156 * Build the private data string to pass to DPS, which they will give back to us with the
158 * transaction result. We are building this as a comma-separated list so as to avoid long URLs.
160 * Parameters passed: a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID
163 $privateData = "a={$params['contactID']},b={$params['contributionID']},c={$params['contributionTypeID']},d={$params['invoiceID']}";
165 if ($component == 'event') {
166 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 27, 20), 0, 24);
167 $privateData .= ",f={$params['participantID']},g={$params['eventID']}";
169 elseif ($component == 'contribute') {
170 $membershipID = CRM_Utils_Array
::value('membershipID', $params);
172 $privateData .= ",e=$membershipID";
174 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 20, 20), 0, 24);
179 'AmountInput' => str_replace(",", "", number_format($params['amount'], 2)),
180 'CurrencyInput' => $params['currencyID'],
181 'MerchantReference' => $merchantRef,
182 'TxnData1' => $params['qfKey'],
183 'TxnData2' => $privateData,
184 'TxnData3' => $component . ",".$this->_paymentProcessor
['id'],
185 'TxnType' => 'Purchase',
186 // Leave this empty for now, causes an error with DPS if we populate it
189 'UrlSuccess' => $url,
191 // Allow further manipulation of params via custom hooks
192 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $dpsParams);
195 * determine whether method is pxaccess or pxpay by whether signature (mac key) is defined
199 if (empty($this->_paymentProcessor
['signature'])) {
203 * This contains the XML/Curl functions we'll need to generate the XML request
206 $dpsParams['PxPayUserId'] = $this->_paymentProcessor
['user_name'];
207 $dpsParams['PxPayKey'] = $this->_paymentProcessor
['password'];
208 // Build a valid XML string to pass to DPS
209 $generateRequest = CRM_Core_Payment_PaymentExpressUtils
::_valueXml($dpsParams);
211 $generateRequest = CRM_Core_Payment_PaymentExpressUtils
::_valueXml('GenerateRequest', $generateRequest);
212 // Get the special validated URL back from DPS by sending them the XML we've generated
213 $curl = CRM_Core_Payment_PaymentExpressUtils
::_initCURL($generateRequest, $this->_paymentProcessor
['url_site']);
216 if ($response = curl_exec($curl)) {
218 $valid = CRM_Core_Payment_PaymentExpressUtils
::_xmlAttribute($response, 'valid');
220 // the request was validated, so we'll get the URL and redirect to it
221 $uri = CRM_Core_Payment_PaymentExpressUtils
::_xmlElement($response, 'URI');
222 CRM_Utils_System
::redirect($uri);
225 // redisplay confirmation page
226 CRM_Utils_System
::redirect($cancelURL);
230 // calling DPS failed
231 CRM_Core_Error
::fatal(ts('Unable to establish connection to the payment gateway.'));
235 $processortype = "pxaccess";
236 require_once ('PaymentExpress/pxaccess.inc.php');
238 $PxAccess_Url = $this->_paymentProcessor
['url_site'];
240 $PxAccess_Userid = $this->_paymentProcessor
['user_name'];
241 // Your DES Key from DPS
242 $PxAccess_Key = $this->_paymentProcessor
['password'];
243 // Your MAC key from DPS
244 $Mac_Key = $this->_paymentProcessor
['signature'];
246 $pxaccess = new PxAccess($PxAccess_Url, $PxAccess_Userid, $PxAccess_Key, $Mac_Key);
247 $request = new PxPayRequest();
248 $request->setAmountInput($dpsParams['AmountInput']);
249 $request->setTxnData1($dpsParams['TxnData1']);
250 $request->setTxnData2($dpsParams['TxnData2']);
251 $request->setTxnData3($dpsParams['TxnData3']);
252 $request->setTxnType($dpsParams['TxnType']);
253 $request->setInputCurrency($dpsParams['InputCurrency']);
254 $request->setMerchantReference($dpsParams['MerchantReference']);
255 $request->setUrlFail($dpsParams['UrlFail']);
256 $request->setUrlSuccess($dpsParams['UrlSuccess']);
257 $request_string = $pxaccess->makeRequest($request);
258 CRM_Utils_System
::redirect($request_string);