CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Best approach to create a contribution that uses a price set via API
Pages: [1]

Author Topic: Best approach to create a contribution that uses a price set via API  (Read 1388 times)

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Best approach to create a contribution that uses a price set via API
June 18, 2013, 11:26:06 am
I have code that successfully creates a contribution via the API in version 4.3.x.   However, it only deals with contributions without a priceset.  I would like to enhance my code to deal with a priceset where each line item will have a different financial type.

My current code:
Code: [Select]
$params = array(
  'version' => 3,
  'sequential' => 1,
  'financial_type_id' => $financial_type_id,
  'payment_instrument_id' => 1,
  'total_amount' => $amount,
  'contact_id' => $cid,
  'trxn_id' => $trxn_id ,
  'contribution_recur_id' => $recur_id,
  'currency' => 'USD',
  'source' => $source,
  'contribution_status_id' => 1,
  'receive_date' => $receive_date,
 
);



$result = civicrm_api('Contribution', 'create', $params);

What would be the parameters for using a priceset?
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

lolas

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 9
    • Freeform Solutions
  • CiviCRM version: Several
  • CMS version: Drupal
  • MySQL version: 5.1+
  • PHP version: Several
Re: Best approach to create a contribution that uses a price set via API
June 19, 2013, 06:38:36 am
Based on other areas of the code I will guess the parameters should be price_xx where xx is the id of the price field. That is what they are usually posted as in forms.

Normally I would read through the api code for creating a contribution to see if there is some special handling there for certain parameters. By doing that I see that there is a parameter called skipLineItem which prevents line items from being processed. It defaults to 0 therefore it looks like they are normally handled automatically.

You could also create the contribution, save the id and then create the line items in a separate api call.
Freeform Solutions provides technology and management consulting, website and database development, and managed internet hosting solutions for not-for-profit organizations (NFPs).

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Best approach to create a contribution that uses a price set via API
June 29, 2013, 11:16:54 am
I have my code working the way I need now.   I am calling the "Contribution" "Create" API with the param: skipLineItem =1.  I  then call the "LineItem" "Create" API with the contribution id passed in as a param.      This is working as I expect.

Some things I noticed with the API while working on this. Not sure if these are bugs or just the way things are supposed to work.

Issue 1:  After deleting stray line item via "LineItem" "Delete" API, the table civicrm_financial_item still has a record for that lineitem.  (The civicrm_line_item table looks fine. No record for the deleted LineItem)

Issue 2: The field "civicrm_financial_trxn.trxn_date" is the date of the moment API call was made, not the contribution date passed as a parameter on the API call.

Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: Best approach to create a contribution that uses a price set via API
June 29, 2013, 06:50:25 pm
Regarding 1, The idea is that deleting live financial information is not supported in order to prevent fraud and support financial audit-ability. We should actually remove the ability in the API to delete live transaction line items. Instead, the proper approach is to post a new transaction that reverses the original. See http://wiki.civicrm.org/confluence/plugins/servlet/mobile#content/view/78151699 for more details.

There are several possible financial transactions associated with a contribution. For example the pay later transaction, the payment received for a pay later transaction, potentially a change in the amount owing, etc. So there is no aim to have the financial transaction time be the same as the contribution time.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Best approach to create a contribution that uses a price set via API
June 29, 2013, 08:44:17 pm
Joe - Regarding your point "There are several possible financial transactions associated with a contribution. For example the pay later transaction, the payment received for a pay later transaction, potentially a change in the amount owing, etc. So there is no aim to have the financial transaction time be the same as the contribution time. "

I agree with this for transactions created by something an end-user does, this makes sense.   

However it does not make sense when using the API to create huge numbers of contributions during a special import.    If the contributions being created via the API are being migrated from an older database, the dates passed in via API parameters need to be honored.   (Otherwise it will seem like the organization received a ton of revenue on the date of the import. )
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: Best approach to create a contribution that uses a price set via API
June 29, 2013, 09:58:50 pm
I don't know this code that well from memory, so I've asked Pradeep to look into this for you. Your use case is convincing, the question for me at this point is the best way to change things so that the time for the financial_trxn can be passed in to do what you want - I think this would mean an extra parameter that would be picked up by the chaining that is occurring if I am understanding correctly. Though I doubt it is needed, if necessary we could change the code to ensure in the case of a create contribution that the financial_trxn has the relevant date set to the contribution datetime field value.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Best approach to create a contribution that uses a price set via API
June 30, 2013, 07:13:22 am
Joe - have you already created a JIRA issue for this? Or should I?   

Also, the "Contributions ... Import Contributions" wizard suffers the same issue as the Contribution API:  All contributions imported via the wizard end up with a transaction date matching the date the import was done, NOT the contribution date.
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Best approach to create a contribution that uses a price set via API
June 30, 2013, 07:42:05 pm
In case anyone else is looking to do the same, I have pasted a code sample below. (Use with caution, as this code is negatively impacted by the API issue at: http://issues.civicrm.org/jira/browse/CRM-12983)

BTW: The code works in so far that it creates a contribution that uses a priceset style LineItem. ( In the real program there are multiple line items of different financial types associated with the contribution. )

Code: [Select]
$source_tmp = 'test of API payment';
$skipLineItem_parm = "1";
$new_contrib_params = array( 'version' => 3,
'sequential' => 1,
'financial_type_id' => 10,
'contact_id' => 12724,
'skipLineItem' => $skipLineItem_parm,
'payment_instrument_id' => 1,
'total_amount' => 53.00 ,
'trxn_id' => '' ,
'contribution_recur_id' => '' ,
'currency' => 'USD' ,
'contribution_campaign_id' => '1',
'source' => $source_tmp ,
'contribution_status_id' => 1,
'receive_date' => '20130616' ) ;


//$new_contrib_params['total_amount'] = $gateway_amount;
$new_contrib_result = civicrm_api('Contribution', 'create', $new_contrib_params ) ;
if($new_contrib_result['is_error'] <> 0 ){
print "<br>Error calling Contribution Create API: <br>";
print_r( $new_contrib_result);

}else{
print "<br>Created test contribution via API<br>";
print_r($new_contrib_result);

$new_contrib_id = $new_contrib_result['id'];

// create line items:
$params = array(
'version' => 3,
'sequential' => 1,
'entity_table' => 'civicrm_contribution',
'entity_id' => $new_contrib_id ,
//'price_field_id' => $original_line_item['price_field_id'],
//'label' => $original_line_item['label'],
'qty' => "53",
'unit_price' => "1",
'line_total' => "53",
'financial_type_id' => '10',

);

//print "<br><br>New line item:<br> ";
//print_r( $params ) ;
$li_result = civicrm_api('LineItem', 'create', $params);
if($li_result['is_error'] <> 0 ){
print "<br>Error calling Line Item API: <br>";
print_r( $li_result);

}else{
print "<br>Called line item API: <br>";
// print_r( $li_result);
}



}
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

adixon

  • I post frequently
  • ***
  • Posts: 314
  • Karma: 19
    • Blackfly Solutions
Re: Best approach to create a contribution that uses a price set via API
October 24, 2014, 08:31:09 am
Possibly in response to your post, I notice that there is now an api function that will do the line items together with the contribution, which seems somewhat more sane, example below:

https://github.com/civicrm/civicrm-core/blob/4.5/api/v3/examples/Contribution/CreateWithNestedLineItems.php

I'd also note that you can include a few more fields in the line items, and sometimes perhaps should ('label','price_field_value_id','financial_type_id').

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Best approach to create a contribution that uses a price set via API

This forum was archived on 2017-11-26.