Commit | Line | Data |
---|---|---|
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 | */ | |
18 | class 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', | |
95 | 'cust_id' => $params['contact_id'], | |
96 | ); | |
97 | ||
98 | // Allow further manipulation of params via custom hooks | |
99 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $txnArray); | |
100 | ||
101 | $pjpgTxn = new pjpgTransaction($txnArray); | |
102 | ||
103 | // set customer info (level 3 data) for the transaction | |
104 | $pjpgTxn->setCustInfo($pjpgCustInfo); | |
105 | ||
6a488035 TO |
106 | // empty installments convert to 999 because PayJunction do not allow open end donation |
107 | if ($params['installments'] == "") { | |
108 | $params['installments'] = "999"; | |
109 | } | |
110 | ||
111 | // create recurring object | |
112 | if ($params['is_recur'] == TRUE && $params['installments'] > 1) { | |
113 | // schedule start date as today | |
114 | // format: YYYY-MM-DD | |
115 | $params['dc_schedule_start'] = date("Y-m-d"); | |
116 | ||
117 | // Recur Variables | |
118 | $dc_schedule_create = $params['is_recur']; | |
353ffa53 TO |
119 | $recurUnit = $params['frequency_unit']; |
120 | $recurInterval = $params['frequency_interval']; | |
121 | $dc_schedule_start = $params['dc_schedule_start']; | |
6a488035 TO |
122 | |
123 | // next payment in moneris required format | |
124 | $startDate = date("Y/m/d", $next); | |
125 | ||
126 | $numRecurs = $params['installments']; | |
127 | ||
128 | $recurArray = array( | |
129 | 'dc_schedule_create' => $dc_schedule_create, | |
130 | // (day | week | month) | |
131 | 'recur_unit' => $recurUnit, | |
132 | // yyyy/mm/dd | |
133 | 'start_date' => $startDate, | |
134 | 'num_recurs' => $numRecurs, | |
135 | 'start_now' => 'false', | |
136 | 'period' => $recurInterval, | |
137 | 'dc_schedule_start' => $dc_schedule_start, | |
138 | 'amount' => sprintf('%01.2f', $params['amount']), | |
139 | ); | |
140 | ||
141 | $pjpgRecur = new pjpgRecur($recurArray); | |
142 | ||
143 | $pjpgTxn->setRecur($pjpgRecur); | |
144 | } | |
145 | ||
146 | // create a pjpgRequest object passing the transaction object | |
147 | $pjpgRequest = new pjpgRequest($pjpgTxn); | |
148 | ||
149 | $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest); | |
150 | ||
151 | // get an pjpgResponse object | |
152 | $pjpgResponse = $pjpgHttpPost->getPJpgResponse(); | |
153 | ||
154 | if (self::isError($pjpgResponse)) { | |
155 | return self::error($pjpgResponse); | |
156 | } | |
157 | ||
158 | /* Check for application errors */ | |
159 | ||
160 | $result = self::checkResult($pjpgResponse); | |
161 | if (is_a($result, 'CRM_Core_Error')) { | |
162 | return $result; | |
163 | } | |
164 | ||
165 | // Success | |
166 | $params['trxn_result_code'] = $pjpgResponse['dc_response_code']; | |
167 | $params['trxn_id'] = $pjpgResponse['dc_transaction_id']; | |
168 | $params['gross_amount'] = $params['amount']; | |
169 | ||
170 | return $params; | |
171 | } | |
172 | // end function doDirectPayment | |
173 | ||
174 | ||
6c786a9b | 175 | /** |
fe482240 | 176 | * This function checks the PayJunction response code. |
6c786a9b EM |
177 | * |
178 | * @return bool | |
179 | */ | |
00be9182 | 180 | public function isError(&$response) { |
6a488035 TO |
181 | $responseCode = $response['dc_response_code']; |
182 | ||
183 | if ($responseCode == "00" || $responseCode == "85") { | |
184 | return FALSE; | |
185 | } | |
186 | else { | |
187 | return TRUE; | |
188 | } | |
189 | } | |
190 | ||
191 | ||
6c786a9b | 192 | /** |
4f1f1f2a | 193 | * ignore for now, more elaborate error handling later. |
6c786a9b EM |
194 | * @param $response |
195 | * | |
196 | * @return mixed | |
197 | */ | |
00be9182 | 198 | public function &checkResult(&$response) { |
6a488035 TO |
199 | return $response; |
200 | } | |
201 | ||
202 | /** | |
fe482240 | 203 | * Get the value of a field if set. |
6a488035 | 204 | * |
6a0b768e TO |
205 | * @param string $field |
206 | * The field. | |
6a488035 | 207 | * |
72b3a70c CW |
208 | * @return mixed |
209 | * value of the field, or empty string if the field is | |
16b10e64 | 210 | * not set |
6a488035 | 211 | */ |
00be9182 | 212 | public function _getParam($field) { |
6a488035 TO |
213 | if (isset($this->_params[$field])) { |
214 | return $this->_params[$field]; | |
215 | } | |
216 | else { | |
217 | return ''; | |
218 | } | |
219 | } | |
220 | ||
6c786a9b EM |
221 | /** |
222 | * @param null $error | |
223 | * | |
224 | * @return object | |
225 | */ | |
00be9182 | 226 | public function &error($error = NULL) { |
6a488035 TO |
227 | $e = CRM_Core_Error::singleton(); |
228 | if ($error) { | |
229 | $e->push($error['dc_response_code'], | |
230 | 0, NULL, | |
231 | $error['dc_response_message'] | |
232 | ); | |
233 | } | |
234 | else { | |
235 | $e->push(9001, 0, NULL, "Unknown System Error."); | |
236 | } | |
237 | return $e; | |
238 | } | |
239 | ||
240 | /** | |
241 | * Set a field to the specified value. Value must be a scalar (int, | |
242 | * float, string, or boolean) | |
243 | * | |
244 | * @param string $field | |
245 | * @param mixed $value | |
246 | * | |
a6c01b45 CW |
247 | * @return bool |
248 | * false if value is not a scalar, true if successful | |
6a488035 | 249 | */ |
00be9182 | 250 | public function _setParam($field, $value) { |
6a488035 TO |
251 | if (!is_scalar($value)) { |
252 | return FALSE; | |
253 | } | |
254 | else { | |
255 | $this->_params[$field] = $value; | |
256 | } | |
257 | } | |
258 | ||
259 | /** | |
fe482240 | 260 | * This function checks to see if we have the right config values. |
6a488035 | 261 | * |
a6c01b45 CW |
262 | * @return string |
263 | * the error message if any | |
6a488035 | 264 | */ |
00be9182 | 265 | public function checkConfig() { |
6a488035 TO |
266 | $error = array(); |
267 | if (empty($this->_paymentProcessor['user_name'])) { | |
268 | $error[] = ts('Username is not set for this payment processor'); | |
269 | } | |
270 | ||
271 | if (empty($this->_paymentProcessor['password'])) { | |
272 | $error[] = ts('Password is not set for this payment processor'); | |
273 | } | |
274 | ||
275 | if (empty($this->_paymentProcessor['url_site'])) { | |
276 | $error[] = ts('Site URL is not set for this payment processor'); | |
277 | } | |
278 | ||
279 | if (!empty($error)) { | |
280 | return implode('<p>', $error); | |
281 | } | |
282 | else { | |
283 | return NULL; | |
284 | } | |
285 | } | |
96025800 | 286 | |
6a488035 TO |
287 | } |
288 | // end class CRM_Core_Payment_PayJunction |