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