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 | ||
602de060 | 11 | use Civi\Payment\Exception\PaymentProcessorException; |
12 | ||
6a488035 TO |
13 | /** |
14 | * | |
15 | * @package CRM | |
16 | * @author Michael Morris and Gene Chi @ Phase2 Technology <mmorris@phase2technology.com> | |
6a488035 | 17 | */ |
9f9eb906 | 18 | require_once 'PayJunction/pjClasses.php'; |
19 | ||
bc854509 | 20 | /** |
21 | * Class CRM_Core_Payment_PayJunction. | |
22 | */ | |
6a488035 | 23 | class CRM_Core_Payment_PayJunction extends CRM_Core_Payment { |
041ecc95 | 24 | // (not used, implicit in the API, might need to convert?) |
7da04cde | 25 | const CHARSET = 'UFT-8'; |
6a488035 | 26 | |
6a488035 | 27 | /** |
fe482240 | 28 | * Constructor. |
6a488035 | 29 | * |
6a0b768e TO |
30 | * @param string $mode |
31 | * The mode of operation: live or test. | |
6a488035 | 32 | * |
602de060 | 33 | * @param array $paymentProcessor |
6a488035 | 34 | */ |
00be9182 | 35 | public function __construct($mode, &$paymentProcessor) { |
6a488035 TO |
36 | $this->_mode = $mode; |
37 | $this->_paymentProcessor = $paymentProcessor; | |
6a488035 TO |
38 | } |
39 | ||
d424ffde | 40 | /** |
6a488035 TO |
41 | * This function sends request and receives response from |
42 | * PayJunction payment process | |
b5c2afd0 | 43 | * |
6a0b768e TO |
44 | * @param array $params |
45 | * Assoc array of input parameters for this transaction. | |
b5c2afd0 | 46 | * |
a6c01b45 CW |
47 | * @return array |
48 | * the result in an nice formatted array (or an error object) | |
bd493d49 | 49 | * @throws \Civi\Payment\Exception\PaymentProcessorException |
b5c2afd0 | 50 | */ |
00be9182 | 51 | public function doDirectPayment(&$params) { |
353ffa53 | 52 | $logon = $this->_paymentProcessor['user_name']; |
6a488035 TO |
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 | ||
be2fb01f | 61 | $billing = [ |
602de060 | 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'], | |
be2fb01f | 72 | ]; |
6a488035 TO |
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 | ||
be2fb01f | 80 | $txnArray = [ |
6a488035 TO |
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', | |
a175672f | 87 | 'cavv' => $params['cvv2'], |
6a488035 | 88 | 'cust_id' => $params['contact_id'], |
be2fb01f | 89 | ]; |
6a488035 TO |
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 | ||
6a488035 | 99 | // empty installments convert to 999 because PayJunction do not allow open end donation |
bd493d49 | 100 | if ($params['installments'] === '') { |
602de060 | 101 | $params['installments'] = '999'; |
6a488035 TO |
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']; | |
353ffa53 TO |
112 | $recurUnit = $params['frequency_unit']; |
113 | $recurInterval = $params['frequency_interval']; | |
114 | $dc_schedule_start = $params['dc_schedule_start']; | |
6a488035 TO |
115 | |
116 | // next payment in moneris required format | |
117 | $startDate = date("Y/m/d", $next); | |
118 | ||
119 | $numRecurs = $params['installments']; | |
120 | ||
be2fb01f | 121 | $recurArray = [ |
6a488035 TO |
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']), | |
be2fb01f | 132 | ]; |
6a488035 TO |
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)) { | |
602de060 | 148 | throw new PaymentProcessorException($pjpgResponse); |
6a488035 TO |
149 | } |
150 | ||
151 | // Success | |
152 | $params['trxn_result_code'] = $pjpgResponse['dc_response_code']; | |
153 | $params['trxn_id'] = $pjpgResponse['dc_transaction_id']; | |
6a488035 TO |
154 | |
155 | return $params; | |
156 | } | |
518fa0ee | 157 | |
6a488035 TO |
158 | // end function doDirectPayment |
159 | ||
6c786a9b | 160 | /** |
fe482240 | 161 | * This function checks the PayJunction response code. |
6c786a9b | 162 | * |
54957108 | 163 | * @param array $response |
164 | * | |
6c786a9b EM |
165 | * @return bool |
166 | */ | |
00be9182 | 167 | public function isError(&$response) { |
6a488035 TO |
168 | $responseCode = $response['dc_response_code']; |
169 | ||
bd493d49 | 170 | if ($responseCode === "00" || $responseCode === "85") { |
6a488035 TO |
171 | return FALSE; |
172 | } | |
bd493d49 | 173 | return TRUE; |
174 | ||
6a488035 TO |
175 | } |
176 | ||
6a488035 | 177 | /** |
fe482240 | 178 | * Get the value of a field if set. |
6a488035 | 179 | * |
6a0b768e TO |
180 | * @param string $field |
181 | * The field. | |
6a488035 | 182 | * |
72b3a70c CW |
183 | * @return mixed |
184 | * value of the field, or empty string if the field is | |
16b10e64 | 185 | * not set |
6a488035 | 186 | */ |
00be9182 | 187 | public function _getParam($field) { |
6a488035 TO |
188 | if (isset($this->_params[$field])) { |
189 | return $this->_params[$field]; | |
190 | } | |
bd493d49 | 191 | return ''; |
6a488035 TO |
192 | } |
193 | ||
6a488035 TO |
194 | /** |
195 | * Set a field to the specified value. Value must be a scalar (int, | |
196 | * float, string, or boolean) | |
197 | * | |
198 | * @param string $field | |
199 | * @param mixed $value | |
200 | * | |
a6c01b45 CW |
201 | * @return bool |
202 | * false if value is not a scalar, true if successful | |
6a488035 | 203 | */ |
00be9182 | 204 | public function _setParam($field, $value) { |
6a488035 TO |
205 | if (!is_scalar($value)) { |
206 | return FALSE; | |
207 | } | |
208 | else { | |
209 | $this->_params[$field] = $value; | |
210 | } | |
211 | } | |
212 | ||
213 | /** | |
fe482240 | 214 | * This function checks to see if we have the right config values. |
6a488035 | 215 | * |
a6c01b45 CW |
216 | * @return string |
217 | * the error message if any | |
6a488035 | 218 | */ |
00be9182 | 219 | public function checkConfig() { |
be2fb01f | 220 | $error = []; |
6a488035 TO |
221 | if (empty($this->_paymentProcessor['user_name'])) { |
222 | $error[] = ts('Username is not set for this payment processor'); | |
223 | } | |
224 | ||
225 | if (empty($this->_paymentProcessor['password'])) { | |
226 | $error[] = ts('Password is not set for this payment processor'); | |
227 | } | |
228 | ||
229 | if (empty($this->_paymentProcessor['url_site'])) { | |
230 | $error[] = ts('Site URL is not set for this payment processor'); | |
231 | } | |
232 | ||
233 | if (!empty($error)) { | |
234 | return implode('<p>', $error); | |
235 | } | |
602de060 | 236 | return NULL; |
6a488035 | 237 | } |
96025800 | 238 | |
6a488035 TO |
239 | } |
240 | // end class CRM_Core_Payment_PayJunction |