Esempio n. 1
0
 /**
  * check method create()
  */
 function testCreate()
 {
     $contactId = $this->individualCreate();
     $contributionTypeId = $this->contributionTypeCreate();
     $contributionId = $this->contributionCreate($contactId, $contributionTypeId);
     $params = array('contribution_id' => $contributionTypeId, 'trxn_date' => 20091021184930.0, 'trxn_type' => 'Debit', 'total_amount' => 10, 'currency' => 'USD', 'payment_processor' => 'Dummy', 'trxn_id' => 'test_01014000');
     $FinancialTrxn = CRM_Contribute_BAO_FinancialTrxn::create($params);
     $result = $this->assertDBNotNull('CRM_Contribute_BAO_FinancialTrxn', $FinancialTrxn->id, 'total_amount', 'id', 'Database check on updated financial trxn record.');
     $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
 }
Esempio n. 2
0
 function completeTransaction(&$input, &$ids, &$objects, &$transaction, $recur = false)
 {
     $contribution =& $objects['contribution'];
     $membership =& $objects['membership'];
     $participant =& $objects['participant'];
     $event =& $objects['event'];
     $values = array();
     if ($input['component'] == 'contribute') {
         require_once 'CRM/Contribute/BAO/ContributionPage.php';
         CRM_Contribute_BAO_ContributionPage::setValues($contribution->contribution_page_id, $values);
         $contribution->source = ts('Online Contribution') . ': ' . $values['title'];
         if ($values['is_email_receipt']) {
             $contribution->receipt_date = self::$_now;
         }
         if ($membership) {
             $format = '%Y%m%d';
             require_once 'CRM/Member/BAO/MembershipType.php';
             require_once 'CRM/Member/BAO/Membership.php';
             $currentMembership = CRM_Member_BAO_Membership::getContactMembership($membership->contact_id, $membership->membership_type_id, $membership->is_test, $membership->id);
             if ($currentMembership) {
                 /*
                  * Fixed FOR CRM-4433
                  * In BAO/Membership.php(renewMembership function), we skip the extend membership date and status 
                  * when Contribution mode is notify and membership is for renewal ) 
                  */
                 CRM_Member_BAO_Membership::fixMembershipStatusBeforeRenew($currentMembership, $changeToday = null);
                 $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id, $changeToday = null);
                 $dates['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format);
             } else {
                 $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membership->membership_type_id);
             }
             //get the status for membership.
             require_once 'CRM/Member/BAO/MembershipStatus.php';
             $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($dates['start_date'], $dates['end_date'], $dates['join_date'], 'today', true);
             $formatedParams = array('status_id' => CRM_Utils_Array::value('id', $calcStatus, 2), 'join_date' => CRM_Utils_Date::customFormat($dates['join_date'], $format), 'start_date' => CRM_Utils_Date::customFormat($dates['start_date'], $format), 'end_date' => CRM_Utils_Date::customFormat($dates['end_date'], $format), 'reminder_date' => CRM_Utils_Date::customFormat($dates['reminder_date'], $format));
             $membership->copyValues($formatedParams);
             $membership->save();
             //updating the membership log
             $membershipLog = array();
             $membershipLog = $formatedParams;
             $logStartDate = CRM_Utils_Date::customFormat($dates['log_start_date'], $format);
             $logStartDate = $logStartDate ? CRM_Utils_Date::isoToMysql($logStartDate) : $formatedParams['start_date'];
             $membershipLog['start_date'] = $logStartDate;
             $membershipLog['membership_id'] = $membership->id;
             $membershipLog['modified_id'] = $membership->contact_id;
             $membershipLog['modified_date'] = date('Ymd');
             require_once 'CRM/Member/BAO/MembershipLog.php';
             CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray);
             //update related Memberships.
             CRM_Member_BAO_Membership::updateRelatedMemberships($membership->id, $formatedParams);
         }
     } else {
         // event
         $eventParams = array('id' => $objects['event']->id);
         $values['event'] = array();
         require_once 'CRM/Event/BAO/Event.php';
         CRM_Event_BAO_Event::retrieve($eventParams, $values['event']);
         $eventParams = array('id' => $objects['event']->id);
         $values['event'] = array();
         require_once 'CRM/Event/BAO/Event.php';
         CRM_Event_BAO_Event::retrieve($eventParams, $values['event']);
         //get location details
         $locationParams = array('entity_id' => $objects['event']->id, 'entity_table' => 'civicrm_event');
         require_once 'CRM/Core/BAO/Location.php';
         require_once 'CRM/Event/Form/ManageEvent/Location.php';
         $values['location'] = CRM_Core_BAO_Location::getValues($locationParams);
         require_once 'CRM/Core/BAO/UFJoin.php';
         $ufJoinParams = array('entity_table' => 'civicrm_event', 'entity_id' => $ids['event'], 'weight' => 1);
         $values['custom_pre_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParams);
         $ufJoinParams['weight'] = 2;
         $values['custom_post_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParams);
         $contribution->source = ts('Online Event Registration') . ': ' . $values['event']['title'];
         if ($values['event']['is_email_confirm']) {
             $contribution->receipt_date = self::$_now;
         }
         $participant->status_id = 1;
         $participant->save();
     }
     if ($input['net_amount'] == 0 && $input['fee_amount'] != 0) {
         $input['net_amount'] = $input['amount'] - $input['fee_amount'];
     }
     $contribution->contribution_status_id = 1;
     $contribution->is_test = $input['is_test'];
     $contribution->fee_amount = $input['fee_amount'];
     $contribution->net_amount = $input['net_amount'];
     $contribution->trxn_id = $input['trxn_id'];
     $contribution->receive_date = CRM_Utils_Date::isoToMysql($contribution->receive_date);
     $contribution->cancel_date = 'null';
     if (CRM_Utils_Array::value('check_number', $input)) {
         $contribution->check_number = $input['check_number'];
     }
     if (CRM_Utils_Array::value('payment_instrument_id', $input)) {
         $contribution->payment_instrument_id = $input['payment_instrument_id'];
     }
     $contribution->save();
     // next create the transaction record
     if (isset($objects['paymentProcessor'])) {
         $paymentProcessor = $objects['paymentProcessor']['payment_processor_type'];
     } else {
         $paymentProcessor = '';
     }
     if ($contribution->trxn_id) {
         $trxnParams = array('contribution_id' => $contribution->id, 'trxn_date' => isset($input['trxn_date']) ? $input['trxn_date'] : self::$_now, 'trxn_type' => 'Debit', 'total_amount' => $input['amount'], 'fee_amount' => $contribution->fee_amount, 'net_amount' => $contribution->net_amount, 'currency' => $contribution->currency, 'payment_processor' => $paymentProcessor, 'trxn_id' => $contribution->trxn_id);
         require_once 'CRM/Contribute/BAO/FinancialTrxn.php';
         $trxn =& CRM_Contribute_BAO_FinancialTrxn::create($trxnParams);
     }
     //update corresponding pledge payment record
     require_once 'CRM/Core/DAO.php';
     $returnProperties = array('id', 'pledge_id');
     if (CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_Payment', 'contribution_id', $contribution->id, $paymentDetails, $returnProperties)) {
         $paymentIDs = array();
         foreach ($paymentDetails as $key => $value) {
             $paymentIDs[] = $value['id'];
             $pledgeId = $value['pledge_id'];
         }
         // update pledge and corresponding payment statuses
         require_once 'CRM/Pledge/BAO/Payment.php';
         CRM_Pledge_BAO_Payment::updatePledgePaymentStatus($pledgeId, $paymentIDs, $contribution->contribution_status_id);
     }
     // create an activity record
     require_once "CRM/Activity/BAO/Activity.php";
     if ($input['component'] == 'contribute') {
         //CRM-4027
         $targetContactID = null;
         if (CRM_Utils_Array::value('related_contact', $ids)) {
             $targetContactID = $contribution->contact_id;
             $contribution->contact_id = $ids['related_contact'];
         }
         CRM_Activity_BAO_Activity::addActivity($contribution, null, $targetContactID);
     } else {
         // event
         CRM_Activity_BAO_Activity::addActivity($participant);
     }
     CRM_Core_Error::debug_log_message("Contribution record updated successfully");
     $transaction->commit();
     self::sendMail($input, $ids, $objects, $values, $recur, false);
     CRM_Core_Error::debug_log_message("Success: Database updated and mail sent");
 }
Esempio n. 3
0
 /**
  * Process the contribution
  *
  * @return void
  * @access public
  */
 static function processContribution(&$form, $params, $result, $contactID, $contributionType, $deductibleMode = true, $pending = false, $online = true)
 {
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $honorCId = $recurringContributionID = null;
     if ($online) {
         if ($form->get('honor_block_is_active')) {
             $honorCId = $form->createHonorContact();
         }
         $recurringContributionID = $form->processRecurringContribution($params, $contactID);
     } else {
         if (!$online && isset($params['honor_contact_id'])) {
             $honorCId = $params['honor_contact_id'];
         }
     }
     $config =& CRM_Core_Config::singleton();
     if (!$online && isset($params['non_deductible_amount'])) {
         $nonDeductibleAmount = $params['non_deductible_amount'];
     } else {
         $nonDeductibleAmount = $params['amount'];
     }
     if ($online && $contributionType->is_deductible && $deductibleMode) {
         $selectProduct = CRM_Utils_Array::value('selectProduct', $premiumParams);
         if ($selectProduct && $selectProduct != 'no_thanks') {
             require_once 'CRM/Contribute/DAO/Product.php';
             $productDAO =& new CRM_Contribute_DAO_Product();
             $productDAO->id = $selectProduct;
             $productDAO->find(true);
             if ($params['amount'] < $productDAO->price) {
                 $nonDeductibleAmount = $params['amount'];
             } else {
                 $nonDeductibleAmount = $productDAO->price;
             }
         } else {
             $nonDeductibleAmount = '0.00';
         }
     }
     $now = date('YmdHis');
     $receiptDate = CRM_Utils_Array::value('receipt_date', $params);
     if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) {
         $receiptDate = $now;
     }
     // check contribution Type
     // first create the contribution record
     $contribParams = array('contact_id' => $contactID, 'contribution_type_id' => $contributionType->id, 'contribution_page_id' => $online ? $form->_id : null, 'receive_date' => $now, 'non_deductible_amount' => $nonDeductibleAmount, 'total_amount' => $params['amount'], 'amount_level' => CRM_Utils_Array::value('amount_level', $params), 'invoice_id' => $params['invoiceID'], 'currency' => $params['currencyID'], 'source' => !$online || CRM_Utils_Array::value('source', $params) ? CRM_Utils_Array::value('source', $params) : CRM_Utils_Array::value('description', $params), 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), 'cancel_reason' => CRM_Utils_Array::value('cancel_reason', $params, 0), 'cancel_date' => isset($params['cancel_date']) ? CRM_Utils_Date::format($params['cancel_date']) : null, 'thankyou_date' => isset($params['thankyou_date']) ? CRM_Utils_Date::format($params['thankyou_date']) : null);
     if (!$online && isset($params['thankyou_date'])) {
         $contribParams['thankyou_date'] = $params['thankyou_date'];
     }
     if (!$online || $form->_values['is_monetary']) {
         if (!CRM_Utils_Array::value('is_pay_later', $params)) {
             $contribParams['payment_instrument_id'] = 1;
         }
     }
     if (!$pending && $result) {
         $contribParams += array('fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']), 'trxn_id' => $result['trxn_id'], 'receipt_date' => $receiptDate, 'trxn_result_code' => $result['trxn_result_code'], 'payment_processor' => $result['payment_processor']);
     }
     if (isset($honorCId)) {
         $contribParams["honor_contact_id"] = $honorCId;
         $contribParams["honor_type_id"] = $params['honor_type_id'];
     }
     if ($recurringContributionID) {
         $contribParams['contribution_recur_id'] = $recurringContributionID;
     }
     $contribParams["contribution_status_id"] = $pending ? 2 : 1;
     if ($form->_mode == 'test') {
         $contribParams["is_test"] = 1;
     }
     $ids = array();
     if (isset($contribParams['invoice_id'])) {
         $contribID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contribParams['invoice_id'], 'id', 'invoice_id');
         if (isset($contribID)) {
             $ids['contribution'] = $contribID;
             $contribParams['id'] = $contribID;
         }
     }
     foreach (array('pcp_display_in_roll', 'pcp_roll_nickname', 'pcp_personal_note') as $val) {
         if (CRM_Utils_Array::value($val, $params)) {
             $contribSoftParams[$val] = $params[$val];
         }
     }
     require_once 'CRM/Contribute/BAO/Contribution.php';
     //create an contribution address
     if ($form->_contributeMode != 'notify' && !CRM_Utils_Array::value('is_pay_later', $params)) {
         $contribParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID);
     }
     // CRM-4038: for non-en_US locales, CRM_Contribute_BAO_Contribution::add() expects localised amounts
     require_once 'CRM/Utils/Money.php';
     $contribParams['non_deductible_amount'] = trim(CRM_Utils_Money::format($contribParams['non_deductible_amount'], ' '));
     $contribParams['total_amount'] = trim(CRM_Utils_Money::format($contribParams['total_amount'], ' '));
     //add contribution record
     $contribution =& CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
     // process price set, CRM-5095
     if ($contribution->id && $form->_priceSetId) {
         require_once 'CRM/Contribute/Form/AdditionalInfo.php';
         CRM_Contribute_Form_AdditionalInfo::processPriceSet($contribution->id, $form->_lineItem);
     }
     //add soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin.
     if (CRM_Utils_Array::value('pcp_made_through_id', $params) || CRM_Utils_Array::value('soft_credit_to', $params)) {
         $contribSoftParams['contribution_id'] = $contribution->id;
         $contribSoftParams['amount'] = $params['amount'];
         //if its due to pcp
         if (CRM_Utils_Array::value('pcp_made_through_id', $params)) {
             $contribSoftParams['pcp_id'] = $params['pcp_made_through_id'];
             $contribSoftParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_PCP', $params['pcp_made_through_id'], 'contact_id');
         } else {
             $contribSoftParams['contact_id'] = CRM_Utils_Array::value('soft_credit_to', $params);
         }
         $softContribution = CRM_Contribute_BAO_Contribution::addSoftContribution($contribSoftParams);
     }
     //handle pledge stuff.
     if (!CRM_Utils_Array::value('separate_membership_payment', $form->_params) && CRM_Utils_Array::value('pledge_block_id', $form->_values) && (CRM_Utils_Array::value('is_pledge', $form->_params) || CRM_Utils_Array::value('pledge_id', $form->_values))) {
         if (CRM_Utils_Array::value('pledge_id', $form->_values)) {
             //when user doing pledge payments.
             //update the schedule when payment(s) are made
             require_once 'CRM/Pledge/BAO/Payment.php';
             foreach ($form->_params['pledge_amount'] as $paymentId => $dontCare) {
                 $pledgePaymentParams = array('id' => $paymentId, 'contribution_id' => $contribution->id, 'status_id' => $contribution->contribution_status_id);
                 CRM_Pledge_BAO_Payment::add($pledgePaymentParams);
             }
             //update pledge status according to the new payment statuses
             CRM_Pledge_BAO_Payment::updatePledgePaymentStatus($form->_values['pledge_id']);
         } else {
             //when user creating pledge record.
             $pledgeParams = array();
             $pledgeParams['contact_id'] = $contribution->contact_id;
             $pledgeParams['installment_amount'] = $contribution->total_amount;
             $pledgeParams['contribution_id'] = $contribution->id;
             $pledgeParams['contribution_page_id'] = $contribution->contribution_page_id;
             $pledgeParams['contribution_type_id'] = $contribution->contribution_type_id;
             $pledgeParams['frequency_interval'] = $params['pledge_frequency_interval'];
             $pledgeParams['installments'] = $params['pledge_installments'];
             $pledgeParams['frequency_unit'] = $params['pledge_frequency_unit'];
             $pledgeParams['frequency_day'] = 1;
             $pledgeParams['create_date'] = $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = date("Ymd");
             $pledgeParams['status_id'] = $contribution->contribution_status_id;
             $pledgeParams['max_reminders'] = $form->_values['max_reminders'];
             $pledgeParams['initial_reminder_day'] = $form->_values['initial_reminder_day'];
             $pledgeParams['additional_reminder_day'] = $form->_values['additional_reminder_day'];
             $pledgeParams['is_test'] = $contribution->is_test;
             $pledgeParams['acknowledge_date'] = date('Ymd');
             require_once 'CRM/Pledge/BAO/Pledge.php';
             $pledge = CRM_Pledge_BAO_Pledge::create($pledgeParams);
             $form->_params['pledge_id'] = $pledge->id;
             //send acknowledgment email. only when pledge is created
             if ($pledge->id) {
                 //build params to send acknowledgment.
                 $pledgeParams['id'] = $pledge->id;
                 $pledgeParams['receipt_from_name'] = $form->_values['receipt_from_name'];
                 $pledgeParams['receipt_from_email'] = $form->_values['receipt_from_email'];
                 //scheduled amount will be same as installment_amount.
                 $pledgeParams['scheduled_amount'] = $pledgeParams['installment_amount'];
                 //get total pledge amount.
                 $pledgeParams['total_pledge_amount'] = $pledge->amount;
                 require_once 'CRM/Pledge/BAO/Pledge.php';
                 CRM_Pledge_BAO_Pledge::sendAcknowledgment($form, $pledgeParams);
             }
         }
     }
     if ($online) {
         require_once 'CRM/Core/BAO/CustomValueTable.php';
         CRM_Core_BAO_CustomValueTable::postProcess($form->_params, CRM_Core_DAO::$_nullArray, 'civicrm_contribution', $contribution->id, 'Contribution');
     } else {
         //handle custom data.
         $params['contribution_id'] = $contribution->id;
         if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom']) && !is_a($contribution, 'CRM_Core_Error')) {
             require_once 'CRM/Core/BAO/CustomValueTable.php';
             CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution', $contribution->id);
         }
     }
     require_once "CRM/Contribute/BAO/Contribution/Utils.php";
     if (isset($params['related_contact'])) {
         $contactID = $params['related_contact'];
     } else {
         if (isset($params['cms_contactID'])) {
             $contactID = $params['cms_contactID'];
         }
     }
     CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email-' . $form->_bltID);
     // return if pending
     if ($pending) {
         return $contribution;
     }
     // next create the transaction record
     if ((!$online || $form->_values['is_monetary']) && $result['trxn_id']) {
         $trxnParams = array('contribution_id' => $contribution->id, 'trxn_date' => $now, 'trxn_type' => 'Debit', 'total_amount' => $params['amount'], 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']), 'currency' => $params['currencyID'], 'payment_processor' => $form->_paymentProcessor['payment_processor_type'], 'trxn_id' => $result['trxn_id'], 'trxn_result_code' => $result['trxn_result_code']);
         require_once 'CRM/Contribute/BAO/FinancialTrxn.php';
         $trxn =& CRM_Contribute_BAO_FinancialTrxn::create($trxnParams);
     }
     //create contribution activity w/ individual and target
     //activity w/ organisation contact id when onbelf, CRM-4027
     $targetContactID = null;
     if (CRM_Utils_Array::value('is_for_organization', $params)) {
         $targetContactID = $contribution->contact_id;
         $contribution->contact_id = $contactID;
     }
     // create an activity record
     require_once 'CRM/Activity/BAO/Activity.php';
     CRM_Activity_BAO_Activity::addActivity($contribution, null, $targetContactID);
     $transaction->commit();
     return $contribution;
 }
