Skip to content

Commit

Permalink
Add test for failed payment
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 2, 2019
1 parent 1316177 commit 65f1a9a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CRM/Core/Payment/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* $Id: Dummy.php 45429 2013-02-06 22:11:18Z lobo $
*/

use Civi\Payment\Exception\PaymentProcessorException;

/**
* Dummy payment processor
*/
Expand Down Expand Up @@ -73,6 +75,7 @@ public function __construct($mode, &$paymentProcessor) {
*
* @return array
* the result in a nice formatted array (or an error object)
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public function doDirectPayment(&$params) {
// Invoke hook_civicrm_paymentProcessor
Expand All @@ -99,6 +102,9 @@ public function doDirectPayment(&$params) {
//end of hook invocation
if (!empty($this->_doDirectPaymentResult)) {
$result = $this->_doDirectPaymentResult;
if (CRM_Utils_Array::value('payment_status_id', $result) === 'failed') {
throw new PaymentProcessorException($result['message'] ?? 'failed');
}
$result['trxn_id'] = array_shift($this->_doDirectPaymentResult['trxn_id']);
return $result;
}
Expand Down
137 changes: 94 additions & 43 deletions tests/phpunit/CRM/Event/Form/ParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testSubmitUnpaidPriceChangeWhileStillPending() {
}
$this->assertEquals(55, $sum);

CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participants['id'], 'participant', $contribution['id'], $this->eventFeeBlock, $lineItem, 100);
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participants['id'], 'participant', $contribution['id'], $this->eventFeeBlock, $lineItem);
$lineItem = CRM_Price_BAO_LineItem::getLineItems($participants['id'], 'participant');
// Participants is updated to 0 but line remains.
$this->assertEquals(0, $lineItem[1]['subTotal']);
Expand Down Expand Up @@ -170,69 +170,65 @@ public function testPaymentAllocationOnMultiLineItemEvent() {
* Initial test of submit function.
*
* @param string $thousandSeparator
* @param array $fromEmails From Emails array to overwrite the default.
*
* @dataProvider getThousandSeparators
*
* @throws \Exception
*/
public function testSubmitWithPayment($thousandSeparator, $fromEmails = []) {
public function testSubmitWithPayment($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
$form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
$form->_mode = 'Live';
$form->_quickConfig = TRUE;
$form->_fromEmails = [
'from_email_id' => ['abc@gmail.com' => 1],
];
$paymentProcessorID = $this->processorCreate(['is_test' => 0]);
if (empty($fromEmails)) {
$fromEmails = [
'from_email_id' => ['abc@gmail.com' => 1],
];
}
$form->_fromEmails = $fromEmails;
$form->submit([
'register_date' => date('Ymd'),
'status_id' => 1,
'role_id' => 1,
'event_id' => $form->_eventId,
'credit_card_number' => 4444333322221111,
'cvv2' => 123,
'credit_card_exp_date' => [
'M' => 9,
'Y' => 2025,
],
'credit_card_type' => 'Visa',
'billing_first_name' => 'Junko',
'billing_middle_name' => '',
'billing_last_name' => 'Adams',
'billing_street_address-5' => '790L Lincoln St S',
'billing_city-5' => 'Maryknoll',
'billing_state_province_id-5' => 1031,
'billing_postal_code-5' => 10545,
'billing_country_id-5' => 1228,
'payment_processor_id' => $paymentProcessorID,
'priceSetId' => '6',
'price_7' => [
13 => 1,
],
'amount_level' => 'Too much',
'fee_amount' => $this->formatMoneyInput(1550.55),
'total_amount' => $this->formatMoneyInput(1550.55),
'from_email_address' => array_keys($form->_fromEmails['from_email_id'])[0],
'send_receipt' => 1,
'receipt_text' => '',
]);
$form->submit($this->getSubmitParams($form->_eventId, $paymentProcessorID));
$participants = $this->callAPISuccess('Participant', 'get', []);
$this->assertEquals(1, $participants['count']);
$contribution = $this->callAPISuccessGetSingle('Contribution', []);
$this->assertEquals(1550.55, $contribution['total_amount']);
$this->assertEquals('Debit Card', $contribution['payment_instrument']);
}

/**
* Initial test of submit function.
*
* @param string $thousandSeparator
* @param array $fromEmails From Emails array to overwrite the default.
*
* @dataProvider getThousandSeparators
*
* @throws \Exception
*/
public function testSubmitWithFailedPayment($thousandSeparator, $fromEmails = []) {
$this->setCurrencySeparators($thousandSeparator);
$form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
$form->_mode = 'Live';
$form->_quickConfig = TRUE;
$paymentProcessorID = $this->processorCreate(['is_test' => 0]);
Civi\Payment\System::singleton()->getById($paymentProcessorID)->setDoDirectPaymentResult(['payment_status_id' => 'failed']);

$form->_fromEmails = [
'from_email_id' => ['abc@gmail.com' => 1],
];
try {
$form->submit($this->getSubmitParams($form->_eventId, $paymentProcessorID));
}
catch (CRM_Core_Exception_PrematureExitException $e) {
return;
}
$this->fail('should have hit premature exit');
}

/**
* Test offline participant mail.
*
* @param string $thousandSeparator
*
* @dataProvider getThousandSeparators
* @throws \Exception
*/
public function testParticipantOfflineReceipt($thousandSeparator) {
$this->setCurrencySeparators($thousandSeparator);
Expand Down Expand Up @@ -269,10 +265,20 @@ public function testParticipantOfflineReceipt($thousandSeparator) {
]);

// Use the email created as the from email ensuring we are passing a numeric from to test dev/core#1069
$this->testSubmitWithPayment($thousandSeparator, ['from_email_id' => [$email['id'] => 1]]);
$this->setCurrencySeparators($thousandSeparator);
$form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
$form->_mode = 'Live';
$form->_quickConfig = TRUE;
$form->_fromEmails = [
'from_email_id' => [$email['id'] => 1],
];
$paymentProcessorID = $this->processorCreate(['is_test' => 0]);
$submitParams = $this->getSubmitParams($form->_eventId, $paymentProcessorID);
$submitParams['from_email_address'] = $email['id'];
$form->submit($submitParams);
//Check if type is correctly populated in mails.
//Also check the string email is present not numeric from.
$mail = $mut->checkMailLog([
$mut->checkMailLog([
'<p>Test event type - 1</p>',
'testloggedinreceiptemail@civicrm.org',
$this->formatMoneyInput(1550.55),
Expand All @@ -286,6 +292,7 @@ public function testParticipantOfflineReceipt($thousandSeparator) {
* @param array $eventParams
*
* @return CRM_Event_Form_Participant
*
* @throws \CRM_Core_Exception
*/
protected function getForm($eventParams = []) {
Expand Down Expand Up @@ -409,4 +416,48 @@ protected function createParticipantRecordsFromTwoFieldPriceSet() {
]);
}

/**
* Get params for submit function.
*
* @param int $eventID
* @param int $paymentProcessorID
*
* @return array
*/
private function getSubmitParams(int $eventID, int $paymentProcessorID): array {
$submitParams = [
'register_date' => date('Ymd'),
'status_id' => 1,
'role_id' => 1,
'event_id' => $eventID,
'credit_card_number' => 4444333322221111,
'cvv2' => 123,
'credit_card_exp_date' => [
'M' => 9,
'Y' => 2025,
],
'credit_card_type' => 'Visa',
'billing_first_name' => 'Junko',
'billing_middle_name' => '',
'billing_last_name' => 'Adams',
'billing_street_address-5' => '790L Lincoln St S',
'billing_city-5' => 'Maryknoll',
'billing_state_province_id-5' => 1031,
'billing_postal_code-5' => 10545,
'billing_country_id-5' => 1228,
'payment_processor_id' => $paymentProcessorID,
'priceSetId' => '6',
'price_7' => [
13 => 1,
],
'amount_level' => 'Too much',
'fee_amount' => $this->formatMoneyInput(1550.55),
'total_amount' => $this->formatMoneyInput(1550.55),
'from_email_address' => 'abc@gmail.com',
'send_receipt' => 1,
'receipt_text' => '',
];
return $submitParams;
}

}

0 comments on commit 65f1a9a

Please sign in to comment.