? cvs-release-notes.php
Index: ec_paypal/ec_paypal.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/ec_paypal/ec_paypal.module,v
retrieving revision 1.4.2.30
diff -u -r1.4.2.30 ec_paypal.module
--- ec_paypal/ec_paypal.module	20 Jan 2011 12:15:33 -0000	1.4.2.30
+++ ec_paypal/ec_paypal.module	1 Feb 2011 11:08:15 -0000
@@ -846,24 +846,53 @@
         break;
     }
   }
+  elseif ($response['payment_status'] == 'Refunded') {
+    ec_paypal_wd($response, 'ec_paypal', t('Processing refund request.'), WATCHDOG_NOTICE);
+    // to run ec_paypal_find_receipt() we need to fiddle with the response.
+    $resp = $response;
+    $resp['txn_id'] = $resp['parent_txn_id'];
+    if ($erid = ec_paypal_find_receipt($resp)) {
+      $receipt = ec_receipt_load($erid);
+      ec_paypal_wd($receipt, 'ec_paypal', t('receipt loaded'), WATCHDOG_NOTICE);
+      if ($receipt && $receipt->status != RECEIPT_STATUS_REFUNDED) {
+        // Reverse all the allocations
+        $process_refund = TRUE;
+        foreach (ec_receipt_get_allocations($receipt->erid) as $allocation) {
+          if (!ec_receipt_reverse_allocation($receipt, $allocation)) {
+            ec_paypal_wd($allocation, 'ec_paypal', t('Unable to reverse allocation'), WATCHDOG_ERROR);
+            $process_refund = FALSE;
+          }
+        }
+        if ($process_refund) {
+          $alloc[] = array(
+              'type' => 'refund', 
+              'id' => $receipt->erid,
+          );
+          ec_receipt_allocate($receipt, $alloc);
+      
+          $receipt->status = RECEIPT_STATUS_REFUNDED;
+          ec_receipt_save($receipt);
+          ec_paypal_wd($receipt, 'ec_paypal', t('Receipt refunded by PayPal'), WATCHDOG_NOTICE);
+        }
+        else {
+          ec_paypal_wd($receipt, 'ec_paypal', t('Unable to process the refund'), WATCHDOG_NOTICE);
+        }
+      }
+      else {
+        ec_paypal_wd($receipt, 'ec_paypal', t('Receipt already Refunded'), WATCHDOG_NOTICE);
+      }
+    }
+    else {
+      ec_paypal_wd($response, 'ec_paypal', t('Unable to find refunded receipt.'), WATCHDOG_WARNING);
+    }
+  }
   elseif ($receipt = ec_paypal_create_receipt($response)) {
     switch ($response['txn_type']) {
       case 'cart':
         break;
 
       case 'subscr_payment':
-        if (!empty($response['custom'])) {
-          parse_str($response['custom'], $ids);
-        }
-        else {
-          /**
-           * Since there is no custom field as this may be a PayPal Express which doesn't have
-           * enough information to populate the custom field as it is created before the transaction
-           * is created. So to get around this we are going to use the original invoice which was
-           * allocated to the first receipt in this transaction.
-           */
-          $ids = db_result(db_query_range("SELECT a.type, a.etid AS invoice FROM {ec_receipt} r INNER JOIN {ec_receipt_allocation} a ON r.erid = a.erid WHERE  r.type LIKE 'paypal_%' AND r.paypal_subscr_id = '%s' ORDER BY r.created ASC", $response['subscr_id'], 0, 1));
-        }
+        $ids = ec_paypal_parse_ipn_custom();
 
         // We also need to tell the allocation interface that it needs to update the subscription.
         if (isset($ids['invoice']) && isset($ids['type']) && ($object = ec_receipt_alloc_invoke($ids['type'], 'load', $ids['invoice']))) {
@@ -887,6 +916,23 @@
   }
 }
 
+function ec_paypal_parse_ipn_custom($response) {
+  if (!empty($response['custom'])) {
+    parse_str($response['custom'], $ids);
+  }
+  else {
+    /**
+     * Since there is no custom field as this may be a PayPal Express which doesn't have
+     * enough information to populate the custom field as it is created before the transaction
+     * is created. So to get around this we are going to use the original invoice which was
+     * allocated to the first receipt in this transaction.
+     */
+    $ids = db_result(db_query_range("SELECT a.type, a.etid AS invoice FROM {ec_receipt} r INNER JOIN {ec_receipt_allocation} a ON r.erid = a.erid WHERE  r.type LIKE 'paypal_%' AND r.paypal_subscr_id = '%s' ORDER BY r.created ASC", $response['subscr_id'], 0, 1));
+  }
+
+  return $ids;
+}
+
 function ec_paypal_find_receipt($response) {
   if (!empty($response['custom'])) {
     parse_str($response['custom'], $ids);
Index: ec_receipt/ec_receipt.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/ec_receipt/ec_receipt.admin.inc,v
retrieving revision 1.4.2.23
diff -u -r1.4.2.23 ec_receipt.admin.inc
--- ec_receipt/ec_receipt.admin.inc	16 Dec 2010 11:55:58 -0000	1.4.2.23
+++ ec_receipt/ec_receipt.admin.inc	1 Feb 2011 11:08:15 -0000
@@ -372,38 +372,6 @@
 }
 
 /**
- * Reverse Allocation.
- * TODO: Convert to use drupal_write_record.
- */
-function ec_receipt_reverse_allocation($receipt, $alloc) {
-  if ($object = ec_receipt_alloc_invoke($alloc->type, 'load', $alloc->etid)) {
-    if ((ec_receipt_alloc_invoke($alloc->type, 'can_reverse', $object)) === FALSE) {
-      return FALSE;
-    }
-    $total = ec_receipt_alloc_invoke($alloc->type, 'get_total', $object);
-    $reversal = drupal_clone($alloc);
-    $reversal->eaid = db_last_insert_id('ec_receipt_allocation', 'eaid');
-    $reversal->created = time();
-    $reversal->amount*= -1;
-    $reversal->reversed = $alloc->eaid;
-
-    $receipt->allocated+= $reversal->amount;
-    $receipt->balance-= $reversal->amount;
-
-    $params = array(
-      'amount' => $reversal->amount,
-    );
-
-    ec_receipt_alloc_invoke($alloc->type, 'allocation', $object, $params, $total - $reversal->amount);
-    $sql = "INSERT INTO {ec_receipt_allocation} (eaid, erid, type, etid, created, amount, reversed) VALUES (%d, %d, '%s', '%s', %d, %d, %d)";
-    db_query($sql, (array)$reversal);
-    db_query('UPDATE {ec_receipt_allocation} SET reversed = %d WHERE eaid = %d', $reversal->eaid, $alloc->eaid);
-    ec_receipt_save($receipt);
-    return TRUE;
-  }
-}
-
-/**
  *
  */
 function ec_receipt_complete_form(&$form_state, $receipt) {
Index: ec_receipt/ec_receipt.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/ec_receipt/ec_receipt.module,v
retrieving revision 1.18.2.60
diff -u -r1.18.2.60 ec_receipt.module
--- ec_receipt/ec_receipt.module	1 Feb 2011 10:54:02 -0000	1.18.2.60
+++ ec_receipt/ec_receipt.module	1 Feb 2011 11:08:16 -0000
@@ -837,6 +837,20 @@
 }
 
 /**
+ * Get all allocated data for a receipt
+ */
+function ec_receipt_get_allocations($erid) {
+  $allocations = array();
+  
+  $result = db_query('SELECT * FROM {ec_receipt_allocation} WHERE erid = %d AND reversed = 0', $erid);
+  while ($allocation = db_fetch_object($result)) {
+    $allocations[$allocation->eaid] = $allocation;
+  }
+  
+  return $allocations;
+}
+
+/**
  * Return the total allocated from the filter
  */
 function ec_receipt_get_allocated_total($filter) {
@@ -849,6 +863,38 @@
 }
 
 /**
+ * Reverse Allocation.
+ * TODO: Convert to use drupal_write_record.
+ */
+function ec_receipt_reverse_allocation($receipt, $alloc) {
+  if ($object = ec_receipt_alloc_invoke($alloc->type, 'load', $alloc->etid)) {
+    if ((ec_receipt_alloc_invoke($alloc->type, 'can_reverse', $object)) === FALSE) {
+      return FALSE;
+    }
+    $total = ec_receipt_alloc_invoke($alloc->type, 'get_total', $object);
+    $reversal = drupal_clone($alloc);
+    $reversal->eaid = db_last_insert_id('ec_receipt_allocation', 'eaid');
+    $reversal->created = time();
+    $reversal->amount*= -1;
+    $reversal->reversed = $alloc->eaid;
+
+    $receipt->allocated+= $reversal->amount;
+    $receipt->balance-= $reversal->amount;
+
+    $params = array(
+      'amount' => $reversal->amount,
+    );
+
+    ec_receipt_alloc_invoke($alloc->type, 'allocation', $object, $params, $total - $reversal->amount);
+    $sql = "INSERT INTO {ec_receipt_allocation} (eaid, erid, type, etid, created, amount, reversed) VALUES (%d, %d, '%s', '%s', %d, %d, %d)";
+    db_query($sql, (array)$reversal);
+    db_query('UPDATE {ec_receipt_allocation} SET reversed = %d WHERE eaid = %d', $reversal->eaid, $alloc->eaid);
+    ec_receipt_save($receipt);
+    return TRUE;
+  }
+}
+
+/**
  * Implementation hook_link().
  */
 function ec_receipt_link($type, $object, $teaser = FALSE) {