Esempio n. 4
0
 /**
  * Function to process the renewal form
  *
  * @access public
  * @return None
  */
 public function postProcess()
 {
     require_once 'CRM/Member/BAO/Membership.php';
     require_once 'CRM/Member/BAO/MembershipType.php';
     require_once 'CRM/Member/BAO/MembershipStatus.php';
     // get the submitted form values.
     $this->_params = $formValues = $this->controller->exportValues($this->_name);
     $params = array();
     $ids = array();
     $config =& CRM_Core_Config::singleton();
     $params['contact_id'] = $this->_contactID;
     if ($this->_mode) {
         $formValues['total_amount'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee');
         $formValues['contribution_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'contribution_type_id');
         require_once 'CRM/Core/BAO/PaymentProcessor.php';
         $this->_paymentProcessor = CRM_Core_BAO_PaymentProcessor::getPayment($formValues['payment_processor_id'], $this->_mode);
         require_once "CRM/Contact/BAO/Contact.php";
         $now = CRM_Utils_Date::getToday($now, 'YmdHis');
         $fields = array();
         // set email for primary location.
         $fields["email-Primary"] = 1;
         $formValues["email-5"] = $formValues["email-Primary"] = $this->_contributorEmail;
         $formValues['register_date'] = $now;
         // now set the values for the billing location.
         foreach ($this->_fields as $name => $dontCare) {
             $fields[$name] = 1;
         }
         // also add location name to the array
         $formValues["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $formValues) . ' ' . CRM_Utils_Array::value('billing_middle_name', $formValues) . ' ' . CRM_Utils_Array::value('billing_last_name', $formValues);
         $formValues["address_name-{$this->_bltID}"] = trim($formValues["address_name-{$this->_bltID}"]);
         $fields["address_name-{$this->_bltID}"] = 1;
         $fields["email-{$this->_bltID}"] = 1;
         $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type');
         $nameFields = array('first_name', 'middle_name', 'last_name');
         foreach ($nameFields as $name) {
             $fields[$name] = 1;
             if (array_key_exists("billing_{$name}", $formValues)) {
                 $formValues[$name] = $formValues["billing_{$name}"];
                 $formValues['preserveDBName'] = true;
             }
         }
         $contactID = CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields, $this->_contactID, null, null, $ctype);
         // add all the additioanl payment params we need
         $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]);
         $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]);
         $this->_params['year'] = $this->_params['credit_card_exp_date']['Y'];
         $this->_params['month'] = $this->_params['credit_card_exp_date']['M'];
         $this->_params['ip_address'] = CRM_Utils_System::ipAddress();
         $this->_params['amount'] = $formValues['total_amount'];
         $this->_params['currencyID'] = $config->defaultCurrency;
         $this->_params['payment_action'] = 'Sale';
         $this->_params['invoiceID'] = md5(uniqid(rand(), true));
         // at this point we've created a contact and stored its address etc
         // all the payment processors expect the name and address to be in the
         // so we copy stuff over to first_name etc.
         $paymentParams = $this->_params;
         if (CRM_Utils_Array::value('send_receipt', $this->_params)) {
             $paymentParams['email'] = $this->_contributorEmail;
         }
         require_once 'CRM/Core/Payment/Form.php';
         CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, true);
         $payment =& CRM_Core_Payment::singleton($this->_mode, 'Contribute', $this->_paymentProcessor, $this);
         $result =& $payment->doDirectPayment($paymentParams);
         if (is_a($result, 'CRM_Core_Error')) {
             CRM_Core_Error::displaySessionError($result);
             CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}"));
         }
         if ($result) {
             $this->_params = array_merge($this->_params, $result);
         }
         $formValues['contribution_status_id'] = 1;
         $formValues['receive_date'] = $now;
         $formValues['invoice_id'] = $this->_params['invoiceID'];
         $formValues['trxn_id'] = $result['trxn_id'];
         $formValues['payment_instrument_id'] = 1;
         $formValues['is_test'] = $this->_mode == 'live' ? 0 : 1;
         if (CRM_Utils_Array::value('send_receipt', $this->_params)) {
             $formValues['receipt_date'] = $now;
         } else {
             $formValues['receipt_date'] = null;
         }
         $this->set('params', $this->_params);
         $this->assign('trxn_id', $result['trxn_id']);
         $this->assign('receive_date', CRM_Utils_Date::mysqlToIso($formValues['receive_date']));
     }
     $renewalDate = null;
     if ($formValues['renewal_date']) {
         $this->set('renewDate', CRM_Utils_Date::processDate($formValues['renewal_date']));
     }
     $this->_membershipId = $this->_id;
     // check for test membership.
     $isTestMembership = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $this->_membershipId, 'is_test');
     $renewMembership = CRM_Member_BAO_Membership::renewMembership($this->_contactID, $this->_memType, $isTestMembership, $this, null);
     $endDate = CRM_Utils_Date::processDate($renewMembership->end_date);
     require_once 'CRM/Contact/BAO/Contact/Location.php';
     // Retrieve the name and email of the current user - this will be the FROM for the receipt email
     $session =& CRM_Core_Session::singleton();
     $userID = $session->get('userID');
     list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID);
     $memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $renewMembership->membership_type_id, 'name');
     if (CRM_Utils_Array::value('record_contribution', $formValues) || $this->_mode) {
         //building contribution params
         $contributionParams = array();
         $config =& CRM_Core_Config::singleton();
         $contributionParams['currency'] = $config->defaultCurrency;
         $contributionParams['contact_id'] = $params['contact_id'];
         $contributionParams['source'] = "{$memType} Membership: Offline membership renewal (by {$userName})";
         $contributionParams['non_deductible_amount'] = 'null';
         $contributionParams['receive_date'] = date('Y-m-d H:i:s');
         $contributionParams['receipt_date'] = CRM_Utils_Array::value('send_receipt', $formValues) ? $contributionParams['receive_date'] : 'null';
         $recordContribution = array('total_amount', 'contribution_type_id', 'payment_instrument_id', 'trxn_id', 'contribution_status_id', 'invoice_id', 'check_number', 'is_test');
         foreach ($recordContribution as $f) {
             $contributionParams[$f] = CRM_Utils_Array::value($f, $formValues);
         }
         require_once 'CRM/Contribute/BAO/Contribution.php';
         $contribution =& CRM_Contribute_BAO_Contribution::create($contributionParams, $ids);
         require_once 'CRM/Member/DAO/MembershipPayment.php';
         $mpDAO =& new CRM_Member_DAO_MembershipPayment();
         $mpDAO->membership_id = $renewMembership->id;
         $mpDAO->contribution_id = $contribution->id;
         $mpDAO->save();
         if ($this->_mode) {
             $trxnParams = array('contribution_id' => $contribution->id, 'trxn_date' => $now, 'trxn_type' => 'Debit', 'total_amount' => $formValues['total_amount'], 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $formValues['total_amount']), 'currency' => $config->defaultCurrency, 'payment_processor' => $this->_paymentProcessor['payment_processor_type'], 'trxn_id' => $result['trxn_id']);
             require_once 'CRM/Contribute/BAO/FinancialTrxn.php';
             $trxn =& CRM_Contribute_BAO_FinancialTrxn::create($trxnParams);
         }
     }
     if (CRM_Utils_Array::value('send_receipt', $formValues)) {
         require_once 'CRM/Core/DAO.php';
         CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipType', CRM_Utils_Array::value('membership_type_id', $params), 'receipt_text_renewal', $formValues['receipt_text_renewal']);
     }
     $receiptSend = false;
     if (CRM_Utils_Array::value('send_receipt', $formValues)) {
         $receiptSend = true;
         // Retrieve the name and email of the contact - this will be the TO for receipt email
         list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
         $receiptFrom = '"' . $userName . '" <' . $userEmail . '>';
         $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
         $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']];
         //get the group Tree
         $this->_groupTree =& CRM_Core_BAO_CustomGroup::getTree('Membership', $this, $this->_id, false, $this->_memType);
         // retrieve custom data
         require_once "CRM/Core/BAO/UFGroup.php";
         $customFields = $customValues = $fo = array();
         foreach ($this->_groupTree as $groupID => $group) {
             if ($groupID == 'info') {
                 continue;
             }
             foreach ($group['fields'] as $k => $field) {
                 $field['title'] = $field['label'];
                 $customFields["custom_{$k}"] = $field;
             }
         }
         CRM_Core_BAO_UFGroup::getValues($this->_contactID, $customFields, $customValues, false, array(array('member_id', '=', $renewMembership->id, 0, 0)));
         $this->assign_by_ref('formValues', $formValues);
         $this->assign('receive_date', $renewalDate);
         $this->assign('module', 'Membership');
         $this->assign('receiptType', 'membership renewal');
         $this->assign('mem_start_date', CRM_Utils_Date::customFormat($renewMembership->start_date));
         $this->assign('mem_end_date', CRM_Utils_Date::customFormat($renewMembership->end_date));
         $this->assign('membership_name', CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $renewMembership->membership_type_id));
         $this->assign('customValues', $customValues);
         if ($this->_mode) {
             if (CRM_Utils_Array::value('billing_first_name', $this->_params)) {
                 $name = $this->_params['billing_first_name'];
             }
             if (CRM_Utils_Array::value('billing_middle_name', $this->_params)) {
                 $name .= " {$this->_params['billing_middle_name']}";
             }
             if (CRM_Utils_Array::value('billing_last_name', $this->_params)) {
                 $name .= " {$this->_params['billing_last_name']}";
             }
             $this->assign('billingName', $name);
             // assign the address formatted up for display
             $addressParts = array("street_address-{$this->_bltID}", "city-{$this->_bltID}", "postal_code-{$this->_bltID}", "state_province-{$this->_bltID}", "country-{$this->_bltID}");
             $addressFields = array();
             foreach ($addressParts as $part) {
                 list($n, $id) = explode('-', $part);
                 if (isset($this->_params['billing_' . $part])) {
                     $addressFields[$n] = $this->_params['billing_' . $part];
                 }
             }
             require_once 'CRM/Utils/Address.php';
             $this->assign('address', CRM_Utils_Address::format($addressFields));
             $date = CRM_Utils_Date::format($this->_params['credit_card_exp_date']);
             $date = CRM_Utils_Date::mysqlToIso($date);
             $this->assign('credit_card_exp_date', $date);
             $this->assign('credit_card_number', CRM_Utils_System::mungeCreditCard($this->_params['credit_card_number']));
             $this->assign('credit_card_type', $this->_params['credit_card_type']);
             $this->assign('contributeMode', 'direct');
             $this->assign('isAmountzero', 0);
             $this->assign('is_pay_later', 0);
             $this->assign('isPrimary', 1);
             if ($this->_mode == 'test') {
                 $this->assign('action', '1024');
             }
         }
         require_once 'CRM/Core/BAO/MessageTemplates.php';
         list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate(array('groupName' => 'msg_tpl_workflow_membership', 'valueName' => 'membership_offline_receipt', 'contactId' => $this->_contactID, 'from' => $receiptFrom, 'toName' => $this->_contributorDisplayName, 'toEmail' => $this->_contributorEmail, 'isTest' => $this->_mode == 'test'));
     }
     $statusMsg = ts('%1 membership for %2 has been renewed.', array(1 => $memType, 2 => $this->_contributorDisplayName));
     $endDate = CRM_Utils_Date::customFormat(CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $this->_id, "end_date"));
     if ($endDate) {
         $statusMsg .= ' ' . ts('The new membership End Date is %1.', array(1 => $endDate));
     }
     if ($receiptSend && $mailSend) {
         $statusMsg .= ' ' . ts('A renewal confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail));
     }
     CRM_Core_Session::setStatus($statusMsg);
 }
