dev/financial#152 Pass contribution directly to completeOrder
[civicrm-core.git] / CRM / Core / Payment / PayPalIPN.php
index 9d3d2e3936073d2bbf25006643ddcf6fa3c1c5ad..6f600ccd1dbb84bb83815db053dcc0d6d4dc4757 100644 (file)
@@ -206,9 +206,11 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
       return;
     }
 
-    $this->single($input, $ids, $objects,
-      TRUE, $first
-    );
+    $this->single($input, [
+      'related_contact' => $ids['related_contact'] ?? NULL,
+      'participant' => !empty($objects['participant']) ? $objects['participant']->id : NULL,
+      'contributionRecur' => !empty($objects['contributionRecur']) ? $objects['contributionRecur']->id : NULL,
+    ], $objects, TRUE);
   }
 
   /**
@@ -216,25 +218,19 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
    * @param array $ids
    * @param array $objects
    * @param bool $recur
-   * @param bool $first
    *
    * @return void
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function single($input, $ids, $objects, $recur = FALSE, $first = FALSE) {
+  public function single($input, $ids, $objects, $recur = FALSE) {
     $contribution = &$objects['contribution'];
 
     // make sure the invoice is valid and matches what we have in the contribution record
-    if ((!$recur) || ($recur && $first)) {
-      if ($contribution->invoice_id != $input['invoice']) {
-        Civi::log()->debug('PayPalIPN: Invoice values dont match between database and IPN request. (ID: ' . $contribution->id . ').');
-        echo "Failure: Invoice values dont match between database and IPN request<p>";
-        return;
-      }
-    }
-    else {
-      $contribution->invoice_id = md5(uniqid(rand(), TRUE));
+    if ($contribution->invoice_id != $input['invoice']) {
+      Civi::log()->debug('PayPalIPN: Invoice values dont match between database and IPN request. (ID: ' . $contribution->id . ').');
+      echo "Failure: Invoice values dont match between database and IPN request<p>";
+      return;
     }
 
     if (!$recur) {
@@ -256,11 +252,7 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
       return;
     }
 
-    CRM_Contribute_BAO_Contribution::completeOrder($input, [
-      'related_contact' => $ids['related_contact'] ?? NULL,
-      'participant' => !empty($objects['participant']) ? $objects['participant']->id : NULL,
-      'contributionRecur' => !empty($objects['contributionRecur']) ? $objects['contributionRecur']->id : NULL,
-    ], $objects);
+    CRM_Contribute_BAO_Contribution::completeOrder($input, $ids, $objects['contribution']);
   }
 
   /**
@@ -335,6 +327,8 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
         return;
       }
 
+      $input['payment_processor_id'] = $paymentProcessorID;
+
       self::$_paymentProcessor = &$objects['paymentProcessor'];
       if ($component == 'contribute') {
         if ($ids['contributionRecur']) {
@@ -365,7 +359,11 @@ class CRM_Core_Payment_PayPalIPN extends CRM_Core_Payment_BaseIPN {
         Civi::log()->debug('Returning since contribution status is not handled');
         return;
       }
-      $this->single($input, $ids, $objects);
+      $this->single($input, [
+        'related_contact' => $ids['related_contact'] ?? NULL,
+        'participant' => !empty($objects['participant']) ? $objects['participant']->id : NULL,
+        'contributionRecur' => !empty($objects['contributionRecur']) ? $objects['contributionRecur']->id : NULL,
+      ], $objects);
     }
     catch (CRM_Core_Exception $e) {
       Civi::log()->debug($e->getMessage());