$this->assertEquals(0.01, $amountOwed, 'Amount does not match');
}
+ /**
+ * Test that financial trxns for fee amounts have the correct financial account.
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testFeeAmountTrxns() {
+ $contactId = $this->individualCreate();
+
+ // Get the expected financial account of a payment made with a credit card.
+ // Also et the financial account of a payment with the default payment method.
+ // I wish we could join here - maybe when API4 EntityBridge is complete.
+ $creditCardOvId = $this->callAPISuccess('OptionValue', 'getvalue', [
+ 'return' => "id",
+ 'option_group_id' => "payment_instrument",
+ 'name' => "Credit Card",
+ ]);
+ $defaultPaymentMethodOvId = $this->callAPISuccess('OptionValue', 'getvalue', [
+ 'return' => "id",
+ 'option_group_id' => "payment_instrument",
+ 'is_default' => 1,
+ ]);
+ $expectedFinancialAccountId = $this->callAPISuccess('EntityFinancialAccount', 'getvalue', [
+ 'return' => "financial_account_id",
+ 'entity_id' => $creditCardOvId,
+ ]);
+ $wrongFinancialAccountId = $this->callAPISuccess('EntityFinancialAccount', 'getvalue', [
+ 'return' => "financial_account_id",
+ 'entity_id' => $defaultPaymentMethodOvId,
+ ]);
+ // If these two are the same, there's no bug but this test is no longer valid and needs rewriting.
+ $this->assertNotEquals($expectedFinancialAccountId, $wrongFinancialAccountId, 'invalid test: Financial Account ID of credit card matches default payment method Financial Account ID');
+
+ // Create a credit card contribution with a fee amount.
+ $price = 100;
+ $cParams = [
+ 'contact_id' => $contactId,
+ 'total_amount' => $price,
+ 'financial_type_id' => 1,
+ 'is_active' => 1,
+ 'payment_instrument_id' => 'Credit Card',
+ 'fee_amount' => 3,
+ ];
+ $contribution = $this->callAPISuccess('Contribution', 'create', $cParams);
+ // Confirm that the from_financial_account_id amount on the fee trxn matches the expected value.
+ $trxnParams = [
+ 'sequential' => 1,
+ 'return' => ["financial_trxn_id.from_financial_account_id"],
+ 'entity_table' => "civicrm_contribution",
+ 'entity_id' => $contribution['id'],
+ 'financial_trxn_id.total_amount' => 3,
+ ];
+ $firstFeeTrxnFromAccountId = $this->callAPISuccess('EntityFinancialTrxn', 'get', $trxnParams)['values'][0]['financial_trxn_id.from_financial_account_id'];
+ $this->assertEquals($expectedFinancialAccountId, $firstFeeTrxnFromAccountId);
+
+ // dev/financial#160 - ensure the from_financial_account_id is correct on a trxn generated by a contribution edit.
+ $updatedContributionParams = [
+ 'id' => $contribution['id'],
+ 'fee_amount' => 5,
+ ];
+ $this->callAPISuccess('Contribution', 'create', $updatedContributionParams);
+
+ // 2 = 5 - 3.
+ $trxnParams['financial_trxn_id.total_amount'] = 2;
+ $secondFeeTrxnFromAccountId = $this->callAPISuccess('EntityFinancialTrxn', 'get', $trxnParams)['values'][0]['financial_trxn_id.from_financial_account_id'];
+ $this->assertEquals($expectedFinancialAccountId, $secondFeeTrxnFromAccountId);
+ }
+
}