Esempio n. 5
0
 /**
  * Process the contribution
  *
  * @return void
  * @access public
  */
 static function processContribution(&$form, $params, $result, $contactID, $pending = false, $isAdditionalAmount = false)
 {
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $config =& CRM_Core_Config::singleton();
     $now = date('YmdHis');
     $receiptDate = null;
     if ($form->_values['event']['is_email_confirm']) {
         $receiptDate = $now;
     }
     //CRM-4196
     if ($isAdditionalAmount) {
         $params['amount_level'] = $params['amount_level'] . ts(' (multiple participants)') . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR;
     }
     $contribParams = array('contact_id' => $contactID, 'contribution_type_id' => $form->_values['event']['contribution_type_id'] ? $form->_values['event']['contribution_type_id'] : $params['contribution_type_id'], 'receive_date' => $now, 'total_amount' => $params['amount'], 'amount_level' => $params['amount_level'], 'invoice_id' => $params['invoiceID'], 'currency' => $params['currencyID'], 'source' => $params['description'], 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0));
     if (!CRM_Utils_Array::value('is_pay_later', $params)) {
         $contribParams['payment_instrument_id'] = 1;
     }
     if (!$pending && $result) {
         $contribParams += array('fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']), 'trxn_id' => $result['trxn_id'], 'receipt_date' => $receiptDate);
     }
     require_once 'CRM/Contribute/PseudoConstant.php';
     $allStatuses = CRM_Contribute_PseudoConstant::contributionStatus();
     $contribParams["contribution_status_id"] = array_search('Completed', $allStatuses);
     if ($pending) {
         $contribParams["contribution_status_id"] = array_search('Pending', $allStatuses);
     }
     $contribParams["is_test"] = 0;
     if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
         $contribParams["is_test"] = 1;
     }
     $contribID = null;
     if (CRM_Utils_Array::value('invoice_id', $contribParams)) {
         $contribID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contribParams['invoice_id'], 'id', 'invoice_id');
     }
     $ids = array();
     if ($contribID) {
         $ids['contribution'] = $contribID;
         $contribParams['id'] = $contribID;
     }
     require_once 'CRM/Contribute/BAO/Contribution.php';
     //create an contribution address
     if ($form->_contributeMode != 'notify' && !CRM_Utils_Array::value('is_pay_later', $params)) {
         $contribParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID);
     }
     // create contribution record
     $contribution =& CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
     // return if pending
     if ($pending || $contribution->total_amount == 0) {
         $transaction->commit();
         return $contribution;
     }
     // next create the transaction record
     $trxnParams = array('contribution_id' => $contribution->id, 'trxn_date' => $now, 'trxn_type' => 'Debit', 'total_amount' => $params['amount'], 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']), 'currency' => $params['currencyID'], 'payment_processor' => $form->_paymentProcessor['payment_processor_type'], 'trxn_id' => $result['trxn_id']);
     require_once 'CRM/Contribute/BAO/FinancialTrxn.php';
     $trxn =& CRM_Contribute_BAO_FinancialTrxn::create($trxnParams);
     $transaction->commit();
     return $contribution;
 }
