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) »
  • API for creating contributions with line items results in bad data
Pages: 1 [2]

Author Topic: API for creating contributions with line items results in bad data  (Read 1944 times)

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: API for creating contributions with line items results in bad data
February 20, 2015, 05:10:33 am
JMA will create a draft of the proposed new API and review with Eileen. We will also create a sample way to properly create all the needed entries in financial table for  the base case and review with Eileen (we will likely add a new optional param to indicate the API should do the extra table entries beyond contribution and line item).
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API for creating contributions with line items results in bad data
February 20, 2015, 11:49:39 am
NB regarding Karen's question as to whether we fix the 'api problem' in the api - I think we should see it as a BAO problem & fix it there. The problem is that unless the BAO does it's job reliably & consistently we wind up with extra bits of handling in forms etc (although I am inclined to think the forms should use the api a little more as they seem to use a lot of code to prepare parameters for the BAO which wouldn't be required with the api.)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

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: API for creating contributions with line items results in bad data
February 20, 2015, 12:12:41 pm
We will try to confirm that the API call Karin cites:
$contributionResult = civicrm_api('contribution','create', $contribution);
creates unbalanced financial transactions, and why, and address that as appropriate. Since non-api transactions are working, and the API testing is less extensive here than the browser UI, it may turn out to be something in the way the API calls the BAO, or perhaps in the BAO itself. We'll see. Certainly we will not try to paper over issues in the BAO with fiddles at the API level.

Just to confirm, Eileen: we want for this round to support an api call to create a contribution that also includes a chained call to create one or more line_items. The result will be creating those records as well as associated records in financial_trxn, financial_item, and civicrm_entity_financial_trxn tables. We will not create membership or participant records, or entries in the member_payment or participant_payment tables, even if the line_items are for memberships or participant registrations.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API for creating contributions with line items results in bad data
February 20, 2015, 12:56:15 pm
Joe, I think it's pretty hard to create line items for participant or membership records without creating the participant records. I've been working on the assumption it would look something like this

http://forum.civicrm.org/index.php/topic,31242.msg134061.html#msg134061

I do prefer to write the spec for this sort of thing as a unit test because then it's really clear exactly what we expect and when it is working. I think this https://github.com/eileenmcnaughton/civicrm-core/commit/73f537055400ceeffdea24c1bdfaa055998441df was an early iteration of doing that for the conversation in the forum link above.

Note that the main reason (from my point of view) that the discussion in the forum didn't go further was that there turned out to be so much work in adjusting the front end forms to alter the way the membership line items were stored. (& of course part of the problem was the logic was in the form layer)
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API for creating contributions with line items results in bad data
February 21, 2015, 12:44:33 pm
Specifically regarding the repeat recurring transactions

 - I have added a PR that adds and tests a contribution.repeattransaction api

https://issues.civicrm.org/jira/browse/CRM-15996

This calls the existing (& now VERY commented) BaseIPN::completeTransaction function and identifies and fixes a bug in that function relating to creating line items for follow up transactions.

The patch is for master but I will also patch the LTS.

Regarding the 'complex api'

In the process of doing the above I realised the problems were in some ways converge on the ones I've been hitting in trying to make the contribution form layer simpler and trying to make the form layer support back office contributions for offsite processors.

If we look at the form layer we find that there are 2-3 flows
1) the flow for immediate payment
2) the flow for confirmed-later payment (ie. offsite processor).
3) the flow for paylater (somewhat the same as #2 & somewhat different - as some of you know I believe pay later should be implemented as a 'manual' processor).

The front end forms work VERY hard to create a fully confirmed payment for immediate payment, only after success, and to create a pending payment for pay later or confirmed-later.

What I propose is one flow
step one civicrm_api('contribution', 'preparetransaction' array(...));
step two civicrm_api('contribution', 'completetransaction', array(...));

The second of these already exists & has defined parameters so lets talk about the first :-)

I'm suggesting that we ALWAYS create a pending transaction with all related entities once we know about the intent. Once we know the outcome we call a second api call to update it to the outcome. No need for separate flows (at least at the form level) for different types of payment. Less form level logic.

The preparetransaction api is similar to the proposal for a 'thick' api - but since we already have some transaction-named api I'm using this name - which also allows us to build it in a new space without dealing with the existing one.

We would probably look at adding failtransaction too - We have agreement in principle from DGG to stop removing failed transactions and keep them in the DB as failed transactions.

In terms of parameters for preparetransaction I would start by looking at what the contribution forms receive as parameters & see how many of those we could pass on unwrangled.



Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

KarinG

  • I post frequently
  • ***
  • Posts: 134
  • Karma: 9
  • CiviCRM version: 4+
  • CMS version: Drupal 6 / 7
  • MySQL version: MariaDB
  • PHP version: 5.3/5.4/5.5
Re: API for creating contributions with line items results in bad data
February 21, 2015, 12:58:57 pm
You are amazing!

Pages: 1 [2]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • API for creating contributions with line items results in bad data

This forum was archived on 2017-11-26.