diff --git a/CRM/Contribute/BAO/Contribution/Utils.php b/CRM/Contribute/BAO/Contribution/Utils.php index ab6453648d4b..9663001056fa 100644 --- a/CRM/Contribute/BAO/Contribution/Utils.php +++ b/CRM/Contribute/BAO/Contribution/Utils.php @@ -85,7 +85,12 @@ public static function processConfirm( $paymentParams['contactID'] = $form->_params['contactID'] = $contactID; //fix for CRM-16317 - $form->_params['receive_date'] = date('YmdHis'); + if (empty($form->_params['receive_date'])) { + $form->_params['receive_date'] = date('YmdHis'); + } + if (!empty($form->_params['start_date'])) { + $form->_params['start_date'] = date('YmdHis'); + } $form->assign('receive_date', CRM_Utils_Date::mysqlToIso($form->_params['receive_date']) ); @@ -134,7 +139,7 @@ public static function processConfirm( $paymentParams['source'] = $paymentParams['contribution_source']; } - if ($form->_values['is_recur'] && $contribution->contribution_recur_id) { + if (CRM_Utils_Array::value('is_recur', $form->_params) && $contribution->contribution_recur_id) { $paymentParams['contributionRecurID'] = $contribution->contribution_recur_id; } if (isset($paymentParams['contribution_source'])) { diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 9a43e70912ed..816c38ceeb15 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -949,10 +949,9 @@ public static function processFormContribution( $pledgeParams['frequency_day'] = 1; } $pledgeParams['create_date'] = $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = date("Ymd"); - $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($contribution->contribution_page_id); - if (CRM_Utils_Array::value('start_date', $params) || !CRM_Utils_Array::value('is_pledge_start_date_visible', $pledgeBlock)) { - $pledgeStartDate = CRM_Utils_Array::value('start_date', $params, NULL); - $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = CRM_Pledge_BAO_Pledge::getPledgeStartDate($pledgeStartDate, $pledgeBlock); + if (CRM_Utils_Array::value('start_date', $params)) { + $pledgeParams['frequency_day'] = intval(date("d", strtotime(CRM_Utils_Array::value('start_date', $params)))); + $pledgeParams['start_date'] = $pledgeParams['scheduled_date'] = date('Ymd', strtotime(CRM_Utils_Array::value('start_date', $params))); } $pledgeParams['status_id'] = $contribution->contribution_status_id; $pledgeParams['max_reminders'] = $form->_values['max_reminders']; @@ -1094,7 +1093,7 @@ public static function processRecurringContribution(&$form, &$params, $contactID $recurParams['start_date'] = $recurParams['create_date'] = $recurParams['modified_date'] = date('YmdHis'); if (!empty($params['receive_date'])) { - $recurParams['start_date'] = $params['receive_date']; + $recurParams['start_date'] = date('YmdHis', strtotime($params['receive_date'])); } $recurParams['invoice_id'] = CRM_Utils_Array::value('invoiceID', $params); $recurParams['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'); @@ -1111,7 +1110,6 @@ public static function processRecurringContribution(&$form, &$params, $contactID $campaignId = CRM_Utils_Array::value('campaign_id', $params, CRM_Utils_Array::value('campaign_id', $form->_values)); $recurParams['campaign_id'] = $campaignId; - $recurring = CRM_Contribute_BAO_ContributionRecur::add($recurParams); if (is_a($recurring, 'CRM_Core_Error')) { CRM_Core_Error::displaySessionError($recurring); @@ -1877,6 +1875,7 @@ public static function submit($params) { $form->_fields['billing_last_name'] = 1; // CRM-18854 - Set form values to allow pledge to be created for api test. if (CRM_Utils_Array::value('pledge_block_id', $params)) { + $form->_values['pledge_id'] = CRM_Utils_Array::value('pledge_id', $params, NULL); $form->_values['pledge_block_id'] = $params['pledge_block_id']; $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($params['id']); $form->_values['max_reminders'] = $pledgeBlock['max_reminders']; @@ -1972,9 +1971,10 @@ protected function processFormSubmission($contactID) { $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency; // CRM-18854 - if (CRM_Utils_Array::value('adjust_recur_start_date', $this->_values)) { + if (CRM_Utils_Array::value('is_pledge', $this->_params) && !CRM_Utils_Array::value('pledge_id', $this->_values) && CRM_Utils_Array::value('adjust_recur_start_date', $this->_values)) { $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::getPledgeBlock($this->_id); - if (CRM_Utils_Array::value('start_date', $this->_params) || !CRM_Utils_Array::value('is_pledge_start_date_visible', $pledgeBlock)) { + if (CRM_Utils_Array::value('start_date', $this->_params) || !CRM_Utils_Array::value('is_pledge_start_date_visible', $pledgeBlock) + || !CRM_Utils_Array::value('is_pledge_start_date_editable', $pledgeBlock)) { $pledgeStartDate = CRM_Utils_Array::value('start_date', $this->_params, NULL); $this->_params['receive_date'] = CRM_Pledge_BAO_Pledge::getPledgeStartDate($pledgeStartDate, $pledgeBlock); $recurParams = CRM_Pledge_BAO_Pledge::buildRecurParams($this->_params); diff --git a/CRM/Contribute/Form/ContributionPage/Amount.php b/CRM/Contribute/Form/ContributionPage/Amount.php index b646d04b94bf..bb381e69db56 100644 --- a/CRM/Contribute/Form/ContributionPage/Amount.php +++ b/CRM/Contribute/Form/ContributionPage/Amount.php @@ -518,7 +518,7 @@ public function postProcess() { 'calendar_month' => 'pledge_calendar_month', ); if ($params['pledge_default_toggle'] == 'contribution_date') { - $fieldValue = json_encode(array('contribution_date' => date('Ymd'))); + $fieldValue = json_encode(array('contribution_date' => date('m/d/Y'))); } else { foreach ($pledgeDateFields as $key => $pledgeDateField) { diff --git a/CRM/Pledge/BAO/Pledge.php b/CRM/Pledge/BAO/Pledge.php index 6eb92d21422f..c8594c0618a0 100644 --- a/CRM/Pledge/BAO/Pledge.php +++ b/CRM/Pledge/BAO/Pledge.php @@ -1204,15 +1204,17 @@ public static function buildRecurParams($params) { public static function getPledgeStartDate($date, $pledgeBlock) { $startDate = (array) json_decode($pledgeBlock['pledge_start_date']); list($field, $value) = each($startDate); - if (!CRM_Utils_Array::value('is_pledge_start_date_visible', $pledgeBlock)) { - return date('Ymd', strtotime($value)); - } - if (!CRM_Utils_Array::value('is_pledge_start_date_editable', $pledgeBlock)) { + if (!empty($date) && !CRM_Utils_Array::value('is_pledge_start_date_editable', $pledgeBlock)) { return $date; } + if (empty($date)) { + $date = $value; + } switch ($field) { case 'contribution_date': - $date = date('Ymd'); + if (empty($date)) { + $date = date('Ymd'); + } break; case 'calendar_date': diff --git a/CRM/Pledge/BAO/PledgeBlock.php b/CRM/Pledge/BAO/PledgeBlock.php index 357686bf4283..a35796dea4c7 100644 --- a/CRM/Pledge/BAO/PledgeBlock.php +++ b/CRM/Pledge/BAO/PledgeBlock.php @@ -310,8 +310,8 @@ public static function buildPledgeBlock($form) { switch ($field) { case 'contribution_date': $form->addDate('start_date', ts('First installment payment')); - $paymentDate = $value = date('d/m/Y'); - list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($value); + $paymentDate = $value = date('m/d/Y'); + list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults(NULL); $form->assign('is_date', TRUE); break; diff --git a/tests/phpunit/CRM/Pledge/BAO/PledgeTest.php b/tests/phpunit/CRM/Pledge/BAO/PledgeTest.php index 1b4634728307..2e07bb2fdbe4 100644 --- a/tests/phpunit/CRM/Pledge/BAO/PledgeTest.php +++ b/tests/phpunit/CRM/Pledge/BAO/PledgeTest.php @@ -148,13 +148,14 @@ public function testGetPledgeStartDate() { $this->assertEquals(date('m/d/Y', strtotime($date)), $paymentDate, "The two dates do not match"); // Try with fixed date + $date = NULL; $params = array( - 'pledge_start_date' => json_encode(array('contribution_date' => '2016-06-10')), + 'pledge_start_date' => json_encode(array('calendar_date' => '06/10/2016')), 'is_pledge_start_date_visible' => FALSE, ); $date = CRM_Pledge_BAO_Pledge::getPledgeStartDate($date, $params); - $this->assertEquals($date, '20160610', "The two dates do not match"); + $this->assertEquals(date('m/d/Y', strtotime($date)), '06/10/2016', "The two dates do not match"); } } diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 442bd348b558..f291b87456c3 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -1003,4 +1003,53 @@ public function testSubmitPledgePaymentPaymentProcessorRecurFuturePayment() { $this->assertEquals($recur['contribution_status_id'], 5); // In progress status. } + /** + * Test submit pledge payment. + * + * - test submitting a pledge payment using contribution form. + */ + public function testSubmitPledgePayment() { + $this->testSubmitPledgePaymentPaymentProcessorRecurFuturePayment(); + $pledge = $this->callAPISuccess('pledge', 'getsingle', array()); + $params = array( + 'pledge_id' => $pledge['id'], + ); + $submitParams = array( + 'id' => (int) $pledge['pledge_contribution_page_id'], + 'pledge_amount' => array(2 => 1), + 'billing_first_name' => 'Billy', + 'billing_middle_name' => 'Goat', + 'billing_last_name' => 'Gruff', + 'email' => 'billy@goat.gruff', + 'payment_processor_id' => 1, + 'credit_card_number' => '4111111111111111', + 'credit_card_type' => 'Visa', + 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040), + 'cvv2' => 123, + 'pledge_id' => $pledge['id'], + 'cid' => $pledge['contact_id'], + 'contact_id' => $pledge['contact_id'], + 'amount' => 100.00, + 'is_pledge' => TRUE, + 'pledge_block_id' => $this->_ids['pledge_block_id'], + ); + $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params); + $this->assertEquals($pledgePayment['values'][2]['status_id'], 2); + + $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL); + + // Check if contribution created. + $contribution = $this->callAPISuccess('contribution', 'getsingle', array( + 'contribution_page_id' => $pledge['pledge_contribution_page_id'], + 'contribution_status_id' => 'Completed', + 'contact_id' => $pledge['contact_id'], + 'contribution_recur_id' => array('IS NULL' => 1), + )); + + $this->assertEquals(100.00, $contribution['total_amount']); + $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params); + $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, "This pledge payment should have been completed"); + $this->assertEquals($pledgePayment['values'][2]['contribution_id'], $contribution['id']); + } + }