Esempio n. 6
0
 function _process_csv($csv_array, $csv_name, $mail_date)
 {
     $this->_addToSummary("Processing {$csv_name}");
     foreach ($csv_array as $row) {
         $this->_addToSummary(null);
         // insert blank line.
         // check if this is a blank line
         if (count($row) <= 1) {
             continue;
         }
         $subscriptionId = $row[0];
         $subscriptionStatus = $row[1];
         $paymentNum = $row[2];
         $totalRecurrences = $row[3];
         $transactionId = $row[4];
         $amount = $row[5];
         $currency = $row[6];
         $custFirstName = $row[8];
         $custLastName = $row[9];
         $contributionStatus = $row[10];
         $recur =& new CRM_Contribute_DAO_ContributionRecur();
         $first_contribution =& new CRM_Contribute_DAO_Contribution();
         // If this is the first payment, load recurring contribution and update
         if ($paymentNum == 1) {
             // Load contribution using SubscriptionID as trxn_id
             $first_contribution->trxn_id = $subscriptionId;
             if (!$first_contribution->find(true)) {
                 $this->_addToSummary("THE RECURRING TRANSACTION FOR SUBSCRIPTION {$subscriptionId} COULD NOT BE FOUND. A TRANSACTION HAS OCCURED THAT WAS NOT EXPECTED.  PLEASE REVIEW {$csv_name}.");
                 continue;
             }
             // Load recurring contribution from contribution
             $recur->id = $first_contribution->contribution_recur_id;
             if (!$recur->find(true)) {
                 $this->_addToSummary("INITIAL RECURRING CONTRIBUTION NOT FOUND FOR {$subscriptionId}. PLEASE REVIEW {$csv_name}");
                 continue;
             }
             $recur->start_date = $mail_date;
             $recur->processor_id = $subscriptionId;
             $recur->trxn_id = $subscriptionId;
             $recur->contribution_status_id = _CRM_PROCESS_AUTHORIZE_REPORT_STATUS_CURRENT;
             // update transaction id for contribution
             $first_contribution->trxn_id = $transactionId;
             $first_contribution->receive_date = $mail_date;
             $first_contribution->contribution_status_id = $this->_get_contribution_status($contributionStatus);
             // load contribution page
             $contribution_page =& new CRM_Contribute_DAO_ContributionPage();
             $contribution_page->id = $first_contribution->contribution_page_id;
             if (!$contribution_page->find(true)) {
                 $this->_addToSummary("COULD NOT FIND CONTRIBUTION PAGE FOR {$subscriptionId}. PLEASE REVIEW {$csv_name}");
                 continue;
             }
             // is there an email receipt
             if ($contribution_page->is_email_receipt) {
                 $first_contribution->receipt_date = date('YmdHis');
             }
         } else {
             $recur->processor_id = $subscriptionId;
             if (!$recur->find(true)) {
                 $this->_addToSummary("THE RECURRING TRANSACTION FOR SUBSCRIPTION {$subscriptionId} COULD NOT BE FOUND. A TRANSACTION HAS OCCURED THAT WAS NOT EXPECTED.  PLEASE REVIEW {$csv_name}.");
                 continue;
             }
             $recur->modified_date = $mail_date;
             // load first contribution
             $first_contribution->contribution_recur_id = $recur->id;
             $first_contribution->orderBy('receive_date');
             $first_contribution->limit(1);
             if (!$first_contribution->find(true)) {
                 $this->_addToSummary("CONTRIBUTION RECORD FOR SUBSCRIPTION {$subscriptonId} COULD NOT BE FOUND.  PLEASE REVIEW {$csv_name}");
                 continue;
             }
             // load contribution page
             $contribution_page =& new CRM_Contribute_DAO_ContributionPage();
             $contribution_page->id = $first_contribution->contribution_page_id;
             if (!$contribution_page->find(true)) {
                 $this->_addToSummary("COULD NOT FIND CONTRIBUTION PAGE FOR {$subscriptionId}. PLEASE REVIEW {$csv_name}");
                 continue;
             }
         }
         // is this valid for failed transactions also?
         if ($amount != $recur->amount) {
             $this->_addToSummary("AN UNEXPECTED AMOUNT WAS RECEIVED FOR SUBSCRIPTION {$subscriptionId}. SKIPPING THIS TRANSACTION. PLEASE REVIEW {$csv_name}");
             continue;
         }
         // Verify contact exists
         if (!$recur->contact_id) {
             // assuming if contact_id is set, contact exists
             $this->_addToSummary("NO USER IS ASSOCIATED WITH THE CONTRIBUTION FOR SUBSCRIPTION {$subscrptionId}, EXPECTED '{$custFirstName} {$custLastName}'. PLEASE REVIEW {$csv_name}");
             continue;
         }
         // Verify number of recurrences
         if ($recur->installments != $totalRecurrences) {
             $this->_addToSummary("SUBSCRIPTION {$subscriptionId} EXPECTS {$recur->installments}, OFFERED {$totalRecurrences}. PLEASE REVIEW {$csv_name}");
             continue;
         }
         // Check if this contribution is complete
         if (!empty($recur->end_date) && $recur->end_date != '0000-00-00 00:00:00') {
             $this->_addToSummary("SUBSCRIPTION {$subscriptionId} IS MARKED AS COMPLETE. PLEASE REVIEW {$csv_name}");
             continue;
         }
         if (!empty($recur->cancel_date) && $recur->cancel_date != '0000-00-00 00:00:00') {
             $this->_addToSummary("SUBSCRIPTION {$subscriptionId} IS MARKED AS CANCELLED. PLEASE REVIEW {$csv_name}");
             continue;
         }
         if ($paymentNum == $totalRecurrences) {
             $recur->end_date = $mail_date;
             $recur->contribution_status_id = _CRM_PROCESS_AUTHORIZE_REPORT_STATUS_COMPLETE;
         }
         if ($contributionStatus != CRM_Core_Payment_AuthorizeNet::AUTH_APPROVED) {
             $recur->failure_count++;
         }
         CRM_Core_DAO::transaction('BEGIN');
         if (!$recur->save()) {
             $this->_addToSummary("THE RECURRING CONTRIBUTION COULD NOT BE UPDATED. PLEASE REVIEW {$csv_name} FOR subscription_id={$subscription_id}");
             CRM_Core_DAO::transaction('ROLLBACK');
             continue;
         }
         $this->_addToSummary("The recurring transaction has been updated.");
         if ($paymentNum == 1) {
             // update first contribution
             if (!$first_contribution->save()) {
                 $this->_addToSummary("THE CONTRIBUTION COULD NOT BE UPDATED. PLEASE REVIEW {$csv_name} FOR subscription_id={$subscription_id}");
                 CRM_Core_DAO::transaction('ROLLBACK');
                 continue;
             }
             // copy $first_contribution to $contribution for use later
             $contribution = $first_contribution;
         } else {
             // create a contribution and then get it processed
             $contribution =& new CRM_Contribute_DAO_Contribution();
             // make sure that the transaction doesn't already exist
             $contribution->trxn_id = $transactionId;
             if ($contribution->find()) {
                 $this->_addToSummary("THE TRANSACTION {$transaction_id} ALREADY EXISTS IN CIVICRM. PLEASE REVIEW {$csv_name} FOR subscription_id={$subscription_id}");
                 CRM_Core_DAO::transaction('ROLLBACK');
                 continue;
             }
             $contribution->contribution_recur_id = $recur->id;
             $contribution->receive_date = $mail_date;
             $contribution->total_amount = $amount;
             $contribution->net_amount = $amount;
             $contribution->trxn_id = $transactionId;
             $contribution->currency = $currency;
             $contribution->contribution_status_id = $this->_get_contribution_status($contributionStatus);
             $contribution->contact_id = $first_contribution->contact_id;
             $contribution->contribution_type_id = $first_contribution->contribution_type_id;
             $contribution->contribution_page_id = $first_contribution->contribution_page_id;
             $contribution->payment_instrument_id = $first_contribution->payment_instrument_id;
             $contribution->is_test = $first_contribution->is_test;
             $contribution->invoice_id = md5(uniqid(rand(), true));
             if ($contribution_page->is_email_receipt) {
                 $contribution->receipt_date = date('YmdHis');
             }
             if (!$contribution->save()) {
                 $this->_addToSummary("THE CONTRIBUTION COULD NOT BE SAVED. PLEASE REVIEW {$csv_name} FOR subscription_id={$subscription_id}");
                 CRM_Core_DAO::transaction('ROLLBACK');
                 continue;
             }
         }
         $this->_addToSummary('Contribution saved');
         // create the transaction record
         $trxnParams = array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution->id, 'trxn_date' => $mail_date, 'trxn_type' => 'Debit', 'total_amount' => $amount, 'fee_amount' => $contribution->fee_amount, 'net_amount' => $contribution->net_amount, 'currency' => $contribution->currency, 'payment_processor' => 'AuthNet_AIM', 'trxn_id' => $contribution->trxn_id);
         require_once 'CRM/Contribute/BAO/FinancialTrxn.php';
         $trxn =& CRM_Contribute_BAO_FinancialTrxn::create($trxnParams);
         if (is_a($trxn, 'CRM_Core_Error')) {
             $this->_addToSummary("A TRANSACTION RECORD COULD NOT BE CREATED. PLEASE REVIEW {$csv_name} FOR subscription_id={$subscription_id}");
             CRM_Core_DAO::transaction('ROLLBACK');
             continue;
         } else {
             $this->_addToSummary("Transaction record created.");
         }
         // get the title of the contribution page
         $title = $contribution_page->title;
         // format the money
         require_once 'CRM/Utils/Money.php';
         $formattedAmount = CRM_Utils_Money::format($amount, $contribution->currency);
         CRM_Core_DAO::transaction('COMMIT');
         // get the contribution type
         require_once 'CRM/Contribute/BAO/ContributionType.php';
         $contribution_type_name = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionType', $contribution->contribution_type_id, 'name');
         // create an activity history record
         $ahParams = array('entity_table' => 'civicrm_contact', 'entity_id' => $recur->contact_id, 'activity_type' => $contribution_type_name, 'module' => 'CiviContribute', 'callback' => 'CRM_Contribute_Page_Contribution::details', 'activity_id' => $contribution->id, 'activity_summary' => "{$formattedAmount} - {$title} (online)", 'activity_date' => $mail_date);
         require_once 'api/History.php';
         if (is_a(crm_create_activity_history($ahParams), 'CRM_Core_Error')) {
             $this->_addToSummary("AN ACTIVITY HISTORY RECORD COULD NOT BE CREATED.");
         } else {
             $this->_addToSummary("Activity History record created.");
         }
         $this->_addToSummary("Transaction {$transactionId} has been processed.");
         $first_contribution->free();
         $contribution_page->free();
         $contribution->free();
         $recur->free();
         $trxn->free();
     }
     $this->_addToSummary("Done processing {$csv_name}");
     $this->_addToSummary('');
 }
