CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • PaypalIPN for recurring - changing IDs
Pages: [1]

Author Topic: PaypalIPN for recurring - changing IDs  (Read 1941 times)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
PaypalIPN for recurring - changing IDs
February 24, 2011, 01:22:21 pm
We have had a situation where we have had to import some recurring contribution profiles from one Civi DB to another and have had to change the contact ids some recurring contributions were assigned to due to data entry error.

In order to cope with this we have hacked our version of Paypal Pro.ipn so that both the contribution_recur id and the contribution_id are looked up from the invoice_id (which is a unique hash generated by Civi). The contact_id is looked up off the civicrm_contribution_recur.contact_id.

Although our situation is pretty unique I can think of 2 situations where having one unique field & flexibility on the others would make sense:

1) 2 contacts merged through deduping - the contact_id in the Paypal IPN would no longer be correct
2) organisation migrating to CiviCRM has already set up recurring payments. If Civi respected the invoice ID these could be migrated into CiviCRM (not straight forward but without contacting the donor / having the credit card to re-initiate it)


Code: [Select]
Index: CRM/Core/Payment/PayPalProIPN.php
===================================================================
--- CRM/Core/Payment/PayPalProIPN.php (revision 32660)
+++ CRM/Core/Payment/PayPalProIPN.php (working copy)
@@ -300,6 +300,56 @@
             $ids['related_contact']     = self::retrieve('relatedContactID' , 'Integer', 'GET', false );
             $ids['onbehalf_dupe_alert'] = self::retrieve('onBehalfDupeAlert', 'Integer', 'GET', false );
         }
+        /*
+         * Fuzion Hack to deal with changed recurring ids. This overrides the
+         * contributionRecur number set in the IPN by the one in the database against that invoice
+         * number. This needs to be preserved until ALL recurring contributions started in January 2011 have
+         * expired.
+         */
+    if ($ids['contributionRecur']){
+      $sql = " SELECT cr.id
+               FROM civicrm_contribution_recur cr
+           WHERE cr.invoice_id = %1";
+      $sqlParams = array( 1 => array( self::getValue('i', false ),      'String' ),
+                              );
+                              echo  self::getValue('i', false );
+            if ( $contributionRecurID = CRM_Core_DAO::singleValueQuery( $sql, $sqlParams ) ) {
+                $ids['contributionRecur'] = $contributionRecurID;
+            }
+           
+    }
+    if ($ids['contribution']){
+ $sql = " SELECT c.id
+          FROM civicrm_contribution c
+           WHERE c.invoice_id = %1";
+     echo  self::getValue('i', false );
+
+ $sqlParams = array( 1 => array( self::getValue('i', false ),      'String' ),
+                              );
+              
+            if ( $contributionID = CRM_Core_DAO::singleValueQuery( $sql, $sqlParams ) ) {
+                $ids['contribution'] = $contributionID;
+            }
+
+ }
+ //end hack for changed contribution IDs & recurring IDs
+ /*
+ *Fuzion hack to look up 'correct' contact ID for recurring contributions incorrectly recorded against wrong contact
+ *(extended to all as she is not alone)
+ *but re-assigned. Recommend this is maintained until all recurring contributions initiated in Feb 2011 are finished
+ */
+         if (!empty($ids['contributionRecur'] )){
+       $sql = " SELECT cr.contact_id
+               FROM civicrm_contribution_recur cr
+           WHERE cr.id = %1";
+      $sqlParams = array( 1 => array( $ids['contributionRecur'],      'String' ),
+                              );
+            if ( $contactID = CRM_Core_DAO::singleValueQuery( $sql, $sqlParams ) ) {
+                $ids['contact'] = $contactID ;
+            }
+           
+ }
+ //end fuzion hack
 
         if ( !$ids['membership'] && $ids['contributionRecur'] ) {
             $sql = "
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: PaypalIPN for recurring - changing IDs
February 24, 2011, 01:34:47 pm
Update  - I just found an instance of someone who had been merged with a recurring profile.

contribution_recur.contact_id = current contact
contribution.contact_id for contributions created after merge = deleted contact's id


If you are reading this & want to check your DB for this use this SQL to check
Code: [Select]
SELECT * FROM civicrm_contribution, civicrm_contribution_recur
 WHERE civicrm_contribution.contact_id <>
 civicrm_contribution_recur.contact_id
 AND
civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id;

and this SQL to re-assign to the correct contact ID

Code: [Select]
UPDATE civicrm_contribution, civicrm_contribution_recur
SET civicrm_contribution.contact_id = civicrm_contribution_recur.contact_id
 WHERE civicrm_contribution.contact_id <>
 civicrm_contribution_recur.contact_id
AND
civicrm_contribution.contribution_recur_id = civicrm_contribution_recur.id;
« Last Edit: February 24, 2011, 01:41:00 pm by Eileen »
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Kiran Jagtap

  • Ask me questions
  • ****
  • Posts: 533
  • Karma: 51
Re: PaypalIPN for recurring - changing IDs
February 28, 2011, 05:45:13 am
Sounds good.

Yes we could stay w/ invoice id as this invoice id is going to same for recurring and related first contribution record.

So once we do load recurring id, first contribution id and valid contact id in $ids variable, our BaseIPN will take care of rest of the things.

For update contribution contact id w/ recurring contact id I do feel we might wanna validate across contact table and query might be like

Code: [Select]
    UPDATE   civicrm_contribution contribution
INNER JOIN   civicrm_contribution_recur recur ON ( recur.id = contribution.contribution_recur_id AND recur.contact_id != contribution.contact_id )
INNER JOIN   civicrm_contact contact ON ( contact.id = recur.contact_id AND contact.is_deleted = 0 )
       SET   contribution.contact_id = recur.contact_id;


thanks,

kiran
You Are Designed To Choose... Defined By Choice.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: PaypalIPN for recurring - changing IDs
February 28, 2011, 11:46:19 am
Are you thinking about putting that update SQL into the upgrade script? It hadn't occurred to me until I saw you post a revised version.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • PaypalIPN for recurring - changing IDs

This forum was archived on 2017-11-26.