Skip to content

Commit

Permalink
intregrate sepa mandates
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMichalsky committed Oct 25, 2023
1 parent f59a731 commit 0a0df6a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
12 changes: 11 additions & 1 deletion CRM/Twingle/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
+-------------------------------------------------------------*/

use CRM_Twingle_ExtensionUtil as E;
use Civi\Twingle\Shop\Exceptions\LineItemException;

class CRM_Twingle_Submission {

Expand Down Expand Up @@ -457,6 +458,7 @@ public static function setCampaign(&$entity_data, $context, $submission, $profil
*
* @throws \CiviCRM_API3_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\Twingle\Shop\Exceptions\LineItemException
*/
public static function createLineItems($values, $submission, $profile): array {
$line_items = [];
Expand All @@ -465,9 +467,17 @@ public static function createLineItems($values, $submission, $profile): array {
$price_field_id = $profile->getAttribute('shop_price_field', 1);
$financial_type_id = $profile->getAttribute('shop_financial_type', 1);

$contribution_id = $values['contribution']['id'] ?? reset($values['sepa_mandate'])['entity_id'];
if (empty($contribution_id)) {
throw new LineItemException(
"Contribution id could for line item assignment could not be found",
LineItemException::ERROR_CODE_CONTRIBUTION_NOT_FOUND
);
}

$line_item_data = [
'entity_table' => "civicrm_contribution",
'contribution_id' => $values['contribution']['id'],
'contribution_id' => $contribution_id,
'entity_id' => $index,
'label' => $product['name'],
'qty' => $product['count'],
Expand Down
14 changes: 14 additions & 0 deletions Civi/Twingle/Shop/Exceptions/LineItemException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Civi\Twingle\Shop\Exceptions;

use Civi\Twingle\Exceptions\BaseException as BaseException;

/**
* A simple custom exception that indicates a problem within the Line Items
*/
class LineItemException extends BaseException {

public const ERROR_CODE_CONTRIBUTION_NOT_FOUND = "contribution_not_found";

}
16 changes: 11 additions & 5 deletions api/v3/TwingleDonation/Submit.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,11 @@ function civicrm_api3_twingle_donation_Submit($params) {
'total_amount' => $params['amount'] / 100,
);

// If the submission contains products, do not auto-create a line item
if (!empty($params['products']) && $profile->isShopEnabled()) {
$contribution_data['skipLineItem'] = 1;
}

// Add custom field values.
if (!empty($custom_fields['Contribution'])) {
$contribution_data += $custom_fields['Contribution'];
Expand Down Expand Up @@ -700,6 +705,12 @@ function civicrm_api3_twingle_donation_Submit($params) {
$mandate = civicrm_api3('SepaMandate', 'createfull', $mandate_data);

$result_values['sepa_mandate'] = $mandate['values'];

// Add products as line items to the contribution
if (!empty($params['products']) && $profile->isShopEnabled()) {
$line_items = CRM_Twingle_Submission::createLineItems($result_values, $params, $profile);
$result_values['contribution']['line_items'] = $line_items;
}
}
else {
// Set financial type depending on donation rhythm. This applies for
Expand Down Expand Up @@ -768,11 +779,6 @@ function civicrm_api3_twingle_donation_Submit($params) {
}
}

// If the submission contains products, do not auto-create a line item
if (!empty($params['products']) && $profile->isShopEnabled()) {
$contribution_data['skipLineItem'] = 1;
}

$contribution = civicrm_api3('Contribution', 'create', $contribution_data);
if ($contribution['is_error']) {
throw new CiviCRM_API3_Exception(
Expand Down

0 comments on commit 0a0df6a

Please sign in to comment.