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 8fb8e7a commit c04ab2b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 32 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
112 changes: 80 additions & 32 deletions tests/phpunit/CRM/Event/Form/ParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,51 +188,53 @@ public function testSubmitWithPayment($thousandSeparator, $fromEmails = []) {
];
}
$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($fromEmails, $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']);
if (empty($fromEmails)) {
$fromEmails = [
'from_email_id' => ['abc@gmail.com' => 1],
];
}
$form->_fromEmails = $fromEmails;
try {
$form->submit($this->getSubmitParams($fromEmails, $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 @@ -286,6 +288,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 +412,49 @@ protected function createParticipantRecordsFromTwoFieldPriceSet() {
]);
}

/**
* Get params for submit function.
*
* @param array $fromEmails
* @param int $eventID
* @param int $paymentProcessorID
*
* @return array
*/
private function getSubmitParams(array $fromEmails, 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' => array_keys($fromEmails)[0],
'send_receipt' => 1,
'receipt_text' => '',
];
return $submitParams;
}

}

0 comments on commit c04ab2b

Please sign in to comment.