[REF] Move handling of form elements back to the Form
[civicrm-core.git] / CRM / Core / Payment / PaymentExpress.php
CommitLineData
6a488035
TO
1<?php
2/*
f452d72c
CW
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035
TO
11
12
13/*
14 * PxPay Functionality Copyright (C) 2008 Lucas Baker, Logistic Information Systems Limited (Logis)
15 * PxAccess Functionality Copyright (C) 2008 Eileen McNaughton
16 * Licensed to CiviCRM under the Academic Free License version 3.0.
17 *
18 * Grateful acknowledgements go to Donald Lobo for invaluable assistance
19 * in creating this payment processor module
20 */
4c6ce474
EM
21
22/**
23 * Class CRM_Core_Payment_PaymentExpress
24 */
6a488035 25class CRM_Core_Payment_PaymentExpress extends CRM_Core_Payment {
7da04cde 26 const CHARSET = 'iso-8859-1';
6a488035
TO
27
28 protected $_mode = NULL;
29
6a488035 30 /**
fe482240 31 * Constructor.
6a488035 32 *
6a0b768e
TO
33 * @param string $mode
34 * The mode of operation: live or test.
6a488035 35 *
77b97be7
EM
36 * @param $paymentProcessor
37 *
38 * @return \CRM_Core_Payment_PaymentExpress
6a488035 39 */
00be9182 40 public function __construct($mode, &$paymentProcessor) {
6a488035
TO
41
42 $this->_mode = $mode;
43 $this->_paymentProcessor = $paymentProcessor;
6a488035
TO
44 }
45
b5c2afd0 46 /**
fe482240 47 * This function checks to see if we have the right config values.
b5c2afd0
EM
48 *
49 * @internal param string $mode the mode we are operating in (live or test)
50 *
a6c01b45
CW
51 * @return string
52 * the error message if any
b5c2afd0 53 */
00be9182 54 public function checkConfig() {
6a488035
TO
55 $config = CRM_Core_Config::singleton();
56
be2fb01f 57 $error = [];
6a488035
TO
58
59 if (empty($this->_paymentProcessor['user_name'])) {
2e122327 60 $error[] = ts('UserID is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
6a488035
TO
61 }
62
63 if (empty($this->_paymentProcessor['password'])) {
2e122327 64 $error[] = ts('pxAccess / pxPay Key is not set in the Administer &raquo; System Settings &raquo; Payment Processors');
6a488035
TO
65 }
66
67 if (!empty($error)) {
68 return implode('<p>', $error);
69 }
70 else {
71 return NULL;
72 }
73 }
74
b5c2afd0
EM
75 /**
76 * This function collects all the information from a web/api form and invokes
77 * the relevant payment processor specific functions to perform the transaction
78 *
6a0b768e
TO
79 * @param array $params
80 * Assoc array of input parameters for this transaction.
b5c2afd0 81 */
00be9182 82 public function doDirectPayment(&$params) {
2d296f18 83 throw new CRM_Core_Exception(ts('This function is not implemented'));
6a488035
TO
84 }
85
86 /**
fe482240 87 * Main transaction function.
6a488035 88 *
6a0b768e
TO
89 * @param array $params
90 * Name value pair of contribution data.
6c8f6e67
EM
91 *
92 * @param $component
6a488035 93 */
00be9182 94 public function doTransferCheckout(&$params, $component) {
7b3e2900 95 // This is broken - in 2015 this commit broke it... https://github.com/civicrm/civicrm-core/commit/204c86d59f0cfc4c4d917cc245fb41633d36916e#diff-b00e65c9829c27da8b34e35f2e64d9b6L114
6a488035
TO
96 $component = strtolower($component);
97 $config = CRM_Core_Config::singleton();
98 if ($component != 'contribute' && $component != 'event') {
2d296f18 99 throw new CRM_Core_Exception(ts('Component is invalid'));
6a488035
TO
100 }
101
9df3628e 102 $url = CRM_Utils_System::externUrl('extern/pxIPN');
6a488035
TO
103
104 if ($component == 'event') {
105 $cancelURL = CRM_Utils_System::url('civicrm/event/register',
106 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
107 FALSE, NULL, FALSE
108 );
109 }
110 elseif ($component == 'contribute') {
111 $cancelURL = CRM_Utils_System::url('civicrm/contribute/transact',
112 "_qf_Confirm_display=true&qfKey={$params['qfKey']}",
113 FALSE, NULL, FALSE
114 );
115 }
116
6a488035 117 /*
d424ffde
CW
118 * Build the private data string to pass to DPS, which they will give back to us with the
119 *
120 * transaction result. We are building this as a comma-separated list so as to avoid long URLs.
121 *
122 * Parameters passed: a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID
123 */
6a488035
TO
124
125 $privateData = "a={$params['contactID']},b={$params['contributionID']},c={$params['contributionTypeID']},d={$params['invoiceID']}";
126
127 if ($component == 'event') {
2aa397bc 128 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 27, 20), 0, 24);
6a488035
TO
129 $privateData .= ",f={$params['participantID']},g={$params['eventID']}";
130 }
131 elseif ($component == 'contribute') {
9c1bc317 132 $membershipID = $params['membershipID'] ?? NULL;
6a488035
TO
133 if ($membershipID) {
134 $privateData .= ",e=$membershipID";
135 }
136 $merchantRef = substr($params['contactID'] . "-" . $params['contributionID'] . " " . substr($params['description'], 20, 20), 0, 24);
137
138 }
139
be2fb01f 140 $dpsParams = [
6a488035
TO
141 'AmountInput' => str_replace(",", "", number_format($params['amount'], 2)),
142 'CurrencyInput' => $params['currencyID'],
143 'MerchantReference' => $merchantRef,
144 'TxnData1' => $params['qfKey'],
145 'TxnData2' => $privateData,
92fcb95f 146 'TxnData3' => $component . "," . $this->_paymentProcessor['id'],
6a488035
TO
147 'TxnType' => 'Purchase',
148 // Leave this empty for now, causes an error with DPS if we populate it
149 'TxnId' => '',
150 'UrlFail' => $url,
151 'UrlSuccess' => $url,
be2fb01f 152 ];
6a488035
TO
153 // Allow further manipulation of params via custom hooks
154 CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $dpsParams);
155
156 /*
d424ffde
CW
157 * determine whether method is pxaccess or pxpay by whether signature (mac key) is defined
158 */
6a488035 159
6a488035
TO
160 if (empty($this->_paymentProcessor['signature'])) {
161 /*
162 * Processor is pxpay
163 *
164 * This contains the XML/Curl functions we'll need to generate the XML request
165 */
166
167 $dpsParams['PxPayUserId'] = $this->_paymentProcessor['user_name'];
168 $dpsParams['PxPayKey'] = $this->_paymentProcessor['password'];
169 // Build a valid XML string to pass to DPS
170 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml($dpsParams);
171
172 $generateRequest = CRM_Core_Payment_PaymentExpressUtils::_valueXml('GenerateRequest', $generateRequest);
173 // Get the special validated URL back from DPS by sending them the XML we've generated
174 $curl = CRM_Core_Payment_PaymentExpressUtils::_initCURL($generateRequest, $this->_paymentProcessor['url_site']);
175 $success = FALSE;
176
177 if ($response = curl_exec($curl)) {
178 curl_close($curl);
179 $valid = CRM_Core_Payment_PaymentExpressUtils::_xmlAttribute($response, 'valid');
180 if (1 == $valid) {
181 // the request was validated, so we'll get the URL and redirect to it
182 $uri = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'URI');
183 CRM_Utils_System::redirect($uri);
184 }
185 else {
186 // redisplay confirmation page
187 CRM_Utils_System::redirect($cancelURL);
188 }
189 }
190 else {
191 // calling DPS failed
2d296f18 192 throw new CRM_Core_Exception(ts('Unable to establish connection to the payment gateway.'));
6a488035
TO
193 }
194 }
195 else {
196 $processortype = "pxaccess";
2aa397bc 197 require_once 'PaymentExpress/pxaccess.inc.php';
6a488035
TO
198 // URL
199 $PxAccess_Url = $this->_paymentProcessor['url_site'];
200 // User ID
201 $PxAccess_Userid = $this->_paymentProcessor['user_name'];
202 // Your DES Key from DPS
203 $PxAccess_Key = $this->_paymentProcessor['password'];
204 // Your MAC key from DPS
205 $Mac_Key = $this->_paymentProcessor['signature'];
206
207 $pxaccess = new PxAccess($PxAccess_Url, $PxAccess_Userid, $PxAccess_Key, $Mac_Key);
208 $request = new PxPayRequest();
209 $request->setAmountInput($dpsParams['AmountInput']);
210 $request->setTxnData1($dpsParams['TxnData1']);
211 $request->setTxnData2($dpsParams['TxnData2']);
212 $request->setTxnData3($dpsParams['TxnData3']);
213 $request->setTxnType($dpsParams['TxnType']);
214 $request->setInputCurrency($dpsParams['InputCurrency']);
215 $request->setMerchantReference($dpsParams['MerchantReference']);
216 $request->setUrlFail($dpsParams['UrlFail']);
217 $request->setUrlSuccess($dpsParams['UrlSuccess']);
218 $request_string = $pxaccess->makeRequest($request);
219 CRM_Utils_System::redirect($request_string);
220 }
221 }
96025800 222
6a488035 223}