/**
  * Test that an object can be retrieved & saved (per CRM-14986).
  *
  * This has been causing a DB error so we are checking for absence of error
  */
 public function testFindSave()
 {
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
     $dao = new CRM_Contribute_BAO_ContributionRecur();
     $dao->id = $contributionRecur['id'];
     $dao->find(TRUE);
     $dao->is_email_receipt = 0;
     $dao->save();
 }
 public function doDirectPayment(&$params)
 {
     if (!defined('CURLOPT_SSLCERT')) {
         CRM_Core_Error::fatal(ts('eWAY - Gateway requires curl with SSL support'));
     }
     /*
      * OPTIONAL: If TEST Card Number force an Override of URL and CustomerID.
      * During testing CiviCRM once used the LIVE URL.
      * This code can be uncommented to override the LIVE URL that if CiviCRM does that again.
      * if ( ( $gateway_URL == "https://www.eway.com.au/gateway_cvn/xmlpayment.asp")
      *   && ( $params['credit_card_number'] == "4444333322221111" ) ) {
      *   $ewayCustomerID = "87654321";
      *   $gateway_URL    = "https://www.eway.com.au/gateway/rebill/test/Upload_test.aspx";
      * }
      */
     // Was the recurring payment check box checked?
     if (isset($params['is_recur']) && $params['is_recur'] == 1) {
         // Create the customer via the API.
         try {
             $result = $this->createToken($this->_paymentProcessor, $params);
         } catch (Exception $e) {
             return self::errorExit(9010, $e->getMessage());
         }
         // We've created the customer successfully.
         $managed_customer_id = $result;
         try {
             $initialPayment = civicrm_api3('ewayrecurring', 'payment', array('invoice_id' => $params['invoiceID'], 'amount_in_cents' => round((double) $params['amount'] * 100), 'managed_customer_id' => $managed_customer_id, 'description' => $params['description'] . ts('first payment'), 'payment_processor_id' => $this->_paymentProcessor['id']));
             // Here we compensate for the fact core accepts 0 as a valid frequency
             // interval and set it.
             $extra = array();
             if (empty($params['frequency_interval'])) {
                 $params['frequency_interval'] = 1;
                 $extra['frequency_interval'] = 1;
             }
             $params['trxn_id'] = $initialPayment['values'][$managed_customer_id]['trxn_id'];
             $params['contribution_status_id'] = 1;
             $params['payment_status_id'] = 1;
             // If there's only one installment, then the recurring contribution is now complete
             if (isset($params['installments']) && $params['installments'] == 1) {
                 $status = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
             } else {
                 $status = CRM_Core_OptionGroup::getValue('contribution_status', 'In Progress', 'name');
             }
             // Save the eWay customer token in the recurring contribution's processor_id field.
             civicrm_api3('contribution_recur', 'create', array_merge(array('id' => $params['contributionRecurID'], 'processor_id' => $managed_customer_id, 'contribution_status_id' => $status, 'next_sched_contribution_date' => CRM_Utils_Date::isoToMysql(date('Y-m-d 00:00:00', strtotime('+' . $params['frequency_interval'] . ' ' . $params['frequency_unit'])))), $extra));
             // Send recurring Notification email for user.
             $recur = new CRM_Contribute_BAO_ContributionRecur();
             $recur->id = $params['contributionRecurID'];
             $recur->find(TRUE);
             // If none found then effectively FALSE.
             $autoRenewMembership = civicrm_api3('membership', 'getcount', array('contribution_recur_id' => $recur->id));
             if (!empty($params['selectMembership']) || !empty($params['membership_type_id']) && !empty($params['auto_renew'])) {
                 $autoRenewMembership = TRUE;
             }
             CRM_Contribute_BAO_ContributionPage::recurringNotify(CRM_Core_Payment::RECURRING_PAYMENT_START, $params['contactID'], CRM_Utils_Array::value('contributionPageID', $params), $recur, $autoRenewMembership);
         } catch (CiviCRM_API3_Exception $e) {
             return self::errorExit(9014, 'Initial payment not processed' . $e->getMessage());
         }
     } else {
         try {
             $result = $this->processSinglePayment($params);
             $params = array_merge($params, $result);
         } catch (CRM_Core_Exception $e) {
             return self::errorExit(9001, $e->getMessage());
         }
     }
     return $params;
 }
