Answers.Body: <p>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.</p>
  
  <p>You can likely fix the issue by setting the contribution_page_id to the string "3". </p>
  
  <p>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).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-30T16:26:00.897Z
Answers.Text: 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.
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-30T18:13:58.760Z
Answers.Text: 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.
Answers.UserDisplayName: ShanjayG
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-29T14:43:09.927Z
Answers.Text: 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.
Answers.UserDisplayName: ShanjayG
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-28T15:41:32.310Z
Answers.LastActivityDate: 2017-06-28T15:41:32.310Z
Answers.UserDisplayName: Alan Dixon
Answers.Body: <p>On the face of things, I don't see any glaring problems.  As @Alan Dixon points out the code is a bit cluttered.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>The only other comment is just questioning your use of the BAO.  You are best served using the API if possible.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-29T14:47:55.527Z
Answers.Text: 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.
Answers.UserDisplayName: ShanjayG
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-28T17:42:11.537Z
Answers.LastActivityDate: 2017-06-28T17:42:11.537Z
Answers.UserDisplayName: DaveD
Body: <p>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
  <code>$params['contribution_page_id'] = 3</code>. The data type that this paramer expects is an integer and I have tried casting it as an integer but does not work.</p>
  
  <p>I am trying to create a contribution record but it throws the error while </p>
  
  <pre><code>$contribution = civicrm_api3('Contribution', 'create', $params); 
  </code></pre>
  
  <p>Here it throws the error and I don't  know why.
  The full error goes like this 
  <code>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).</code></p>
  
  <p>So here is my full code </p>
  
  <pre><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-&gt;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-&gt;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);
  </code></pre>
  
  <p>That is where if fails and spits out the error.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-27T14:59:55.793Z
Comments.Text: 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
Comments.UserDisplayName: Ramesh - ARTECH Consultancy
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-27T16:28:37.887Z
Comments.Text: @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.
Comments.UserDisplayName: ShanjayG
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-27T16:41:00.467Z
Comments.Text: 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?
Comments.UserDisplayName: DaveD
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-28T11:21:02.180Z
Comments.Text: @DaveD-BackOfficeThinking I have added a more of my code till where it fails.
Comments.UserDisplayName: ShanjayG
Tags: <civicontribute>
Title: CiviCRM_API3_Exception: '3' is not a valid option for field contribution_page_id
ThreadText: # CiviCRM_API3_Exception: '3' is not a valid option for field contribution_page_id
  
  (Asked by: username_0 on 2017-06-27)
  
  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.
  
  **Tags:** civicontribute
  
  > **Comments:**
  > **username_1**: 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  
  > **username_0**: @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.  
  > **username_2**: 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?  
  > **username_0**: @username_2-BackOfficeThinking I have added a more of my code till where it fails.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2017-06-28. Score: 0 | Accepted answer
  
  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).
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_3**: 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.  
  > **username_0**: 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.  
  
  ---
  
  ### Answer by username_2 on 2017-06-28. Score: 0
  
  On the face of things, I don't see any glaring problems. As @username_3 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.
  
  > **Comments:**
  > **username_0**: 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.  
  
  ---
  
  