*/
protected $participantRecord;
+ /**
+ * Params for creating a payment to add to the contribution.
+ *
+ * @var array
+ */
+ protected $createPaymentParams = [];
+
+ /**
+ * Get params to create payments.
+ *
+ * @return array
+ */
+ public function getCreatePaymentParams(): array {
+ return $this->createPaymentParams;
+ }
+
+ /**
+ * Set params to create payments.
+ *
+ * @param array $createPaymentParams
+ */
+ public function setCreatePaymentParams(array $createPaymentParams) {
+ $this->createPaymentParams = $createPaymentParams;
+ }
+
/**
* Explicitly declare the entity api name.
*/
// CRM-13964 partial_payment_total
if ($amountOwed > $params['total_amount']) {
// the owed amount
- $contributionParams['partial_payment_total'] = $amountOwed;
- // the actual amount paid
- $contributionParams['partial_amount_to_pay'] = $params['total_amount'];
- $this->assign('balanceAmount', $contributionParams['partial_payment_total'] - $contributionParams['partial_amount_to_pay']);
+ $contributionParams['total_amount'] = $amountOwed;
+ $contributionParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
+ $this->assign('balanceAmount', $amountOwed - $params['total_amount']);
+ $this->storePaymentCreateParams($params);
}
}
}
}
foreach ($contributions as $contribution) {
- if ('Partially paid' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id)) {
- CRM_Contribute_BAO_Contribution::addPayments($contribution);
+ if (!empty($this->getCreatePaymentParams())) {
+ civicrm_api3('Payment', 'create', array_merge(['contribution_id' => $contribution->id], $this->getCreatePaymentParams()));
}
}
}
}
}
- $this->assign('totalAmount', $contributionParams['total_amount']);
+ $this->assign('totalAmount', $params['total_amount'] ?? $contributionParams['total_amount']);
$this->assign('isPrimary', 1);
$this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params));
}
return '';
}
+ /**
+ * Store the parameters to create a payment, if approprite, on the form.
+ *
+ * @param array $params
+ * Params as submitted.
+ */
+ protected function storePaymentCreateParams($params) {
+ if ('Completed' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $params['contribution_status_id'])) {
+ $this->setCreatePaymentParams([
+ 'total_amount' => $params['total_amount'],
+ 'is_send_contribution_notification' => FALSE,
+ 'payment_instrument_id' => $params['payment_instrument_id'],
+ 'trxn_date' => $params['receive_date'] ?? date('Y-m-d'),
+ 'trxn_id' => $params['trxn_id'],
+ 'pan_truncation' => $params['pan_truncation'] ?? '',
+ 'card_type_id' => $params['card_type_id'] ?? '',
+ 'check_number' => $params['check_number'] ?? '',
+ 'skipCleanMoney' => TRUE,
+ ]);
+ }
+ }
+
}
$event = $this->eventCreate($eventParams);
}
- $contactID = $this->individualCreate();
+ $this->ids['contact']['event'] = (int) $this->individualCreate();
/** @var CRM_Event_Form_Participant $form */
$form = $this->getFormObject('CRM_Event_Form_Participant');
$form->_single = TRUE;
- $form->_contactID = $form->_contactId = $contactID;
+ $form->_contactID = $form->_contactId = $this->ids['contact']['event'];
$form->setCustomDataTypes();
$form->_eventId = $event['id'];
if (!empty($eventParams['is_monetary'])) {
* @param bool $isQuickConfig
*
* @throws \CRM_Core_Exception
- * @throws \CiviCRM_API3_Exception
*/
public function testSubmitPartialPayment($isQuickConfig) {
$mut = new CiviMailUtils($this, TRUE);
'hidden_custom' => '1',
'hidden_custom_group_count' => ['' => 1],
'custom_4_-1' => '',
- 'contact_id' => $form->_contactID,
+ 'contact_id' => $this->getContactID(),
'event_id' => $this->getEventID(),
'campaign_id' => '',
'register_date' => '2020-01-31 00:50:00',
'MAX_FILE_SIZE' => '33554432',
];
$form->submit($submitParams);
+ $this->assertPartialPaymentResult($isQuickConfig, $mut);
+ }
+
+ /**
+ * Test submitting a partially paid event registration, recording a pending contribution.
+ *
+ * This tests
+ *
+ * @dataProvider getBooleanDataProvider
+ *
+ * @param bool $isQuickConfig
+ *
+ * @throws \CRM_Core_Exception
+ * @throws \CiviCRM_API3_Exception
+ */
+ public function testSubmitPendingPartiallyPaidAddPayment($isQuickConfig) {
+ $mut = new CiviMailUtils($this, TRUE);
+ $form = $this->getForm(['is_monetary' => 1]);
+ $this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID()]);
+ $paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
+ $submitParams = $this->getRecordContributionParams('Partially paid', $form);
+ $form->submit($submitParams);
+ $this->callAPISuccess('Payment', 'create', [
+ 'contribution_id' => $this->callAPISuccessGetValue('Contribution', ['return' => 'id']),
+ 'total_amount' => 20,
+ 'check_number' => 879,
+ 'payment_instrument_id' => $paymentInstrumentID,
+ ]);
+ $this->assertPartialPaymentResult($isQuickConfig, $mut);
+ }
+
+ /**
+ * Test submitting a partially paid event registration, recording a pending contribution.
+ *
+ * This tests
+ *
+ * @dataProvider getBooleanDataProvider
+ *
+ * @param bool $isQuickConfig
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testSubmitPendingAddPayment($isQuickConfig) {
+ $mut = new CiviMailUtils($this, TRUE);
+ $form = $this->getForm(['is_monetary' => 1]);
+ $this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID()]);
+ $paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
+ $submitParams = $this->getRecordContributionParams('Pending from pay later', 'Pending');
+ // Create the pending contribution for the full amount to be paid.
+ $submitParams['total_amount'] = 1550.55;
+ $form->submit($submitParams);
+ $this->callAPISuccess('Payment', 'create', [
+ 'contribution_id' => $this->callAPISuccessGetValue('Contribution', ['return' => 'id']),
+ 'total_amount' => 20,
+ 'check_number' => 879,
+ 'payment_instrument_id' => $paymentInstrumentID,
+ ]);
+ $this->assertPartialPaymentResult($isQuickConfig, $mut, FALSE);
+ }
+
+ /**
+ * @param bool $isQuickConfig
+ * @param \CiviMailUtils $mut
+ * @param bool $isAmountPaidOnForm
+ * Was the amount paid entered on the form (if so this should be on the receipt)
+ */
+ protected function assertPartialPaymentResult($isQuickConfig, CiviMailUtils $mut, $isAmountPaidOnForm = TRUE) {
+ $paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$contribution = $this->callAPISuccessGetSingle('Contribution', []);
$expected = [
- 'contact_id' => $form->_contactID,
+ 'contact_id' => $this->getContactID(),
'total_amount' => '1550.55',
'fee_amount' => '0.00',
- 'net_amount' => '20.00',
+ 'net_amount' => '1550.55',
'contribution_source' => 'I wrote this',
'amount_level' => '',
'is_template' => '0',
$participant = $this->callAPISuccessGetSingle('Participant', []);
$this->assertAttributesEquals([
- 'contact_id' => $form->_contactID,
+ 'contact_id' => $this->getContactID(),
'event_title' => 'Annual CiviCRM meet',
'participant_fee_level' => [0 => 'big - 1'],
'participant_fee_amount' => '1550.55',
'tax_amount' => '0.00',
], $lineItem);
- $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', ['is_payment' => 0]);
- $this->assertAttributesEquals([
- 'to_financial_account_id' => '7',
- 'total_amount' => '1550.55',
- 'fee_amount' => '0.00',
- 'net_amount' => '1550.55',
- 'currency' => 'USD',
- 'status_id' => '1',
- 'payment_instrument_id' => $paymentInstrumentID,
- 'check_number' => '879',
- ], $financialTrxn);
-
$payment = $this->callAPISuccessGetSingle('FinancialTrxn', ['is_payment' => 1]);
$this->assertAttributesEquals([
'to_financial_account_id' => 6,
$financialItem = $this->callAPISuccessGetSingle('FinancialItem', []);
$this->assertAttributesEquals([
'description' => 'big',
- 'contact_id' => $form->_contactID,
+ 'contact_id' => $this->getContactID(),
'amount' => 1550.55,
'currency' => 'USD',
- 'status_id' => 2,
+ 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_FinancialItem', 'status_id', 'Unpaid'),
'entity_table' => 'civicrm_line_item',
'entity_id' => $lineItem['id'],
'financial_account_id' => 4,
'Annual CiviCRM meet',
'Registered Email',
$isQuickConfig ? $this->formatMoneyInput(1550.55) . ' big - 1' : 'Price Field - big',
- 'Total Paid: $ 20.00',
+ $isAmountPaidOnForm ? 'Total Paid: $ 20.00' : ' ',
'Balance: $ 1,530.55',
'Financial Type: Event Fee',
'Paid By: Check',
return (int) $this->_ids['price_field_value'][1];
}
+ /**
+ * Get the parameters for recording a contribution.
+ *
+ * @param string $participantStatus
+ * @param string $contributionStatus
+ *
+ * @return array
+ */
+ protected function getRecordContributionParams($participantStatus, $contributionStatus): array {
+ $submitParams = [
+ 'hidden_feeblock' => '1',
+ 'hidden_eventFullMsg' => '',
+ 'priceSetId' => $this->getPriceSetID(),
+ $this->getPriceFieldKey() => $this->getPriceFieldValueID(),
+ 'check_number' => '879',
+ 'record_contribution' => '1',
+ 'financial_type_id' => '4',
+ 'receive_date' => '2020-01-31 00:51:00',
+ 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
+ 'trxn_id' => '',
+ 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contributionStatus),
+ 'total_amount' => '20',
+ 'send_receipt' => '1',
+ 'from_email_address' => $this->getFromEmailAddress(),
+ 'receipt_text' => 'Contact the Development Department if you need to make any changes to your registration.',
+ 'hidden_custom' => '1',
+ 'hidden_custom_group_count' => ['' => 1],
+ 'custom_4_-1' => '',
+ 'contact_id' => $this->getContactID(),
+ 'event_id' => $this->getEventID(),
+ 'campaign_id' => '',
+ 'register_date' => '2020-01-31 00:50:00',
+ 'role_id' => [0 => CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'role_id', 'Attendee')],
+ 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', $participantStatus),
+ 'source' => 'I wrote this',
+ 'note' => 'I wrote a note',
+ 'MAX_FILE_SIZE' => '33554432',
+ ];
+ return $submitParams;
+ }
+
/**
* Get the id of the created event.
*
return $this->ids['event']['event'];
}
+ /**
+ * Get created contact ID.
+ *
+ * @return int
+ */
+ protected function getContactID(): int {
+ return $this->ids['contact']['event'];
+ }
+
}