Skip to content

Commit

Permalink
Convert remaining Authorize.net test to use guzzle
Browse files Browse the repository at this point in the history
This allows us to remove the handling for when a.net doesn't reply
  • Loading branch information
eileenmcnaughton committed Jun 2, 2020
1 parent 3e401df commit c438341
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 58 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/Payment/AuthorizeNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function doRecurPayment() {
$intervalLength *= 12;
$intervalUnit = 'months';
}
elseif ($intervalUnit == 'day') {
elseif ($intervalUnit === 'day') {
$intervalUnit = 'days';
}
elseif ($intervalUnit == 'month') {
Expand Down
102 changes: 45 additions & 57 deletions tests/phpunit/CRM/Core/Payment/AuthorizeNetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testCreateSingleNowDated() {
$contactId = $this->individualCreate($nameParams);

$invoiceID = 123456;
$amount = rand(100, 1000) . '.00';
$amount = 7;

$recur = $this->callAPISuccess('ContributionRecur', 'create', [
'contact_id' => $contactId,
Expand All @@ -72,7 +72,7 @@ public function testCreateSingleNowDated() {

$contribution = $this->callAPISuccess('Contribution', 'create', [
'contact_id' => $contactId,
'financial_type_id' => $this->_financialTypeId,
'financial_type_id' => 'Donation',
'receive_date' => date('Ymd'),
'total_amount' => $amount,
'invoice_id' => $invoiceID,
Expand Down Expand Up @@ -156,7 +156,7 @@ public function testCreateSingleNowDated() {

// turn verifySSL off
Civi::settings()->set('verifySSL', '0');
$this->doPayment($params);
$this->processor->doPayment($params);
// turn verifySSL on
Civi::settings()->set('verifySSL', '0');

Expand Down Expand Up @@ -187,16 +187,18 @@ public function testCreateSingleNowDated() {
* Create a single post dated payment as a recurring transaction.
*/
public function testCreateSinglePostDated() {
$start_date = date('Ymd', strtotime("+ 1 week"));
$this->createMockHandler([$this->getExpectedResponse()]);
$this->setUpClientWithHistoryContainer();
$this->processor->setGuzzleClient($this->getGuzzleClient());
$start_date = date('Ymd', strtotime('+ 1 week'));

$firstName = 'John_' . substr(sha1(rand()), 0, 7);
$lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
$firstName = 'John';
$lastName = 'Smith';
$nameParams = ['first_name' => $firstName, 'last_name' => $lastName];
$contactId = $this->individualCreate($nameParams);

$ids = ['contribution' => NULL];
$invoiceID = sha1(rand());
$amount = rand(100, 1000) . '.00';
$invoiceID = 123456;
$amount = 70.23;

$contributionRecurParams = [
'contact_id' => $contactId,
Expand All @@ -208,11 +210,11 @@ public function testCreateSinglePostDated() {
'start_date' => $start_date,
'create_date' => date('Ymd'),
'invoice_id' => $invoiceID,
'contribution_status_id' => 2,
'contribution_status_id' => '',
'is_test' => 1,
'payment_processor_id' => $this->_paymentProcessorID,
];
$recur = CRM_Contribute_BAO_ContributionRecur::add($contributionRecurParams, $ids);
$recur = $this->callAPISuccess('ContributionRecur', 'create', $contributionRecurParams);

$contributionParams = [
'contact_id' => $contactId,
Expand All @@ -221,18 +223,18 @@ public function testCreateSinglePostDated() {
'total_amount' => $amount,
'invoice_id' => $invoiceID,
'currency' => 'USD',
'contribution_recur_id' => $recur->id,
'contribution_recur_id' => $recur['id'],
'is_test' => 1,
'contribution_status_id' => 2,
];

$contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
$contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);

$params = [
'qfKey' => '00ed21c7ca00a1f7d555555596ef7_4454',
'hidden_CreditCard' => 1,
'billing_first_name' => $firstName,
'billing_middle_name' => "",
'billing_middle_name' => '',
'billing_last_name' => $lastName,
'billing_street_address-5' => '8 Hobbitton Road',
'billing_city-5' => 'The Shire',
Expand All @@ -259,7 +261,7 @@ public function testCreateSinglePostDated() {
'price_set_id' => '',
'total_amount' => $amount,
'currency' => 'USD',
'source' => "Mordor",
'source' => 'Mordor',
'soft_credit_to' => '',
'soft_contact_id' => '',
'billing_state_province-5' => 'IL',
Expand All @@ -269,24 +271,24 @@ public function testCreateSinglePostDated() {
'year' => 2022,
'month' => 10,
'ip_address' => '127.0.0.1',
'amount' => 70,
'amount' => 70.23,
'amount_level' => 0,
'currencyID' => 'USD',
'pcp_display_in_roll' => "",
'pcp_roll_nickname' => "",
'pcp_personal_note' => "",
'non_deductible_amount' => "",
'fee_amount' => "",
'net_amount' => "",
'invoice_id' => "",
'contribution_page_id' => "",
'pcp_display_in_roll' => '',
'pcp_roll_nickname' => '',
'pcp_personal_note' => '',
'non_deductible_amount' => '',
'fee_amount' => '',
'net_amount' => '',
'invoice_id' => '',
'contribution_page_id' => '',
'thankyou_date' => NULL,
'honor_contact_id' => NULL,
'invoiceID' => $invoiceID,
'first_name' => $firstName,
'middle_name' => 'bob',
'last_name' => $lastName,
'street_address' => '8 Hobbiton Road' . uniqid(),
'street_address' => '8 Hobbiton Road',
'city' => 'The Shire',
'state_province' => 'IL',
'postal_code' => 5010,
Expand All @@ -295,60 +297,46 @@ public function testCreateSinglePostDated() {
'email' => "{$firstName}.{$lastName}@example.com",
'contactID' => $contactId,
'contributionID' => $contribution['id'],
'contributionRecurID' => $recur->id,
'contributionRecurID' => $recur['id'],
];

// if cancel-subscription has been called earlier 'subscriptionType' would be set to cancel.
// to make a successful call for another trxn, we need to set it to something else.
$smarty = CRM_Core_Smarty::singleton();
$smarty->assign('subscriptionType', 'create');

// turn verifySSL off
Civi::settings()->set('verifySSL', '0');
$this->doPayment($params);
// turn verifySSL on
Civi::settings()->set('verifySSL', '0');
$this->processor->doPayment($params);

// if subscription was successful, processor_id / subscription-id must not be null
$this->assertDBNotNull('CRM_Contribute_DAO_ContributionRecur', $recur->id, 'processor_id',
$this->assertDBNotNull('CRM_Contribute_DAO_ContributionRecur', $recur['id'], 'processor_id',
'id', 'Failed to create subscription with Authorize.'
);

// cancel it or the transaction will be rejected by A.net if the test is re-run
$subscriptionID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $recur->id, 'processor_id');
$subscriptionID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionRecur', $recur['id'], 'processor_id');
$message = '';
$result = $this->processor->cancelSubscription($message, ['subscriptionId' => $subscriptionID]);
$this->assertTrue($result, 'Failed to cancel subscription with Authorize.');
}

/**
* Process payment against the Authorize.net test server.
*
* Skip the test if the server is unresponsive.
*
* @param array $params
*
* @throws PHPUnit\Framework\SkippedTestError
*/
public function doPayment($params) {
try {
$this->processor->doPayment($params);
}
catch (Exception $e) {
$this->assertTrue((strpos($e->getMessage(), 'E00001: Internal Error Occurred.') !== FALSE),
'AuthorizeNet failed for unknown reason.' . $e->getMessage());
$this->markTestSkipped('AuthorizeNet test server is not in a good mood so we can\'t test this right now');
}
$response = $this->getResponseBodies();
$this->assertEquals($this->getExpectedResponse(), $response[0], 3);
$requests = $this->getRequestBodies();
$this->assertEquals($this->getExpectedRequest($start_date, 70.23, 3, 4007000000027, '2022-10'), $requests[0]);
}

/**
* Get the content that we expect to see sent out.
*
* @param string $startDate
*
* @param int $amount
* @param int $occurrences
* @param int $cardNumber
* @param string $cardExpiry
*
* @return string
*/
public function getExpectedRequest($startDate) {
public function getExpectedRequest($startDate, $amount = 7, $occurrences = 12, $cardNumber = 4444333322221111, $cardExpiry = '2025-09') {
return '<?xml version="1.0" encoding="utf-8"?>
<ARBCreateSubscriptionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<merchantAuthentication>
Expand All @@ -363,13 +351,13 @@ public function getExpectedRequest($startDate) {
<unit>months</unit>
</interval>
<startDate>' . $startDate . '</startDate>
<totalOccurrences>12</totalOccurrences>
<totalOccurrences>' . $occurrences . '</totalOccurrences>
</paymentSchedule>
<amount>7</amount>
<amount>' . $amount . '</amount>
<payment>
<creditCard>
<cardNumber>4444333322221111</cardNumber>
<expirationDate>2025-09</expirationDate>
<cardNumber>' . $cardNumber . '</cardNumber>
<expirationDate>' . $cardExpiry . '</expirationDate>
</creditCard>
</payment>
<order>
Expand Down

0 comments on commit c438341

Please sign in to comment.