示例#3
0
 static function processAPIContribution($params)
 {
     if (empty($params) || array_key_exists('error', $params)) {
         return false;
     }
     // add contact using dedupe rule
     require_once 'CRM/Dedupe/Finder.php';
     $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual');
     $dedupeParams['check_permission'] = false;
     $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');
     // if we find more than one contact, use the first one
     if (CRM_Utils_Array::value(0, $dupeIds)) {
         $params['contact_id'] = $dupeIds[0];
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     $contact = CRM_Contact_BAO_Contact::create($params);
     if (!$contact->id) {
         return false;
     }
     // only pass transaction params to contribution::create, if available
     if (array_key_exists('transaction', $params)) {
         $params = $params['transaction'];
         $params['contact_id'] = $contact->id;
     }
     // handle contribution custom data
     $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', false, false, CRM_Utils_Array::value('contribution_type_id', $params));
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, CRM_Utils_Array::value('id', $params, null), 'Contribution');
     // create contribution
     // if this is a recurring contribution then process it first
     if ($params['trxn_type'] == 'subscrpayment') {
         // see if a recurring record already exists
         require_once 'CRM/Contribute/BAO/ContributionRecur.php';
         $recurring = new CRM_Contribute_BAO_ContributionRecur();
         $recurring->processor_id = $params['processor_id'];
         if (!$recurring->find(true)) {
             $recurring = new CRM_Contribute_BAO_ContributionRecur();
             $recurring->invoice_id = $params['invoice_id'];
             $recurring->find(true);
         }
         // This is the same thing the CiviCRM IPN handler does to handle
         // subsequent recurring payments to avoid duplicate contribution
         // errors due to invoice ID. See:
         // ./CRM/Core/Payment/PayPalIPN.php:200
         if ($recurring->id) {
             $params['invoice_id'] = md5(uniqid(rand(), true));
         }
         $recurring->copyValues($params);
         $recurring->save();
         if (is_a($recurring, 'CRM_Core_Error')) {
             return false;
         } else {
             $params['contribution_recur_id'] = $recurring->id;
         }
     }
     require_once 'CRM/Contribute/BAO/Contribution.php';
     $contribution =& CRM_Contribute_BAO_Contribution::create($params, CRM_Core_DAO::$_nullArray);
     if (!$contribution->id) {
         return false;
     }
     return true;
 }
