Remove unused singleton from core payment processors
[civicrm-core.git] / CRM / Core / Payment / PayJunction.php
1 <?php
2
3 /**
4 * Copyright (C) 2007
5 * Licensed to CiviCRM under the Academic Free License version 3.0.
6 *
7 * Written and contributed by Phase2 Technology, LLC (http://www.phase2technology.com)
8 *
9 */
10
11 /**
12 *
13 * @package CRM
14 * @author Michael Morris and Gene Chi @ Phase2 Technology <mmorris@phase2technology.com>
15 */
16 require_once 'PayJunction/pjClasses.php';
17
18 /**
19 * Class CRM_Core_Payment_PayJunction.
20 */
21 class CRM_Core_Payment_PayJunction extends CRM_Core_Payment {
22 // (not used, implicit in the API, might need to convert?)
23 const CHARSET = 'UFT-8';
24
25 /**
26 * Constructor.
27 *
28 * @param string $mode
29 * The mode of operation: live or test.
30 *
31 * @param $paymentProcessor
32 *
33 * @return \CRM_Core_Payment_PayJunction
34 */
35 public function __construct($mode, &$paymentProcessor) {
36 $this->_mode = $mode;
37 $this->_paymentProcessor = $paymentProcessor;
38 $this->_processorName = ts('PayJunction');
39 }
40
41 /**
42 * This function sends request and receives response from
43 * PayJunction payment process
44 *
45 * @param array $params
46 * Assoc array of input parameters for this transaction.
47 *
48 * @return array
49 * the result in an nice formatted array (or an error object)
50 */
51 public function doDirectPayment(&$params) {
52 $logon = $this->_paymentProcessor['user_name'];
53 $password = $this->_paymentProcessor['password'];
54 $url_site = $this->_paymentProcessor['url_site'];
55
56 // create pjpgCustInfo object
57 $pjpgCustInfo = new pjpgCustInfo();
58
59 $pjpgCustInfo->setEmail($params['email']);
60
61 $billing = [
62 "logon" => $logon,
63 "password" => $password,
64 "url_site" => $url_site,
65 "first_name" => $params['first_name'],
66 "last_name" => $params['last_name'],
67 "address" => $params['street_address'],
68 "city" => $params['city'],
69 "province" => $params['state_province'],
70 "postal_code" => $params['postal_code'],
71 "country" => $params['country'],
72 ];
73 $pjpgCustInfo->setBilling($billing);
74
75 // create pjpgTransaction object
76 $my_orderid = $params['invoiceID'];
77
78 $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']);
79
80 $txnArray = [
81 'type' => 'purchase',
82 'order_id' => $my_orderid,
83 'amount' => sprintf('%01.2f', $params['amount']),
84 'pan' => $params['credit_card_number'],
85 'expdate' => $expiry_string,
86 'crypt_type' => '7',
87 'cavv' => $params['cvv2'],
88 'cust_id' => $params['contact_id'],
89 ];
90
91 // Allow further manipulation of params via custom hooks
92 CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $txnArray);
93
94 $pjpgTxn = new pjpgTransaction($txnArray);
95
96 // set customer info (level 3 data) for the transaction
97 $pjpgTxn->setCustInfo($pjpgCustInfo);
98
99 // empty installments convert to 999 because PayJunction do not allow open end donation
100 if ($params['installments'] == "") {
101 $params['installments'] = "999";
102 }
103
104 // create recurring object
105 if ($params['is_recur'] == TRUE && $params['installments'] > 1) {
106 // schedule start date as today
107 // format: YYYY-MM-DD
108 $params['dc_schedule_start'] = date("Y-m-d");
109
110 // Recur Variables
111 $dc_schedule_create = $params['is_recur'];
112 $recurUnit = $params['frequency_unit'];
113 $recurInterval = $params['frequency_interval'];
114 $dc_schedule_start = $params['dc_schedule_start'];
115
116 // next payment in moneris required format
117 $startDate = date("Y/m/d", $next);
118
119 $numRecurs = $params['installments'];
120
121 $recurArray = [
122 'dc_schedule_create' => $dc_schedule_create,
123 // (day | week | month)
124 'recur_unit' => $recurUnit,
125 // yyyy/mm/dd
126 'start_date' => $startDate,
127 'num_recurs' => $numRecurs,
128 'start_now' => 'false',
129 'period' => $recurInterval,
130 'dc_schedule_start' => $dc_schedule_start,
131 'amount' => sprintf('%01.2f', $params['amount']),
132 ];
133
134 $pjpgRecur = new pjpgRecur($recurArray);
135
136 $pjpgTxn->setRecur($pjpgRecur);
137 }
138
139 // create a pjpgRequest object passing the transaction object
140 $pjpgRequest = new pjpgRequest($pjpgTxn);
141
142 $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest);
143
144 // get an pjpgResponse object
145 $pjpgResponse = $pjpgHttpPost->getPJpgResponse();
146
147 if (self::isError($pjpgResponse)) {
148 return self::error($pjpgResponse);
149 }
150
151 /* Check for application errors */
152
153 $result = self::checkResult($pjpgResponse);
154 if (is_a($result, 'CRM_Core_Error')) {
155 return $result;
156 }
157
158 // Success
159 $params['trxn_result_code'] = $pjpgResponse['dc_response_code'];
160 $params['trxn_id'] = $pjpgResponse['dc_transaction_id'];
161 $params['gross_amount'] = $params['amount'];
162
163 return $params;
164 }
165
166 // end function doDirectPayment
167
168 /**
169 * This function checks the PayJunction response code.
170 *
171 * @param array $response
172 *
173 * @return bool
174 */
175 public function isError(&$response) {
176 $responseCode = $response['dc_response_code'];
177
178 if ($responseCode == "00" || $responseCode == "85") {
179 return FALSE;
180 }
181 else {
182 return TRUE;
183 }
184 }
185
186 /**
187 * ignore for now, more elaborate error handling later.
188 * @param $response
189 *
190 * @return mixed
191 */
192 public function &checkResult(&$response) {
193 return $response;
194 }
195
196 /**
197 * Get the value of a field if set.
198 *
199 * @param string $field
200 * The field.
201 *
202 * @return mixed
203 * value of the field, or empty string if the field is
204 * not set
205 */
206 public function _getParam($field) {
207 if (isset($this->_params[$field])) {
208 return $this->_params[$field];
209 }
210 else {
211 return '';
212 }
213 }
214
215 /**
216 * @param null $error
217 *
218 * @return object
219 */
220 public function &error($error = NULL) {
221 $e = CRM_Core_Error::singleton();
222 if ($error) {
223 $e->push($error['dc_response_code'],
224 0, NULL,
225 $error['dc_response_message']
226 );
227 }
228 else {
229 $e->push(9001, 0, NULL, "Unknown System Error.");
230 }
231 return $e;
232 }
233
234 /**
235 * Set a field to the specified value. Value must be a scalar (int,
236 * float, string, or boolean)
237 *
238 * @param string $field
239 * @param mixed $value
240 *
241 * @return bool
242 * false if value is not a scalar, true if successful
243 */
244 public function _setParam($field, $value) {
245 if (!is_scalar($value)) {
246 return FALSE;
247 }
248 else {
249 $this->_params[$field] = $value;
250 }
251 }
252
253 /**
254 * This function checks to see if we have the right config values.
255 *
256 * @return string
257 * the error message if any
258 */
259 public function checkConfig() {
260 $error = [];
261 if (empty($this->_paymentProcessor['user_name'])) {
262 $error[] = ts('Username is not set for this payment processor');
263 }
264
265 if (empty($this->_paymentProcessor['password'])) {
266 $error[] = ts('Password is not set for this payment processor');
267 }
268
269 if (empty($this->_paymentProcessor['url_site'])) {
270 $error[] = ts('Site URL is not set for this payment processor');
271 }
272
273 if (!empty($error)) {
274 return implode('<p>', $error);
275 }
276 else {
277 return NULL;
278 }
279 }
280
281 }
282 // end class CRM_Core_Payment_PayJunction