Esempio n. 7
0
 /**
  * Function to process the form
  *
  * @access public
  * @return None
  */
 public function postProcess()
 {
     require_once 'CRM/Member/BAO/Membership.php';
     require_once 'CRM/Member/BAO/MembershipType.php';
     require_once 'CRM/Member/BAO/MembershipStatus.php';
     if ($this->_action & CRM_Core_Action::DELETE) {
         CRM_Member_BAO_Membership::deleteRelatedMemberships($this->_id);
         CRM_Member_BAO_Membership::deleteMembership($this->_id);
         return;
     }
     $config =& CRM_Core_Config::singleton();
     // get the submitted form values.
     $this->_params = $formValues = $this->controller->exportValues($this->_name);
     $params = array();
     $ids = array();
     // set the contact, when contact is selected
     if (CRM_Utils_Array::value('contact_select_id', $formValues)) {
         $this->_contactID = CRM_Utils_Array::value('contact_select_id', $formValues);
     }
     $params['contact_id'] = $this->_contactID;
     // we need to retrieve email address
     if ($this->_context == 'standalone' && CRM_Utils_Array::value('send_receipt', $formValues)) {
         require_once 'CRM/Contact/BAO/Contact/Location.php';
         list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
     }
     $fields = array('status_id', 'source', 'is_override');
     foreach ($fields as $f) {
         $params[$f] = CRM_Utils_Array::value($f, $formValues);
     }
     // fix for CRM-3724
     // when is_override false ignore is_admin statuses during membership
     // status calculation. similarly we did fix for import in CRM-3570.
     if (!CRM_Utils_Array::value('is_override', $params)) {
         $params['exclude_is_admin'] = true;
     }
     $params['membership_type_id'] = $formValues['membership_type_id'][1];
     $joinDate = CRM_Utils_Date::processDate($formValues['join_date']);
     $startDate = CRM_Utils_Date::processDate($formValues['start_date']);
     $endDate = CRM_Utils_Date::processDate($formValues['end_date']);
     $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($params['membership_type_id'], $joinDate, $startDate, $endDate);
     $dates = array('join_date', 'start_date', 'end_date', 'reminder_date', 'receive_date');
     $currentTime = getDate();
     foreach ($dates as $d) {
         if (isset($formValues[$d]) && !CRM_Utils_System::isNull($formValues[$d])) {
             $params[$d] = CRM_Utils_Date::processDate($formValues[$d]);
         } else {
             if (isset($calcDates[$d])) {
                 $params[$d] = CRM_Utils_Date::processDate($calcDates[$d]);
             }
         }
     }
     if ($this->_id) {
         $ids['membership'] = $params['id'] = $this->_id;
     }
     $session = CRM_Core_Session::singleton();
     $ids['userId'] = $session->get('userID');
     // membership type custom data
     $customFields = CRM_Core_BAO_CustomField::getFields('Membership', false, false, CRM_Utils_Array::value('membership_type_id', $params));
     $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFields('Membership', false, false, null, null, true));
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($formValues, $customFields, $this->_id, 'Membership');
     // Retrieve the name and email of the current user - this will be the FROM for the receipt email
     require_once 'CRM/Contact/BAO/Contact/Location.php';
     list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']);
     if (CRM_Utils_Array::value('record_contribution', $formValues)) {
         $recordContribution = array('total_amount', 'contribution_type_id', 'payment_instrument_id', 'trxn_id', 'contribution_status_id', 'check_number');
         foreach ($recordContribution as $f) {
             $params[$f] = CRM_Utils_Array::value($f, $formValues);
         }
         $membershipType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $formValues['membership_type_id'][1]);
         if (!$this->_onlinePendingContributionId) {
             $params['contribution_source'] = "{$membershipType} Membership: Offline membership signup (by {$userName})";
         }
         if ($formValues['send_receipt']) {
             $params['receipt_date'] = $params['receive_date'];
         }
         //insert contribution type name in receipt.
         $formValues['contributionType_name'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionType', $formValues['contribution_type_id']);
     }
     if ($this->_mode) {
         $params['total_amount'] = $formValues['total_amount'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $params['membership_type_id'], 'minimum_fee');
         $params['contribution_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $params['membership_type_id'], 'contribution_type_id');
         require_once 'CRM/Core/BAO/PaymentProcessor.php';
         $this->_paymentProcessor = CRM_Core_BAO_PaymentProcessor::getPayment($formValues['payment_processor_id'], $this->_mode);
         require_once "CRM/Contact/BAO/Contact.php";
         $now = date('YmdHis');
         $fields = array();
         // set email for primary location.
         $fields["email-Primary"] = 1;
         $formValues["email-5"] = $formValues["email-Primary"] = $this->_contributorEmail;
         $params['register_date'] = $now;
         // now set the values for the billing location.
         foreach ($this->_fields as $name => $dontCare) {
             $fields[$name] = 1;
         }
         // also add location name to the array
         $formValues["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $formValues) . ' ' . CRM_Utils_Array::value('billing_middle_name', $formValues) . ' ' . CRM_Utils_Array::value('billing_last_name', $formValues);
         $formValues["address_name-{$this->_bltID}"] = trim($formValues["address_name-{$this->_bltID}"]);
         $fields["address_name-{$this->_bltID}"] = 1;
         $fields["email-{$this->_bltID}"] = 1;
         $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type');
         $nameFields = array('first_name', 'middle_name', 'last_name');
         foreach ($nameFields as $name) {
             $fields[$name] = 1;
             if (array_key_exists("billing_{$name}", $formValues)) {
                 $formValues[$name] = $formValues["billing_{$name}"];
                 $formValues['preserveDBName'] = true;
             }
         }
         $contactID = CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields, $this->_contactID, null, null, $ctype);
         // add all the additioanl payment params we need
         $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]);
         $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]);
         $this->_params['year'] = $this->_params['credit_card_exp_date']['Y'];
         $this->_params['month'] = $this->_params['credit_card_exp_date']['M'];
         $this->_params['ip_address'] = CRM_Utils_System::ipAddress();
         $this->_params['amount'] = $params['total_amount'];
         $this->_params['currencyID'] = $config->defaultCurrency;
         $this->_params['payment_action'] = 'Sale';
         $this->_params['invoiceID'] = md5(uniqid(rand(), true));
         // at this point we've created a contact and stored its address etc
         // all the payment processors expect the name and address to be in the
         // so we copy stuff over to first_name etc.
         $paymentParams = $this->_params;
         if (CRM_Utils_Array::value('send_receipt', $this->_params)) {
             $paymentParams['email'] = $this->_contributorEmail;
         }
         require_once 'CRM/Core/Payment/Form.php';
         CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, true);
         $payment =& CRM_Core_Payment::singleton($this->_mode, 'Contribute', $this->_paymentProcessor, $this);
         $result =& $payment->doDirectPayment($paymentParams);
         if (is_a($result, 'CRM_Core_Error')) {
             CRM_Core_Error::displaySessionError($result);
             CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/membership', "reset=1&action=add&cid={$this->_contactID}&context=&mode={$this->_mode}"));
         }
         if ($result) {
             $this->_params = array_merge($this->_params, $result);
         }
         $params['contribution_status_id'] = 1;
         $params['receive_date'] = $now;
         $params['invoice_id'] = $this->_params['invoiceID'];
         $params['contribution_source'] = ts('Online Membership: Admin Interface');
         $params['source'] = $formValues['source'] ? $formValues['source'] : $params['contribution_source'];
         $params['trxn_id'] = $result['trxn_id'];
         $params['payment_instrument_id'] = 1;
         $params['is_test'] = $this->_mode == 'live' ? 0 : 1;
         if (CRM_Utils_Array::value('send_receipt', $this->_params)) {
             $params['receipt_date'] = $now;
         } else {
             $params['receipt_date'] = null;
         }
         $this->set('params', $this->_params);
         $this->assign('trxn_id', $result['trxn_id']);
         $this->assign('receive_date', CRM_Utils_Date::mysqlToIso($params['receive_date']));
         // required for creating membership for related contacts
         $params['action'] = $this->_action;
         $membership =& CRM_Member_BAO_Membership::create($params, $ids);
         $contribution = new CRM_Contribute_BAO_Contribution();
         $contribution->trxn_id = $result['trxn_id'];
         if ($contribution->find(true)) {
             // next create the transaction record
             $trxnParams = array('contribution_id' => $contribution->id, 'trxn_date' => $now, 'trxn_type' => 'Debit', 'total_amount' => $params['total_amount'], 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['total_amount']), 'currency' => $config->defaultCurrency, 'payment_processor' => $this->_paymentProcessor['payment_processor_type'], 'trxn_id' => $result['trxn_id']);
             require_once 'CRM/Contribute/BAO/FinancialTrxn.php';
             $trxn =& CRM_Contribute_BAO_FinancialTrxn::create($trxnParams);
         }
     } else {
         $params['action'] = $this->_action;
         if ($this->_onlinePendingContributionId && CRM_Utils_Array::value('record_contribution', $formValues)) {
             // update membership as well as contribution object, CRM-4395
             require_once 'CRM/Contribute/Form/Contribution.php';
             $params['contribution_id'] = $this->_onlinePendingContributionId;
             $params['componentId'] = $params['id'];
             $params['componentName'] = 'contribute';
             $result = CRM_Contribute_BAO_Contribution::transitionComponents($params, true);
             //carry updated membership object.
             $membership =& new CRM_Member_DAO_Membership();
             $membership->id = $this->_id;
             $membership->find(true);
             $cancelled = true;
             if ($membership->end_date) {
                 //display end date w/ status message.
                 $endDate = $membership->end_date;
                 require_once 'CRM/Member/PseudoConstant.php';
                 $membershipStatues = CRM_Member_PseudoConstant::membershipStatus();
                 if (!in_array($membership->status_id, array(array_search('Cancelled', $membershipStatues), array_search('Expired', $membershipStatues)))) {
                     $cancelled = false;
                 }
             }
             // suppress form values in template.
             $this->assign('cancelled', $cancelled);
         } else {
             $membership =& CRM_Member_BAO_Membership::create($params, $ids);
         }
     }
     if (CRM_Utils_Array::value('send_receipt', $formValues)) {
         require_once 'CRM/Core/DAO.php';
         CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipType', $params['membership_type_id'], 'receipt_text_signup', $formValues['receipt_text_signup']);
     }
     $receiptSend = false;
     if (CRM_Utils_Array::value('send_receipt', $formValues)) {
         $receiptSend = true;
         $receiptFrom = "{$userName} <{$userEmail}>";
         if (CRM_Utils_Array::value('payment_instrument_id', $formValues)) {
             $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument();
             $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']];
         }
         // retrieve custom data
         require_once "CRM/Core/BAO/UFGroup.php";
         $customFields = $customValues = array();
         foreach ($this->_groupTree as $groupID => $group) {
             if ($groupID == 'info') {
                 continue;
             }
             foreach ($group['fields'] as $k => $field) {
                 $field['title'] = $field['label'];
                 $customFields["custom_{$k}"] = $field;
             }
         }
         $members = array(array('member_id', '=', $membership->id, 0, 0));
         // check whether its a test drive
         if ($this->_mode) {
             $members[] = array('member_test', '=', 1, 0, 0);
         }
         CRM_Core_BAO_UFGroup::getValues($this->_contactID, $customFields, $customValues, false, $members);
         if ($this->_mode) {
             if (CRM_Utils_Array::value('billing_first_name', $this->_params)) {
                 $name = $this->_params['billing_first_name'];
             }
             if (CRM_Utils_Array::value('billing_middle_name', $this->_params)) {
                 $name .= " {$this->_params['billing_middle_name']}";
             }
             if (CRM_Utils_Array::value('billing_last_name', $this->_params)) {
                 $name .= " {$this->_params['billing_last_name']}";
             }
             $this->assign('billingName', $name);
             // assign the address formatted up for display
             $addressParts = array("street_address-{$this->_bltID}", "city-{$this->_bltID}", "postal_code-{$this->_bltID}", "state_province-{$this->_bltID}", "country-{$this->_bltID}");
             $addressFields = array();
             foreach ($addressParts as $part) {
                 list($n, $id) = explode('-', $part);
                 if (isset($this->_params['billing_' . $part])) {
                     $addressFields[$n] = $this->_params['billing_' . $part];
                 }
             }
             require_once 'CRM/Utils/Address.php';
             $this->assign('address', CRM_Utils_Address::format($addressFields));
             $date = CRM_Utils_Date::format($this->_params['credit_card_exp_date']);
             $date = CRM_Utils_Date::mysqlToIso($date);
             $this->assign('credit_card_exp_date', $date);
             $this->assign('credit_card_number', CRM_Utils_System::mungeCreditCard($this->_params['credit_card_number']));
             $this->assign('credit_card_type', $this->_params['credit_card_type']);
             $this->assign('contributeMode', 'direct');
             $this->assign('isAmountzero', 0);
             $this->assign('is_pay_later', 0);
             $this->assign('isPrimary', 1);
         }
         $this->assign('module', 'Membership');
         $this->assign('contactID', $this->_contactID);
         $this->assign('membershipID', $params['membership_id']);
         $this->assign('receiptType', 'membership signup');
         $this->assign('receive_date', $params['receive_date']);
         $this->assign('formValues', $formValues);
         $this->assign('mem_start_date', CRM_Utils_Date::customFormat($calcDates['start_date']));
         $this->assign('mem_end_date', CRM_Utils_Date::customFormat($calcDates['end_date']));
         $this->assign('membership_name', CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $formValues['membership_type_id'][1]));
         $this->assign('customValues', $customValues);
         require_once 'CRM/Core/BAO/MessageTemplates.php';
         list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate(array('groupName' => 'msg_tpl_workflow_contribution', 'valueName' => 'contribution_offline_receipt', 'contactId' => $this->_contactID, 'from' => $receiptFrom, 'toName' => $this->_contributorDisplayName, 'toEmail' => $this->_contributorEmail, 'isTest' => (bool) ($this->_action & CRM_Core_Action::PREVIEW)));
     }
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $statusMsg = ts('Membership for %1 has been updated.', array(1 => $this->_contributorDisplayName));
         if ($endDate) {
             $endDate = CRM_Utils_Date::customFormat($endDate);
             $statusMsg .= ' ' . ts('The membership End Date is %1.', array(1 => $endDate));
         }
         if ($receiptSend) {
             $statusMsg .= ' ' . ts('A confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail));
         }
     } elseif ($this->_action & CRM_Core_Action::ADD) {
         require_once 'CRM/Core/DAO.php';
         $memType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $params['membership_type_id']);
         $statusMsg = ts('%1 membership for %2 has been added.', array(1 => $memType, 2 => $this->_contributorDisplayName));
         //get the end date from calculated dates.
         $endDate = $endDate ? $endDate : CRM_Utils_Array::value('end_date', $calcDates);
         if ($endDate) {
             $endDate = CRM_Utils_Date::customFormat($endDate);
             $statusMsg .= ' ' . ts('The new membership End Date is %1.', array(1 => $endDate));
         }
         if ($receiptSend && $mailSend) {
             $statusMsg .= ' ' . ts('A membership confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail));
         }
     }
     CRM_Core_Session::setStatus($statusMsg);
     $buttonName = $this->controller->getButtonName();
     if ($this->_context == 'standalone') {
         if ($buttonName == $this->getButtonName('upload', 'new')) {
             $session->replaceUserContext(CRM_Utils_System::url('civicrm/member/add', 'reset=1&action=add&context=standalone'));
         } else {
             $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_contactID}&selectedChild=member"));
         }
     } else {
         if ($buttonName == $this->getButtonName('upload', 'new')) {
             $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/membership', "reset=1&action=add&context=membership&cid={$this->_contactID}"));
         }
     }
 }
