/** * Takes an associative array and creates a contribution object. * * the function extract all the params it needs to initialize the create a * contribution object. the params array could contain additional unused name/value * pairs * * @param array $params * (reference ) an assoc array of name/value pairs. * * @return CRM_Contribute_BAO_Contribution * @todo move hook calls / extended logic to create - requires changing calls to call create not add */ public static function add(&$params) { if (!empty($params['id'])) { CRM_Utils_Hook::pre('edit', 'ContributionRecur', $params['id'], $params); } else { CRM_Utils_Hook::pre('create', 'ContributionRecur', NULL, $params); } // make sure we're not creating a new recurring contribution with the same transaction ID // or invoice ID as an existing recurring contribution $duplicates = array(); if (self::checkDuplicate($params, $duplicates)) { $error = CRM_Core_Error::singleton(); $d = implode(', ', $duplicates); $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', array($d), "Found matching recurring contribution(s): {$d}"); return $error; } $recurring = new CRM_Contribute_BAO_ContributionRecur(); $recurring->copyValues($params); $recurring->id = CRM_Utils_Array::value('id', $params); // set currency for CRM-1496 if (empty($params['id']) && !isset($recurring->currency)) { $config = CRM_Core_Config::singleton(); $recurring->currency = $config->defaultCurrency; } $result = $recurring->save(); if (!empty($params['id'])) { CRM_Utils_Hook::post('edit', 'ContributionRecur', $recurring->id, $recurring); } else { CRM_Utils_Hook::post('create', 'ContributionRecur', $recurring->id, $recurring); } if (!empty($params['custom']) && is_array($params['custom'])) { CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contribution_recur', $recurring->id); } return $result; }
/** * takes an associative array and creates a contribution object * * the function extract all the params it needs to initialize the create a * contribution object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Contribute_BAO_Contribution object * @access public * @static */ static function add(&$params, &$ids) { $duplicates = array(); if (self::checkDuplicate($params, $duplicates)) { $error =& CRM_Core_Error::singleton(); $d = implode(', ', $duplicates); $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', array($d), "Found matching contribution(s): {$d}"); return $error; } $recurring = new CRM_Contribute_BAO_ContributionRecur(); $recurring->copyValues($params); $recurring->id = CRM_Utils_Array::value('contribution', $ids); // set currency for CRM-1496 if (!isset($recurring->currency)) { $config =& CRM_Core_Config::singleton(); $recurring->currency = $config->defaultCurrency; } return $recurring->save(); }
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; }
/** * build all the data structures needed to build the form * * @return void * @access public */ public function preProcess() { // Check permission for action. if (!CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); } $this->setPageTitle(ts('Recurring Contribution record')); // Get the contact id $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); $this->assign('contactID', $this->_contactID); // Get the action. $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); $this->assign('action', $this->_action); // Get the contribution recur id if update $this->_id = CRM_Utils_Request::retrieve('crid', 'Positive', $this); if (!empty($this->_id)) { $this->assign('contribRecurID', $this->_id); } $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); $this->assign('context', $this->_context); if ($this->_contactID) { list($this->userDisplayName, $this->userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID); $this->assign('displayName', $this->userDisplayName); } if ($this->_action & CRM_Core_Action::DELETE) { return; } // Get backoffice payment processors $this->assignPaymentRelatedVariables(); $this->_paymentProcessors = $this->getValidProcessors(); // Get recurring processors $this->_mode = 'live'; $this->assignProcessors(); $this->_values = array(); $ids = array(); $params = array('id' => $this->_id); if (!empty($this->_id)) { $recurring = new CRM_Contribute_BAO_ContributionRecur(); $recurring->copyValues($params); $recurring->find(TRUE); $ids['contributionrecur'] = $recurring->id; CRM_Core_DAO::storeValues($recurring, $this->_values); $membership = new CRM_Member_DAO_Membership(); $membership->contribution_recur_id = $this->_id; $membership->is_test = 0; if ($membership->find(TRUE)) { $this->_membershipID = $membership->id; $this->assign('membershipID', $this->_membershipID); } // Done allow Edit, if no back office support if (!array_key_exists($this->_values['payment_processor_id'], $this->_paymentProcessors)) { CRM_Core_Error::fatal(ts('You are not allowed to edit this recurring record, as back office edit is not supported by the related payment processor.')); } } // when custom data is included in this page if (!empty($_POST['hidden_custom'])) { $this->assign('type', 'ContributionRecur'); $this->assign('entityId', $this->_id); CRM_Custom_Form_CustomData::preProcess($this); CRM_Custom_Form_CustomData::buildQuickForm($this); CRM_Custom_Form_CustomData::setDefaultValues($this); } parent::preProcess(); }