1

Say I have two price fields, thumbtacks at $1/unit and ketchup at $20/unit (it's very good ketchup). Suppose they each have a different financial type, such as "Office" and "Condiments".

If I use the order api to create a contribution where you've bought one of each this looks fine. I get correct looking records in civicrm_line_item etc. The problem is when I record the payment. It properly records two items in civicrm_financial_item corresponding to the line items, but then in civicrm_entity_financial_trxn, it properly creates two records, but the entity_id for both of them points to the same entry in civicrm_financial_item. This then shows up in the UI on the contribution as "Condiments, Condiments" for the payment instead of "Office, Condiments".

When I record the same contribution from the start using the UI and the price set, it has entity_id pointing to the separate items, as I'd expect, and in the UI shows "Office, Condiments" for the payment.

The code looks something like this:

$result = civicrm_api3('Order', 'create', [
  'contact_id' => 17,
  'financial_type_id' => 'Donation', // this seems irrelevant
  'receive_date' => date('YmdHis'),
  'contribution_status_id' => 'Pending',  // this is required according to the docs - the payment below completes it
  'line_items' => [
    [
      'params' => [],
      'line_item' => [
        [
          'label' => 'line 1',
          'qty' => 1,
          'unit_price' => '1',
          'line_total' => '1',
          'financial_type_id' => 22, // Office
          'price_field_id' => 12, // this seems almost irrelevant - using "1" has the same effect but then oddly doesn't show the line item breakdown when viewing the contribution
        ],
        // It doesn't seem to make a difference if I put the 2nd line item here or as a separate array in the outer array.
        // It's the payment part that is creating the wrong entity_id.
        [
          'label' => 'line 2',
          'qty' => 1,
          'unit_price' => '20',
          'line_total' => '20',
          'financial_type_id' => 23, // Condiments
          'price_field_id' => 13,
        ],
      ],
    ],
  ],
  'api.Payment.create' => ['total_amount' => 21],
]);

1 Answer 1

1

As per this old issue and PR

https://github.com/colemanw/webform_civicrm/pull/58#issuecomment-289561525

you want to specify the price_field_value_id or else CiviCRM will try and be clever and guess one for you and will often get it wrong. I think it's using the same mechanism it uses when you specify values for a payment page without using a real price set - i.e. it access a hidden price set that it uses for the purpose.

1
  • Ah yes price_field_value_id seems to be what's missing. Thanks.
    – Demerit
    May 6, 2021 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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