Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix preferred repeattransaction flow to correctly create the activity contacts for the contribution #19200

Merged
merged 2 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -2442,7 +2442,7 @@ protected static function repeatTransaction(&$contribution, $input, $contributio
else {
$contributionParams['financial_type_id'] = $templateContribution['financial_type_id'];
}
foreach (['contact_id', 'currency', 'source', 'amount_level', 'address_id'] as $fieldName) {
foreach (['contact_id', 'currency', 'source', 'amount_level', 'address_id', 'on_behalf', 'source_contact_id'] as $fieldName) {
if (isset($templateContribution[$fieldName])) {
$contributionParams[$fieldName] = $templateContribution[$fieldName];
}
Expand Down Expand Up @@ -4267,18 +4267,6 @@ public static function completeOrder($input, $ids, $contribution, $isPostPayment
CRM_Contribute_BAO_ContributionRecur::updateRecurLinkedPledge($contributionID, $recurringContributionID,
$contributionParams['contribution_status_id'], $input['amount']);

// create an activity record
// @todo - check if Contribution::create does this, test, remove.
if ($input['component'] == 'contribute') {
//CRM-4027
$targetContactID = NULL;
if ($contributionContactID) {
$targetContactID = $contribution->contact_id;
$contribution->contact_id = $contributionContactID;
}
CRM_Activity_BAO_Activity::addActivity($contribution, 'Contribution', $targetContactID);
}

if (self::isEmailReceipt($input, $contribution->contribution_page_id, $recurringContributionID)) {
civicrm_api3('Contribution', 'sendconfirmation', [
'id' => $contributionID,
Expand Down
1 change: 1 addition & 0 deletions CRM/Contribute/BAO/ContributionPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function create($params) {
CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']);
}
CRM_Utils_Hook::post($hook, 'ContributionPage', $dao->id, $dao);
CRM_Core_PseudoConstant::flush();
return $dao;
}

Expand Down
7 changes: 7 additions & 0 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ public static function getTemplateContribution($id, $overrides = []) {
}
$result = array_merge($templateContribution, $overrides);
$result['line_item'] = self::reformatLineItemsForRepeatContribution($result['total_amount'], $result['financial_type_id'], $lineItems, (array) $templateContribution);
// If the template contribution was made on-behalf then add the
// relevant values to ensure the activity reflects that.
$relatedContact = CRM_Contribute_BAO_Contribution::getOnbehalfIds($result['id']);
if (!empty($relatedContact['individual_id'])) {
$result['on_behalf'] = TRUE;
$result['source_contact_id'] = $relatedContact['individual_id'];
}
return $result;
}
return [];
Expand Down
1 change: 1 addition & 0 deletions CRM/Financial/BAO/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public static function create($params) {
'is_email_receipt' => $params['is_send_contribution_notification'],
'trxn_date' => $params['trxn_date'],
'payment_instrument_id' => $paymentTrxnParams['payment_instrument_id'],
'payment_processor_id' => $paymentTrxnParams['payment_processor_id'] ?? NULL,
]);
// Get the trxn
$trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
Expand Down
123 changes: 75 additions & 48 deletions tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,38 @@ public function tearDown() {
}

/**
* CRM-21200: Test that making online payment for pending contribution doesn't overwite the contribution details
* CRM-21200: Test that making online payment for pending contribution
* doesn't overwite the contribution details
*
* @throws \CiviCRM_API3_Exception
* @throws \CRM_Core_Exception
*/
public function testPayNowPayment() {
$contactID = $this->individualCreate();
public function testPayNowPayment(): void {
$individualID = $this->individualCreate();
$paymentProcessorID = $this->paymentProcessorCreate(['payment_processor_type_id' => 'Dummy']);
CRM_Core_Config::singleton()->userPermissionClass->permissions = [];

// create a contribution page which is later used to make pay-later contribution
$result = $this->callAPISuccess('ContributionPage', 'create', [
'title' => 'Test Contribution Page',
'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
'currency' => 'USD',
'financial_account_id' => 1,
'payment_processor' => $paymentProcessorID,
'is_active' => 1,
'is_allow_other_amount' => 1,
'min_amount' => 20,
'max_amount' => 2000,
]);
$contributionPageID1 = $result['id'];
$contributionPageID1 = $this->createContributionPage(['payment_processor' => $paymentProcessorID]);

// create pending contribution
$contribution = $this->callAPISuccess('Contribution', 'create', [
'contact_id' => $contactID,
'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
'contact_id' => $individualID,
'financial_type_id' => 'Campaign Contribution',
'currency' => 'USD',
'total_amount' => 100.00,
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
'contribution_status_id' => 'Pending',
'contribution_page_id' => $contributionPageID1,
'source' => 'backoffice pending contribution',
]);

// create a contribution page which is later used to make online payment for pending contribution
$result = $this->callAPISuccess('ContributionPage', 'create', [
'title' => 'Test Contribution Page',
'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
'currency' => 'USD',
'financial_account_id' => 1,
'payment_processor' => $paymentProcessorID,
'is_active' => 1,
'is_allow_other_amount' => 1,
'min_amount' => 10,
'max_amount' => 1000,
]);
$form = new CRM_Contribute_Form_Contribution_Confirm();
$contributionPageID2 = $result['id'];
$contributionPageID2 = $this->createContributionPage(['payment_processor' => $paymentProcessorID]);

/* @var CRM_Contribute_Form_Contribution_Confirm $form*/
$form = $this->getFormObject('CRM_Contribute_Form_Contribution_Confirm');
$form->_id = $contributionPageID2;
$form->_values = $result['values'][$contributionPageID2];

$form->_paymentProcessor = [
'id' => $paymentProcessorID,
'billing_mode' => CRM_Core_Payment::BILLING_MODE_FORM,
Expand All @@ -81,7 +66,6 @@ public function testPayNowPayment() {
'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit card'),
];
$form->_params = [
'qfKey' => 'do not care',
'contribution_id' => $contribution['id'],
'credit_card_number' => 4111111111111111,
'cvv2' => 234,
Expand All @@ -103,17 +87,19 @@ public function testPayNowPayment() {
'is_quick_config' => 1,
'description' => $contribution['values'][$contribution['id']]['source'],
'skipLineItem' => 0,
'frequency_interval' => 1,
'frequency_unit' => 'month',
];

$processConfirmResult = $form->processConfirm(
$form->_params,
$contactID,
$form->_values['financial_type_id'],
$individualID,
CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
0, FALSE
);

// Make sure that certain parameters are set on return from processConfirm
$this->assertEquals($form->_values['financial_type_id'], $processConfirmResult['financial_type_id']);
$this->assertEquals('Campaign Contribution', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'financial_type_id', $processConfirmResult['financial_type_id']));

// Based on the processed contribution, complete transaction which update the contribution status based on payment result.
if (!empty($processConfirmResult['contribution'])) {
Expand All @@ -140,39 +126,80 @@ public function testPayNowPayment() {
// check that contribution status is changed to 'Completed' from 'Pending'
$this->assertEquals('Completed', $contribution['contribution_status']);

//delete contribution.
// Delete contribution.
// @todo - figure out why & document properly. If this is just to partially
// re-use some test set up then split into 2 tests.
$this->callAPISuccess('contribution', 'delete', [
'id' => $processConfirmResult['contribution']->id,
]);

//Process on behalf contribution.
unset($form->_params['contribution_id']);
$form->_contactID = $form->_values['related_contact'] = $form->_params['onbehalf_contact_id'] = $contactID;
$form->_params['contact_id'] = $this->organizationCreate();
$form->_contactID = $form->_values['related_contact'] = $form->_params['onbehalf_contact_id'] = $individualID;
$organizationID = $this->organizationCreate();
$form->_params['contact_id'] = $organizationID;
$this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $contactID,
'contact_id_b' => $form->_params['contact_id'],
'contact_id_a' => $individualID,
'contact_id_b' => $organizationID,
'relationship_type_id' => 5,
'is_current_employer' => 1,
]);

$form->_params['onbehalf_contact_id'] = $individualID;
$form->_values['id'] = $contributionPageID1;
$form->processConfirm(
$form->_params,
$form->_params['onbehalf_contact_id'],
$form->_values['financial_type_id'],
0, FALSE
$organizationID,
CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
0, TRUE
);
//check if contribution is created on org.
$contribution = $this->callAPISuccessGetSingle('Contribution', [
'contact_id' => $form->_params['onbehalf_contact_id'],
'contact_id' => $organizationID,
]);

$activity = civicrm_api3('Activity', 'get', [
'sequential' => 1,
$activity = $this->callAPISuccessGetSingle('Activity', [
'source_record_id' => $contribution['id'],
'contact_id' => $form->_params['onbehalf_contact_id'],
'activity_type_id' => 'Contribution',
'return' => 'target_contact_id',
]);
$this->assertEquals([$form->_params['contact_id']], $activity['target_contact_id']);
$this->assertEquals($individualID, $activity['source_contact_id']);
$repeatContribution = $this->callAPISuccess('Contribution', 'repeattransaction', [
'original_contribution_id' => $contribution['id'],
'contribution_status_id' => 'Pending',
'api.Payment.create' => [
'total_amount' => 100,
'payment_processor_id' => $paymentProcessorID,
],
]);
$this->assertEquals(1, $activity['count']);
$activity = $this->callAPISuccessGetSingle('Activity', [
'source_record_id' => $repeatContribution['id'],
'activity_type_id' => 'Contribution',
'return' => ['target_contact_id', 'source_contact_id'],
]);
$this->assertEquals([$organizationID], $activity['target_contact_id']);
$this->assertEquals($individualID, $activity['source_contact_id']);
}

/**
* @param array $params
*
* @return mixed
* @throws \CRM_Core_Exception
*/
protected function createContributionPage(array $params): int {
return (int) $this->callAPISuccess('ContributionPage', 'create', array_merge([
'title' => 'Test Contribution Page',
'financial_type_id' => 'Campaign Contribution',
'currency' => 'USD',
'financial_account_id' => 1,
'is_active' => 1,
'is_allow_other_amount' => 1,
'min_amount' => 20,
'max_amount' => 2000,
], $params))['id'];
}

}