示例#4
0
 /**
  * Load objects relations to contribution object.
  * Objects are stored in the $_relatedObjects property
  * In the first instance we are just moving functionality from BASEIpn -
  * @see http://issues.civicrm.org/jira/browse/CRM-9996
  *
  * Note that the unit test for the BaseIPN class tests this function
  *
  * @param array $input
  *   Input as delivered from Payment Processor.
  * @param array $ids
  *   Ids as Loaded by Payment Processor.
  * @param bool $required
  *   Is Payment processor / contribution page required.
  * @param bool $loadAll
  *   Load all related objects - even where id not passed in? (allows API to call this).
  *
  * @return bool
  * @throws Exception
  */
 public function loadRelatedObjects(&$input, &$ids, $required = FALSE, $loadAll = FALSE)
 {
     if ($loadAll) {
         $ids = array_merge($this->getComponentDetails($this->id), $ids);
         if (empty($ids['contact']) && isset($this->contact_id)) {
             $ids['contact'] = $this->contact_id;
         }
     }
     if (empty($this->_component)) {
         if (!empty($ids['event'])) {
             $this->_component = 'event';
         } else {
             $this->_component = strtolower(CRM_Utils_Array::value('component', $input, 'contribute'));
         }
     }
     $paymentProcessorID = CRM_Utils_Array::value('paymentProcessor', $ids);
     $contributionType = new CRM_Financial_BAO_FinancialType();
     $contributionType->id = $this->financial_type_id;
     $contributionType->find(TRUE);
     if (!empty($ids['contact'])) {
         $this->_relatedObjects['contact'] = new CRM_Contact_BAO_Contact();
         $this->_relatedObjects['contact']->id = $ids['contact'];
         $this->_relatedObjects['contact']->find(TRUE);
     }
     $this->_relatedObjects['contributionType'] = $contributionType;
     if ($this->_component == 'contribute') {
         // retrieve the other optional objects first so
         // stuff down the line can use this info and do things
         // CRM-6056
         //in any case get the memberships associated with the contribution
         //because we now support multiple memberships w/ price set
         // see if there are any other memberships to be considered for same contribution.
         $query = "\n            SELECT membership_id\n            FROM   civicrm_membership_payment\nWHERE  contribution_id = %1 ";
         $params = array(1 => array($this->id, 'Integer'));
         $dao = CRM_Core_DAO::executeQuery($query, $params);
         while ($dao->fetch()) {
             if ($dao->membership_id) {
                 if (!is_array($ids['membership'])) {
                     $ids['membership'] = array();
                 }
                 $ids['membership'][] = $dao->membership_id;
             }
         }
         if (array_key_exists('membership', $ids) && is_array($ids['membership'])) {
             foreach ($ids['membership'] as $id) {
                 if (!empty($id)) {
                     $membership = new CRM_Member_BAO_Membership();
                     $membership->id = $id;
                     if (!$membership->find(TRUE)) {
                         throw new Exception("Could not find membership record: {$id}");
                     }
                     $membership->join_date = CRM_Utils_Date::isoToMysql($membership->join_date);
                     $membership->start_date = CRM_Utils_Date::isoToMysql($membership->start_date);
                     $membership->end_date = CRM_Utils_Date::isoToMysql($membership->end_date);
                     $this->_relatedObjects['membership'][$membership->membership_type_id] = $membership;
                     $membership->free();
                 }
             }
         }
         if (!empty($ids['pledge_payment'])) {
             foreach ($ids['pledge_payment'] as $key => $paymentID) {
                 if (empty($paymentID)) {
                     continue;
                 }
                 $payment = new CRM_Pledge_BAO_PledgePayment();
                 $payment->id = $paymentID;
                 if (!$payment->find(TRUE)) {
                     throw new Exception("Could not find pledge payment record: " . $paymentID);
                 }
                 $this->_relatedObjects['pledge_payment'][] = $payment;
             }
         }
         if (!empty($ids['contributionRecur'])) {
             $recur = new CRM_Contribute_BAO_ContributionRecur();
             $recur->id = $ids['contributionRecur'];
             if (!$recur->find(TRUE)) {
                 throw new Exception("Could not find recur record: " . $ids['contributionRecur']);
             }
             $this->_relatedObjects['contributionRecur'] =& $recur;
             //get payment processor id from recur object.
             $paymentProcessorID = $recur->payment_processor_id;
         }
         //for normal contribution get the payment processor id.
         if (!$paymentProcessorID) {
             if ($this->contribution_page_id) {
                 // get the payment processor id from contribution page
                 $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->contribution_page_id, 'payment_processor');
             } elseif (empty($ids['pledge_payment'])) {
                 $loadObjectSuccess = TRUE;
                 if ($required) {
                     throw new Exception("Could not find contribution page for contribution record: " . $this->id);
                 }
                 return $loadObjectSuccess;
             }
         }
     } else {
         // we are in event mode
         // make sure event exists and is valid
         $event = new CRM_Event_BAO_Event();
         $event->id = $ids['event'];
         if ($ids['event'] && !$event->find(TRUE)) {
             throw new Exception("Could not find event: " . $ids['event']);
         }
         $this->_relatedObjects['event'] =& $event;
         $participant = new CRM_Event_BAO_Participant();
         $participant->id = $ids['participant'];
         if ($ids['participant'] && !$participant->find(TRUE)) {
             throw new Exception("Could not find participant: " . $ids['participant']);
         }
         $participant->register_date = CRM_Utils_Date::isoToMysql($participant->register_date);
         $this->_relatedObjects['participant'] =& $participant;
         if (!$paymentProcessorID) {
             $paymentProcessorID = $this->_relatedObjects['event']->payment_processor;
         }
     }
     if ($paymentProcessorID) {
         $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $this->is_test ? 'test' : 'live');
         $ids['paymentProcessor'] = $paymentProcessorID;
         $this->_relatedObjects['paymentProcessor'] = $paymentProcessor;
     } elseif ($required) {
         throw new Exception("Could not find payment processor for contribution record: " . $this->id);
     }
     return TRUE;
 }
/**
 * Gets recurring contributions that are scheduled to be processed today.
 *
 * @param $eway_token_clients
 *
 * @return array
 *   An array of contribution_recur objects.
 */
