From e376b3006239c419608e7d71a70ac4c06d049a6e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 16 Jun 2021 09:40:57 +1200 Subject: [PATCH] Fix unreleased api change An unreleased change adds the entity ids for membership to the return values. However, on digging into further cleanup I realised that the entity ids are in the line item array, along with other values which may or may not be useful, and it's cleaner, more complete and more maintainable to return the line_items. It requires an extra foreach loop in the calling code but I don't see that as a downside. We should change this in the rc so what is released is consistent Test cover in testSubmitRecur & other tests in CRM_Member_Form_MembershipTest --- CRM/Member/Form/Membership.php | 15 ++++++++++++++- api/v3/Order.php | 5 +++-- tests/phpunit/CRMTraits/Financial/OrderTrait.php | 7 ++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index fb53ee2101..ed0442b8d5 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -1168,7 +1168,7 @@ DESC limit 1"); ] ); $this->ids['Contribution'] = $contribution['id']; - $this->setMembershipIDs($contribution['values'][$contribution['id']]['membership_id']); + $this->setMembershipIDsFromOrder($contribution); //create new soft-credit record, CRM-13981 if ($softParams) { @@ -1988,4 +1988,17 @@ DESC limit 1"); return array_merge($this->getFormMembershipParams(), $this->getMembershipParameters()[$membershipTypeID]); } + /** + * @param array $contribution + */ + protected function setMembershipIDsFromOrder(array $contribution): void { + $ids = []; + foreach ($contribution['values'][$contribution['id']]['line_item'] as $line) { + if ($line['entity_table'] ?? '' === 'civicrm_membership') { + $ids[] = $line['entity_id']; + } + } + $this->setMembershipIDs($ids); + } + } diff --git a/api/v3/Order.php b/api/v3/Order.php index 2a9a5e4936..870b07fbd4 100644 --- a/api/v3/Order.php +++ b/api/v3/Order.php @@ -76,9 +76,9 @@ function civicrm_api3_order_create(array $params): array { $entity = NULL; $entityIds = []; $params['contribution_status_id'] = 'Pending'; + $priceSetID = NULL; if (!empty($params['line_items']) && is_array($params['line_items'])) { - $priceSetID = NULL; CRM_Contribute_BAO_Contribution::checkLineItems($params); foreach ($params['line_items'] as $lineItems) { $entityParams = $lineItems['params'] ?? []; @@ -149,6 +149,8 @@ function civicrm_api3_order_create(array $params): array { } $contribution = civicrm_api3('Contribution', 'create', $contributionParams); + $contribution['values'][$contribution['id']]['line_item'] = $params['line_item'][$priceSetID] ?? []; + // add payments if ($entity && !empty($contribution['id'])) { foreach ($entityIds as $entityId) { @@ -161,7 +163,6 @@ function civicrm_api3_order_create(array $params): array { $paymentParams += $entityParams; } elseif ($entity === 'membership') { - $contribution['values'][$contribution['id']]['membership_id'][] = $entityId; $paymentParams['isSkipLineItem'] = TRUE; } civicrm_api3($entity . '_payment', 'create', $paymentParams); diff --git a/tests/phpunit/CRMTraits/Financial/OrderTrait.php b/tests/phpunit/CRMTraits/Financial/OrderTrait.php index 1ea8177966..ae0eff8c78 100644 --- a/tests/phpunit/CRMTraits/Financial/OrderTrait.php +++ b/tests/phpunit/CRMTraits/Financial/OrderTrait.php @@ -127,7 +127,12 @@ trait CRMTraits_Financial_OrderTrait { ]); $this->ids['Contribution'][0] = $order['id']; - $this->ids['Membership']['order'] = $order['values'][$order['id']]['membership_id'][0]; + foreach ($order['values'][$order['id']]['line_item'] as $line) { + if (($line['entity_table'] ?? '') === 'civicrm_membership') { + $this->ids['Membership']['order'] = $line['entity_id']; + } + } + } /** -- 2.25.1