3 * This file is part of CiviCRM.
5 * CiviCRM is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * CiviCRM is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with CiviCRM. If not, see <http://www.gnu.org/licenses/>.
19 * Licensed to CiviCRM under the GPL v3 or higher
21 * Modified by Lisa Marie Maginnis <lisa@fsf.org> (http://www.fsf.org)
25 class CRM_Core_Payment_trustcommerce_IPN
extends CRM_Core_Payment_BaseIPN
{
26 function __construct() {
27 parent
::__construct();
31 function main($component = 'contribute') {
33 $billingid = CRM_Utils_Request
::retrieve('billingid', 'String', $no, FALSE, 'GET');
34 $input['status'] = CRM_Utils_Request
::retrieve('status', 'String', $no, FALSE, 'GET');
35 $input['amount'] = CRM_Utils_Request
::retrieve('amount', 'String', $no, FALSE, 'GET');
36 $input['date'] = CRM_Utils_Request
::retrieve('date', 'String', $no, FALSE, 'GET');
37 $input['trxn_id'] = CRM_Utils_Request
::retrieve('trxn_id', 'String', $no, FALSE, 'GET');
38 $checksum = CRM_Utils_Request
::retrieve('checksum', 'String', $no, FALSE, 'GET');
41 if( $input['status'] == '' ||
$input['amount'] == '' ||
$input['date'] == '' ||
$input['trxn_id'] == '' ||
md5($billingid.$input['trxn_id'].$input['amount'].$input['date']) != $checksum) {
42 CRM_Core_Error
::debug_log_message("Error: IPN called with out proper fields");
43 echo "Error: invalid paramaters<p>\n";
48 $ids = $objects = array();
49 $input['component'] = $component;
51 // load post ids in $ids
53 $ids = $this->getIDs($billingid, $input, $input['component']);
55 $ids['trxn_id'] = $input['trxn_id'];
57 if($this->checkDuplicate($input, $ids) != NULL) {
58 $msg = 'TrustCommerceIPN: Skipping duplicate contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id']."\n";
60 CRM_Core_Error
::debug_log_message($msg);
64 if(array_key_exists('membership', $ids)) {
65 $membership = array();
66 $params = array('id' => $ids['membership']);
67 $obj = CRM_Member_BAO_Membership
::retrieve($params, $membership);
68 $objects['membership'] = array(&$obj);
71 $paymentProcessorID = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
72 'TrustCommerce', 'id', 'name'
75 if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) {
78 // var_dump($objects);
80 if ($component == 'contribute' && $ids['contributionRecur']) {
81 // check if first contribution is completed, else complete first contribution
83 if ($objects['contribution']->contribution_status_id
== 1) {
88 return $this->processRecur($input, $ids, $objects, $first);
95 protected function checkDuplicate($input, $ids) {
96 // $sql='select id from civicrm_contribution where receive_date like \''.$input['date'].'%\' and total_amount='.$input['amount'].' and contact_id='.$ids['contact'].' and contribution_status_id = 1 limit 1';
97 $sql="select id from civicrm_contribution where trxn_id = '".$ids['trxn_id']."' and contribution_status_id != 2";
100 $result = CRM_Core_DAO
::executeQuery($sql);
101 if($result->fetch()) {
109 protected function processRecur($input, $ids, $objects, $first) {
110 $recur = &$objects['contributionRecur'];
111 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
113 $transaction = new CRM_Core_Transaction();
115 $now = date('YmdHis');
117 // fix dates that already exist
118 $dates = array('create_date', 'start_date', 'end_date', 'cancel_date', 'modified_date');
119 foreach ($dates as $name) {
121 $recur->$name = CRM_Utils_Date
::isoToMysql($recur->$name);
126 // create a contribution and then get it processed
127 $contribution = new CRM_Contribute_BAO_Contribution();
128 $contribution->contact_id
= $ids['contact'];
129 $contribution->financial_type_id
= $objects['contributionType']->id
;
130 $contribution->contribution_page_id
= $ids['contributionPage'];
131 $contribution->contribution_recur_id
= $ids['contributionRecur'];
132 $contribution->receive_date
= $input['date'];
133 $contribution->currency
= $objects['contribution']->currency
;
134 $contribution->payment_instrument_id
= 1;
135 $contribution->amount_level
= $objects['contribution']->amount_level
;
136 $contribution->address_id
= $objects['contribution']->address_id
;
137 $contribution->campaign_id
= $objects['contribution']->campaign_id
;
138 $contribution->total_amount
= $input['amount'];
140 $objects['contribution'] = &$contribution;
143 $objects['contribution']->invoice_id
= md5(uniqid(rand(), TRUE));
144 // $objects['contribution']->total_amount = $objects['contribution']->total_amount;
145 $objects['contribution']->trxn_id
= $input['trxn_id'];
147 // check if contribution is already completed, if so we ignore this ipn
148 if ($objects['contribution']->contribution_status_id
== 1) {
149 $transaction->commit();
150 CRM_Core_Error
::debug_log_message("returning since contribution has already been handled");
151 echo 'Success: Contribution has already been handled<p>';
156 $sendNotification = FALSE;
158 $recur->trxn_id
= $input['trxn_id'];
159 $recur->total_amount
= $input['amount'];
160 $recur->payment_instrument_id
= 1;
162 $recur->net_amount
= $input['amount'];
164 if ($input['status'] == 1) {
168 $recur->start_date
= $now;
169 $sendNotification = TRUE;
170 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_START
;
172 $statusName = 'In Progress';
173 if (($recur->installments
> 0) &&
174 ($input['subscription_paynum'] >= $recur->installments
)
176 // this is the last payment
177 $statusName = 'Completed';
178 $recur->end_date
= $now;
180 $sendNotification = TRUE;
181 $subscriptionPaymentStatus = CRM_Core_Payment
::RECURRING_PAYMENT_END
;
184 $recur->modified_date
= $now;
185 $recur->contribution_status_id
= array_search($statusName, $contributionStatus);
187 $input['is_test'] = 0;
188 $msg = 'TrustCommerceIPN: Created contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Completed'."\n";
190 CRM_Core_Error
::debug_log_message($msg);
192 $this->completeTransaction($input, $ids, $objects, $transaction, $recur);
194 else if( $input['status'] == 4 ) {
198 $recur->contribution_status_id
= array_search('Failed', $contributionStatus);
199 $recur->cancel_date
= $now;
202 $msg = 'TrustCommerceIPN: Created contribution: '.$ids['contribution'].' for contact: '.$ids['contact'].' amount: $'.$input['amount'].' trxn_id: '.$input['trxn_id'].' status: Failed'."\n";
204 CRM_Core_Error
::debug_log_message($msg);
206 /* Disable cancelling transactions */
207 $input['skipComponentSync'] = 1;
208 return $this->failed($objects, $transaction, $input);
211 if ($sendNotification) {
212 $autoRenewMembership = FALSE;
213 if ($recur->id
&& isset($ids['membership']) && $ids['membership'] ) {
214 $autoRenewMembership = TRUE;
217 //send recurring Notification email for user
218 CRM_Contribute_BAO_ContributionPage
::recurringNotify($subscriptionPaymentStatus,
220 $ids['contributionPage'],
227 protected function getIDs($billingid, $input, $module) {
228 $sql = "SELECT cr.id, cr.contact_id, co.id as coid
229 FROM civicrm_contribution_recur cr
230 INNER JOIN civicrm_contribution co ON co.contribution_recur_id = cr.id
231 WHERE cr.processor_id = '$billingid' LIMIT 1";
234 $result = CRM_Core_DAO
::executeQuery($sql);
236 $ids['contribution'] = $result->coid
;
237 $ids['contributionRecur'] = $result->id
;
238 $ids['contact'] = $result->contact_id
;
240 if (!$ids['contributionRecur']) {
241 CRM_Core_Error
::debug_log_message("Could not find billingid: ".$billingid);
242 echo "Failure: Could not find contributionRecur: $billingid <p>\n";
246 // get page id based on contribution id
247 $ids['contributionPage'] = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution',
248 $ids['contribution'],
249 'contribution_page_id'
252 if ($module == 'event') {
253 // FIXME: figure out fields for event
256 // get the optional ids
258 // Get membershipId. Join with membership payment table for additional checks
261 FROM civicrm_membership as m
262 WHERE m.contribution_recur_id = '{$ids['contributionRecur']}'
264 if ($membershipId = CRM_Core_DAO
::singleValueQuery($sql)) {
266 $ids['membership'] = $membershipId;