This error is really frustrating and annoying. It only happens on the live server but works fine on the local server. I have drupal 7 site with civicrm installed. One of the key I am passing on the $params array is $params['contribution_page_id'] = 3. The data type that this paramer expects is an integer and I have tried casting it as an integer but does not work.

I am trying to create a contribution record but it throws the error while

$contribution = civicrm_api3('Contribution', 'create', $params); 

Here it throws the error and I don't know why. The full error goes like this CiviCRM_API3_Exception: '3' is not a valid option for field contribution_page_id in civicrm_api3() (line 45 of /home/mysite/public_html/v2/sites/all/modules/civicrm/api/api.php).

So here is my full code

    $params['contact_id'] = $attendee;
    $params['event_id'] = $event_id;
    $params['register_date'] = date('YmdHis');
    $params['register_date_time'] =date('h:i A');
    $params['role_id'] =1;
    $params['status_id'] = 1;
    $params['status_id'] = 1;
    $params['fee_level'] = '';
    $params['is_pay_later'] = 1;
    $params['fee_amount'] = calculate_total();
    $params['fee_currency'] = 'GBP';
    $bao =  new CRM_Event_BAO_Participant();
    $participnt = $bao->create($params);

    //add Contribution
    $non_deductible_amount = round(calculate_total(),2);
    $total_amount = round(calculate_total(),2);
    $tax_amount = round(calculate_total_vat(),2);

    if($non_deductible_amount == 0) {
        $non_deductible_amount = 0;
    if($total_amount == 0) {
        $total_amount = 0;
    if($tax_amount == 0) {
        $tax_amount = 0;

    $bao =  new CRM_Contribute_BAO_Contribution();
    $params = array();
    $params['contact_id'] = $attendee;
    $params['financial_type_id'] = 4;
    $params['contribution_page_id'] = 3;
    $params['currency'] = $params['currencyID'] = 'GBP'; /**/
    $params['non_deductible_amount'] = $non_deductible_amount; /**/
    $params['total_amount'] = $total_amount; /**/
    $params['Source'] = 'Event Registration';
    //$params['is_pay_later'] = 1;
    $params['tax_amount'] = $tax_amount;
    $params['contribution_status_id'] = 'Pending';
    $params['skipLineItem'] =1;
    //$contribution = $bao->create($params);

    $params['invoice_id'] = md5(uniqid(rand(), TRUE));

    $params['payment_processor'] = 1;

    $params['is_test'] = 0;

    $params['description'] = 'Event Registration payment';

    $params['qfKey'] =  md5(uniqid(rand(), TRUE));

    $params['email'] = 'sanjok@hotmail.com';

    //This is where it fails. 
    $contribution = civicrm_api3('Contribution', 'create', $params);

That is where if fails and spits out the error.

  • I guess you are not getting answer even after 1000 views is because you han't explained properly - The line below doesn't mean anything $contribution = civicrm_api3('Contribution', 'create', $params); Please try to post the error message which will help others to say what is wrong - If you could please post the error message which you are getting also please do mention the Civicrm Version too - thanks – Ramesh - ARTECH Consultancy Jun 27 '17 at 14:59
  • @Ramesh I have updated my answer by adding the full watchdog message. Let me know if I could make it more clear or how to make it more clear. – Sanjok Gurung Jun 27 '17 at 16:28
  • Please post the entire snippet of code (anonymizing as necessary of course) from the setup of the $params array though the call. -- If you don't think you are going to get an answer here, why post it? – DaveD - BackOffice Thinking Jun 27 '17 at 16:41
  • @DaveD-BackOfficeThinking I have added a more of my code till where it fails. – Sanjok Gurung Jun 28 '17 at 11:21

The fact that this works on a local server but not a production server sounds fishy. My guess is that your local server is a different platform and/or php version.

You can likely fix the issue by setting the contribution_page_id to the string "3".

But I'd suggest you're probably going in the wrong direction for this project - that's a lot of custom code, most of it kind of funky (e.g. the unused bao object, and setting the qfKey), which suggests you've been trying to reverse engineer and/or copy paste stuff. If you simplify your code and only use the api as intended, you'll likely hit fewer oddities like this. I'd also question why you want to set the contribution_page_id anyway, since this isn't getting set via the contribution page, i.e. you're kind of tricking it into an unpredictable state. You'd likely do better categorizing your contributions using other mechanisms (or maybe you don't need to categorize this any further that you already have).

  • Thank you for your answer, I do understand that the code is cluttery. I did set the ['contribution_id'] = "3", that did not work, however commenting it out worked. On why contribution_page_id was hard set this way , the answer is I don't know since previous developer did it this way and its only now after migrating we are suddenly experiencing this. I am very new to Civicrm and I would like to ask you what could be the consequences of removing the 'contribution_page_id' ??. Feels like we don't need that but I am not entirely sure. – Sanjok Gurung Jun 29 '17 at 14:43
  • The contribution_page_id is mainly a reference so that you can track which contribution page a contribution came from. You wouldn't normally set it via the api, unless you're trying to trick your system into thinking your contributions actually came from a civi contribution page. I suspect it's a CiviCore error, but for your case leaving it blank is probably a better idea anyway. – Alan Dixon Jun 30 '17 at 16:26
  • Thank you, yes you are right as to leaving the contribution_page_id blank. Looks like we were forcing the page id as 3. Removing it will not hurt at all thank you. I guess I will mark your answer as the solution. – Sanjok Gurung Jun 30 '17 at 18:13

On the face of things, I don't see any glaring problems. As @Alan Dixon points out the code is a bit cluttered.

I do see a number of entries that are not part of the API call for Contribution/Create, such as email address, qfkey, description. While they shouldn't matter, I wouldn't include them... just in case.

I think the best approach is to use the API explorer (on the live server - /civicrm/api#explorer) to debug what the issue is. Enter in your API call there and see if an error occurs; you possibly would want to try it after entering just a few params at a time. This will also provide you with the best PHP code to use, that you can drop right into your code.

The only other comment is just questioning your use of the BAO. You are best served using the API if possible.

  • Thank you for your answer. Although the code is cluttery it should not create a problem. I did try using the #api explore and with the same settings as my code it works there. I did comment out the $params['contribution_page_id'] = 3; and it worked. Would you know of any consequences of removing the contribution_page_id. I am very new to civicrm. – Sanjok Gurung Jun 29 '17 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.