Minor readability improvement
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 11 Apr 2022 01:16:11 +0000 (13:16 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 11 Apr 2022 01:16:11 +0000 (13:16 +1200)
This just adds isSuccess as a function to avoid having to understand what
response code is.

I grepped & didn't find 'response_code' outside the class/test

I also confirmed that the retrieve function casts to an int (via
validate) so the triple = is correct

CRM/Core/Payment/AuthorizeNetIPN.php

index 939bd86836b1158de34b19766bc10a1613423276..941dbdc943023d952e4e9dca64b00621fbe17d78 100644 (file)
@@ -118,7 +118,7 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
     $now = date('YmdHis');
 
     $isFirstOrLastRecurringPayment = FALSE;
-    if ($input['response_code'] == 1) {
+    if ($this->isSuccess()) {
       // Approved
       if ($first) {
         $recur->start_date = $now;
@@ -168,7 +168,6 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
   public function getInput(&$input) {
     $input['amount'] = $this->retrieve('x_amount', 'String');
     $input['subscription_id'] = $this->getRecurProcessorID();
-    $input['response_code'] = $this->retrieve('x_response_code', 'Integer');
     $input['response_reason_code'] = $this->retrieve('x_response_reason_code', 'String', FALSE);
     $input['response_reason_text'] = $this->retrieve('x_response_reason_text', 'String', FALSE);
     $input['subscription_paynum'] = $this->retrieve('x_subscription_paynum', 'Integer', FALSE, 0);
@@ -180,7 +179,7 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
     }
     // Only assume trxn_id 'should' have been returned for success.
     // Per CRM-17611 it would also not be passed back for a decline.
-    elseif ($input['response_code'] == 1) {
+    elseif ($this->isSuccess()) {
       $input['is_test'] = 1;
       $input['trxn_id'] = md5(uniqid(rand(), TRUE));
     }
@@ -201,6 +200,16 @@ class CRM_Core_Payment_AuthorizeNetIPN extends CRM_Core_Payment_BaseIPN {
     }
   }
 
+  /**
+   * Was the transaction successful.
+   *
+   * @return bool
+   * @throws \CRM_Core_Exception
+   */
+  private function isSuccess(): bool {
+    return $this->retrieve('x_response_code', 'Integer') === 1;
+  }
+
   /**
    * Get ids from input.
    *