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