function get_scheduled_contributions($eway_token_clients)
{
    if (empty($eway_token_clients)) {
        return array();
    }
    $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
    // Get Recurring Contributions that are In Progress and are due to be processed by the eWAY Recurring processor
    $scheduled_today = new CRM_Contribute_BAO_ContributionRecur();
    if (_versionAtLeast(4.4)) {
        $scheduled_today->whereAdd("`next_sched_contribution_date` <= '" . date('Y-m-d 00:00:00') . "'");
    } else {
        $scheduled_today->whereAdd("`next_sched_contribution` <= '" . date('Y-m-d 00:00:00') . "'");
    }
    $scheduled_today->whereAdd("`contribution_status_id` = " . array_search('In Progress', $contributionStatus));
    $scheduled_today->whereAdd("`payment_processor_id` in (" . implode(', ', array_keys($eway_token_clients)) . ")");
    $scheduled_today->find();
    $result = array();
    while ($scheduled_today->fetch()) {
        $past_contribution = get_first_contribution_from_recurring($scheduled_today->id);
        $new_contribution_record = new CRM_Contribute_BAO_Contribution();
        $new_contribution_record->contact_id = $scheduled_today->contact_id;
        $new_contribution_record->receive_date = CRM_Utils_Date::isoToMysql(date('Y-m-d H:i:s'));
        $new_contribution_record->total_amount = $scheduled_today->amount;
        $new_contribution_record->non_deductible_amount = $scheduled_today->amount;
        $new_contribution_record->net_amount = $scheduled_today->amount;
        $new_contribution_record->invoice_id = md5(uniqid(rand(), TRUE));
        $new_contribution_record->contribution_recur_id = $scheduled_today->id;
        $new_contribution_record->contribution_status_id = array_search('Pending', $contributionStatus);
        if (_versionAtLeast(4.4)) {
            $new_contribution_record->financial_type_id = $scheduled_today->financial_type_id;
        } else {
            $new_contribution_record->contribution_type_id = $scheduled_today->contribution_type_id;
        }
        $new_contribution_record->currency = $scheduled_today->currency;
        // copy info from previous contribution belonging to the same recurring contribution
        if ($past_contribution != NULL) {
            $new_contribution_record->contribution_page_id = $past_contribution->contribution_page_id;
            $new_contribution_record->payment_instrument_id = $past_contribution->payment_instrument_id;
            $new_contribution_record->source = $past_contribution->source;
            $new_contribution_record->address_id = $past_contribution->address_id;
        }
        $result[] = array('type' => 'Scheduled', 'contribution' => clone $new_contribution_record, 'contribution_recur' => clone $scheduled_today);
    }
    return $result;
}
 public function moveRecurringRecord($submittedValues)
 {
     // Move recurring record to another contact
     if (!empty($submittedValues['contact_id']) && $submittedValues['contact_id'] != $this->_contactID) {
         $selected_cid = $submittedValues['contact_id'];
         // FIXME: Not getting the below value in $submittedValues
         // So taking the value from $_POST
         if (isset($_POST['membership_record'])) {
             $membership_record = $_POST['membership_record'];
         }
         // Update contact id in civicrm_contribution_recur table
         $recurring = new CRM_Contribute_BAO_ContributionRecur();
         $recurring->id = $this->_id;
         if ($recurring->find(TRUE)) {
             $recurParams = (array) $recurring;
             $recurParams['contact_id'] = $selected_cid;
             CRM_Contribute_BAO_ContributionRecur::create($recurParams);
         }
         // Update contact id in civicrm_contribution table, if 'Move Existing Contributions?' is ticked
         if (isset($submittedValues['move_existing_contributions']) && $submittedValues['move_existing_contributions'] == 1) {
             $contribution = new CRM_Contribute_DAO_Contribution();
             $contribution->contribution_recur_id = $this->_id;
             $contribution->find();
             while ($contribution->fetch()) {
                 $contributionParams = (array) $contribution;
                 $contributionParams['contact_id'] = $selected_cid;
                 // Update contact_id of contributions
                 // related to the recurring contribution
                 CRM_Contribute_BAO_Contribution::create($contributionParams);
             }
         }
     }
     if (!empty($membership_record)) {
         // Remove the contribution_recur_id from existing membership
         if (!empty($this->_membershipID)) {
             $membership = new CRM_Member_DAO_Membership();
             $membership->id = $this->_membershipID;
             if ($membership->find(TRUE)) {
                 $membershipParams = (array) $membership;
                 $membershipParams['contribution_recur_id'] = 'NULL';
                 CRM_Member_BAO_Membership::add($membershipParams);
             }
         }
         // Update contribution_recur_id to the new membership
         $membership = new CRM_Member_DAO_Membership();
         $membership->id = $membership_record;
         if ($membership->find(TRUE)) {
             $membershipParams = (array) $membership;
             $membershipParams['contribution_recur_id'] = $this->_id;
             CRM_Member_BAO_Membership::add($membershipParams);
         }
     }
 }
 /**
  * Test we don't change unintended fields on API edit
  */
 public function testUpdateRecur()
 {
     $createParams = $this->_params;
     $createParams['currency'] = 'XAU';
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $createParams);
     $editParams = array('id' => $contributionRecur['id'], 'end_date' => '+ 4 weeks');
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $editParams);
     $dao = new CRM_Contribute_BAO_ContributionRecur();
     $dao->id = $contributionRecur['id'];
     $dao->find(TRUE);
     $this->assertEquals('XAU', $dao->currency, 'Edit clobbered recur currency');
 }