Permalink
Browse files

Re-applied: Allow loading existing IPN record, based on tnx_id or par…

…ent_tnx_id

- Simplified the InstantPaymentNotification::searchIPNId() method from original implementation

Thanks to cali1/CakePHP-Paypal-IPN-Plugin@2b76b98 on github
  • Loading branch information...
josegonzalez committed Oct 2, 2011
1 parent 4baae69 commit 9a03059b007357664d0c27e8654a67808b85c1f6
@@ -21,15 +21,22 @@ function beforeFilter(){
/**
* Paypal IPN processing action..
- * This action is the intake for a paypal_ipn callback performed by paypal itself.
- * This action will take the paypal callback, verify it (so trickery) and save the transaction into your database for later review
+ * Intake for a paypal_ipn callback performed by paypal itself.
+ * This action will take the paypal callback, verify it (so trickery) and
+ * save the transaction into your database for later review
*
* @access public
* @author Nick Baker
*/
function process() {
if ($this->InstantPaymentNotification->isValid($_POST)) {
$notification = $this->InstantPaymentNotification->buildAssociationsFromIPN($_POST);
+
+ $existingIPNId = $this->InstantPaymentNotification->searchIPNId($notification);
+ if ($existingIPNId !== false) {
+ $notification['InstantPaymentNotification']['id'] = $existingIPNId;
+ }
+
$this->InstantPaymentNotification->saveAll($notification);
$this->__processTransaction($this->InstantPaymentNotification->id);
}
@@ -163,4 +163,29 @@ function buildAssociationsFromIPN($post) {
return $retval;
}
+/**
+ * searches existing IPNs transactions using tnx_id or parent_tnx_id
+ *
+ * @param array $data
+ * @return mixed false if not matched, existingIpnId otherwise
+ * @todo Figure out what to do if more than one record is found
+ */
+ function searchIPNId($data = array()) {
+ if (empty($data['InstantPaymentNotification']['tnx_id'])) {
+ return false;
+ }
+
+ $ipns = $this->findAllByTnxId($data['InstantPaymentNotification']['tnx_id']);
+ if (!empty($ipns) && (count($ipns) == 1)) {
+ return $ipns[0]['InstantPaymentNotification']['id'];
+ }
+
+ $ipns = $this->findAllByParentTnxId($data['InstantPaymentNotification']['tnx_id']);
+ if (!empty($ipns) && (count($ipns) == 1)) {
+ return $ipns[0]['InstantPaymentNotification']['id'];
+ }
+
+ return false;
+ }
+
}

0 comments on commit 9a03059

Please sign in to comment.