5 * Licensed to CiviCRM under the Academic Free License version 3.0.
7 * Written and contributed by Phase2 Technology, LLC (http://www.phase2technology.com)
11 use Civi\Payment\Exception\PaymentProcessorException
;
16 * @author Michael Morris and Gene Chi @ Phase2 Technology <mmorris@phase2technology.com>
18 require_once 'PayJunction/pjClasses.php';
21 * Class CRM_Core_Payment_PayJunction.
23 class CRM_Core_Payment_PayJunction
extends CRM_Core_Payment
{
24 // (not used, implicit in the API, might need to convert?)
25 const CHARSET
= 'UFT-8';
31 * The mode of operation: live or test.
33 * @param array $paymentProcessor
35 public function __construct($mode, &$paymentProcessor) {
37 $this->_paymentProcessor
= $paymentProcessor;
41 * This function sends request and receives response from
42 * PayJunction payment process
44 * @param array $params
45 * Assoc array of input parameters for this transaction.
48 * the result in an nice formatted array (or an error object)
49 * @throws \Civi\Payment\Exception\PaymentProcessorException
51 public function doDirectPayment(&$params) {
52 $logon = $this->_paymentProcessor
['user_name'];
53 $password = $this->_paymentProcessor
['password'];
54 $url_site = $this->_paymentProcessor
['url_site'];
56 // create pjpgCustInfo object
57 $pjpgCustInfo = new pjpgCustInfo();
59 $pjpgCustInfo->setEmail($params['email']);
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'],
73 $pjpgCustInfo->setBilling($billing);
75 // create pjpgTransaction object
76 $my_orderid = $params['invoiceID'];
78 $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']);
82 'order_id' => $my_orderid,
83 'amount' => sprintf('%01.2f', $params['amount']),
84 'pan' => $params['credit_card_number'],
85 'expdate' => $expiry_string,
87 'cavv' => $params['cvv2'],
88 'cust_id' => $params['contact_id'],
91 // Allow further manipulation of params via custom hooks
92 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $txnArray);
94 $pjpgTxn = new pjpgTransaction($txnArray);
96 // set customer info (level 3 data) for the transaction
97 $pjpgTxn->setCustInfo($pjpgCustInfo);
99 // empty installments convert to 999 because PayJunction do not allow open end donation
100 if ($params['installments'] === '') {
101 $params['installments'] = '999';
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");
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'];
116 // next payment in moneris required format
117 $startDate = date("Y/m/d", $next);
119 $numRecurs = $params['installments'];
122 'dc_schedule_create' => $dc_schedule_create,
123 // (day | week | month)
124 'recur_unit' => $recurUnit,
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']),
134 $pjpgRecur = new pjpgRecur($recurArray);
136 $pjpgTxn->setRecur($pjpgRecur);
139 // create a pjpgRequest object passing the transaction object
140 $pjpgRequest = new pjpgRequest($pjpgTxn);
142 $pjpgHttpPost = new pjpgHttpsPost($pjpgRequest);
144 // get an pjpgResponse object
145 $pjpgResponse = $pjpgHttpPost->getPJpgResponse();
147 if (self
::isError($pjpgResponse)) {
148 throw new PaymentProcessorException($pjpgResponse);
152 $params['trxn_result_code'] = $pjpgResponse['dc_response_code'];
153 $params['trxn_id'] = $pjpgResponse['dc_transaction_id'];
158 // end function doDirectPayment
161 * This function checks the PayJunction response code.
163 * @param array $response
167 public function isError(&$response) {
168 $responseCode = $response['dc_response_code'];
170 if ($responseCode === "00" ||
$responseCode === "85") {
178 * Get the value of a field if set.
180 * @param string $field
184 * value of the field, or empty string if the field is
187 public function _getParam($field) {
188 if (isset($this->_params
[$field])) {
189 return $this->_params
[$field];
195 * Set a field to the specified value. Value must be a scalar (int,
196 * float, string, or boolean)
198 * @param string $field
199 * @param mixed $value
202 * false if value is not a scalar, true if successful
204 public function _setParam($field, $value) {
205 if (!is_scalar($value)) {
209 $this->_params
[$field] = $value;
214 * This function checks to see if we have the right config values.
217 * the error message if any
219 public function checkConfig() {
221 if (empty($this->_paymentProcessor
['user_name'])) {
222 $error[] = ts('Username is not set for this payment processor');
225 if (empty($this->_paymentProcessor
['password'])) {
226 $error[] = ts('Password is not set for this payment processor');
229 if (empty($this->_paymentProcessor
['url_site'])) {
230 $error[] = ts('Site URL is not set for this payment processor');
233 if (!empty($error)) {
234 return implode('<p>', $error);
240 // end class CRM_Core_Payment_PayJunction