Esempio n. 8
0
 /**
  * Process the form
  *
  * @return void
  * @access public
  */
 function postProcess()
 {
     $contactID = $this->get('contactID');
     if (!$contactID) {
         // make a copy of params so we dont destroy our params
         // (since we pass this by reference)
         $premiumParams = $params = $this->_params;
         // so now we have a confirmed financial transaction
         // lets create or update a contact first
         require_once 'api/crm.php';
         $ids = CRM_Core_BAO_UFGroup::findContact($params);
         $contactsIDs = explode(',', $ids);
         // if we find more than one contact, use the first one
         $contact_id = $contactsIDs[0];
         $contact = null;
         if ($contact_id) {
             $contact =& crm_get_contact(array('contact_id' => $contact_id));
         }
         $ids = array();
         if (!$contact || !is_a($contact, 'CRM_Contact_BAO_Contact')) {
             $contact =& CRM_Contact_BAO_Contact::createFlat($params, $ids);
         } else {
             // need to fix and unify all contact creation
             $idParams = array('id' => $contact_id, 'contact_id' => $contact_id);
             $defaults = array();
             CRM_Contact_BAO_Contact::retrieve($idParams, $defaults, $ids);
             $contact =& CRM_Contact_BAO_Contact::createFlat($params, $ids);
         }
         if (is_a($contact, 'CRM_Core_Error')) {
             CRM_Core_Error::fatal("Failed creating contact for contributor");
         }
         $contactID = $contact->id;
         $this->set('contactID', $contactID);
     }
     $contributionType =& new CRM_Contribute_DAO_ContributionType();
     $contributionType->id = $this->_values['contribution_type_id'];
     if (!$contributionType->find(true)) {
         CRM_Core_Error::fatal("Could not find a system table");
     }
     // add some contribution type details to the params list
     // if folks need to use it
     $this->_params['contributionType_name'] = $contributionType->name;
     $this->_params['contributionType_accounting_code'] = $contributionType->accounting_code;
     $this->_params['contributionForm_id'] = $this->_values['id'];
     require_once 'CRM/Contribute/Payment.php';
     $payment =& CRM_Contribute_Payment::singleton($this->_mode);
     if ($this->_contributeMode == 'express') {
         $result =& $payment->doExpressCheckout($this->_params);
     } else {
         $result =& $payment->doDirectPayment($this->_params);
     }
     if (is_a($result, 'CRM_Core_Error')) {
         CRM_Core_Error::displaySessionError($result);
         CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', '_qf_Main_display=true'));
     }
     $now = date('YmdHis');
     $this->_params = array_merge($this->_params, $result);
     $this->_params['receive_date'] = $now;
     $this->set('params', $this->_params);
     $this->assign('trxn_id', $result['trxn_id']);
     $this->assign('receive_date', CRM_Utils_Date::mysqlToIso($this->_params['receive_date']));
     // result has all the stuff we need
     // lets archive it to a financial transaction
     $config =& CRM_Core_Config::singleton();
     $receiptDate = null;
     if ($this->_values['is_email_receipt']) {
         $receiptDate = $now;
     }
     if ($contributionType->is_deductible) {
         $this->assign('is_deductible', true);
         $this->set('is_deductible', true);
     }
     // assigning Premium information to receipt tpl
     if ($premiumParams['selectProduct'] && $premiumParams['selectProduct'] != 'no_thanks') {
         $startDate = $endDate = "";
         $this->assign('selectPremium', true);
         require_once 'CRM/Contribute/DAO/Product.php';
         $productDAO =& new CRM_Contribute_DAO_Product();
         $productDAO->id = $premiumParams['selectProduct'];
         $productDAO->find(true);
         $this->assign('product_name', $productDAO->name);
         $this->assign('price', $productDAO->price);
         $this->assign('sku', $productDAO->sku);
         $this->assign('option', $premiumParams['options_' . $premiumParams['selectProduct']]);
         $periodType = $productDAO->period_type;
         if ($periodType) {
             $fixed_period_start_day = $productDAO->fixed_period_start_day;
             $duration_unit = $productDAO->duration_unit;
             $duration_interval = $productDAO->duration_interval;
             if ($periodType == 'rolling') {
                 $startDate = date('Y-m-d');
             } else {
                 if ($periodType == 'fixed') {
                     if ($fixed_period_start_day) {
                         $date = explode('-', date('Y-m-d'));
                         $month = substr($fixed_period_start_day, 0, strlen($fixed_period_start_day) - 2);
                         $day = substr($fixed_period_start_day, -2) . "<br>";
                         $year = $date[0];
                         $startDate = $year . '-' . $month . '-' . $day;
                     } else {
                         $startDate = date('Y-m-d');
                     }
                 }
             }
             $date = explode('-', $startDate);
             $year = $date[0];
             $month = $date[1];
             $day = $date[2];
             switch ($duration_unit) {
                 case 'year':
                     $year = $year + $duration_interval;
                     break;
                 case 'month':
                     $month = $month + $duration_interval;
                     break;
                 case 'day':
                     $day = $day + $duration_interval;
                     break;
                 case 'week':
                     $day = $day + $duration_interval * 7;
             }
             $endDate = date('Y-m-d H:i:s', mktime($hour, $minute, $second, $month, $day, $year));
             $this->assign('start_date', $startDate);
             $this->assign('end_date', $endDate);
         }
         require_once 'CRM/Contribute/DAO/Premium.php';
         $dao =& new CRM_Contribute_DAO_Premium();
         $dao->entity_table = 'civicrm_contribution_page';
         $dao->entity_id = $this->_id;
         $dao->find(true);
         $this->assign('contact_phone', $dao->premiums_contact_phone);
         $this->assign('contact_email', $dao->premiums_contact_email);
     }
     CRM_Core_DAO::transaction('BEGIN');
     $nonDeductibleAmount = $result['gross_amount'];
     if ($contributionType->is_deductible) {
         if ($premiumParams['selectProduct'] != 'no_thanks') {
             require_once 'CRM/Contribute/DAO/Product.php';
             $productDAO =& new CRM_Contribute_DAO_Product();
             $productDAO->id = $premiumParams['selectProduct'];
             $productDAO->find(true);
             if ($result['gross_amount'] < $productDAO->price) {
                 $nonDeductibleAmount = $result['gross_amount'];
             } else {
                 $nonDeductibleAmount = $productDAO->price;
             }
         } else {
             $nonDeductibleAmount = '0.00';
         }
     }
     // check contribution Type
     // first create the contribution record
     $params = array('contact_id' => $contactID, 'contribution_type_id' => $contributionType->id, 'payment_instrument_id' => 1, 'receive_date' => $now, 'non_deductible_amount' => $nonDeductibleAmount, 'total_amount' => $result['gross_amount'], 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $result['gross_amount']), 'trxn_id' => $result['trxn_id'], 'invoice_id' => $this->_params['invoiceID'], 'currency' => $this->_params['currencyID'], 'receipt_date' => $receiptDate, 'source' => ts('Online Contribution:') . ' ' . $this->_values['title']);
     $ids = array();
     $contribution =& CRM_Contribute_BAO_Contribution::add($params, $ids);
     //create Premium record
     if ($premiumParams['selectProduct'] && $premiumParams['selectProduct'] != 'no_thanks') {
         require_once 'CRM/Contribute/DAO/Product.php';
         $productDAO =& new CRM_Contribute_DAO_Product();
         $productDAO->id = $premiumParams['selectProduct'];
         $productDAO->find(true);
         $periodType = $productDAO->period_type;
         require_once 'CRM/Utils/Date.php';
         $params = array('product_id' => $premiumParams['selectProduct'], 'contribution_id' => $contribution->id, 'product_option' => $premiumParams['options_' . $premiumParams['selectProduct']], 'quantity' => 1, 'start_date' => CRM_Utils_Date::customFormat($startDate, '%Y%m%d'), 'end_date' => CRM_Utils_Date::customFormat($endDate, '%Y%m%d'));
         CRM_Contribute_BAO_Contribution::addPremium($params);
     }
     // process the custom data that is submitted or that came via the url
     $groupTree = $this->get('groupTree');
     $customValues = $this->get('customGetValues');
     $customValues = array_merge($this->_params, $customValues);
     require_once 'CRM/Core/BAO/CustomGroup.php';
     CRM_Core_BAO_CustomGroup::postProcess($groupTree, $customValues);
     CRM_Core_BAO_CustomGroup::updateCustomData($groupTree, 'Contribution', $contribution->id);
     // next create the transaction record
     $params = array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution->id, 'trxn_date' => $now, 'trxn_type' => 'Debit', 'total_amount' => $result['gross_amount'], 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $result['gross_amount']), 'currency' => $this->_params['currencyID'], 'payment_processor' => $config->paymentProcessor, 'trxn_id' => $result['trxn_id']);
     require_once 'CRM/Contribute/BAO/FinancialTrxn.php';
     $trxn =& CRM_Contribute_BAO_FinancialTrxn::create($params);
     // also create an activity history record
     require_once 'CRM/Utils/Money.php';
     $params = array('entity_table' => 'civicrm_contact', 'entity_id' => $contactID, 'activity_type' => $contributionType->name, 'module' => 'CiviContribute', 'callback' => 'CRM_Contribute_Page_Contribution::details', 'activity_id' => $contribution->id, 'activity_summary' => 'Online - ' . CRM_Utils_Money::format($this->_params['amount']), 'activity_date' => $now);
     if (is_a(crm_create_activity_history($params), 'CRM_Core_Error')) {
         CRM_Core_Error::fatal("Could not create a system record");
     }
     CRM_Core_DAO::transaction('COMMIT');
     // finally send an email receipt
     if ($this->_values['is_email_receipt']) {
         list($displayName, $email) = CRM_Contact_BAO_Contact::getEmailDetails($contactID);
         $template =& CRM_Core_Smarty::singleton();
         $subject = trim($template->fetch('CRM/Contribute/Form/Contribution/ReceiptSubject.tpl'));
         $message = $template->fetch('CRM/Contribute/Form/Contribution/ReceiptMessage.tpl');
         $receiptFrom = '"' . $this->_values['receipt_from_name'] . '" <' . $this->_values['receipt_from_email'] . '>';
         require_once 'CRM/Utils/Mail.php';
         CRM_Utils_Mail::send($receiptFrom, $displayName, $email, $subject, $message, $this->_values['cc_receipt'], $this->_values['bcc_receipt']);
     }
 }